py_ext/modules/svgui/svgui.py
changeset 728 e0424e96e3fd
parent 727 3edd2f19bce2
child 729 25054c592dc4
equal deleted inserted replaced
727:3edd2f19bce2 728:e0424e96e3fd
     1 import wx
       
     2 import os, sys, shutil
       
     3 
       
     4 from ConfigTreeNode import opjimg
       
     5 from py_ext import PythonCodeTemplate
       
     6 
       
     7 from pyjs import translate
       
     8 
       
     9 from docutils import *
       
    10 
       
    11 class RootClass:
       
    12 
       
    13     ConfNodeMethods = [
       
    14         {"bitmap" : os.path.join("images","ImportSVG"),
       
    15          "name" : _("Import SVG"),
       
    16          "tooltip" : _("Import SVG"),
       
    17          "method" : "_ImportSVG"},
       
    18         {"bitmap" : os.path.join("images","ImportSVG"),
       
    19          "name" : _("Inkscape"),
       
    20          "tooltip" : _("Create HMI"),
       
    21          "method" : "_StartInkscape"},
       
    22     ]
       
    23 
       
    24     def ConfNodePath(self):
       
    25         return os.path.join(self.CTNParent.ConfNodePath(), "modules", self.CTNType)
       
    26 
       
    27     def _getSVGpath(self):
       
    28         # define name for IEC raw code file
       
    29         return os.path.join(self.CTNPath(), "gui.svg")
       
    30 
       
    31     def _getSVGUIserverpath(self):
       
    32         return os.path.join(os.path.dirname(__file__), "svgui_server.py")
       
    33 
       
    34     def CTNGenerate_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):
       
    90         dialog = wx.FileDialog(self.GetCTRoot().AppFrame, _("Choose a SVG file"), os.getcwd(), "",  _("SVG files (*.svg)|*.svg|All files|*.*"), wx.OPEN)
       
    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:
       
    96                 self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n")%svgpath)
       
    97         dialog.Destroy()  
       
    98 
       
    99     def _StartInkscape(self):
       
   100         svgfile = self._getSVGpath()
       
   101         open_inkscape = True
       
   102         if not self.GetCTRoot().CheckProjectPathPerm():
       
   103             dialog = wx.MessageDialog(self.GetCTRoot().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)