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.DurationEditorDialog import DurationEditorDialog laurent@586: laurent@586: class DurationCellControl(wx.PyControl): laurent@586: laurent@586: ''' laurent@586: Custom cell editor control with a text box and a button that launches laurent@586: the DurationEditorDialog. laurent@586: ''' laurent@586: 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.Duration = wx.TextCtrl(self, size=wx.Size(0, -1), Laurent@723: style=wx.TE_PROCESS_ENTER) Laurent@714: self.Duration.Bind(wx.EVT_KEY_DOWN, self.OnDurationChar) Laurent@714: main_sizer.AddWindow(self.Duration, flag=wx.GROW) Laurent@714: Laurent@714: # create browse button Laurent@723: self.EditButton = wx.Button(self, label='...', size=wx.Size(30, -1)) Laurent@714: self.Bind(wx.EVT_BUTTON, self.OnEditButtonClick, self.EditButton) Laurent@714: main_sizer.AddWindow(self.EditButton, flag=wx.GROW) Laurent@714: Laurent@714: self.Bind(wx.EVT_SIZE, self.OnSize) Laurent@714: Laurent@714: self.SetSizer(main_sizer) laurent@586: laurent@655: self.Default = None laurent@655: laurent@586: def SetValue(self, value): laurent@655: self.Default = value laurent@586: self.Duration.SetValue(value) laurent@586: laurent@586: def GetValue(self): laurent@586: return self.Duration.GetValue() laurent@586: laurent@586: def OnSize(self, event): laurent@586: self.Layout() laurent@586: laurent@586: def OnEditButtonClick(self, event): laurent@586: # pop up the Duration Editor dialog laurent@586: dialog = DurationEditorDialog(self) laurent@586: dialog.SetDuration(self.Duration.GetValue()) laurent@586: if dialog.ShowModal() == wx.ID_OK: laurent@586: # set the duration laurent@586: self.Duration.SetValue(dialog.GetDuration()) laurent@586: laurent@586: dialog.Destroy() laurent@586: laurent@586: self.Duration.SetFocus() laurent@586: laurent@586: def OnDurationChar(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.Duration.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.Duration.SetInsertionPoint(i) laurent@586: laurent@586: def SetFocus(self): laurent@586: self.Duration.SetFocus() laurent@586: laurent@586: class DurationCellEditor(wx.grid.PyGridCellEditor): laurent@586: ''' laurent@586: Grid cell editor that uses DurationCellControl to display an edit button. laurent@586: ''' laurent@586: def __init__(self, table): laurent@586: wx.grid.PyGridCellEditor.__init__(self) Laurent@714: laurent@586: self.Table = table laurent@586: laurent@586: def __del__(self): laurent@586: self.CellControl = None laurent@586: laurent@586: def Create(self, parent, id, evt_handler): laurent@586: self.CellControl = DurationCellControl(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@586: self.CellControl.SetValue(self.Table.GetValueByName(row, 'Interval')) laurent@586: self.CellControl.SetFocus() laurent@586: laurent@586: def EndEdit(self, row, col, grid): laurent@586: duration = self.CellControl.GetValue() laurent@586: old_duration = self.Table.GetValueByName(row, 'Interval') laurent@657: changed = duration != old_duration laurent@657: if changed: laurent@586: self.Table.SetValueByName(row, 'Interval', duration) laurent@657: self.CellControl.Disable() laurent@663: return changed laurent@586: 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@586: return DurationCellEditor(self.Table)