author | Laurent Bessard |
Mon, 06 May 2013 11:15:05 +0200 | |
changeset 1092 | e91f2c8d6f51 |
parent 1062 | fd7c9a7cf882 |
child 1124 | b1705000eba1 |
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 |
|
728
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
725
diff
changeset
|
16 |
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
|
17 |
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
|
18 |
|
1061 | 19 |
def _getWXGLADEpath(self, project_path=None): |
20 |
if project_path is None: |
|
21 |
project_path = self.CTNPath() |
|
22 |
# define name for wxGlade gui file |
|
23 |
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
|
24 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
25 |
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
|
26 |
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
|
27 |
path = os.path.dirname(fileName) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
28 |
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
|
29 |
if wx.Platform == '__WXMSW__': |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
30 |
glade = "\"%s\""%glade |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
31 |
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
|
32 |
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
|
33 |
|
1061 | 34 |
def OnCTNSave(self, from_project_path=None): |
35 |
if from_project_path is not None: |
|
36 |
shutil.copyfile(self._getWXGLADEpath(from_project_path), |
|
37 |
self._getWXGLADEpath()) |
|
38 |
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
|
39 |
|
718 | 40 |
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
|
41 |
""" |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
42 |
Return C code generated by iec2c compiler |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
43 |
when _generate_softPLC have been called |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
44 |
@param locations: ignored |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
45 |
@return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND |
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 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
48 |
current_location = self.GetCurrentLocation() |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
49 |
# define a unique name for the generated C file |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
50 |
location_str = "_".join(map(lambda x:str(x), current_location)) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
51 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
52 |
runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
53 |
runtimefile = open(runtimefile_path, 'w') |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
54 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
55 |
hmi_frames = {} |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
56 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
57 |
wxgfile_path=self._getWXGLADEpath() |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
58 |
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
|
59 |
wxgfile = open(wxgfile_path, 'r') |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
60 |
wxgtree = minidom.parse(wxgfile) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
61 |
wxgfile.close() |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
62 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
63 |
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
|
64 |
if node.nodeType == wxgtree.ELEMENT_NODE: |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
65 |
hmi_frames[node._attrs["name"].value] = node._attrs["class"].value |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
66 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
67 |
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
|
68 |
if wx.Platform == '__WXMSW__': |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
69 |
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
|
70 |
wxghmipyfile_path = "\"%s\""%hmipyfile_path |
50f2069a06b3
Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents:
367
diff
changeset
|
71 |
else: |
50f2069a06b3
Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents:
367
diff
changeset
|
72 |
wxghmipyfile_path = hmipyfile_path |
50f2069a06b3
Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents:
367
diff
changeset
|
73 |
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
|
74 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
75 |
hmipyfile = open(hmipyfile_path, 'r') |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
76 |
runtimefile.write(hmipyfile.read()) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
77 |
hmipyfile.close() |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
78 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
79 |
runtimefile.write(self.GetPythonCode()) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
80 |
runtimefile.write(""" |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
81 |
%(declare)s |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
82 |
|
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
834
diff
changeset
|
83 |
def _runtime_%(location)s_start(): |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
84 |
global %(global)s |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
85 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
86 |
def OnCloseFrame(evt): |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
87 |
wx.MessageBox(_("Please stop PLC to close")) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
88 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
89 |
%(init)s |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
90 |
|
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
834
diff
changeset
|
91 |
def _runtime_%(location)s_stop(): |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
92 |
global %(global)s |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
93 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
94 |
%(cleanup)s |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
95 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
96 |
""" % {"location": location_str, |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
97 |
"declare": "\n".join(map(lambda x:"%s = None" % x, hmi_frames.keys())), |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
98 |
"global": ",".join(hmi_frames.keys()), |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
99 |
"init": "\n".join(map(lambda x: """ |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
100 |
%(name)s = %(class)s(None) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
101 |
%(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
102 |
%(name)s.Show() |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
103 |
""" % {"name": x[0], "class": x[1]}, |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
104 |
hmi_frames.items())), |
1053
b0ac30ba7eaf
Fixed error when stopping PLC with wxglade HMI
Edouard Tisserant
parents:
1014
diff
changeset
|
105 |
"cleanup": "\n ".join(map(lambda x:"if %s is not None: %s.Destroy()" % (x,x), hmi_frames.keys()))}) |
367
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
106 |
runtimefile.close() |
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 |
return [], "", False, ("runtime_%s.py"%location_str, file(runtimefile_path,"rb")) |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
109 |
|
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
110 |
def _editWXGLADE(self): |
a76ee5307bb7
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff
changeset
|
111 |
wxg_filename = self._getWXGLADEpath() |
427 | 112 |
open_wxglade = True |
718 | 113 |
if not self.GetCTRoot().CheckProjectPathPerm(): |
114 |
dialog = wx.MessageDialog(self.GetCTRoot().AppFrame, |
|
427 | 115 |
_("You don't have write permissions.\nOpen wxGlade anyway ?"), |
116 |
_("Open wxGlade"), |
|
117 |
wx.YES_NO|wx.ICON_QUESTION) |
|
118 |
open_wxglade = dialog.ShowModal() == wx.ID_YES |
|
119 |
dialog.Destroy() |
|
120 |
if open_wxglade: |
|
121 |
if not os.path.exists(wxg_filename): |
|
122 |
hmi_name = self.BaseParams.getName() |
|
123 |
open(wxg_filename,"w").write("""<?xml version="1.0"?> |
|
124 |
<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"> |
|
125 |
<object class="%(class)s" name="%(name)s" base="EditFrame"> |
|
126 |
<style>wxDEFAULT_FRAME_STYLE</style> |
|
127 |
<title>frame_1</title> |
|
834
d613696aad01
Adding default sizer in main frame in default wxGlade GUI file
laurent
parents:
734
diff
changeset
|
128 |
<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
|
129 |
<orient>wxVERTICAL</orient> |
d613696aad01
Adding default sizer in main frame in default wxGlade GUI file
laurent
parents:
734
diff
changeset
|
130 |
<object class="sizerslot" /> |
d613696aad01
Adding default sizer in main frame in default wxGlade GUI file
laurent
parents:
734
diff
changeset
|
131 |
</object> |
427 | 132 |
</object> |
133 |
</application> |
|
134 |
""" % {"name": hmi_name, "class": "Class_%s" % hmi_name}) |
|
135 |
if wx.Platform == '__WXMSW__': |
|
136 |
wxg_filename = "\"%s\""%wxg_filename |
|
137 |
self.launch_wxglade([wxg_filename]) |
|
1061 | 138 |