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