svgui/svgui.py
changeset 1730 64d8f52bc8c8
parent 1680 6db967480b7d
child 1732 94ffe74e6895
equal deleted inserted replaced
1726:d51af006fa6b 1730:64d8f52bc8c8
    33 from docutil import open_svg
    33 from docutil import open_svg
    34 from py_ext import PythonFileCTNMixin
    34 from py_ext import PythonFileCTNMixin
    35 
    35 
    36 class SVGUILibrary(POULibrary):
    36 class SVGUILibrary(POULibrary):
    37     def GetLibraryPath(self):
    37     def GetLibraryPath(self):
    38         return paths.AbsNeighbourFile(__file__, "pous.xml") 
    38         return paths.AbsNeighbourFile(__file__, "pous.xml")
    39 
    39 
    40 class SVGUI(PythonFileCTNMixin):
    40 class SVGUI(PythonFileCTNMixin):
    41 
    41 
    42     ConfNodeMethods = [
    42     ConfNodeMethods = [
    43         {"bitmap" : "ImportSVG",
    43         {"bitmap" : "ImportSVG",
    68                             self._getSVGpath())
    68                             self._getSVGpath())
    69         return PythonFileCTNMixin.OnCTNSave(self, from_project_path)
    69         return PythonFileCTNMixin.OnCTNSave(self, from_project_path)
    70 
    70 
    71     def CTNGenerate_C(self, buildpath, locations):
    71     def CTNGenerate_C(self, buildpath, locations):
    72         """
    72         """
    73         Return C code generated by iec2c compiler 
    73         Return C code generated by iec2c compiler
    74         when _generate_softPLC have been called
    74         when _generate_softPLC have been called
    75         @param locations: ignored
    75         @param locations: ignored
    76         @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
    76         @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
    77         """
    77         """
    78         
    78 
    79         current_location = self.GetCurrentLocation()
    79         current_location = self.GetCurrentLocation()
    80         # define a unique name for the generated C file
    80         # define a unique name for the generated C file
    81         location_str = "_".join(map(lambda x:str(x), current_location))
    81         location_str = "_".join(map(lambda x:str(x), current_location))
    82         
    82 
    83         res = ([], "", False)
    83         res = ([], "", False)
    84         
    84 
    85         svgfile=self._getSVGpath()
    85         svgfile=self._getSVGpath()
    86         if os.path.exists(svgfile):
    86         if os.path.exists(svgfile):
    87             res += (("gui.svg", file(svgfile,"rb")),)
    87             res += (("gui.svg", file(svgfile,"rb")),)
    88 
    88 
    89         svguiserverfile = open(self._getSVGUIserverpath(), 'r')
    89         svguiserverfile = open(self._getSVGUIserverpath(), 'r')
   101         svguilibfile.write(open(os.path.join(fpath, "pyjs", "lib", "json.js"), 'r').read())
   101         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())
   102         svguilibfile.write(open(os.path.join(fpath, "livesvg.js"), 'r').read())
   103         svguilibfile.close()
   103         svguilibfile.close()
   104         jsmodules = {"LiveSVGPage": "svguilib.js"}
   104         jsmodules = {"LiveSVGPage": "svguilib.js"}
   105         res += (("svguilib.js", file(svguilibpath,"rb")),)
   105         res += (("svguilib.js", file(svguilibpath,"rb")),)
   106         
   106 
   107         runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str)
   107         runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str)
   108         runtimefile = open(runtimefile_path, 'w')
   108         runtimefile = open(runtimefile_path, 'w')
   109         runtimefile.write(svguiservercode % {"svgfile" : "gui.svg"})
   109         runtimefile.write(svguiservercode % {"svgfile" : "gui.svg"})
   110         runtimefile.write("""
   110         runtimefile.write("""
   111 def _runtime_%(location)s_start():
   111 def _runtime_%(location)s_start():
   112     website.LoadHMI(%(svgui_class)s, %(jsmodules)s)
   112     website.LoadHMI(%(svgui_class)s, %(jsmodules)s)
   113     
   113 
   114 def _runtime_%(location)s_stop():
   114 def _runtime_%(location)s_stop():
   115     website.UnLoadHMI()
   115     website.UnLoadHMI()
   116     
   116 
   117 """ % {"location": location_str,
   117 """ % {"location": location_str,
   118        "svgui_class": "SVGUI_HMI",
   118        "svgui_class": "SVGUI_HMI",
   119        "jsmodules" : str(jsmodules),
   119        "jsmodules" : str(jsmodules),
   120       })
   120       })
   121         runtimefile.close()
   121         runtimefile.close()
   122         
   122 
   123         res += (("runtime_%s.py"%location_str, file(runtimefile_path,"rb")),)
   123         res += (("runtime_%s.py"%location_str, file(runtimefile_path,"rb")),)
   124         
   124 
   125         return res
   125         return res
   126 
   126 
   127     def _ImportSVG(self):
   127     def _ImportSVG(self):
   128         dialog = wx.FileDialog(self.GetCTRoot().AppFrame, _("Choose a SVG file"), os.getcwd(), "",  _("SVG files (*.svg)|*.svg|All files|*.*"), wx.OPEN)
   128         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:
   129         if dialog.ShowModal() == wx.ID_OK:
   130             svgpath = dialog.GetPath()
   130             svgpath = dialog.GetPath()
   131             if os.path.isfile(svgpath):
   131             if os.path.isfile(svgpath):
   132                 shutil.copy(svgpath, self._getSVGpath())
   132                 shutil.copy(svgpath, self._getSVGpath())
   133             else:
   133             else:
   134                 self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n")%svgpath)
   134                 self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n")%svgpath)
   135         dialog.Destroy()  
   135         dialog.Destroy()
   136 
   136 
   137     def _StartInkscape(self):
   137     def _StartInkscape(self):
   138         svgfile = self._getSVGpath()
   138         svgfile = self._getSVGpath()
   139         open_inkscape = True
   139         open_inkscape = True
   140         if not self.GetCTRoot().CheckProjectPathPerm():
   140         if not self.GetCTRoot().CheckProjectPathPerm():