author | Edouard TISSERANT <edouard.tisserant@gmail.com> |
Sun, 06 Dec 2009 23:05:47 +0100 | |
changeset 466 | 11263fd24566 |
parent 427 | 7ac746c07ff2 |
permissions | -rw-r--r-- |
371 | 1 |
import wx |
407 | 2 |
import os, sys, shutil |
371 | 3 |
|
4 |
from plugger import opjimg |
|
5 |
from plugins.python import PythonCodeTemplate |
|
6 |
||
7 |
from pyjs import translate |
|
8 |
||
9 |
from docutils import * |
|
10 |
||
11 |
class RootClass: |
|
12 |
||
13 |
PluginMethods = [ |
|
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 |
||
24 |
def PluginPath(self): |
|
25 |
return os.path.join(self.PlugParent.PluginPath(), "modules", self.PlugType) |
|
26 |
||
27 |
def _getSVGpath(self): |
|
28 |
# define name for IEC raw code file |
|
29 |
return os.path.join(self.PlugPath(), "gui.svg") |
|
30 |
||
31 |
def _getSVGUIserverpath(self): |
|
32 |
return os.path.join(os.path.dirname(__file__), "svgui_server.py") |
|
33 |
||
34 |
def PlugGenerate_C(self, buildpath, locations): |
|
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): |
|
415
339fa2542481
improved english spelling and grammar and internationalization updated
laurent
parents:
410
diff
changeset
|
90 |
dialog = wx.FileDialog(self.GetPlugRoot().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: |
|
421 | 96 |
self.GetPlugRoot().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 |
102 |
if not self.GetPlugRoot().CheckProjectPathPerm(): |
|
103 |
dialog = wx.MessageDialog(self.GetPlugRoot().AppFrame, |
|
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) |