author | Laurent Bessard |
Tue, 11 Jun 2013 23:50:44 +0200 | |
changeset 1247 | 92588e69d853 |
parent 1246 | 101625efb1c1 |
child 1249 | b956c924cbbd |
permissions | -rw-r--r-- |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
2 |
# -*- coding: utf-8 -*- |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
3 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
5 |
#based on the plcopen standard. |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
6 |
# |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
7 |
#Copyright (C) 2013: Edouard TISSERANT and Laurent BESSARD |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
8 |
# |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
9 |
#See COPYING file for copyrights details. |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
10 |
# |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
11 |
#This library is free software; you can redistribute it and/or |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
12 |
#modify it under the terms of the GNU General Public |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
13 |
#License as published by the Free Software Foundation; either |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
15 |
# |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
16 |
#This library is distributed in the hope that it will be useful, |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
19 |
#General Public License for more details. |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
20 |
# |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
21 |
#You should have received a copy of the GNU General Public |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
22 |
#License along with this library; if not, write to the Free Software |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
24 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
25 |
import wx |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
26 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
27 |
from plcopen.structures import TestIdentifier, IEC_KEYWORDS |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
28 |
from graphics.GraphicCommons import FREEDRAWING_MODE |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
29 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
30 |
#------------------------------------------------------------------------------- |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
31 |
# Dialog with preview for graphic block |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
32 |
#------------------------------------------------------------------------------- |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
33 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
34 |
""" |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
35 |
Class that implements a generic dialog containing a preview panel for displaying |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
36 |
graphic created by dialog |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
37 |
""" |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
38 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
39 |
class BlockPreviewDialog(wx.Dialog): |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
40 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
41 |
def __init__(self, parent, controller, tagname, size, title): |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
42 |
""" |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
43 |
Constructor |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
44 |
@param parent: Parent wx.Window of dialog for modal |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
45 |
@param controller: Reference to project controller |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
46 |
@param tagname: Tagname of project POU edited |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
47 |
@param size: wx.Size object containing size of dialog |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
48 |
@param title: Title of dialog frame |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
49 |
""" |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
50 |
wx.Dialog.__init__(self, parent, size=size, title=title) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
51 |
|
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
52 |
# Save reference to |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
53 |
self.Controller = controller |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
54 |
self.TagName = tagname |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
55 |
|
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
56 |
# Label for preview |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
57 |
self.PreviewLabel = wx.StaticText(self, label=_('Preview:')) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
58 |
|
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
59 |
# Create Preview panel |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
60 |
self.Preview = wx.Panel(self, style=wx.SIMPLE_BORDER) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
61 |
self.Preview.SetBackgroundColour(wx.WHITE) |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
62 |
|
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
63 |
# Add function to preview panel so that it answers to graphic elements |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
64 |
# like Viewer |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
65 |
setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
66 |
setattr(self.Preview, "GetScaling", lambda:None) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
67 |
setattr(self.Preview, "GetBlockType", controller.GetBlockType) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
68 |
setattr(self.Preview, "IsOfType", controller.IsOfType) |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
69 |
|
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
70 |
# Bind paint event on Preview panel |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
71 |
self.Preview.Bind(wx.EVT_PAINT, self.OnPaint) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
72 |
|
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
73 |
# Add default dialog buttons sizer |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
74 |
self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
75 |
self.Bind(wx.EVT_BUTTON, self.OnOK, |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
76 |
self.ButtonSizer.GetAffirmativeButton()) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
77 |
|
1244 | 78 |
self.Element = None # Graphic element to display in preview |
79 |
self.MinElementSize = None # Graphic element minimal size |
|
80 |
||
81 |
# Variable containing the graphic element name when dialog is opened |
|
82 |
self.DefaultElementName = None |
|
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
83 |
|
1246 | 84 |
# List of variables defined in POU {var_name: (var_class, var_type),...} |
85 |
self.VariableList = {} |
|
86 |
||
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
87 |
def __del__(self): |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
88 |
""" |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
89 |
Destructor |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
90 |
""" |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
91 |
# Remove reference to project controller |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
92 |
self.Controller = None |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
93 |
|
1244 | 94 |
def SetMinElementSize(self, size): |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
95 |
""" |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
96 |
Define minimal graphic element size |
1244 | 97 |
@param size: Tuple containing minimal size (width, height) |
98 |
""" |
|
99 |
self.MinElementSize = size |
|
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
100 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
101 |
def SetPreviewFont(self, font): |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
102 |
""" |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
103 |
Set font of Preview panel |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
104 |
@param font: wx.Font object containing font style |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
105 |
""" |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
106 |
self.Preview.SetFont(font) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
107 |
|
1246 | 108 |
def RefreshVariableList(self): |
109 |
""" |
|
110 |
Extract list of variables defined in POU |
|
111 |
""" |
|
112 |
# Get list of variables defined in POU |
|
113 |
self.VariableList = { |
|
114 |
var["Name"]: (var["Class"], var["Type"]) |
|
115 |
for var in self.Controller.GetEditedElementInterfaceVars( |
|
116 |
self.TagName) |
|
117 |
if var["Edit"]} |
|
118 |
||
119 |
# Add POU name to variable list if POU is a function |
|
120 |
returntype = self.Controller.GetEditedElementInterfaceReturnType( |
|
121 |
self.TagName) |
|
122 |
if returntype is not None: |
|
123 |
self.VariableList[ |
|
124 |
self.Controller.GetEditedElementName(self.TagName)] = \ |
|
125 |
("Output", returntype) |
|
126 |
||
127 |
# Add POU name if POU is a transition |
|
128 |
words = self.TagName.split("::") |
|
129 |
if words[0] == "T": |
|
130 |
self.VariableList[words[2]] = ("Output", "BOOL") |
|
131 |
||
1244 | 132 |
def TestElementName(self, element_name): |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
133 |
""" |
1245 | 134 |
Test displayed graphic element name |
1244 | 135 |
@param element_name: Graphic element name |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
136 |
""" |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
137 |
# Variable containing error message format |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
138 |
message_format = None |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
139 |
# Get graphic element name in upper case |
1244 | 140 |
uppercase_element_name = element_name.upper() |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
141 |
|
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
142 |
# Test if graphic element name is a valid identifier |
1244 | 143 |
if not TestIdentifier(element_name): |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
144 |
message_format = _("\"%s\" is not a valid identifier!") |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
145 |
|
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
146 |
# Test that graphic element name isn't a keyword |
1244 | 147 |
elif uppercase_element_name in IEC_KEYWORDS: |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
148 |
message_format = _("\"%s\" is a keyword. It can't be used!") |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
149 |
|
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
150 |
# Test that graphic element name isn't a POU name |
1244 | 151 |
elif uppercase_element_name in self.Controller.GetProjectPouNames(): |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
152 |
message_format = _("\"%s\" pou already exists!") |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
153 |
|
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
154 |
# Test that graphic element name isn't already used in POU by a variable |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
155 |
# or another graphic element |
1244 | 156 |
elif ((self.DefaultElementName is None or |
157 |
self.DefaultElementName.upper() != uppercase_element_name) and |
|
158 |
uppercase_element_name in self.Controller.\ |
|
159 |
GetEditedElementVariables(self.TagName)): |
|
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
160 |
message_format = _("\"%s\" element for this pou already exists!") |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
161 |
|
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
162 |
# If an error have been identify, show error message dialog |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
163 |
if message_format is not None: |
1244 | 164 |
self.ShowErrorMessage(message_format % element_name) |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
165 |
# Test failed |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
166 |
return False |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
167 |
|
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
168 |
# Test succeed |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
169 |
return True |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
170 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
171 |
def ShowErrorMessage(self, message): |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
172 |
""" |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
173 |
Show an error message dialog over this dialog |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
174 |
@param message: Error message to display |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
175 |
""" |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
176 |
dialog = wx.MessageDialog(self, message, |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
177 |
_("Error"), |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
178 |
wx.OK|wx.ICON_ERROR) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
179 |
dialog.ShowModal() |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
180 |
dialog.Destroy() |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
181 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
182 |
def OnOK(self, event): |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
183 |
""" |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
184 |
Called when dialog OK button is pressed |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
185 |
Need to be overridden by inherited classes to check that dialog values |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
186 |
are valid |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
187 |
@param event: wx.Event from OK button |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
188 |
""" |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
189 |
# Close dialog |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
190 |
self.EndModal(wx.ID_OK) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
191 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
192 |
def RefreshPreview(self): |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
193 |
""" |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
194 |
Refresh preview panel of graphic element |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
195 |
May be overridden by inherited classes |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
196 |
""" |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
197 |
# Init preview panel paint device context |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
198 |
dc = wx.ClientDC(self.Preview) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
199 |
dc.SetFont(self.Preview.GetFont()) |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
200 |
dc.Clear() |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
201 |
|
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
202 |
# Return immediately if no graphic element defined |
1244 | 203 |
if self.Element is None: |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
204 |
return |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
205 |
|
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
206 |
# Calculate block size according to graphic element min size due to its |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
207 |
# parameters and graphic element min size defined |
1244 | 208 |
min_width, min_height = self.Element.GetMinSize() |
209 |
width = max(self.MinElementSize[0], min_width) |
|
210 |
height = max(self.MinElementSize[1], min_height) |
|
211 |
self.Element.SetSize(width, height) |
|
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
212 |
|
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
213 |
# Get Preview panel size |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
214 |
client_size = self.Preview.GetClientSize() |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
215 |
|
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
216 |
# If graphic element is too big to be displayed in preview panel, |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
217 |
# calculate preview panel scale so that graphic element fit inside |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
218 |
scale = (max(float(width) / client_size.width, |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
219 |
float(height) / client_size.height) * 1.2 |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
220 |
if width * 1.2 > client_size.width or |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
221 |
height * 1.2 > client_size.height |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
222 |
else 1.0) |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
223 |
dc.SetUserScale(1.0 / scale, 1.0 / scale) |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
224 |
|
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
225 |
# Center graphic element in preview panel |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
226 |
x = int(client_size.width * scale - width) / 2 |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
227 |
y = int(client_size.height * scale - height) / 2 |
1244 | 228 |
self.Element.SetPosition(x, y) |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
229 |
|
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
230 |
# Draw graphic element |
1244 | 231 |
self.Element.Draw(dc) |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
232 |
|
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
233 |
def OnPaint(self, event): |
1242
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
234 |
""" |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
235 |
Called when Preview panel need to be redraw |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
236 |
@param event: wx.PaintEvent |
ec2c415fc65e
Rewrite FBDBlockDialog and BlockPreviewDialog
Laurent Bessard
parents:
1241
diff
changeset
|
237 |
""" |
1236
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
238 |
self.RefreshPreview() |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
239 |
event.Skip() |
a5d1d2a2f366
Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff
changeset
|
240 |