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