laurent@371: import wx laurent@407: import os, sys, shutil laurent@371: laurent@371: from plugger import opjimg laurent@371: from plugins.python import PythonCodeTemplate laurent@371: laurent@371: from pyjs import translate laurent@371: laurent@371: from docutils import * laurent@371: laurent@371: class RootClass: laurent@371: laurent@371: PluginMethods = [ laurent@371: {"bitmap" : os.path.join("images","ImportSVG"), laurent@415: "name" : _("Import SVG"), laurent@415: "tooltip" : _("Import SVG"), laurent@371: "method" : "_ImportSVG"}, laurent@371: {"bitmap" : os.path.join("images","ImportSVG"), laurent@415: "name" : _("Inkscape"), laurent@415: "tooltip" : _("Create HMI"), laurent@371: "method" : "_StartInkscape"}, laurent@371: ] laurent@371: laurent@371: def PluginPath(self): laurent@371: return os.path.join(self.PlugParent.PluginPath(), "modules", self.PlugType) laurent@371: laurent@371: def _getSVGpath(self): laurent@371: # define name for IEC raw code file laurent@371: return os.path.join(self.PlugPath(), "gui.svg") laurent@371: laurent@371: def _getSVGUIserverpath(self): laurent@371: return os.path.join(os.path.dirname(__file__), "svgui_server.py") laurent@371: laurent@371: def PlugGenerate_C(self, buildpath, locations): laurent@371: """ laurent@371: Return C code generated by iec2c compiler laurent@371: when _generate_softPLC have been called laurent@371: @param locations: ignored laurent@371: @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND laurent@371: """ laurent@371: laurent@371: current_location = self.GetCurrentLocation() laurent@371: # define a unique name for the generated C file laurent@371: location_str = "_".join(map(lambda x:str(x), current_location)) laurent@371: laurent@371: res = ([], "", False) laurent@371: laurent@371: svgfile=self._getSVGpath() laurent@371: if os.path.exists(svgfile): laurent@371: res += (("gui.svg", file(svgfile,"rb")),) laurent@371: laurent@371: svguiserverfile = open(self._getSVGUIserverpath(), 'r') laurent@371: svguiservercode = svguiserverfile.read() laurent@371: svguiserverfile.close() laurent@371: laurent@371: svguilibpath = os.path.join(self._getBuildPath(), "svguilib.js") laurent@371: svguilibfile = open(svguilibpath, 'w') laurent@371: svguilibfile.write(translate(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "sys.py"), "sys")) laurent@371: svguilibfile.write(open(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "_pyjs.js"), 'r').read()) laurent@371: svguilibfile.write(translate(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "pyjslib.py"), "pyjslib")) laurent@371: svguilibfile.write(translate(os.path.join(os.path.dirname(__file__), "svguilib.py"), "svguilib")) laurent@371: svguilibfile.write("pyjslib();\nsvguilib();\n") laurent@371: svguilibfile.write(open(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "json.js"), 'r').read()) laurent@371: svguilibfile.write(open(os.path.join(os.path.dirname(__file__), "livesvg.js"), 'r').read()) laurent@371: svguilibfile.close() laurent@371: jsmodules = {"LiveSVGPage": "svguilib.js"} laurent@371: res += (("svguilib.js", file(svguilibpath,"rb")),) laurent@371: laurent@371: runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str) laurent@371: runtimefile = open(runtimefile_path, 'w') laurent@371: runtimefile.write(svguiservercode % {"svgfile" : "gui.svg"}) laurent@371: runtimefile.write(""" laurent@371: def _runtime_%(location)s_begin(): laurent@371: website.LoadHMI(%(svgui_class)s, %(jsmodules)s) laurent@371: laurent@371: def _runtime_%(location)s_cleanup(): laurent@371: website.UnLoadHMI() laurent@371: laurent@371: """ % {"location": location_str, laurent@371: "svgui_class": "SVGUI_HMI", laurent@371: "jsmodules" : str(jsmodules), laurent@371: }) laurent@371: runtimefile.close() laurent@371: laurent@371: res += (("runtime_%s.py"%location_str, file(runtimefile_path,"rb")),) laurent@371: laurent@371: return res laurent@371: laurent@371: def _ImportSVG(self): laurent@415: dialog = wx.FileDialog(self.GetPlugRoot().AppFrame, _("Choose a SVG file"), os.getcwd(), "", _("SVG files (*.svg)|*.svg|All files|*.*"), wx.OPEN) laurent@371: if dialog.ShowModal() == wx.ID_OK: laurent@371: svgpath = dialog.GetPath() laurent@371: if os.path.isfile(svgpath): laurent@371: shutil.copy(svgpath, self._getSVGpath()) laurent@371: else: laurent@421: self.GetPlugRoot().logger.write_error(_("No such SVG file: %s\n")%svgpath) laurent@371: dialog.Destroy() laurent@371: laurent@371: def _StartInkscape(self): laurent@371: svgfile = self._getSVGpath() greg@427: open_inkscape = True greg@427: if not self.GetPlugRoot().CheckProjectPathPerm(): greg@427: dialog = wx.MessageDialog(self.GetPlugRoot().AppFrame, greg@427: _("You don't have write permissions.\nOpen Inkscape anyway ?"), greg@427: _("Open Inkscape"), greg@427: wx.YES_NO|wx.ICON_QUESTION) greg@427: open_inkscape = dialog.ShowModal() == wx.ID_YES greg@427: dialog.Destroy() greg@427: if open_inkscape: greg@427: if not os.path.isfile(svgfile): greg@427: svgfile = None greg@427: open_svg(svgfile)