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