controls/LocationCellEditor.py
branch1.1 Korean release
changeset 968 eee7625de1f7
parent 855 b30421d07e8c
child 1004 bf3b6998f7b6
equal deleted inserted replaced
808:6e205c1f05a0 968:eee7625de1f7
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
       
     5 #based on the plcopen standard. 
       
     6 #
       
     7 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU General Public
       
    22 #License along with this library; if not, write to the Free Software
       
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    24 
       
    25 import wx
       
    26 
       
    27 from dialogs.BrowseLocationsDialog import BrowseLocationsDialog
       
    28 
       
    29 class LocationCellControl(wx.PyControl):
       
    30     
       
    31     '''
       
    32     Custom cell editor control with a text box and a button that launches
       
    33     the BrowseLocationsDialog.
       
    34     '''
       
    35     def __init__(self, parent):
       
    36         wx.Control.__init__(self, parent)
       
    37         
       
    38         main_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0)
       
    39         main_sizer.AddGrowableCol(0)
       
    40         main_sizer.AddGrowableRow(0)
       
    41         
       
    42         # create location text control
       
    43         self.Location = wx.TextCtrl(self, size=wx.Size(0, -1), 
       
    44               style=wx.TE_PROCESS_ENTER)
       
    45         self.Location.Bind(wx.EVT_KEY_DOWN, self.OnLocationChar)
       
    46         main_sizer.AddWindow(self.Location, flag=wx.GROW)
       
    47         
       
    48         # create browse button
       
    49         self.BrowseButton = wx.Button(self, label='...', size=wx.Size(30, -1))
       
    50         self.BrowseButton.Bind(wx.EVT_BUTTON, self.OnBrowseButtonClick)
       
    51         main_sizer.AddWindow(self.BrowseButton, flag=wx.GROW)
       
    52         
       
    53         self.Bind(wx.EVT_SIZE, self.OnSize)
       
    54         
       
    55         self.SetSizer(main_sizer)
       
    56 
       
    57         self.Controller = None
       
    58         self.VarType = None
       
    59         self.Default = False
       
    60 
       
    61     def __del__(self):
       
    62         self.Controller = None
       
    63 
       
    64     def SetController(self, controller):
       
    65         self.Controller = controller
       
    66 
       
    67     def SetVarType(self, vartype):
       
    68         self.VarType = vartype
       
    69 
       
    70     def GetVarType(self):
       
    71         return self.VarType
       
    72 
       
    73     def SetValue(self, value):
       
    74         self.Default = value
       
    75         self.Location.SetValue(value)
       
    76     
       
    77     def GetValue(self):
       
    78         return self.Location.GetValue()
       
    79 
       
    80     def OnSize(self, event):
       
    81         self.Layout()
       
    82 
       
    83     def OnBrowseButtonClick(self, event):
       
    84         # pop up the location browser dialog
       
    85         dialog = BrowseLocationsDialog(self, self.VarType, self.Controller)
       
    86         if dialog.ShowModal() == wx.ID_OK:
       
    87             infos = dialog.GetValues()
       
    88 
       
    89             # set the location
       
    90             self.Location.SetValue(infos["location"])
       
    91             self.VarType = infos["IEC_type"]
       
    92 
       
    93         dialog.Destroy()
       
    94 
       
    95         self.Location.SetFocus()
       
    96 
       
    97     def OnLocationChar(self, event):
       
    98         keycode = event.GetKeyCode()
       
    99         if keycode in [wx.WXK_RETURN, wx.WXK_TAB]:
       
   100             self.Parent.Parent.ProcessEvent(event)
       
   101         elif keycode == wx.WXK_ESCAPE:
       
   102             self.Location.SetValue(self.Default)
       
   103             self.Parent.Parent.CloseEditControl()
       
   104         else:
       
   105             event.Skip()
       
   106 
       
   107     def SetInsertionPoint(self, i):
       
   108         self.Location.SetInsertionPoint(i)
       
   109     
       
   110     def SetFocus(self):
       
   111         self.Location.SetFocus()
       
   112 
       
   113 class LocationCellEditor(wx.grid.PyGridCellEditor):
       
   114     '''
       
   115     Grid cell editor that uses LocationCellControl to display a browse button.
       
   116     '''
       
   117     def __init__(self, table, controller):
       
   118         wx.grid.PyGridCellEditor.__init__(self)
       
   119         
       
   120         self.Table = table
       
   121         self.Controller = controller
       
   122 
       
   123     def __del__(self):
       
   124         self.CellControl = None
       
   125         self.Controller = None
       
   126     
       
   127     def Create(self, parent, id, evt_handler):
       
   128         self.CellControl = LocationCellControl(parent)
       
   129         self.SetControl(self.CellControl)
       
   130         if evt_handler:
       
   131             self.CellControl.PushEventHandler(evt_handler)
       
   132 
       
   133     def BeginEdit(self, row, col, grid):
       
   134         self.CellControl.Enable()
       
   135         self.CellControl.SetController(self.Controller)
       
   136         self.CellControl.SetValue(self.Table.GetValueByName(row, 'Location'))
       
   137         if isinstance(self.CellControl, LocationCellControl):
       
   138             self.CellControl.SetVarType(self.Table.GetValueByName(row, 'Type'))
       
   139         self.CellControl.SetFocus()
       
   140 
       
   141     def EndEdit(self, row, col, grid):
       
   142         loc = self.CellControl.GetValue()
       
   143         old_loc = self.Table.GetValueByName(row, 'Location')
       
   144         changed = loc != old_loc
       
   145         if changed:
       
   146             self.Table.SetValueByName(row, 'Location', loc)
       
   147             self.Table.SetValueByName(row, 'Type', self.CellControl.GetVarType())
       
   148         self.CellControl.Disable()
       
   149         return changed
       
   150     
       
   151     def SetSize(self, rect):
       
   152         self.CellControl.SetDimensions(rect.x + 1, rect.y,
       
   153                                         rect.width, rect.height,
       
   154                                         wx.SIZE_ALLOW_MINUS_ONE)
       
   155 
       
   156     def Clone(self):
       
   157         return LocationCellEditor(self.Table, self.Controller)
       
   158