22 # You should have received a copy of the GNU General Public License |
22 # You should have received a copy of the GNU General Public License |
23 # along with this program; if not, write to the Free Software |
23 # along with this program; if not, write to the Free Software |
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
25 |
25 |
26 import wx |
26 import wx |
27 import os, sys, shutil |
27 import os |
|
28 import sys |
|
29 import shutil |
28 |
30 |
29 from pyjs import translate |
31 from pyjs import translate |
30 |
32 |
31 import util.paths as paths |
33 import util.paths as paths |
32 from POULibrary import POULibrary |
34 from POULibrary import POULibrary |
33 from docutil import open_svg |
35 from docutil import open_svg |
34 from py_ext import PythonFileCTNMixin |
36 from py_ext import PythonFileCTNMixin |
35 |
37 |
|
38 |
36 class SVGUILibrary(POULibrary): |
39 class SVGUILibrary(POULibrary): |
37 def GetLibraryPath(self): |
40 def GetLibraryPath(self): |
38 return paths.AbsNeighbourFile(__file__, "pous.xml") |
41 return paths.AbsNeighbourFile(__file__, "pous.xml") |
|
42 |
39 |
43 |
40 class SVGUI(PythonFileCTNMixin): |
44 class SVGUI(PythonFileCTNMixin): |
41 |
45 |
42 ConfNodeMethods = [ |
46 ConfNodeMethods = [ |
43 {"bitmap" : "ImportSVG", |
47 { |
44 "name" : _("Import SVG"), |
48 "bitmap": "ImportSVG", |
45 "tooltip" : _("Import SVG"), |
49 "name": _("Import SVG"), |
46 "method" : "_ImportSVG"}, |
50 "tooltip": _("Import SVG"), |
47 {"bitmap" : "ImportSVG", # should be something different |
51 "method": "_ImportSVG" |
48 "name" : _("Inkscape"), |
52 }, |
49 "tooltip" : _("Create HMI"), |
53 { |
50 "method" : "_StartInkscape"}, |
54 "bitmap": "ImportSVG", # should be something different |
|
55 "name": _("Inkscape"), |
|
56 "tooltip": _("Create HMI"), |
|
57 "method": "_StartInkscape" |
|
58 }, |
51 ] |
59 ] |
52 |
60 |
53 def ConfNodePath(self): |
61 def ConfNodePath(self): |
54 return paths.AbsDir(__file__) |
62 return paths.AbsDir(__file__) |
55 |
63 |
68 self._getSVGpath()) |
76 self._getSVGpath()) |
69 return PythonFileCTNMixin.OnCTNSave(self, from_project_path) |
77 return PythonFileCTNMixin.OnCTNSave(self, from_project_path) |
70 |
78 |
71 def CTNGenerate_C(self, buildpath, locations): |
79 def CTNGenerate_C(self, buildpath, locations): |
72 """ |
80 """ |
73 Return C code generated by iec2c compiler |
81 Return C code generated by iec2c compiler |
74 when _generate_softPLC have been called |
82 when _generate_softPLC have been called |
75 @param locations: ignored |
83 @param locations: ignored |
76 @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND |
84 @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND |
77 """ |
85 """ |
78 |
86 |
79 current_location = self.GetCurrentLocation() |
87 current_location = self.GetCurrentLocation() |
80 # define a unique name for the generated C file |
88 # define a unique name for the generated C file |
81 location_str = "_".join(map(lambda x:str(x), current_location)) |
89 location_str = "_".join(map(lambda x: str(x), current_location)) |
82 |
90 |
83 res = ([], "", False) |
91 res = ([], "", False) |
84 |
92 |
85 svgfile=self._getSVGpath() |
93 svgfile = self._getSVGpath() |
86 if os.path.exists(svgfile): |
94 if os.path.exists(svgfile): |
87 res += (("gui.svg", file(svgfile,"rb")),) |
95 res += (("gui.svg", file(svgfile, "rb")),) |
88 |
96 |
89 svguiserverfile = open(self._getSVGUIserverpath(), 'r') |
97 svguiserverfile = open(self._getSVGUIserverpath(), 'r') |
90 svguiservercode = svguiserverfile.read() |
98 svguiservercode = svguiserverfile.read() |
91 svguiserverfile.close() |
99 svguiserverfile.close() |
92 |
100 |
93 svguilibpath = os.path.join(self._getBuildPath(), "svguilib.js") |
101 svguilibpath = os.path.join(self._getBuildPath(), "svguilib.js") |
94 svguilibfile = open(svguilibpath, 'w') |
102 svguilibfile = open(svguilibpath, 'w') |
95 fpath=paths.AbsDir(__file__) |
103 fpath = paths.AbsDir(__file__) |
96 svguilibfile.write(translate(os.path.join(fpath, "pyjs", "lib", "sys.py"), "sys")) |
104 svguilibfile.write(translate(os.path.join(fpath, "pyjs", "lib", "sys.py"), "sys")) |
97 svguilibfile.write(open(os.path.join(fpath, "pyjs", "lib", "_pyjs.js"), 'r').read()) |
105 svguilibfile.write(open(os.path.join(fpath, "pyjs", "lib", "_pyjs.js"), 'r').read()) |
98 svguilibfile.write(translate(os.path.join(fpath, "pyjs", "lib", "pyjslib.py"), "pyjslib")) |
106 svguilibfile.write(translate(os.path.join(fpath, "pyjs", "lib", "pyjslib.py"), "pyjslib")) |
99 svguilibfile.write(translate(os.path.join(fpath, "svguilib.py"), "svguilib")) |
107 svguilibfile.write(translate(os.path.join(fpath, "svguilib.py"), "svguilib")) |
100 svguilibfile.write("pyjslib();\nsvguilib();\n") |
108 svguilibfile.write("pyjslib();\nsvguilib();\n") |
101 svguilibfile.write(open(os.path.join(fpath, "pyjs", "lib", "json.js"), 'r').read()) |
109 svguilibfile.write(open(os.path.join(fpath, "pyjs", "lib", "json.js"), 'r').read()) |
102 svguilibfile.write(open(os.path.join(fpath, "livesvg.js"), 'r').read()) |
110 svguilibfile.write(open(os.path.join(fpath, "livesvg.js"), 'r').read()) |
103 svguilibfile.close() |
111 svguilibfile.close() |
104 jsmodules = {"LiveSVGPage": "svguilib.js"} |
112 jsmodules = {"LiveSVGPage": "svguilib.js"} |
105 res += (("svguilib.js", file(svguilibpath,"rb")),) |
113 res += (("svguilib.js", file(svguilibpath, "rb")),) |
106 |
114 |
107 runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str) |
115 runtimefile_path = os.path.join(buildpath, "runtime_%s.py" % location_str) |
108 runtimefile = open(runtimefile_path, 'w') |
116 runtimefile = open(runtimefile_path, 'w') |
109 runtimefile.write(svguiservercode % {"svgfile" : "gui.svg"}) |
117 runtimefile.write(svguiservercode % {"svgfile": "gui.svg"}) |
110 runtimefile.write(""" |
118 runtimefile.write(""" |
111 def _runtime_%(location)s_start(): |
119 def _runtime_%(location)s_start(): |
112 website.LoadHMI(%(svgui_class)s, %(jsmodules)s) |
120 website.LoadHMI(%(svgui_class)s, %(jsmodules)s) |
113 |
121 |
114 def _runtime_%(location)s_stop(): |
122 def _runtime_%(location)s_stop(): |
115 website.UnLoadHMI() |
123 website.UnLoadHMI() |
116 |
124 |
117 """ % {"location": location_str, |
125 """ % {"location": location_str, |
118 "svgui_class": "SVGUI_HMI", |
126 "svgui_class": "SVGUI_HMI", |
119 "jsmodules" : str(jsmodules), |
127 "jsmodules": str(jsmodules)}) |
120 }) |
|
121 runtimefile.close() |
128 runtimefile.close() |
122 |
129 |
123 res += (("runtime_%s.py"%location_str, file(runtimefile_path,"rb")),) |
130 res += (("runtime_%s.py" % location_str, file(runtimefile_path, "rb")),) |
124 |
131 |
125 return res |
132 return res |
126 |
133 |
127 def _ImportSVG(self): |
134 def _ImportSVG(self): |
128 dialog = wx.FileDialog(self.GetCTRoot().AppFrame, _("Choose a SVG file"), os.getcwd(), "", _("SVG files (*.svg)|*.svg|All files|*.*"), wx.OPEN) |
135 dialog = wx.FileDialog(self.GetCTRoot().AppFrame, _("Choose a SVG file"), os.getcwd(), "", _("SVG files (*.svg)|*.svg|All files|*.*"), wx.OPEN) |
129 if dialog.ShowModal() == wx.ID_OK: |
136 if dialog.ShowModal() == wx.ID_OK: |
130 svgpath = dialog.GetPath() |
137 svgpath = dialog.GetPath() |
131 if os.path.isfile(svgpath): |
138 if os.path.isfile(svgpath): |
132 shutil.copy(svgpath, self._getSVGpath()) |
139 shutil.copy(svgpath, self._getSVGpath()) |
133 else: |
140 else: |
134 self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n")%svgpath) |
141 self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n") % svgpath) |
135 dialog.Destroy() |
142 dialog.Destroy() |
136 |
143 |
137 def _StartInkscape(self): |
144 def _StartInkscape(self): |
138 svgfile = self._getSVGpath() |
145 svgfile = self._getSVGpath() |
139 open_inkscape = True |
146 open_inkscape = True |
140 if not self.GetCTRoot().CheckProjectPathPerm(): |
147 if not self.GetCTRoot().CheckProjectPathPerm(): |
141 dialog = wx.MessageDialog(self.GetCTRoot().AppFrame, |
148 dialog = wx.MessageDialog(self.GetCTRoot().AppFrame, |
142 _("You don't have write permissions.\nOpen Inkscape anyway ?"), |
149 _("You don't have write permissions.\nOpen Inkscape anyway ?"), |
143 _("Open Inkscape"), |
150 _("Open Inkscape"), |
144 wx.YES_NO|wx.ICON_QUESTION) |
151 wx.YES_NO | wx.ICON_QUESTION) |
145 open_inkscape = dialog.ShowModal() == wx.ID_YES |
152 open_inkscape = dialog.ShowModal() == wx.ID_YES |
146 dialog.Destroy() |
153 dialog.Destroy() |
147 if open_inkscape: |
154 if open_inkscape: |
148 if not os.path.isfile(svgfile): |
155 if not os.path.isfile(svgfile): |
149 svgfile = None |
156 svgfile = None |