author | Laurent Bessard |
Thu, 29 Aug 2013 19:48:29 +0200 | |
changeset 1296 | 5f8e02717560 |
parent 1275 | 8d4de18c9f29 |
child 1499 | 8626bba4b2a8 |
permissions | -rw-r--r-- |
814 | 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.DurationEditorDialog import DurationEditorDialog |
|
28 |
||
29 |
class DurationCellControl(wx.PyControl): |
|
30 |
||
31 |
''' |
|
32 |
Custom cell editor control with a text box and a button that launches |
|
33 |
the DurationEditorDialog. |
|
34 |
''' |
|
35 |
def __init__(self, parent): |
|
36 |
wx.Control.__init__(self, parent) |
|
37 |
||
38 |
main_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0) |
|
39 |
main_sizer.AddGrowableCol(0) |
|
40 |
main_sizer.AddGrowableRow(0) |
|
41 |
||
42 |
# create location text control |
|
43 |
self.Duration = wx.TextCtrl(self, size=wx.Size(0, -1), |
|
44 |
style=wx.TE_PROCESS_ENTER) |
|
45 |
self.Duration.Bind(wx.EVT_KEY_DOWN, self.OnDurationChar) |
|
46 |
main_sizer.AddWindow(self.Duration, flag=wx.GROW) |
|
47 |
||
48 |
# create browse button |
|
49 |
self.EditButton = wx.Button(self, label='...', size=wx.Size(30, -1)) |
|
50 |
self.Bind(wx.EVT_BUTTON, self.OnEditButtonClick, self.EditButton) |
|
51 |
main_sizer.AddWindow(self.EditButton, flag=wx.GROW) |
|
52 |
||
53 |
self.Bind(wx.EVT_SIZE, self.OnSize) |
|
54 |
||
55 |
self.SetSizer(main_sizer) |
|
56 |
||
57 |
self.Default = None |
|
58 |
||
59 |
def SetValue(self, value): |
|
60 |
self.Default = value |
|
61 |
self.Duration.SetValue(value) |
|
62 |
||
63 |
def GetValue(self): |
|
64 |
return self.Duration.GetValue() |
|
65 |
||
66 |
def OnSize(self, event): |
|
67 |
self.Layout() |
|
68 |
||
69 |
def OnEditButtonClick(self, event): |
|
70 |
# pop up the Duration Editor dialog |
|
71 |
dialog = DurationEditorDialog(self) |
|
72 |
dialog.SetDuration(self.Duration.GetValue()) |
|
73 |
if dialog.ShowModal() == wx.ID_OK: |
|
74 |
# set the duration |
|
75 |
self.Duration.SetValue(dialog.GetDuration()) |
|
76 |
||
77 |
dialog.Destroy() |
|
78 |
||
79 |
self.Duration.SetFocus() |
|
80 |
||
81 |
def OnDurationChar(self, event): |
|
82 |
keycode = event.GetKeyCode() |
|
83 |
if keycode in [wx.WXK_RETURN, wx.WXK_TAB]: |
|
84 |
self.Parent.Parent.ProcessEvent(event) |
|
85 |
elif keycode == wx.WXK_ESCAPE: |
|
86 |
self.Duration.SetValue(self.Default) |
|
87 |
self.Parent.Parent.CloseEditControl() |
|
88 |
else: |
|
89 |
event.Skip() |
|
90 |
||
91 |
def SetInsertionPoint(self, i): |
|
92 |
self.Duration.SetInsertionPoint(i) |
|
93 |
||
94 |
def SetFocus(self): |
|
95 |
self.Duration.SetFocus() |
|
96 |
||
97 |
class DurationCellEditor(wx.grid.PyGridCellEditor): |
|
98 |
''' |
|
99 |
Grid cell editor that uses DurationCellControl to display an edit button. |
|
100 |
''' |
|
1275
8d4de18c9f29
Fixed DurationCellEditor to make it reusable
Laurent Bessard
parents:
814
diff
changeset
|
101 |
def __init__(self, table, colname): |
814 | 102 |
wx.grid.PyGridCellEditor.__init__(self) |
103 |
||
104 |
self.Table = table |
|
1275
8d4de18c9f29
Fixed DurationCellEditor to make it reusable
Laurent Bessard
parents:
814
diff
changeset
|
105 |
self.Colname = colname |
814 | 106 |
|
107 |
def __del__(self): |
|
108 |
self.CellControl = None |
|
109 |
||
110 |
def Create(self, parent, id, evt_handler): |
|
111 |
self.CellControl = DurationCellControl(parent) |
|
112 |
self.SetControl(self.CellControl) |
|
113 |
if evt_handler: |
|
114 |
self.CellControl.PushEventHandler(evt_handler) |
|
115 |
||
116 |
def BeginEdit(self, row, col, grid): |
|
117 |
self.CellControl.Enable() |
|
1275
8d4de18c9f29
Fixed DurationCellEditor to make it reusable
Laurent Bessard
parents:
814
diff
changeset
|
118 |
self.CellControl.SetValue(self.Table.GetValueByName(row, self.Colname)) |
814 | 119 |
self.CellControl.SetFocus() |
120 |
||
121 |
def EndEdit(self, row, col, grid): |
|
122 |
duration = self.CellControl.GetValue() |
|
1275
8d4de18c9f29
Fixed DurationCellEditor to make it reusable
Laurent Bessard
parents:
814
diff
changeset
|
123 |
old_duration = self.Table.GetValueByName(row, self.Colname) |
814 | 124 |
changed = duration != old_duration |
125 |
if changed: |
|
1275
8d4de18c9f29
Fixed DurationCellEditor to make it reusable
Laurent Bessard
parents:
814
diff
changeset
|
126 |
self.Table.SetValueByName(row, self.Colname, duration) |
814 | 127 |
self.CellControl.Disable() |
128 |
return changed |
|
129 |
||
130 |
def SetSize(self, rect): |
|
131 |
self.CellControl.SetDimensions(rect.x + 1, rect.y, |
|
132 |
rect.width, rect.height, |
|
133 |
wx.SIZE_ALLOW_MINUS_ONE) |
|
134 |
||
135 |
def Clone(self): |
|
136 |
return DurationCellEditor(self.Table) |