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