author | Laurent Bessard |
Thu, 11 Jul 2013 12:49:04 +0200 | |
changeset 1277 | 358db9d64aa1 |
parent 1250 | 7e6de17c687a |
child 1487 | 789acd7cc347 |
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 |
||
1245 | 27 |
from graphics.GraphicCommons import CONNECTOR, CONTINUATION |
28 |
from graphics.FBD_Objects import FBD_Connector |
|
29 |
from BlockPreviewDialog import BlockPreviewDialog |
|
814 | 30 |
|
31 |
#------------------------------------------------------------------------------- |
|
1245 | 32 |
# Set Connection Parameters Dialog |
814 | 33 |
#------------------------------------------------------------------------------- |
34 |
||
1249 | 35 |
""" |
36 |
Class that implements a dialog for defining parameters of a connection graphic |
|
37 |
element |
|
38 |
""" |
|
39 |
||
1245 | 40 |
class ConnectionDialog(BlockPreviewDialog): |
814 | 41 |
|
1245 | 42 |
def __init__(self, parent, controller, tagname, apply_button=False): |
43 |
""" |
|
44 |
Constructor |
|
45 |
@param parent: Parent wx.Window of dialog for modal |
|
46 |
@param controller: Reference to project controller |
|
47 |
@param tagname: Tagname of project POU edited |
|
48 |
@param apply_button: Enable button for applying connector modification |
|
49 |
to all connector having the same name in POU (default: False) |
|
50 |
""" |
|
51 |
BlockPreviewDialog.__init__(self, parent, controller, tagname, |
|
814 | 52 |
size=wx.Size(350, 220), title=_('Connection Properties')) |
53 |
||
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1249
diff
changeset
|
54 |
# Init common sizers |
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1249
diff
changeset
|
55 |
self._init_sizers(2, 0, 5, None, 2, 1) |
814 | 56 |
|
1245 | 57 |
# Create label for connection type |
814 | 58 |
type_label = wx.StaticText(self, label=_('Type:')) |
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1249
diff
changeset
|
59 |
self.LeftGridSizer.AddWindow(type_label, flag=wx.GROW) |
814 | 60 |
|
1245 | 61 |
# Create radio buttons for selecting connection type |
1246 | 62 |
self.TypeRadioButtons = {} |
1245 | 63 |
first = True |
64 |
for type, label in [(CONNECTOR, _('Connector')), |
|
65 |
(CONTINUATION, _('Continuation'))]: |
|
66 |
radio_button = wx.RadioButton(self, label=label, |
|
1247 | 67 |
style=(wx.RB_GROUP if first else 0)) |
1245 | 68 |
radio_button.SetValue(first) |
69 |
self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, radio_button) |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1249
diff
changeset
|
70 |
self.LeftGridSizer.AddWindow(radio_button, flag=wx.GROW) |
1246 | 71 |
self.TypeRadioButtons[type] = radio_button |
1245 | 72 |
first = False |
73 |
||
74 |
# Create label for connection name |
|
814 | 75 |
name_label = wx.StaticText(self, label=_('Name:')) |
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1249
diff
changeset
|
76 |
self.LeftGridSizer.AddWindow(name_label, flag=wx.GROW) |
814 | 77 |
|
1245 | 78 |
# Create text control for defining connection name |
814 | 79 |
self.ConnectionName = wx.TextCtrl(self) |
80 |
self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.ConnectionName) |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1249
diff
changeset
|
81 |
self.LeftGridSizer.AddWindow(self.ConnectionName, flag=wx.GROW) |
814 | 82 |
|
1245 | 83 |
# Add preview panel and associated label to sizers |
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1249
diff
changeset
|
84 |
self.RightGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW) |
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1249
diff
changeset
|
85 |
self.RightGridSizer.AddWindow(self.Preview, flag=wx.GROW) |
814 | 86 |
|
1245 | 87 |
# Add buttons sizer to sizers |
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1249
diff
changeset
|
88 |
self.MainSizer.AddSizer(self.ButtonSizer, border=20, |
814 | 89 |
flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
90 |
||
1245 | 91 |
# Add button for applying connection name modification to all connection |
92 |
# of POU |
|
856
b64e436f000e
Adding button in ConnectionDialog to propagate connection name modification to all connections with the same name in POU
Laurent Bessard
parents:
814
diff
changeset
|
93 |
if apply_button: |
b64e436f000e
Adding button in ConnectionDialog to propagate connection name modification to all connections with the same name in POU
Laurent Bessard
parents:
814
diff
changeset
|
94 |
self.ApplyToAllButton = wx.Button(self, label=_("Propagate Name")) |
b64e436f000e
Adding button in ConnectionDialog to propagate connection name modification to all connections with the same name in POU
Laurent Bessard
parents:
814
diff
changeset
|
95 |
self.ApplyToAllButton.SetToolTipString( |
b64e436f000e
Adding button in ConnectionDialog to propagate connection name modification to all connections with the same name in POU
Laurent Bessard
parents:
814
diff
changeset
|
96 |
_("Apply name modification to all continuations with the same name")) |
b64e436f000e
Adding button in ConnectionDialog to propagate connection name modification to all connections with the same name in POU
Laurent Bessard
parents:
814
diff
changeset
|
97 |
self.Bind(wx.EVT_BUTTON, self.OnApplyToAll, self.ApplyToAllButton) |
1247 | 98 |
self.ButtonSizer.AddWindow(self.ApplyToAllButton, |
99 |
border=(3 if wx.Platform == '__WXMSW__' else 10), |
|
1245 | 100 |
flag=wx.LEFT) |
101 |
else: |
|
102 |
self.ConnectionName.ChangeValue( |
|
103 |
controller.GenerateNewName( |
|
104 |
tagname, None, "Connection%d", 0)) |
|
856
b64e436f000e
Adding button in ConnectionDialog to propagate connection name modification to all connections with the same name in POU
Laurent Bessard
parents:
814
diff
changeset
|
105 |
|
1245 | 106 |
# Connector radio button is default control having keyboard focus |
1246 | 107 |
self.TypeRadioButtons[CONNECTOR].SetFocus() |
814 | 108 |
|
1249 | 109 |
def GetConnectionType(self): |
110 |
""" |
|
111 |
Return type selected for connection |
|
112 |
@return: Type selected (CONNECTOR or CONTINUATION) |
|
113 |
""" |
|
114 |
return (CONNECTOR |
|
115 |
if self.TypeRadioButtons[CONNECTOR].GetValue() |
|
116 |
else CONTINUATION) |
|
117 |
||
814 | 118 |
def SetValues(self, values): |
1245 | 119 |
""" |
120 |
Set default connection parameters |
|
121 |
@param values: Connection parameters values |
|
122 |
""" |
|
123 |
# For each parameters defined, set corresponding control value |
|
814 | 124 |
for name, value in values.items(): |
1245 | 125 |
|
126 |
# Parameter is connection type |
|
814 | 127 |
if name == "type": |
1246 | 128 |
self.TypeRadioButtons[value].SetValue(True) |
1245 | 129 |
|
130 |
# Parameter is connection name |
|
814 | 131 |
elif name == "name": |
132 |
self.ConnectionName.SetValue(value) |
|
1245 | 133 |
|
134 |
# Refresh preview panel |
|
814 | 135 |
self.RefreshPreview() |
136 |
||
137 |
def GetValues(self): |
|
1245 | 138 |
""" |
139 |
Return connection parameters defined in dialog |
|
140 |
@return: {parameter_name: parameter_value,...} |
|
141 |
""" |
|
142 |
values = { |
|
1249 | 143 |
"type": self.GetConnectionType(), |
1245 | 144 |
"name": self.ConnectionName.GetValue()} |
145 |
values["width"], values["height"] = self.Element.GetSize() |
|
814 | 146 |
return values |
147 |
||
1245 | 148 |
def TestConnectionName(self): |
149 |
""" |
|
150 |
Test that connection name is valid |
|
151 |
@return: True if connection name is valid |
|
152 |
""" |
|
814 | 153 |
message = None |
1245 | 154 |
|
155 |
# Get connection name typed by user |
|
814 | 156 |
connection_name = self.ConnectionName.GetValue() |
1245 | 157 |
|
158 |
# Test that a name have been defined |
|
814 | 159 |
if connection_name == "": |
160 |
message = _("Form isn't complete. Name must be filled!") |
|
1245 | 161 |
|
162 |
# If an error have been identify, show error message dialog |
|
814 | 163 |
if message is not None: |
1245 | 164 |
self.ShowErrorMessage(message) |
165 |
# Test failed |
|
856
b64e436f000e
Adding button in ConnectionDialog to propagate connection name modification to all connections with the same name in POU
Laurent Bessard
parents:
814
diff
changeset
|
166 |
return False |
1245 | 167 |
|
168 |
# Return result of element name test |
|
169 |
return self.TestElementName(connection_name) |
|
856
b64e436f000e
Adding button in ConnectionDialog to propagate connection name modification to all connections with the same name in POU
Laurent Bessard
parents:
814
diff
changeset
|
170 |
|
b64e436f000e
Adding button in ConnectionDialog to propagate connection name modification to all connections with the same name in POU
Laurent Bessard
parents:
814
diff
changeset
|
171 |
def OnOK(self, event): |
1245 | 172 |
""" |
173 |
Called when dialog OK button is pressed |
|
174 |
Test if connection name is valid |
|
175 |
@param event: wx.Event from OK button |
|
176 |
""" |
|
177 |
# Close dialog if connection name is valid |
|
178 |
if self.TestConnectionName(): |
|
814 | 179 |
self.EndModal(wx.ID_OK) |
180 |
||
856
b64e436f000e
Adding button in ConnectionDialog to propagate connection name modification to all connections with the same name in POU
Laurent Bessard
parents:
814
diff
changeset
|
181 |
def OnApplyToAll(self, event): |
1245 | 182 |
""" |
183 |
Called when Apply To All button is pressed |
|
184 |
Test if connection name is valid |
|
185 |
@param event: wx.Event from OK button |
|
186 |
""" |
|
187 |
# Close dialog if connection name is valid |
|
188 |
if self.TestConnectionName(): |
|
856
b64e436f000e
Adding button in ConnectionDialog to propagate connection name modification to all connections with the same name in POU
Laurent Bessard
parents:
814
diff
changeset
|
189 |
self.EndModal(wx.ID_YESTOALL) |
b64e436f000e
Adding button in ConnectionDialog to propagate connection name modification to all connections with the same name in POU
Laurent Bessard
parents:
814
diff
changeset
|
190 |
|
814 | 191 |
def OnTypeChanged(self, event): |
1245 | 192 |
""" |
193 |
Called when connection type changed |
|
194 |
@param event: wx.RadioButtonEvent |
|
195 |
""" |
|
814 | 196 |
self.RefreshPreview() |
197 |
event.Skip() |
|
198 |
||
199 |
def OnNameChanged(self, event): |
|
1245 | 200 |
""" |
201 |
Called when connection name value changed |
|
202 |
@param event: wx.TextEvent |
|
203 |
""" |
|
814 | 204 |
self.RefreshPreview() |
205 |
event.Skip() |
|
206 |
||
207 |
def RefreshPreview(self): |
|
1245 | 208 |
""" |
209 |
Refresh preview panel of graphic element |
|
210 |
Override BlockPreviewDialog function |
|
211 |
""" |
|
212 |
# Set graphic element displayed, creating a FBD connection element |
|
213 |
self.Element = FBD_Connector(self.Preview, |
|
1249 | 214 |
self.GetConnectionType(), |
1245 | 215 |
self.ConnectionName.GetValue()) |
216 |
||
217 |
# Call BlockPreviewDialog function |
|
218 |
BlockPreviewDialog.RefreshPreview(self) |
|
219 |