controls/LocationCellEditor.py
changeset 586 9aa96a36cf33
child 615 8baeb9dff775
equal deleted inserted replaced
585:bd8c7a033b17 586:9aa96a36cf33
       
     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     def _init_coll_MainSizer_Items(self, parent):
       
    32         parent.AddWindow(self.Location, 0, border=0, flag=wx.GROW)
       
    33         parent.AddWindow(self.BrowseButton, 0, border=0, flag=wx.GROW)
       
    34         
       
    35     def _init_coll_MainSizer_Growables(self, parent):
       
    36         parent.AddGrowableCol(0)
       
    37         parent.AddGrowableRow(0)
       
    38     
       
    39     def _init_sizers(self):
       
    40         self.MainSizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0)
       
    41         
       
    42         self._init_coll_MainSizer_Items(self.MainSizer)
       
    43         self._init_coll_MainSizer_Growables(self.MainSizer)
       
    44         
       
    45         self.SetSizer(self.MainSizer)
       
    46     
       
    47     def _init_ctrls(self, prnt):
       
    48         wx.Control.__init__(self, id=-1, 
       
    49               name='LocationCellControl', parent=prnt,  
       
    50               size=wx.DefaultSize, style=0)
       
    51         
       
    52         # create location text control
       
    53         self.Location = wx.TextCtrl(id=-1, name='Location', parent=self,
       
    54               pos=wx.Point(0, 0), size=wx.Size(0, 0), style=wx.TE_PROCESS_ENTER)
       
    55         self.Location.Bind(wx.EVT_KEY_DOWN, self.OnLocationChar)
       
    56         
       
    57         # create browse button
       
    58         self.BrowseButton = wx.Button(id=-1, label='...', 
       
    59               name='BrowseButton', parent=self, pos=wx.Point(0, 0), 
       
    60               size=wx.Size(30, 0), style=0)
       
    61         self.BrowseButton.Bind(wx.EVT_BUTTON, self.OnBrowseButtonClick)
       
    62 
       
    63         self.Bind(wx.EVT_SIZE, self.OnSize)
       
    64         
       
    65         self._init_sizers()
       
    66 
       
    67     '''
       
    68     Custom cell editor control with a text box and a button that launches
       
    69     the BrowseLocationsDialog.
       
    70     '''
       
    71     def __init__(self, parent, locations):
       
    72         self._init_ctrls(parent)
       
    73         self.Locations = locations
       
    74         self.VarType = None
       
    75 
       
    76     def SetVarType(self, vartype):
       
    77         self.VarType = vartype
       
    78 
       
    79     def SetValue(self, value):
       
    80         self.Location.SetValue(value)
       
    81     
       
    82     def GetValue(self):
       
    83         return self.Location.GetValue()
       
    84 
       
    85     def OnSize(self, event):
       
    86         self.Layout()
       
    87 
       
    88     def OnBrowseButtonClick(self, event):
       
    89         # pop up the location browser dialog
       
    90         dialog = BrowseLocationsDialog(self, self.VarType, self.Locations)
       
    91         if dialog.ShowModal() == wx.ID_OK:
       
    92             infos = dialog.GetValues()
       
    93 
       
    94             # set the location
       
    95             self.Location.SetValue(infos["location"])
       
    96 
       
    97         dialog.Destroy()
       
    98 
       
    99         self.Location.SetFocus()
       
   100 
       
   101     def OnLocationChar(self, event):
       
   102         keycode = event.GetKeyCode()
       
   103         if keycode == wx.WXK_RETURN or keycode == wx.WXK_TAB:
       
   104             self.Parent.Parent.ProcessEvent(event)
       
   105             self.Parent.Parent.SetFocus()
       
   106         else:
       
   107             event.Skip()
       
   108 
       
   109     def SetInsertionPoint(self, i):
       
   110         self.Location.SetInsertionPoint(i)
       
   111     
       
   112     def SetFocus(self):
       
   113         self.Location.SetFocus()
       
   114 
       
   115 class LocationCellEditor(wx.grid.PyGridCellEditor):
       
   116     '''
       
   117     Grid cell editor that uses LocationCellControl to display a browse button.
       
   118     '''
       
   119     def __init__(self, table, controler):
       
   120         wx.grid.PyGridCellEditor.__init__(self)
       
   121         self.Table = table
       
   122         self.Controler = controler
       
   123 
       
   124     def __del__(self):
       
   125         self.CellControl = None
       
   126     
       
   127     def Create(self, parent, id, evt_handler):
       
   128         locations = self.Controler.GetVariableLocationTree()
       
   129         if len(locations) > 0:
       
   130             self.CellControl = LocationCellControl(parent, locations)
       
   131         else:
       
   132             self.CellControl = wx.TextCtrl(parent, -1)
       
   133         self.SetControl(self.CellControl)
       
   134         if evt_handler:
       
   135             self.CellControl.PushEventHandler(evt_handler)
       
   136 
       
   137     def BeginEdit(self, row, col, grid):
       
   138         self.CellControl.SetValue(self.Table.GetValueByName(row, 'Location'))
       
   139         if isinstance(self.CellControl, LocationCellControl):
       
   140             self.CellControl.SetVarType(self.Controler.GetBaseType(self.Table.GetValueByName(row, 'Type')))
       
   141         self.CellControl.SetFocus()
       
   142 
       
   143     def EndEdit(self, row, col, grid):
       
   144         loc = self.CellControl.GetValue()
       
   145         old_loc = self.Table.GetValueByName(row, 'Location')
       
   146         if loc != old_loc:
       
   147             self.Table.SetValueByName(row, 'Location', loc)
       
   148             return True
       
   149         return False
       
   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.Controler)
       
   158