wxglade_hmi/wxglade_hmi.py
changeset 1730 64d8f52bc8c8
parent 1688 457155545922
child 1732 94ffe74e6895
equal deleted inserted replaced
1726:d51af006fa6b 1730:64d8f52bc8c8
    54 
    54 
    55     def GetWxGladePath(self):
    55     def GetWxGladePath(self):
    56         path = None
    56         path = None
    57         try:
    57         try:
    58             from wxglade import __file__ as fileName
    58             from wxglade import __file__ as fileName
    59             path = os.path.dirname(fileName)         
    59             path = os.path.dirname(fileName)
    60             return path
    60             return path
    61         except ImportError:
    61         except ImportError:
    62             pass
    62             pass
    63 
    63 
    64         defLibDir="/usr/share/wxglade"
    64         defLibDir="/usr/share/wxglade"
    65         if os.path.isdir(defLibDir):
    65         if os.path.isdir(defLibDir):
    66             path = defLibDir
    66             path = defLibDir
    67 
    67 
    68         return path
    68         return path
    69     
    69 
    70     def launch_wxglade(self, options, wait=False):
    70     def launch_wxglade(self, options, wait=False):
    71         path = self.GetWxGladePath()
    71         path = self.GetWxGladePath()
    72         glade = os.path.join(path, 'wxglade.py')
    72         glade = os.path.join(path, 'wxglade.py')
    73         if wx.Platform == '__WXMSW__':
    73         if wx.Platform == '__WXMSW__':
    74             glade = "\"%s\""%glade
    74             glade = "\"%s\""%glade
    80             shutil.copyfile(self._getWXGLADEpath(from_project_path),
    80             shutil.copyfile(self._getWXGLADEpath(from_project_path),
    81                             self._getWXGLADEpath())
    81                             self._getWXGLADEpath())
    82         return PythonFileCTNMixin.OnCTNSave(self, from_project_path)
    82         return PythonFileCTNMixin.OnCTNSave(self, from_project_path)
    83 
    83 
    84     def CTNGenerate_C(self, buildpath, locations):
    84     def CTNGenerate_C(self, buildpath, locations):
    85         
    85 
    86         hmi_frames = []
    86         hmi_frames = []
    87         
    87 
    88         wxgfile_path=self._getWXGLADEpath()
    88         wxgfile_path=self._getWXGLADEpath()
    89         if os.path.exists(wxgfile_path):
    89         if os.path.exists(wxgfile_path):
    90             wxgfile = open(wxgfile_path, 'r')
    90             wxgfile = open(wxgfile_path, 'r')
    91             wxgtree = minidom.parse(wxgfile)
    91             wxgtree = minidom.parse(wxgfile)
    92             wxgfile.close()
    92             wxgfile.close()
    93             
    93 
    94             for node in wxgtree.childNodes[1].childNodes:
    94             for node in wxgtree.childNodes[1].childNodes:
    95                 if node.nodeType == wxgtree.ELEMENT_NODE:
    95                 if node.nodeType == wxgtree.ELEMENT_NODE:
    96                     hmi_frames.append({
    96                     hmi_frames.append({
    97                         "name" : node.getAttribute("name"),
    97                         "name" : node.getAttribute("name"),
    98                         "class" : node.getAttribute("class"),
    98                         "class" : node.getAttribute("class"),
    99                         "handlers" : [
    99                         "handlers" : [
   100                             hnode.firstChild.data for hnode in 
   100                             hnode.firstChild.data for hnode in
   101                             node.getElementsByTagName("handler")]})
   101                             node.getElementsByTagName("handler")]})
   102                     
   102 
   103             hmipyfile_path=os.path.join(self._getBuildPath(), "hmi.py")
   103             hmipyfile_path=os.path.join(self._getBuildPath(), "hmi.py")
   104             if wx.Platform == '__WXMSW__':
   104             if wx.Platform == '__WXMSW__':
   105                 wxgfile_path = "\"%s\""%wxgfile_path
   105                 wxgfile_path = "\"%s\""%wxgfile_path
   106                 wxghmipyfile_path = "\"%s\""%hmipyfile_path
   106                 wxghmipyfile_path = "\"%s\""%hmipyfile_path
   107             else:
   107             else:
   108                 wxghmipyfile_path = hmipyfile_path
   108                 wxghmipyfile_path = hmipyfile_path
   109             self.launch_wxglade(['-o', wxghmipyfile_path, '-g', 'python', wxgfile_path], wait=True)
   109             self.launch_wxglade(['-o', wxghmipyfile_path, '-g', 'python', wxgfile_path], wait=True)
   110             
   110 
   111             hmipyfile = open(hmipyfile_path, 'r')
   111             hmipyfile = open(hmipyfile_path, 'r')
   112             define_hmi = hmipyfile.read().decode('utf-8')
   112             define_hmi = hmipyfile.read().decode('utf-8')
   113             hmipyfile.close()
   113             hmipyfile.close()
   114         
   114 
   115         else:
   115         else:
   116             define_hmi = ""
   116             define_hmi = ""
   117         
   117 
   118         declare_hmi = "\n".join(["%(name)s = None\n" % x + 
   118         declare_hmi = "\n".join(["%(name)s = None\n" % x +
   119                           "\n".join(["%(class)s.%(h)s = %(h)s"%
   119                           "\n".join(["%(class)s.%(h)s = %(h)s"%
   120                             dict(x,h=h) for h in x['handlers']])
   120                             dict(x,h=h) for h in x['handlers']])
   121                                 for x in hmi_frames])
   121                                 for x in hmi_frames])
   122         global_hmi = ("global %s\n"%",".join(
   122         global_hmi = ("global %s\n"%",".join(
   123                          [x["name"] for x in hmi_frames]) 
   123                          [x["name"] for x in hmi_frames])
   124                       if len(hmi_frames) > 0 else "")
   124                       if len(hmi_frames) > 0 else "")
   125         init_hmi = "\n".join(["""\
   125         init_hmi = "\n".join(["""\
   126 def OnCloseFrame(evt):
   126 def OnCloseFrame(evt):
   127     wx.MessageBox(_("Please stop PLC to close"))
   127     wx.MessageBox(_("Please stop PLC to close"))
   128 
   128 
   129 %(name)s = %(class)s(None)
   129 %(name)s = %(class)s(None)
   130 %(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame)
   130 %(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame)
   131 %(name)s.Show()
   131 %(name)s.Show()
   132 """ % x for x in hmi_frames])
   132 """ % x for x in hmi_frames])
   133         cleanup_hmi = "\n".join(
   133         cleanup_hmi = "\n".join(
   134             ["if %(name)s is not None: %(name)s.Destroy()" % x 
   134             ["if %(name)s is not None: %(name)s.Destroy()" % x
   135                 for x in hmi_frames])
   135                 for x in hmi_frames])
   136         
   136 
   137         self.PreSectionsTexts = {
   137         self.PreSectionsTexts = {
   138             "globals":define_hmi,
   138             "globals":define_hmi,
   139             "start":global_hmi,
   139             "start":global_hmi,
   140             "stop":global_hmi + cleanup_hmi
   140             "stop":global_hmi + cleanup_hmi
   141         }
   141         }
   172     </application>
   172     </application>
   173     """ % {"name": hmi_name, "class": "Class_%s" % hmi_name})
   173     """ % {"name": hmi_name, "class": "Class_%s" % hmi_name})
   174             if wx.Platform == '__WXMSW__':
   174             if wx.Platform == '__WXMSW__':
   175                 wxg_filename = "\"%s\""%wxg_filename
   175                 wxg_filename = "\"%s\""%wxg_filename
   176             self.launch_wxglade([wxg_filename])
   176             self.launch_wxglade([wxg_filename])
   177