plugins/python/modules/svgui/svgui.py
changeset 371 b7cb57a2da08
child 407 0bef13d73350
equal deleted inserted replaced
370:ee802ef00ca5 371:b7cb57a2da08
       
     1 import wx
       
     2 import os, sys
       
     3 
       
     4 from plugger import opjimg
       
     5 from plugins.python import PythonCodeTemplate
       
     6 
       
     7 from pyjs import translate
       
     8 
       
     9 from docutils import *
       
    10 
       
    11 class RootClass:
       
    12 
       
    13     PluginMethods = [
       
    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 PluginPath(self):
       
    25         return os.path.join(self.PlugParent.PluginPath(), "modules", self.PlugType)
       
    26 
       
    27     def _getSVGpath(self):
       
    28         # define name for IEC raw code file
       
    29         return os.path.join(self.PlugPath(), "gui.svg")
       
    30 
       
    31     def _getSVGUIserverpath(self):
       
    32         return os.path.join(os.path.dirname(__file__), "svgui_server.py")
       
    33 
       
    34     def PlugGenerate_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     print "SVGUI start"
       
    75     website.LoadHMI(%(svgui_class)s, %(jsmodules)s)
       
    76     
       
    77 def _runtime_%(location)s_cleanup():
       
    78     print "SVGUI stop"
       
    79     website.UnLoadHMI()
       
    80     
       
    81 """ % {"location": location_str,
       
    82        "svgui_class": "SVGUI_HMI",
       
    83        "jsmodules" : str(jsmodules),
       
    84       })
       
    85         runtimefile.close()
       
    86         
       
    87         res += (("runtime_%s.py"%location_str, file(runtimefile_path,"rb")),)
       
    88         
       
    89         return res
       
    90 
       
    91     def _ImportSVG(self):
       
    92         dialog = wx.FileDialog(self.GetPlugRoot().AppFrame, "Choose a SVG file", os.getcwd(), "",  "SVG files (*.svg)|*.svg|All files|*.*", wx.OPEN)
       
    93         if dialog.ShowModal() == wx.ID_OK:
       
    94             svgpath = dialog.GetPath()
       
    95             if os.path.isfile(svgpath):
       
    96                 shutil.copy(svgpath, self._getSVGpath())
       
    97             else:
       
    98                 self.logger.write_error("No such SVG file: %s\n"%svgpath)
       
    99         dialog.Destroy()  
       
   100 
       
   101     def _StartInkscape(self):
       
   102         svgfile = self._getSVGpath()
       
   103         if not os.path.isfile(svgfile):
       
   104             svgfile = None
       
   105         open_svg(svgfile)