author | Laurent Bessard |
Wed, 02 Oct 2013 17:58:16 +0200 | |
changeset 1340 | 7e5702d1f246 |
parent 1061 | 02f371f3e063 |
child 1511 | 91538d0c242c |
permissions | -rw-r--r-- |
371 | 1 |
import wx |
407 | 2 |
import os, sys, shutil |
371 | 3 |
|
4 |
from pyjs import translate |
|
5 |
||
728
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
725
diff
changeset
|
6 |
from POULibrary import POULibrary |
737 | 7 |
from docutil import open_svg |
1061 | 8 |
from py_ext import PythonFileCTNMixin |
371 | 9 |
|
728
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
725
diff
changeset
|
10 |
class SVGUILibrary(POULibrary): |
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
725
diff
changeset
|
11 |
def GetLibraryPath(self): |
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
725
diff
changeset
|
12 |
return os.path.join(os.path.split(__file__)[0], "pous.xml") |
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
725
diff
changeset
|
13 |
|
1061 | 14 |
class SVGUI(PythonFileCTNMixin): |
371 | 15 |
|
717 | 16 |
ConfNodeMethods = [ |
734 | 17 |
{"bitmap" : "ImportSVG", |
415
339fa2542481
improved english spelling and grammar and internationalization updated
laurent
parents:
410
diff
changeset
|
18 |
"name" : _("Import SVG"), |
339fa2542481
improved english spelling and grammar and internationalization updated
laurent
parents:
410
diff
changeset
|
19 |
"tooltip" : _("Import SVG"), |
371 | 20 |
"method" : "_ImportSVG"}, |
734 | 21 |
{"bitmap" : "ImportSVG", # should be something different |
415
339fa2542481
improved english spelling and grammar and internationalization updated
laurent
parents:
410
diff
changeset
|
22 |
"name" : _("Inkscape"), |
339fa2542481
improved english spelling and grammar and internationalization updated
laurent
parents:
410
diff
changeset
|
23 |
"tooltip" : _("Create HMI"), |
371 | 24 |
"method" : "_StartInkscape"}, |
25 |
] |
|
26 |
||
717 | 27 |
def ConfNodePath(self): |
728
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
725
diff
changeset
|
28 |
return os.path.join(os.path.dirname(__file__)) |
371 | 29 |
|
1061 | 30 |
def _getSVGpath(self, project_path=None): |
31 |
if project_path is None: |
|
32 |
project_path = self.CTNPath() |
|
33 |
# define name for SVG file containing gui layout |
|
34 |
return os.path.join(project_path, "gui.svg") |
|
371 | 35 |
|
36 |
def _getSVGUIserverpath(self): |
|
37 |
return os.path.join(os.path.dirname(__file__), "svgui_server.py") |
|
38 |
||
1061 | 39 |
def OnCTNSave(self, from_project_path=None): |
40 |
if from_project_path is not None: |
|
41 |
shutil.copyfile(self._getSVGpath(from_project_path), |
|
42 |
self._getSVGpath()) |
|
43 |
return PythonFileCTNMixin.OnCTNSave(self, from_project_path) |
|
44 |
||
718 | 45 |
def CTNGenerate_C(self, buildpath, locations): |
371 | 46 |
""" |
47 |
Return C code generated by iec2c compiler |
|
48 |
when _generate_softPLC have been called |
|
49 |
@param locations: ignored |
|
50 |
@return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND |
|
51 |
""" |
|
52 |
||
53 |
current_location = self.GetCurrentLocation() |
|
54 |
# define a unique name for the generated C file |
|
55 |
location_str = "_".join(map(lambda x:str(x), current_location)) |
|
56 |
||
57 |
res = ([], "", False) |
|
58 |
||
59 |
svgfile=self._getSVGpath() |
|
60 |
if os.path.exists(svgfile): |
|
61 |
res += (("gui.svg", file(svgfile,"rb")),) |
|
62 |
||
63 |
svguiserverfile = open(self._getSVGUIserverpath(), 'r') |
|
64 |
svguiservercode = svguiserverfile.read() |
|
65 |
svguiserverfile.close() |
|
66 |
||
67 |
svguilibpath = os.path.join(self._getBuildPath(), "svguilib.js") |
|
68 |
svguilibfile = open(svguilibpath, 'w') |
|
69 |
svguilibfile.write(translate(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "sys.py"), "sys")) |
|
70 |
svguilibfile.write(open(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "_pyjs.js"), 'r').read()) |
|
71 |
svguilibfile.write(translate(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "pyjslib.py"), "pyjslib")) |
|
72 |
svguilibfile.write(translate(os.path.join(os.path.dirname(__file__), "svguilib.py"), "svguilib")) |
|
73 |
svguilibfile.write("pyjslib();\nsvguilib();\n") |
|
74 |
svguilibfile.write(open(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "json.js"), 'r').read()) |
|
75 |
svguilibfile.write(open(os.path.join(os.path.dirname(__file__), "livesvg.js"), 'r').read()) |
|
76 |
svguilibfile.close() |
|
77 |
jsmodules = {"LiveSVGPage": "svguilib.js"} |
|
78 |
res += (("svguilib.js", file(svguilibpath,"rb")),) |
|
79 |
||
80 |
runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str) |
|
81 |
runtimefile = open(runtimefile_path, 'w') |
|
82 |
runtimefile.write(svguiservercode % {"svgfile" : "gui.svg"}) |
|
83 |
runtimefile.write(""" |
|
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
737
diff
changeset
|
84 |
def _runtime_%(location)s_start(): |
371 | 85 |
website.LoadHMI(%(svgui_class)s, %(jsmodules)s) |
86 |
||
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
737
diff
changeset
|
87 |
def _runtime_%(location)s_stop(): |
371 | 88 |
website.UnLoadHMI() |
89 |
||
90 |
""" % {"location": location_str, |
|
91 |
"svgui_class": "SVGUI_HMI", |
|
92 |
"jsmodules" : str(jsmodules), |
|
93 |
}) |
|
94 |
runtimefile.close() |
|
95 |
||
96 |
res += (("runtime_%s.py"%location_str, file(runtimefile_path,"rb")),) |
|
97 |
||
98 |
return res |
|
99 |
||
100 |
def _ImportSVG(self): |
|
718 | 101 |
dialog = wx.FileDialog(self.GetCTRoot().AppFrame, _("Choose a SVG file"), os.getcwd(), "", _("SVG files (*.svg)|*.svg|All files|*.*"), wx.OPEN) |
371 | 102 |
if dialog.ShowModal() == wx.ID_OK: |
103 |
svgpath = dialog.GetPath() |
|
104 |
if os.path.isfile(svgpath): |
|
105 |
shutil.copy(svgpath, self._getSVGpath()) |
|
106 |
else: |
|
718 | 107 |
self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n")%svgpath) |
371 | 108 |
dialog.Destroy() |
109 |
||
110 |
def _StartInkscape(self): |
|
111 |
svgfile = self._getSVGpath() |
|
427 | 112 |
open_inkscape = True |
718 | 113 |
if not self.GetCTRoot().CheckProjectPathPerm(): |
114 |
dialog = wx.MessageDialog(self.GetCTRoot().AppFrame, |
|
427 | 115 |
_("You don't have write permissions.\nOpen Inkscape anyway ?"), |
116 |
_("Open Inkscape"), |
|
117 |
wx.YES_NO|wx.ICON_QUESTION) |
|
118 |
open_inkscape = dialog.ShowModal() == wx.ID_YES |
|
119 |
dialog.Destroy() |
|
120 |
if open_inkscape: |
|
121 |
if not os.path.isfile(svgfile): |
|
122 |
svgfile = None |
|
123 |
open_svg(svgfile) |