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