|
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) 2013: 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 plcopen.structures import TestIdentifier, IEC_KEYWORDS |
|
28 from graphics.GraphicCommons import FREEDRAWING_MODE |
|
29 |
|
30 #------------------------------------------------------------------------------- |
|
31 # Dialog with preview for graphic block |
|
32 #------------------------------------------------------------------------------- |
|
33 |
|
34 """ |
|
35 Class that implements a generic dialog containing a preview panel for displaying |
|
36 graphic created by dialog |
|
37 """ |
|
38 |
|
39 class BlockPreviewDialog(wx.Dialog): |
|
40 |
|
41 def __init__(self, parent, controller, tagname, size, title): |
|
42 wx.Dialog.__init__(self, parent, size=size, title=title) |
|
43 |
|
44 self.Controller = controller |
|
45 self.TagName = tagname |
|
46 |
|
47 self.PreviewLabel = wx.StaticText(self, label=_('Preview:')) |
|
48 |
|
49 self.Preview = wx.Panel(self, style=wx.SIMPLE_BORDER) |
|
50 self.Preview.SetBackgroundColour(wx.WHITE) |
|
51 setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE) |
|
52 setattr(self.Preview, "GetScaling", lambda:None) |
|
53 setattr(self.Preview, "GetBlockType", controller.GetBlockType) |
|
54 setattr(self.Preview, "IsOfType", controller.IsOfType) |
|
55 self.Preview.Bind(wx.EVT_PAINT, self.OnPaint) |
|
56 |
|
57 self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
|
58 self.Bind(wx.EVT_BUTTON, self.OnOK, |
|
59 self.ButtonSizer.GetAffirmativeButton()) |
|
60 |
|
61 self.Block = None |
|
62 self.DefaultBlockName = None |
|
63 self.MinBlockSize = None |
|
64 |
|
65 def __del__(self): |
|
66 self.Controller = None |
|
67 |
|
68 def SetMinBlockSize(self, size): |
|
69 self.MinBlockSize = size |
|
70 |
|
71 def SetPreviewFont(self, font): |
|
72 self.Preview.SetFont(font) |
|
73 |
|
74 def TestBlockName(self, block_name): |
|
75 format = None |
|
76 uppercase_block_name = block_name.upper() |
|
77 if not TestIdentifier(block_name): |
|
78 format = _("\"%s\" is not a valid identifier!") |
|
79 elif uppercase_block_name in IEC_KEYWORDS: |
|
80 format = _("\"%s\" is a keyword. It can't be used!") |
|
81 elif uppercase_block_name in self.Controller.GetProjectPouNames(): |
|
82 format = _("\"%s\" pou already exists!") |
|
83 elif (self.DefaultBlockName.upper() != uppercase_block_name and |
|
84 uppercase_block_name in self.Controller.GetEditedElementVariables( |
|
85 self.TagName)): |
|
86 format = _("\"%s\" element for this pou already exists!") |
|
87 |
|
88 if format is not None: |
|
89 self.ShowErrorMessage(format % block_name) |
|
90 return False |
|
91 |
|
92 return True |
|
93 |
|
94 def ShowErrorMessage(self, message): |
|
95 dialog = wx.MessageDialog(self, message, |
|
96 _("Error"), |
|
97 wx.OK|wx.ICON_ERROR) |
|
98 dialog.ShowModal() |
|
99 dialog.Destroy() |
|
100 |
|
101 def OnOK(self, event): |
|
102 self.EndModal(wx.ID_OK) |
|
103 |
|
104 def RefreshPreview(self): |
|
105 dc = wx.ClientDC(self.Preview) |
|
106 dc.SetFont(self.Preview.GetFont()) |
|
107 dc.Clear() |
|
108 |
|
109 if self.Block is not None: |
|
110 min_width, min_height = self.Block.GetMinSize() |
|
111 width = max(self.MinBlockSize[0], min_width) |
|
112 height = max(self.MinBlockSize[1], min_height) |
|
113 self.Block.SetSize(width, height) |
|
114 clientsize = self.Preview.GetClientSize() |
|
115 x = (clientsize.width - width) / 2 |
|
116 y = (clientsize.height - height) / 2 |
|
117 self.Block.SetPosition(x, y) |
|
118 self.Block.Draw(dc) |
|
119 |
|
120 def OnPaint(self, event): |
|
121 self.RefreshPreview() |
|
122 event.Skip() |
|
123 |