Laurent@814: #!/usr/bin/env python Laurent@814: # -*- coding: utf-8 -*- Laurent@814: Laurent@814: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor Laurent@814: #based on the plcopen standard. Laurent@814: # Laurent@814: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD Laurent@814: # Laurent@814: #See COPYING file for copyrights details. Laurent@814: # Laurent@814: #This library is free software; you can redistribute it and/or Laurent@814: #modify it under the terms of the GNU General Public Laurent@814: #License as published by the Free Software Foundation; either Laurent@814: #version 2.1 of the License, or (at your option) any later version. Laurent@814: # Laurent@814: #This library is distributed in the hope that it will be useful, Laurent@814: #but WITHOUT ANY WARRANTY; without even the implied warranty of Laurent@814: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Laurent@814: #General Public License for more details. Laurent@814: # Laurent@814: #You should have received a copy of the GNU General Public Laurent@814: #License along with this library; if not, write to the Free Software Laurent@814: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Laurent@814: Laurent@814: import wx Laurent@814: Laurent@814: from dialogs.BrowseLocationsDialog import BrowseLocationsDialog Laurent@814: Laurent@814: class LocationCellControl(wx.PyControl): Laurent@814: Laurent@814: ''' Laurent@814: Custom cell editor control with a text box and a button that launches Laurent@814: the BrowseLocationsDialog. Laurent@814: ''' Laurent@814: def __init__(self, parent): Laurent@814: wx.Control.__init__(self, parent) Laurent@814: Laurent@814: main_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0) Laurent@814: main_sizer.AddGrowableCol(0) Laurent@814: main_sizer.AddGrowableRow(0) Laurent@814: Laurent@814: # create location text control Laurent@814: self.Location = wx.TextCtrl(self, size=wx.Size(0, -1), Laurent@814: style=wx.TE_PROCESS_ENTER) Laurent@814: self.Location.Bind(wx.EVT_KEY_DOWN, self.OnLocationChar) Laurent@814: main_sizer.AddWindow(self.Location, flag=wx.GROW) Laurent@814: Laurent@814: # create browse button Laurent@814: self.BrowseButton = wx.Button(self, label='...', size=wx.Size(30, -1)) Laurent@814: self.BrowseButton.Bind(wx.EVT_BUTTON, self.OnBrowseButtonClick) Laurent@814: main_sizer.AddWindow(self.BrowseButton, flag=wx.GROW) Laurent@814: Laurent@814: self.Bind(wx.EVT_SIZE, self.OnSize) Laurent@814: Laurent@814: self.SetSizer(main_sizer) Laurent@814: Laurent@814: self.Locations = None Laurent@814: self.VarType = None Laurent@814: self.Default = False Laurent@814: Laurent@814: def SetLocations(self, locations): Laurent@814: self.Locations = locations Laurent@814: Laurent@814: def SetVarType(self, vartype): Laurent@814: self.VarType = vartype Laurent@814: Laurent@814: def SetValue(self, value): Laurent@814: self.Default = value Laurent@814: self.Location.SetValue(value) Laurent@814: Laurent@814: def GetValue(self): Laurent@814: return self.Location.GetValue() Laurent@814: Laurent@814: def OnSize(self, event): Laurent@814: self.Layout() Laurent@814: Laurent@814: def OnBrowseButtonClick(self, event): Laurent@814: # pop up the location browser dialog Laurent@814: dialog = BrowseLocationsDialog(self, self.VarType, self.Locations) Laurent@814: if dialog.ShowModal() == wx.ID_OK: Laurent@814: infos = dialog.GetValues() Laurent@814: Laurent@814: # set the location Laurent@814: self.Location.SetValue(infos["location"]) Laurent@814: Laurent@814: dialog.Destroy() Laurent@814: Laurent@814: self.Location.SetFocus() Laurent@814: Laurent@814: def OnLocationChar(self, event): Laurent@814: keycode = event.GetKeyCode() Laurent@814: if keycode in [wx.WXK_RETURN, wx.WXK_TAB]: Laurent@814: self.Parent.Parent.ProcessEvent(event) Laurent@814: elif keycode == wx.WXK_ESCAPE: Laurent@814: self.Location.SetValue(self.Default) Laurent@814: self.Parent.Parent.CloseEditControl() Laurent@814: else: Laurent@814: event.Skip() Laurent@814: Laurent@814: def SetInsertionPoint(self, i): Laurent@814: self.Location.SetInsertionPoint(i) Laurent@814: Laurent@814: def SetFocus(self): Laurent@814: self.Location.SetFocus() Laurent@814: Laurent@814: class LocationCellEditor(wx.grid.PyGridCellEditor): Laurent@814: ''' Laurent@814: Grid cell editor that uses LocationCellControl to display a browse button. Laurent@814: ''' Laurent@814: def __init__(self, table, controller): Laurent@814: wx.grid.PyGridCellEditor.__init__(self) Laurent@814: Laurent@814: self.Table = table Laurent@814: self.Controller = controller Laurent@814: Laurent@814: def __del__(self): Laurent@814: self.CellControl = None Laurent@814: Laurent@814: def Create(self, parent, id, evt_handler): Laurent@814: self.CellControl = LocationCellControl(parent) Laurent@814: self.SetControl(self.CellControl) Laurent@814: if evt_handler: Laurent@814: self.CellControl.PushEventHandler(evt_handler) Laurent@814: Laurent@814: def BeginEdit(self, row, col, grid): Laurent@814: self.CellControl.Enable() Laurent@814: self.CellControl.SetLocations(self.Controller.GetVariableLocationTree()) Laurent@814: self.CellControl.SetValue(self.Table.GetValueByName(row, 'Location')) Laurent@814: if isinstance(self.CellControl, LocationCellControl): Laurent@814: self.CellControl.SetVarType(self.Controller.GetBaseType(self.Table.GetValueByName(row, 'Type'))) Laurent@814: self.CellControl.SetFocus() Laurent@814: Laurent@814: def EndEdit(self, row, col, grid): Laurent@814: loc = self.CellControl.GetValue() Laurent@814: old_loc = self.Table.GetValueByName(row, 'Location') Laurent@814: changed = loc != old_loc Laurent@814: if changed: Laurent@814: self.Table.SetValueByName(row, 'Location', loc) Laurent@814: self.CellControl.Disable() Laurent@814: return changed Laurent@814: Laurent@814: def SetSize(self, rect): Laurent@814: self.CellControl.SetDimensions(rect.x + 1, rect.y, Laurent@814: rect.width, rect.height, Laurent@814: wx.SIZE_ALLOW_MINUS_ONE) Laurent@814: Laurent@814: def Clone(self): Laurent@814: return LocationCellEditor(self.Table, self.Controller) Laurent@814: