controls/LocationCellEditor.py
changeset 1730 64d8f52bc8c8
parent 1578 f8e2a04c4445
child 1736 7e61baa047f0
equal deleted inserted replaced
1726:d51af006fa6b 1730:64d8f52bc8c8
    25 import wx
    25 import wx
    26 
    26 
    27 from dialogs.BrowseLocationsDialog import BrowseLocationsDialog
    27 from dialogs.BrowseLocationsDialog import BrowseLocationsDialog
    28 
    28 
    29 class LocationCellControl(wx.PyControl):
    29 class LocationCellControl(wx.PyControl):
    30     
    30 
    31     '''
    31     '''
    32     Custom cell editor control with a text box and a button that launches
    32     Custom cell editor control with a text box and a button that launches
    33     the BrowseLocationsDialog.
    33     the BrowseLocationsDialog.
    34     '''
    34     '''
    35     def __init__(self, parent):
    35     def __init__(self, parent):
    36         wx.Control.__init__(self, parent)
    36         wx.Control.__init__(self, parent)
    37         
    37 
    38         main_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0)
    38         main_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0)
    39         main_sizer.AddGrowableCol(0)
    39         main_sizer.AddGrowableCol(0)
    40         main_sizer.AddGrowableRow(0)
    40         main_sizer.AddGrowableRow(0)
    41         
    41 
    42         # create location text control
    42         # create location text control
    43         self.Location = wx.TextCtrl(self, size=wx.Size(0, -1), 
    43         self.Location = wx.TextCtrl(self, size=wx.Size(0, -1),
    44               style=wx.TE_PROCESS_ENTER)
    44               style=wx.TE_PROCESS_ENTER)
    45         self.Location.Bind(wx.EVT_KEY_DOWN, self.OnLocationChar)
    45         self.Location.Bind(wx.EVT_KEY_DOWN, self.OnLocationChar)
    46         main_sizer.AddWindow(self.Location, flag=wx.GROW)
    46         main_sizer.AddWindow(self.Location, flag=wx.GROW)
    47         
    47 
    48         # create browse button
    48         # create browse button
    49         self.BrowseButton = wx.Button(self, label='...', size=wx.Size(30, -1))
    49         self.BrowseButton = wx.Button(self, label='...', size=wx.Size(30, -1))
    50         self.BrowseButton.Bind(wx.EVT_BUTTON, self.OnBrowseButtonClick)
    50         self.BrowseButton.Bind(wx.EVT_BUTTON, self.OnBrowseButtonClick)
    51         main_sizer.AddWindow(self.BrowseButton, flag=wx.GROW)
    51         main_sizer.AddWindow(self.BrowseButton, flag=wx.GROW)
    52         
    52 
    53         self.Bind(wx.EVT_SIZE, self.OnSize)
    53         self.Bind(wx.EVT_SIZE, self.OnSize)
    54         
    54 
    55         self.SetSizer(main_sizer)
    55         self.SetSizer(main_sizer)
    56 
    56 
    57         self.Controller = None
    57         self.Controller = None
    58         self.VarType = None
    58         self.VarType = None
    59         self.Default = False
    59         self.Default = False
    71         return self.VarType
    71         return self.VarType
    72 
    72 
    73     def SetValue(self, value):
    73     def SetValue(self, value):
    74         self.Default = value
    74         self.Default = value
    75         self.Location.SetValue(value)
    75         self.Location.SetValue(value)
    76     
    76 
    77     def GetValue(self):
    77     def GetValue(self):
    78         return self.Location.GetValue()
    78         return self.Location.GetValue()
    79 
    79 
    80     def OnSize(self, event):
    80     def OnSize(self, event):
    81         self.Layout()
    81         self.Layout()
    86         if dialog.ShowModal() == wx.ID_OK:
    86         if dialog.ShowModal() == wx.ID_OK:
    87             infos = dialog.GetValues()
    87             infos = dialog.GetValues()
    88         else:
    88         else:
    89             infos = None
    89             infos = None
    90         dialog.Destroy()
    90         dialog.Destroy()
    91         
    91 
    92         if infos is not None:
    92         if infos is not None:
    93             location = infos["location"]
    93             location = infos["location"]
    94             # set the location
    94             # set the location
    95             if not infos["location"].startswith("%"):
    95             if not infos["location"].startswith("%"):
    96                 dialog = wx.SingleChoiceDialog(self, 
    96                 dialog = wx.SingleChoiceDialog(self,
    97                       _("Select a variable class:"), _("Variable class"), 
    97                       _("Select a variable class:"), _("Variable class"),
    98                       [_("Input"), _("Output"), _("Memory")], 
    98                       [_("Input"), _("Output"), _("Memory")],
    99                       wx.DEFAULT_DIALOG_STYLE|wx.OK|wx.CANCEL)
    99                       wx.DEFAULT_DIALOG_STYLE|wx.OK|wx.CANCEL)
   100                 if dialog.ShowModal() == wx.ID_OK:
   100                 if dialog.ShowModal() == wx.ID_OK:
   101                     selected = dialog.GetSelection()
   101                     selected = dialog.GetSelection()
   102                 else:
   102                 else:
   103                     selected = None
   103                     selected = None
   109                     location = "%I" + location
   109                     location = "%I" + location
   110                 elif selected == 1:
   110                 elif selected == 1:
   111                     location = "%Q" + location
   111                     location = "%Q" + location
   112                 else:
   112                 else:
   113                     location = "%M" + location
   113                     location = "%M" + location
   114             
   114 
   115             self.Location.SetValue(location)
   115             self.Location.SetValue(location)
   116             self.VarType = infos["IEC_type"]
   116             self.VarType = infos["IEC_type"]
   117 
   117 
   118         self.Location.SetFocus()
   118         self.Location.SetFocus()
   119 
   119 
   127         else:
   127         else:
   128             event.Skip()
   128             event.Skip()
   129 
   129 
   130     def SetInsertionPoint(self, i):
   130     def SetInsertionPoint(self, i):
   131         self.Location.SetInsertionPoint(i)
   131         self.Location.SetInsertionPoint(i)
   132     
   132 
   133     def SetFocus(self):
   133     def SetFocus(self):
   134         self.Location.SetFocus()
   134         self.Location.SetFocus()
   135 
   135 
   136 class LocationCellEditor(wx.grid.PyGridCellEditor):
   136 class LocationCellEditor(wx.grid.PyGridCellEditor):
   137     '''
   137     '''
   138     Grid cell editor that uses LocationCellControl to display a browse button.
   138     Grid cell editor that uses LocationCellControl to display a browse button.
   139     '''
   139     '''
   140     def __init__(self, table, controller):
   140     def __init__(self, table, controller):
   141         wx.grid.PyGridCellEditor.__init__(self)
   141         wx.grid.PyGridCellEditor.__init__(self)
   142         
   142 
   143         self.Table = table
   143         self.Table = table
   144         self.Controller = controller
   144         self.Controller = controller
   145 
   145 
   146     def __del__(self):
   146     def __del__(self):
   147         self.CellControl = None
   147         self.CellControl = None
   148         self.Controller = None
   148         self.Controller = None
   149     
   149 
   150     def Create(self, parent, id, evt_handler):
   150     def Create(self, parent, id, evt_handler):
   151         self.CellControl = LocationCellControl(parent)
   151         self.CellControl = LocationCellControl(parent)
   152         self.SetControl(self.CellControl)
   152         self.SetControl(self.CellControl)
   153         if evt_handler:
   153         if evt_handler:
   154             self.CellControl.PushEventHandler(evt_handler)
   154             self.CellControl.PushEventHandler(evt_handler)
   167         if changed:
   167         if changed:
   168             self.Table.SetValueByName(row, 'Location', loc)
   168             self.Table.SetValueByName(row, 'Location', loc)
   169             self.Table.SetValueByName(row, 'Type', self.CellControl.GetVarType())
   169             self.Table.SetValueByName(row, 'Type', self.CellControl.GetVarType())
   170         self.CellControl.Disable()
   170         self.CellControl.Disable()
   171         return changed
   171         return changed
   172         
   172 
   173     if wx.VERSION >= (3, 0, 0):
   173     if wx.VERSION >= (3, 0, 0):
   174         def EndEdit(self, row, col, grid, oldval):
   174         def EndEdit(self, row, col, grid, oldval):
   175             return self.EndEditInternal(row, col, grid, oldval)
   175             return self.EndEditInternal(row, col, grid, oldval)
   176     else:
   176     else:
   177         def EndEdit(self, row, col, grid):
   177         def EndEdit(self, row, col, grid):
   178             old_loc = self.Table.GetValueByName(row, 'Location')            
   178             old_loc = self.Table.GetValueByName(row, 'Location')
   179             return self.EndEditInternal(row, col, grid, old_loc)
   179             return self.EndEditInternal(row, col, grid, old_loc)
   180     
   180 
   181     def SetSize(self, rect):
   181     def SetSize(self, rect):
   182         self.CellControl.SetDimensions(rect.x + 1, rect.y,
   182         self.CellControl.SetDimensions(rect.x + 1, rect.y,
   183                                         rect.width, rect.height,
   183                                         rect.width, rect.height,
   184                                         wx.SIZE_ALLOW_MINUS_ONE)
   184                                         wx.SIZE_ALLOW_MINUS_ONE)
   185 
   185 
   186     def Clone(self):
   186     def Clone(self):
   187         return LocationCellEditor(self.Table, self.Controller)
   187         return LocationCellEditor(self.Table, self.Controller)
   188