author | Edouard Tisserant |
Mon, 09 Feb 2015 13:38:00 +0100 | |
changeset 1444 | c162f1b0fbac |
parent 1256 | 02a710c7b5ed |
child 1511 | 91538d0c242c |
permissions | -rw-r--r-- |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
1 |
import wx |
1061 | 2 |
import os, sys, shutil |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
3 |
from xml.dom import minidom |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
4 |
|
728
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
725
diff
changeset
|
5 |
from py_ext import PythonFileCTNMixin |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
6 |
|
728
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
725
diff
changeset
|
7 |
class WxGladeHMI(PythonFileCTNMixin): |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
8 |
|
717 | 9 |
ConfNodeMethods = [ |
734 | 10 |
{"bitmap" : "editWXGLADE", |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
11 |
"name" : _("WXGLADE GUI"), |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
12 |
"tooltip" : _("Edit a WxWidgets GUI with WXGlade"), |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
13 |
"method" : "_editWXGLADE"}, |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
14 |
] |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
15 |
|
1163
32599dcf311f
Added icon for wxGlade_hmi extension editor
Laurent Bessard
parents:
1155
diff
changeset
|
16 |
def GetIconName(self): |
32599dcf311f
Added icon for wxGlade_hmi extension editor
Laurent Bessard
parents:
1155
diff
changeset
|
17 |
return "wxGlade" |
32599dcf311f
Added icon for wxGlade_hmi extension editor
Laurent Bessard
parents:
1155
diff
changeset
|
18 |
|
728
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
725
diff
changeset
|
19 |
def ConfNodePath(self): |
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
725
diff
changeset
|
20 |
return os.path.join(os.path.dirname(__file__)) |
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
725
diff
changeset
|
21 |
|
1061 | 22 |
def _getWXGLADEpath(self, project_path=None): |
23 |
if project_path is None: |
|
24 |
project_path = self.CTNPath() |
|
25 |
# define name for wxGlade gui file |
|
26 |
return os.path.join(project_path, "hmi.wxg") |
|
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
27 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
28 |
def launch_wxglade(self, options, wait=False): |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
29 |
from wxglade import __file__ as fileName |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
30 |
path = os.path.dirname(fileName) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
31 |
glade = os.path.join(path, 'wxglade.py') |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
32 |
if wx.Platform == '__WXMSW__': |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
33 |
glade = "\"%s\""%glade |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
34 |
mode = {False:os.P_NOWAIT, True:os.P_WAIT}[wait] |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
35 |
os.spawnv(mode, sys.executable, ["\"%s\""%sys.executable] + [glade] + options) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
36 |
|
1061 | 37 |
def OnCTNSave(self, from_project_path=None): |
38 |
if from_project_path is not None: |
|
39 |
shutil.copyfile(self._getWXGLADEpath(from_project_path), |
|
40 |
self._getWXGLADEpath()) |
|
41 |
return PythonFileCTNMixin.OnCTNSave(self, from_project_path) |
|
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
42 |
|
718 | 43 |
def CTNGenerate_C(self, buildpath, locations): |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
44 |
|
1155
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
45 |
hmi_frames = [] |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
46 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
47 |
wxgfile_path=self._getWXGLADEpath() |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
48 |
if os.path.exists(wxgfile_path): |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
49 |
wxgfile = open(wxgfile_path, 'r') |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
50 |
wxgtree = minidom.parse(wxgfile) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
51 |
wxgfile.close() |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
52 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
53 |
for node in wxgtree.childNodes[1].childNodes: |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
54 |
if node.nodeType == wxgtree.ELEMENT_NODE: |
1155
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
55 |
hmi_frames.append({ |
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
56 |
"name" : node.getAttribute("name"), |
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
57 |
"class" : node.getAttribute("class"), |
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
58 |
"handlers" : [ |
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
59 |
hnode.firstChild.data for hnode in |
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
60 |
node.getElementsByTagName("handler")]}) |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
61 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
62 |
hmipyfile_path=os.path.join(self._getBuildPath(), "hmi.py") |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
63 |
if wx.Platform == '__WXMSW__': |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
64 |
wxgfile_path = "\"%s\""%wxgfile_path |
384
50f2069a06b3
Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents:
367
diff
changeset
|
65 |
wxghmipyfile_path = "\"%s\""%hmipyfile_path |
50f2069a06b3
Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents:
367
diff
changeset
|
66 |
else: |
50f2069a06b3
Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents:
367
diff
changeset
|
67 |
wxghmipyfile_path = hmipyfile_path |
50f2069a06b3
Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents:
367
diff
changeset
|
68 |
self.launch_wxglade(['-o', wxghmipyfile_path, '-g', 'python', wxgfile_path], wait=True) |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
69 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
70 |
hmipyfile = open(hmipyfile_path, 'r') |
1132
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
71 |
define_hmi = hmipyfile.read().decode('utf-8') |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
72 |
hmipyfile.close() |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
73 |
|
1256
02a710c7b5ed
Fixed bug in wxGlade extension when compiling and starting project with no hmi defined
Laurent Bessard
parents:
1163
diff
changeset
|
74 |
else: |
02a710c7b5ed
Fixed bug in wxGlade extension when compiling and starting project with no hmi defined
Laurent Bessard
parents:
1163
diff
changeset
|
75 |
define_hmi = "" |
02a710c7b5ed
Fixed bug in wxGlade extension when compiling and starting project with no hmi defined
Laurent Bessard
parents:
1163
diff
changeset
|
76 |
|
1155
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
77 |
declare_hmi = "\n".join(["%(name)s = None\n" % x + |
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
78 |
"\n".join(["%(class)s.%(h)s = %(h)s"% |
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
79 |
dict(x,h=h) for h in x['handlers']]) |
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
80 |
for x in hmi_frames]) |
1256
02a710c7b5ed
Fixed bug in wxGlade extension when compiling and starting project with no hmi defined
Laurent Bessard
parents:
1163
diff
changeset
|
81 |
global_hmi = ("global %s\n"%",".join( |
02a710c7b5ed
Fixed bug in wxGlade extension when compiling and starting project with no hmi defined
Laurent Bessard
parents:
1163
diff
changeset
|
82 |
[x["name"] for x in hmi_frames]) |
02a710c7b5ed
Fixed bug in wxGlade extension when compiling and starting project with no hmi defined
Laurent Bessard
parents:
1163
diff
changeset
|
83 |
if len(hmi_frames) > 0 else "") |
1155
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
84 |
init_hmi = "\n".join(["""\ |
1132
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
85 |
def OnCloseFrame(evt): |
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
86 |
wx.MessageBox(_("Please stop PLC to close")) |
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
87 |
|
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
88 |
%(name)s = %(class)s(None) |
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
89 |
%(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame) |
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
90 |
%(name)s.Show() |
1155
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
91 |
""" % x for x in hmi_frames]) |
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
92 |
cleanup_hmi = "\n".join( |
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
93 |
["if %(name)s is not None: %(name)s.Destroy()" % x |
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
94 |
for x in hmi_frames]) |
1124
b1705000eba1
Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents:
1062
diff
changeset
|
95 |
|
1132
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
96 |
self.PreSectionsTexts = { |
1155
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
97 |
"globals":define_hmi, |
1132
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
98 |
"start":global_hmi, |
1151
38d6aaad8ffd
Fixed bug wxGlade hmi not closed when stopping PLC
Laurent Bessard
parents:
1132
diff
changeset
|
99 |
"stop":global_hmi + cleanup_hmi |
1132
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
100 |
} |
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
101 |
self.PostSectionsTexts = { |
1155
412e30abf7e5
Extended WxGlade HMI with automatic mapping of event handlers declared in wxglade editor
Edouard Tisserant
parents:
1151
diff
changeset
|
102 |
"globals":declare_hmi, |
1132
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
103 |
"start":init_hmi, |
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
104 |
} |
1124
b1705000eba1
Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents:
1062
diff
changeset
|
105 |
|
1132
28f96aa9c070
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents:
1131
diff
changeset
|
106 |
return PythonFileCTNMixin.CTNGenerate_C(self, buildpath, locations) |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
107 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
108 |
def _editWXGLADE(self): |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
109 |
wxg_filename = self._getWXGLADEpath() |
427 | 110 |
open_wxglade = True |
718 | 111 |
if not self.GetCTRoot().CheckProjectPathPerm(): |
112 |
dialog = wx.MessageDialog(self.GetCTRoot().AppFrame, |
|
427 | 113 |
_("You don't have write permissions.\nOpen wxGlade anyway ?"), |
114 |
_("Open wxGlade"), |
|
115 |
wx.YES_NO|wx.ICON_QUESTION) |
|
116 |
open_wxglade = dialog.ShowModal() == wx.ID_YES |
|
117 |
dialog.Destroy() |
|
118 |
if open_wxglade: |
|
119 |
if not os.path.exists(wxg_filename): |
|
120 |
hmi_name = self.BaseParams.getName() |
|
121 |
open(wxg_filename,"w").write("""<?xml version="1.0"?> |
|
122 |
<application path="" name="" class="" option="0" language="python" top_window="%(name)s" encoding="UTF-8" use_gettext="0" overwrite="0" use_new_namespace="1" for_version="2.8" is_template="0"> |
|
123 |
<object class="%(class)s" name="%(name)s" base="EditFrame"> |
|
124 |
<style>wxDEFAULT_FRAME_STYLE</style> |
|
125 |
<title>frame_1</title> |
|
834
d613696aad01
Adding default sizer in main frame in default wxGlade GUI file
laurent
parents:
734
diff
changeset
|
126 |
<object class="wxBoxSizer" name="sizer_1" base="EditBoxSizer"> |
d613696aad01
Adding default sizer in main frame in default wxGlade GUI file
laurent
parents:
734
diff
changeset
|
127 |
<orient>wxVERTICAL</orient> |
d613696aad01
Adding default sizer in main frame in default wxGlade GUI file
laurent
parents:
734
diff
changeset
|
128 |
<object class="sizerslot" /> |
d613696aad01
Adding default sizer in main frame in default wxGlade GUI file
laurent
parents:
734
diff
changeset
|
129 |
</object> |
427 | 130 |
</object> |
131 |
</application> |
|
132 |
""" % {"name": hmi_name, "class": "Class_%s" % hmi_name}) |
|
133 |
if wx.Platform == '__WXMSW__': |
|
134 |
wxg_filename = "\"%s\""%wxg_filename |
|
135 |
self.launch_wxglade([wxg_filename]) |
|
1061 | 136 |