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.BrowseLocationsDialog import BrowseLocationsDialog
|
|
28 |
|
|
29 |
class LocationCellControl(wx.PyControl):
|
|
30 |
|
|
31 |
'''
|
|
32 |
Custom cell editor control with a text box and a button that launches
|
|
33 |
the BrowseLocationsDialog.
|
|
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.Location = wx.TextCtrl(self, size=wx.Size(0, -1),
|
|
44 |
style=wx.TE_PROCESS_ENTER)
|
|
45 |
self.Location.Bind(wx.EVT_KEY_DOWN, self.OnLocationChar)
|
|
46 |
main_sizer.AddWindow(self.Location, flag=wx.GROW)
|
|
47 |
|
|
48 |
# create browse button
|
|
49 |
self.BrowseButton = wx.Button(self, label='...', size=wx.Size(30, -1))
|
|
50 |
self.BrowseButton.Bind(wx.EVT_BUTTON, self.OnBrowseButtonClick)
|
|
51 |
main_sizer.AddWindow(self.BrowseButton, flag=wx.GROW)
|
|
52 |
|
|
53 |
self.Bind(wx.EVT_SIZE, self.OnSize)
|
|
54 |
|
|
55 |
self.SetSizer(main_sizer)
|
|
56 |
|
|
57 |
self.Locations = None
|
|
58 |
self.VarType = None
|
|
59 |
self.Default = False
|
|
60 |
|
|
61 |
def SetLocations(self, locations):
|
|
62 |
self.Locations = locations
|
|
63 |
|
|
64 |
def SetVarType(self, vartype):
|
|
65 |
self.VarType = vartype
|
|
66 |
|
|
67 |
def SetValue(self, value):
|
|
68 |
self.Default = value
|
|
69 |
self.Location.SetValue(value)
|
|
70 |
|
|
71 |
def GetValue(self):
|
|
72 |
return self.Location.GetValue()
|
|
73 |
|
|
74 |
def OnSize(self, event):
|
|
75 |
self.Layout()
|
|
76 |
|
|
77 |
def OnBrowseButtonClick(self, event):
|
|
78 |
# pop up the location browser dialog
|
|
79 |
dialog = BrowseLocationsDialog(self, self.VarType, self.Locations)
|
|
80 |
if dialog.ShowModal() == wx.ID_OK:
|
|
81 |
infos = dialog.GetValues()
|
|
82 |
|
|
83 |
# set the location
|
|
84 |
self.Location.SetValue(infos["location"])
|
|
85 |
|
|
86 |
dialog.Destroy()
|
|
87 |
|
|
88 |
self.Location.SetFocus()
|
|
89 |
|
|
90 |
def OnLocationChar(self, event):
|
|
91 |
keycode = event.GetKeyCode()
|
|
92 |
if keycode in [wx.WXK_RETURN, wx.WXK_TAB]:
|
|
93 |
self.Parent.Parent.ProcessEvent(event)
|
|
94 |
elif keycode == wx.WXK_ESCAPE:
|
|
95 |
self.Location.SetValue(self.Default)
|
|
96 |
self.Parent.Parent.CloseEditControl()
|
|
97 |
else:
|
|
98 |
event.Skip()
|
|
99 |
|
|
100 |
def SetInsertionPoint(self, i):
|
|
101 |
self.Location.SetInsertionPoint(i)
|
|
102 |
|
|
103 |
def SetFocus(self):
|
|
104 |
self.Location.SetFocus()
|
|
105 |
|
|
106 |
class LocationCellEditor(wx.grid.PyGridCellEditor):
|
|
107 |
'''
|
|
108 |
Grid cell editor that uses LocationCellControl to display a browse button.
|
|
109 |
'''
|
|
110 |
def __init__(self, table, controller):
|
|
111 |
wx.grid.PyGridCellEditor.__init__(self)
|
|
112 |
|
|
113 |
self.Table = table
|
|
114 |
self.Controller = controller
|
|
115 |
|
|
116 |
def __del__(self):
|
|
117 |
self.CellControl = None
|
|
118 |
|
|
119 |
def Create(self, parent, id, evt_handler):
|
|
120 |
self.CellControl = LocationCellControl(parent)
|
|
121 |
self.SetControl(self.CellControl)
|
|
122 |
if evt_handler:
|
|
123 |
self.CellControl.PushEventHandler(evt_handler)
|
|
124 |
|
|
125 |
def BeginEdit(self, row, col, grid):
|
|
126 |
self.CellControl.Enable()
|
|
127 |
self.CellControl.SetLocations(self.Controller.GetVariableLocationTree())
|
|
128 |
self.CellControl.SetValue(self.Table.GetValueByName(row, 'Location'))
|
|
129 |
if isinstance(self.CellControl, LocationCellControl):
|
|
130 |
self.CellControl.SetVarType(self.Controller.GetBaseType(self.Table.GetValueByName(row, 'Type')))
|
|
131 |
self.CellControl.SetFocus()
|
|
132 |
|
|
133 |
def EndEdit(self, row, col, grid):
|
|
134 |
loc = self.CellControl.GetValue()
|
|
135 |
old_loc = self.Table.GetValueByName(row, 'Location')
|
|
136 |
changed = loc != old_loc
|
|
137 |
if changed:
|
|
138 |
self.Table.SetValueByName(row, 'Location', loc)
|
|
139 |
self.CellControl.Disable()
|
|
140 |
return changed
|
|
141 |
|
|
142 |
def SetSize(self, rect):
|
|
143 |
self.CellControl.SetDimensions(rect.x + 1, rect.y,
|
|
144 |
rect.width, rect.height,
|
|
145 |
wx.SIZE_ALLOW_MINUS_ONE)
|
|
146 |
|
|
147 |
def Clone(self):
|
|
148 |
return LocationCellEditor(self.Table, self.Controller)
|
|
149 |
|