author | Laurent Bessard |
Mon, 10 Jun 2013 21:42:30 +0200 | |
changeset 1241 | 368f8516706c |
parent 1237 | 0c8b8ef9559b |
child 1242 | ec2c415fc65e |
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 |
||
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
25 |
import re |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
26 |
|
814 | 27 |
import wx |
28 |
||
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
29 |
from graphics.FBD_Objects import FBD_Block |
814 | 30 |
from controls.LibraryPanel import LibraryPanel |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
31 |
from BlockPreviewDialog import BlockPreviewDialog |
814 | 32 |
|
33 |
#------------------------------------------------------------------------------- |
|
34 |
# Create New Block Dialog |
|
35 |
#------------------------------------------------------------------------------- |
|
36 |
||
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
37 |
class FBDBlockDialog(BlockPreviewDialog): |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
38 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
39 |
def __init__(self, parent, controller, tagname): |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
40 |
BlockPreviewDialog.__init__(self, parent, controller, tagname, |
814 | 41 |
size=wx.Size(600, 450), title=_('Block Properties')) |
42 |
||
43 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=4, vgap=10) |
|
44 |
main_sizer.AddGrowableCol(0) |
|
45 |
main_sizer.AddGrowableRow(0) |
|
46 |
||
47 |
column_sizer = wx.BoxSizer(wx.HORIZONTAL) |
|
48 |
main_sizer.AddSizer(column_sizer, border=20, |
|
49 |
flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) |
|
50 |
||
51 |
type_staticbox = wx.StaticBox(self, label=_('Type:')) |
|
52 |
left_staticboxsizer = wx.StaticBoxSizer(type_staticbox, wx.VERTICAL) |
|
53 |
column_sizer.AddSizer(left_staticboxsizer, 1, border=5, |
|
54 |
flag=wx.GROW|wx.RIGHT) |
|
55 |
||
56 |
self.LibraryPanel = LibraryPanel(self) |
|
57 |
setattr(self.LibraryPanel, "_OnTreeItemSelected", |
|
58 |
self.OnLibraryTreeItemSelected) |
|
59 |
left_staticboxsizer.AddWindow(self.LibraryPanel, 1, border=5, |
|
60 |
flag=wx.GROW|wx.TOP) |
|
61 |
||
62 |
right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=5) |
|
63 |
right_gridsizer.AddGrowableCol(0) |
|
64 |
right_gridsizer.AddGrowableRow(2) |
|
65 |
column_sizer.AddSizer(right_gridsizer, 1, border=5, |
|
66 |
flag=wx.GROW|wx.LEFT) |
|
67 |
||
68 |
top_right_gridsizer = wx.FlexGridSizer(cols=2, hgap=0, rows=4, vgap=5) |
|
69 |
top_right_gridsizer.AddGrowableCol(1) |
|
70 |
right_gridsizer.AddSizer(top_right_gridsizer, flag=wx.GROW) |
|
71 |
||
72 |
name_label = wx.StaticText(self, label=_('Name:')) |
|
73 |
top_right_gridsizer.AddWindow(name_label, |
|
74 |
flag=wx.ALIGN_CENTER_VERTICAL) |
|
75 |
||
76 |
self.BlockName = wx.TextCtrl(self) |
|
77 |
self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.BlockName) |
|
78 |
top_right_gridsizer.AddWindow(self.BlockName, flag=wx.GROW) |
|
79 |
||
80 |
inputs_label = wx.StaticText(self, label=_('Inputs:')) |
|
81 |
top_right_gridsizer.AddWindow(inputs_label, |
|
82 |
flag=wx.ALIGN_CENTER_VERTICAL) |
|
83 |
||
84 |
self.Inputs = wx.SpinCtrl(self, min=2, max=20, |
|
85 |
style=wx.SP_ARROW_KEYS) |
|
86 |
self.Bind(wx.EVT_SPINCTRL, self.OnInputsChanged, self.Inputs) |
|
87 |
top_right_gridsizer.AddWindow(self.Inputs, flag=wx.GROW) |
|
88 |
||
89 |
execution_order_label = wx.StaticText(self, label=_('Execution Order:')) |
|
90 |
top_right_gridsizer.AddWindow(execution_order_label, |
|
91 |
flag=wx.ALIGN_CENTER_VERTICAL) |
|
92 |
||
93 |
self.ExecutionOrder = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS) |
|
94 |
self.Bind(wx.EVT_SPINCTRL, self.OnExecutionOrderChanged, self.ExecutionOrder) |
|
95 |
top_right_gridsizer.AddWindow(self.ExecutionOrder, flag=wx.GROW) |
|
96 |
||
97 |
execution_control_label = wx.StaticText(self, label=_('Execution Control:')) |
|
98 |
top_right_gridsizer.AddWindow(execution_control_label, |
|
99 |
flag=wx.ALIGN_CENTER_VERTICAL) |
|
100 |
||
101 |
self.ExecutionControl = wx.CheckBox(self) |
|
102 |
self.Bind(wx.EVT_CHECKBOX, self.OnExecutionOrderChanged, self.ExecutionControl) |
|
103 |
top_right_gridsizer.AddWindow(self.ExecutionControl, flag=wx.GROW) |
|
104 |
||
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
105 |
right_gridsizer.AddWindow(self.PreviewLabel, flag=wx.GROW) |
814 | 106 |
right_gridsizer.AddWindow(self.Preview, flag=wx.GROW) |
107 |
||
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
108 |
main_sizer.AddSizer(self.ButtonSizer, border=20, |
814 | 109 |
flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
110 |
||
111 |
self.SetSizer(main_sizer) |
|
112 |
||
113 |
self.BlockName.SetValue("") |
|
114 |
self.BlockName.Enable(False) |
|
115 |
self.Inputs.Enable(False) |
|
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
116 |
self.CurrentBlockName = None |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
117 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
118 |
self.LibraryPanel.SetBlockList(controller.GetBlockTypes(tagname)) |
814 | 119 |
self.LibraryPanel.SetFocus() |
120 |
||
121 |
def OnOK(self, event): |
|
122 |
message = None |
|
123 |
selected = self.LibraryPanel.GetSelectedBlock() |
|
124 |
block_name = self.BlockName.GetValue() |
|
125 |
name_enabled = self.BlockName.IsEnabled() |
|
126 |
if selected is None: |
|
127 |
message = _("Form isn't complete. Valid block type must be selected!") |
|
128 |
elif name_enabled and block_name == "": |
|
129 |
message = _("Form isn't complete. Name must be filled!") |
|
130 |
if message is not None: |
|
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
131 |
self.ShowMessage(message) |
1237
0c8b8ef9559b
Fixed support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1236
diff
changeset
|
132 |
elif not name_enabled or self.TestBlockName(block_name): |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
133 |
BlockPreviewDialog.OnOK(self, event) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
134 |
|
814 | 135 |
def SetValues(self, values): |
136 |
blocktype = values.get("type", None) |
|
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
137 |
default_name_model = re.compile("%s[0-9]+" % blocktype) |
814 | 138 |
if blocktype is not None: |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
139 |
self.LibraryPanel.SelectTreeItem(blocktype, |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
140 |
values.get("inputs", None)) |
814 | 141 |
for name, value in values.items(): |
142 |
if name == "name": |
|
1237
0c8b8ef9559b
Fixed support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1236
diff
changeset
|
143 |
if value != "": |
0c8b8ef9559b
Fixed support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1236
diff
changeset
|
144 |
self.DefaultBlockName = value |
0c8b8ef9559b
Fixed support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1236
diff
changeset
|
145 |
if default_name_model.match(value) is None: |
0c8b8ef9559b
Fixed support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1236
diff
changeset
|
146 |
self.CurrentBlockName = value |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
147 |
self.BlockName.ChangeValue(value) |
814 | 148 |
elif name == "extension": |
149 |
self.Inputs.SetValue(value) |
|
150 |
elif name == "executionOrder": |
|
151 |
self.ExecutionOrder.SetValue(value) |
|
152 |
elif name == "executionControl": |
|
153 |
self.ExecutionControl.SetValue(value) |
|
154 |
self.RefreshPreview() |
|
155 |
||
156 |
def GetValues(self): |
|
157 |
values = self.LibraryPanel.GetSelectedBlock() |
|
158 |
if self.BlockName.IsEnabled() and self.BlockName.GetValue() != "": |
|
159 |
values["name"] = self.BlockName.GetValue() |
|
160 |
values["width"], values["height"] = self.Block.GetSize() |
|
161 |
values["extension"] = self.Inputs.GetValue() |
|
162 |
values["executionOrder"] = self.ExecutionOrder.GetValue() |
|
163 |
values["executionControl"] = self.ExecutionControl.GetValue() |
|
164 |
return values |
|
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
165 |
|
814 | 166 |
def OnLibraryTreeItemSelected(self, event): |
167 |
values = self.LibraryPanel.GetSelectedBlock() |
|
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
168 |
blocktype = (self.Controller.GetBlockType(values["type"], |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
169 |
values["inputs"]) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
170 |
if values is not None else None) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
171 |
|
814 | 172 |
if blocktype is not None: |
173 |
self.Inputs.SetValue(len(blocktype["inputs"])) |
|
174 |
self.Inputs.Enable(blocktype["extensible"]) |
|
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
175 |
else: |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
176 |
self.Inputs.SetValue(2) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
177 |
self.Inputs.Enable(False) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
178 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
179 |
if blocktype is not None and blocktype["type"] != "function": |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
180 |
self.BlockName.Enable(True) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
181 |
self.BlockName.ChangeValue( |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
182 |
self.CurrentBlockName |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
183 |
if self.CurrentBlockName is not None |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
184 |
else self.Controller.GenerateNewName( |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
185 |
self.TagName, None, values["type"]+"%d", 0)) |
814 | 186 |
else: |
187 |
self.BlockName.Enable(False) |
|
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
188 |
self.BlockName.ChangeValue("") |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
189 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
190 |
self.RefreshPreview() |
814 | 191 |
|
192 |
def OnNameChanged(self, event): |
|
193 |
if self.BlockName.IsEnabled(): |
|
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
194 |
self.CurrentBlockName = self.BlockName.GetValue() |
814 | 195 |
self.RefreshPreview() |
196 |
event.Skip() |
|
197 |
||
198 |
def OnInputsChanged(self, event): |
|
199 |
if self.Inputs.IsEnabled(): |
|
200 |
self.RefreshPreview() |
|
201 |
event.Skip() |
|
202 |
||
203 |
def OnExecutionOrderChanged(self, event): |
|
204 |
self.RefreshPreview() |
|
205 |
event.Skip() |
|
206 |
||
207 |
def OnExecutionControlChanged(self, event): |
|
208 |
self.RefreshPreview() |
|
209 |
event.Skip() |
|
210 |
||
211 |
def RefreshPreview(self): |
|
212 |
values = self.LibraryPanel.GetSelectedBlock() |
|
213 |
if values is not None: |
|
214 |
if self.BlockName.IsEnabled(): |
|
215 |
blockname = self.BlockName.GetValue() |
|
216 |
else: |
|
217 |
blockname = "" |
|
218 |
self.Block = FBD_Block(self.Preview, values["type"], |
|
219 |
blockname, |
|
220 |
extension = self.Inputs.GetValue(), |
|
221 |
inputs = values["inputs"], |
|
222 |
executionControl = self.ExecutionControl.GetValue(), |
|
223 |
executionOrder = self.ExecutionOrder.GetValue()) |
|
224 |
else: |
|
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
225 |
self.Block = None |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
1230
diff
changeset
|
226 |
BlockPreviewDialog.RefreshPreview(self) |