author | Laurent Bessard |
Wed, 12 Jun 2013 00:20:05 +0200 | |
changeset 1248 | 2f08985625c0 |
parent 1247 | 92588e69d853 |
child 1249 | b956c924cbbd |
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 |
||
1245 | 35 |
class ConnectionDialog(BlockPreviewDialog): |
814 | 36 |
|
1245 | 37 |
def __init__(self, parent, controller, tagname, apply_button=False): |
38 |
""" |
|
39 |
Constructor |
|
40 |
@param parent: Parent wx.Window of dialog for modal |
|
41 |
@param controller: Reference to project controller |
|
42 |
@param tagname: Tagname of project POU edited |
|
43 |
@param apply_button: Enable button for applying connector modification |
|
44 |
to all connector having the same name in POU (default: False) |
|
45 |
""" |
|
46 |
BlockPreviewDialog.__init__(self, parent, controller, tagname, |
|
814 | 47 |
size=wx.Size(350, 220), title=_('Connection Properties')) |
48 |
||
1245 | 49 |
# Create dialog main sizer |
814 | 50 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) |
51 |
main_sizer.AddGrowableCol(0) |
|
52 |
main_sizer.AddGrowableRow(0) |
|
53 |
||
1245 | 54 |
# Create a sizer for dividing FBD connection parameters in two columns |
814 | 55 |
column_sizer = wx.BoxSizer(wx.HORIZONTAL) |
56 |
main_sizer.AddSizer(column_sizer, border=20, |
|
57 |
flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) |
|
58 |
||
1245 | 59 |
# Create a sizer for left column |
814 | 60 |
left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=5, vgap=5) |
61 |
left_gridsizer.AddGrowableCol(0) |
|
62 |
column_sizer.AddSizer(left_gridsizer, 1, border=5, |
|
63 |
flag=wx.GROW|wx.RIGHT) |
|
64 |
||
1245 | 65 |
# Create label for connection type |
814 | 66 |
type_label = wx.StaticText(self, label=_('Type:')) |
67 |
left_gridsizer.AddWindow(type_label, flag=wx.GROW) |
|
68 |
||
1245 | 69 |
# Create radio buttons for selecting connection type |
1246 | 70 |
self.TypeRadioButtons = {} |
1245 | 71 |
first = True |
72 |
for type, label in [(CONNECTOR, _('Connector')), |
|
73 |
(CONTINUATION, _('Continuation'))]: |
|
74 |
radio_button = wx.RadioButton(self, label=label, |
|
1247 | 75 |
style=(wx.RB_GROUP if first else 0)) |
1245 | 76 |
radio_button.SetValue(first) |
77 |
self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, radio_button) |
|
78 |
left_gridsizer.AddWindow(radio_button, flag=wx.GROW) |
|
1246 | 79 |
self.TypeRadioButtons[type] = radio_button |
1245 | 80 |
first = False |
81 |
||
82 |
# Create label for connection name |
|
814 | 83 |
name_label = wx.StaticText(self, label=_('Name:')) |
84 |
left_gridsizer.AddWindow(name_label, flag=wx.GROW) |
|
85 |
||
1245 | 86 |
# Create text control for defining connection name |
814 | 87 |
self.ConnectionName = wx.TextCtrl(self) |
88 |
self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.ConnectionName) |
|
89 |
left_gridsizer.AddWindow(self.ConnectionName, flag=wx.GROW) |
|
90 |
||
1245 | 91 |
# Create a sizer for right column |
814 | 92 |
right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5) |
93 |
right_gridsizer.AddGrowableCol(0) |
|
94 |
right_gridsizer.AddGrowableRow(1) |
|
95 |
column_sizer.AddSizer(right_gridsizer, 1, border=5, |
|
96 |
flag=wx.GROW|wx.LEFT) |
|
97 |
||
1245 | 98 |
# Add preview panel and associated label to sizers |
99 |
right_gridsizer.AddWindow(self.PreviewLabel, flag=wx.GROW) |
|
814 | 100 |
right_gridsizer.AddWindow(self.Preview, flag=wx.GROW) |
101 |
||
1245 | 102 |
# Add buttons sizer to sizers |
103 |
main_sizer.AddSizer(self.ButtonSizer, border=20, |
|
814 | 104 |
flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
105 |
||
1245 | 106 |
# Add button for applying connection name modification to all connection |
107 |
# 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
|
108 |
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
|
109 |
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
|
110 |
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
|
111 |
_("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
|
112 |
self.Bind(wx.EVT_BUTTON, self.OnApplyToAll, self.ApplyToAllButton) |
1247 | 113 |
self.ButtonSizer.AddWindow(self.ApplyToAllButton, |
114 |
border=(3 if wx.Platform == '__WXMSW__' else 10), |
|
1245 | 115 |
flag=wx.LEFT) |
116 |
else: |
|
117 |
self.ConnectionName.ChangeValue( |
|
118 |
controller.GenerateNewName( |
|
119 |
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
|
120 |
|
814 | 121 |
self.SetSizer(main_sizer) |
122 |
||
1245 | 123 |
# Connector radio button is default control having keyboard focus |
1246 | 124 |
self.TypeRadioButtons[CONNECTOR].SetFocus() |
814 | 125 |
|
126 |
def SetValues(self, values): |
|
1245 | 127 |
""" |
128 |
Set default connection parameters |
|
129 |
@param values: Connection parameters values |
|
130 |
""" |
|
131 |
# For each parameters defined, set corresponding control value |
|
814 | 132 |
for name, value in values.items(): |
1245 | 133 |
|
134 |
# Parameter is connection type |
|
814 | 135 |
if name == "type": |
1246 | 136 |
self.TypeRadioButtons[value].SetValue(True) |
1245 | 137 |
|
138 |
# Parameter is connection name |
|
814 | 139 |
elif name == "name": |
140 |
self.ConnectionName.SetValue(value) |
|
1245 | 141 |
|
142 |
# Refresh preview panel |
|
814 | 143 |
self.RefreshPreview() |
144 |
||
145 |
def GetValues(self): |
|
1245 | 146 |
""" |
147 |
Return connection parameters defined in dialog |
|
148 |
@return: {parameter_name: parameter_value,...} |
|
149 |
""" |
|
150 |
values = { |
|
151 |
"type": (CONNECTOR |
|
1246 | 152 |
if self.TypeRadioButtons[CONNECTOR].GetValue() |
1245 | 153 |
else CONTINUATION), |
154 |
"name": self.ConnectionName.GetValue()} |
|
155 |
values["width"], values["height"] = self.Element.GetSize() |
|
814 | 156 |
return values |
157 |
||
1245 | 158 |
def TestConnectionName(self): |
159 |
""" |
|
160 |
Test that connection name is valid |
|
161 |
@return: True if connection name is valid |
|
162 |
""" |
|
814 | 163 |
message = None |
1245 | 164 |
|
165 |
# Get connection name typed by user |
|
814 | 166 |
connection_name = self.ConnectionName.GetValue() |
1245 | 167 |
|
168 |
# Test that a name have been defined |
|
814 | 169 |
if connection_name == "": |
170 |
message = _("Form isn't complete. Name must be filled!") |
|
1245 | 171 |
|
172 |
# If an error have been identify, show error message dialog |
|
814 | 173 |
if message is not None: |
1245 | 174 |
self.ShowErrorMessage(message) |
175 |
# 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
|
176 |
return False |
1245 | 177 |
|
178 |
# Return result of element name test |
|
179 |
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
|
180 |
|
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 OnOK(self, event): |
1245 | 182 |
""" |
183 |
Called when dialog OK 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(): |
|
814 | 189 |
self.EndModal(wx.ID_OK) |
190 |
||
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
|
191 |
def OnApplyToAll(self, event): |
1245 | 192 |
""" |
193 |
Called when Apply To All button is pressed |
|
194 |
Test if connection name is valid |
|
195 |
@param event: wx.Event from OK button |
|
196 |
""" |
|
197 |
# Close dialog if connection name is valid |
|
198 |
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
|
199 |
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
|
200 |
|
814 | 201 |
def OnTypeChanged(self, event): |
1245 | 202 |
""" |
203 |
Called when connection type changed |
|
204 |
@param event: wx.RadioButtonEvent |
|
205 |
""" |
|
814 | 206 |
self.RefreshPreview() |
207 |
event.Skip() |
|
208 |
||
209 |
def OnNameChanged(self, event): |
|
1245 | 210 |
""" |
211 |
Called when connection name value changed |
|
212 |
@param event: wx.TextEvent |
|
213 |
""" |
|
814 | 214 |
self.RefreshPreview() |
215 |
event.Skip() |
|
216 |
||
217 |
def RefreshPreview(self): |
|
1245 | 218 |
""" |
219 |
Refresh preview panel of graphic element |
|
220 |
Override BlockPreviewDialog function |
|
221 |
""" |
|
222 |
# Set graphic element displayed, creating a FBD connection element |
|
223 |
self.Element = FBD_Connector(self.Preview, |
|
224 |
(CONNECTOR |
|
1246 | 225 |
if self.TypeRadioButtons[CONNECTOR].GetValue() |
1245 | 226 |
else CONTINUATION), |
227 |
self.ConnectionName.GetValue()) |
|
228 |
||
229 |
# Call BlockPreviewDialog function |
|
230 |
BlockPreviewDialog.RefreshPreview(self) |
|
231 |