wxglade_hmi/wxglade_hmi.py
changeset 2248 d9353e440887
parent 2188 7f59aa398669
child 2488 889c43872f4c
equal deleted inserted replaced
2247:921ba5658183 2248:d9353e440887
    78         path = self.GetWxGladePath()
    78         path = self.GetWxGladePath()
    79         glade = os.path.join(path, 'wxglade.py')
    79         glade = os.path.join(path, 'wxglade.py')
    80         if wx.Platform == '__WXMSW__':
    80         if wx.Platform == '__WXMSW__':
    81             glade = "\"%s\"" % glade
    81             glade = "\"%s\"" % glade
    82         mode = {False: os.P_NOWAIT, True: os.P_WAIT}[wait]
    82         mode = {False: os.P_NOWAIT, True: os.P_WAIT}[wait]
    83         os.spawnv(mode, sys.executable, ["\"%s\"" % sys.executable] + [glade] + options)
    83         os.spawnv(mode, sys.executable,
       
    84                   ["\"%s\"" % sys.executable] + [glade] + options)
    84 
    85 
    85     def OnCTNSave(self, from_project_path=None):
    86     def OnCTNSave(self, from_project_path=None):
    86         if from_project_path is not None:
    87         if from_project_path is not None:
    87             shutil.copyfile(self._getWXGLADEpath(from_project_path),
    88             shutil.copyfile(self._getWXGLADEpath(from_project_path),
    88                             self._getWXGLADEpath())
    89                             self._getWXGLADEpath())
    90 
    91 
    91     def CTNGenerate_C(self, buildpath, locations):
    92     def CTNGenerate_C(self, buildpath, locations):
    92 
    93 
    93         # list containing description of all objects declared in wxglade
    94         # list containing description of all objects declared in wxglade
    94         hmi_objects = []
    95         hmi_objects = []
    95         # list containing only description of the main frame object 
    96         # list containing only description of the main frame object
    96         main_frames = []
    97         main_frames = []
    97 
    98 
    98         wxgfile_path = self._getWXGLADEpath()
    99         wxgfile_path = self._getWXGLADEpath()
    99         if os.path.exists(wxgfile_path):
   100         if os.path.exists(wxgfile_path):
   100             wxgfile = open(wxgfile_path, 'r')
   101             wxgfile = open(wxgfile_path, 'r')
   110                         "handlers": [
   111                         "handlers": [
   111                             hnode.firstChild.data for hnode in
   112                             hnode.firstChild.data for hnode in
   112                             node.getElementsByTagName("handler")]}
   113                             node.getElementsByTagName("handler")]}
   113 
   114 
   114                     hmi_objects.append(wxglade_object_desc)
   115                     hmi_objects.append(wxglade_object_desc)
   115                     if name == self.CTNName() :
   116                     if name == self.CTNName():
   116                         main_frames.append(wxglade_object_desc)
   117                         main_frames.append(wxglade_object_desc)
   117 
   118 
   118             hmipyfile_path = os.path.join(self._getBuildPath(), "hmi.py")
   119             hmipyfile_path = os.path.join(self._getBuildPath(), "hmi.py")
   119             if wx.Platform == '__WXMSW__':
   120             if wx.Platform == '__WXMSW__':
   120                 wxgfile_path = "\"%s\"" % wxgfile_path
   121                 wxgfile_path = "\"%s\"" % wxgfile_path
   121                 wxghmipyfile_path = "\"%s\"" % hmipyfile_path
   122                 wxghmipyfile_path = "\"%s\"" % hmipyfile_path
   122             else:
   123             else:
   123                 wxghmipyfile_path = hmipyfile_path
   124                 wxghmipyfile_path = hmipyfile_path
   124             self.launch_wxglade(['-o', wxghmipyfile_path, '-g', 'python', wxgfile_path], wait=True)
   125             self.launch_wxglade(
       
   126                 ['-o', wxghmipyfile_path, '-g', 'python', wxgfile_path], wait=True)
   125 
   127 
   126             hmipyfile = open(hmipyfile_path, 'r')
   128             hmipyfile = open(hmipyfile_path, 'r')
   127             define_hmi = hmipyfile.read().decode('utf-8')
   129             define_hmi = hmipyfile.read().decode('utf-8')
   128             hmipyfile.close()
   130             hmipyfile.close()
   129 
   131 
   130         else:
   132         else:
   131             define_hmi = ""
   133             define_hmi = ""
   132 
   134 
   133         declare_hmi = "\n".join(["%(name)s = None\n" % x for x in main_frames])
   135         declare_hmi = "\n".join(["%(name)s = None\n" % x for x in main_frames])
   134         declare_hmi += "\n".join(["\n".join(["%(class)s.%(h)s = %(h)s" %
   136         declare_hmi += "\n".join(["\n".join(["%(class)s.%(h)s = %(h)s" %
   135                                             dict(x, h=h) for h in x['handlers']])
   137                                              dict(x, h=h) for h in x['handlers']])
   136                                  for x in hmi_objects])
   138                                   for x in hmi_objects])
   137         global_hmi = ("global %s\n" % ",".join(
   139         global_hmi = ("global %s\n" % ",".join(
   138             [x["name"] for x in main_frames]) if len(main_frames) > 0 else "")
   140             [x["name"] for x in main_frames]) if len(main_frames) > 0 else "")
   139         init_hmi = "\n".join(["""\
   141         init_hmi = "\n".join(["""\
   140 def OnCloseFrame(evt):
   142 def OnCloseFrame(evt):
   141     wx.MessageBox(_("Please stop PLC to close"))
   143     wx.MessageBox(_("Please stop PLC to close"))
   158             "start":   init_hmi,
   160             "start":   init_hmi,
   159         }
   161         }
   160 
   162 
   161         if len(main_frames) == 0 and \
   163         if len(main_frames) == 0 and \
   162            len(getattr(self.CodeFile, "start").getanyText().strip()) == 0:
   164            len(getattr(self.CodeFile, "start").getanyText().strip()) == 0:
   163                 self.GetCTRoot().logger.write_warning(
   165             self.GetCTRoot().logger.write_warning(
   164                     _("Warning: WxGlade HMI has no object with name identical to extension name, and no python code is provided in start section to create object.\n"))
   166                 _("Warning: WxGlade HMI has no object with name identical to extension name, and no python code is provided in start section to create object.\n"))
   165             
   167 
   166         return PythonFileCTNMixin.CTNGenerate_C(self, buildpath, locations)
   168         return PythonFileCTNMixin.CTNGenerate_C(self, buildpath, locations)
   167 
   169 
   168     def _editWXGLADE(self):
   170     def _editWXGLADE(self):
   169         wxg_filename = self._getWXGLADEpath()
   171         wxg_filename = self._getWXGLADEpath()
   170         open_wxglade = True
   172         open_wxglade = True