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