wxglade_hmi/wxglade_hmi.py
author Edouard Tisserant
Tue, 14 May 2013 18:41:33 +0900
changeset 1132 28f96aa9c070
parent 1131 f794fbff8f02
child 1151 38d6aaad8ffd
permissions -rw-r--r--
Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     1
import wx
1061
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
     2
import os, sys, shutil
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     3
from xml.dom import minidom
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     4
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
     5
from py_ext import PythonFileCTNMixin
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     6
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
     7
class WxGladeHMI(PythonFileCTNMixin):
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     8
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 427
diff changeset
     9
    ConfNodeMethods = [
734
5c42cafaee15 Moved LPC sources to a separate project
Edouard Tisserant
parents: 728
diff changeset
    10
        {"bitmap" : "editWXGLADE",
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    11
         "name" : _("WXGLADE GUI"),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    12
         "tooltip" : _("Edit a WxWidgets GUI with WXGlade"),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    13
         "method" : "_editWXGLADE"},
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    14
    ]
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    15
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    16
    def ConfNodePath(self):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    17
        return os.path.join(os.path.dirname(__file__))
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    18
1061
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    19
    def _getWXGLADEpath(self, project_path=None):
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    20
        if project_path is None:
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    21
            project_path = self.CTNPath()
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    22
        # define name for wxGlade gui file
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    23
        return os.path.join(project_path, "hmi.wxg")
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    24
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    25
    def launch_wxglade(self, options, wait=False):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    26
        from wxglade import __file__ as fileName
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    27
        path = os.path.dirname(fileName)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    28
        glade = os.path.join(path, 'wxglade.py')
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    29
        if wx.Platform == '__WXMSW__':
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    30
            glade = "\"%s\""%glade
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    31
        mode = {False:os.P_NOWAIT, True:os.P_WAIT}[wait]
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    32
        os.spawnv(mode, sys.executable, ["\"%s\""%sys.executable] + [glade] + options)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    33
1061
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    34
    def OnCTNSave(self, from_project_path=None):
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    35
        if from_project_path is not None:
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    36
            shutil.copyfile(self._getWXGLADEpath(from_project_path),
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    37
                            self._getWXGLADEpath())
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    38
        return PythonFileCTNMixin.OnCTNSave(self, from_project_path)
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    39
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    40
    def CTNGenerate_C(self, buildpath, locations):
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    41
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    42
        hmi_frames = {}
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    43
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    44
        wxgfile_path=self._getWXGLADEpath()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    45
        if os.path.exists(wxgfile_path):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    46
            wxgfile = open(wxgfile_path, 'r')
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    47
            wxgtree = minidom.parse(wxgfile)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    48
            wxgfile.close()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    49
            
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    50
            for node in wxgtree.childNodes[1].childNodes:
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    51
                if node.nodeType == wxgtree.ELEMENT_NODE:
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    52
                    hmi_frames[node._attrs["name"].value] =  node._attrs["class"].value
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    53
                    
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    54
            hmipyfile_path=os.path.join(self._getBuildPath(), "hmi.py")
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    55
            if wx.Platform == '__WXMSW__':
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    56
                wxgfile_path = "\"%s\""%wxgfile_path
384
50f2069a06b3 Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents: 367
diff changeset
    57
                wxghmipyfile_path = "\"%s\""%hmipyfile_path
50f2069a06b3 Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents: 367
diff changeset
    58
            else:
50f2069a06b3 Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents: 367
diff changeset
    59
                wxghmipyfile_path = hmipyfile_path
50f2069a06b3 Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents: 367
diff changeset
    60
            self.launch_wxglade(['-o', wxghmipyfile_path, '-g', 'python', wxgfile_path], wait=True)
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    61
            
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    62
            hmipyfile = open(hmipyfile_path, 'r')
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    63
            define_hmi = hmipyfile.read().decode('utf-8')
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    64
            hmipyfile.close()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    65
        
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    66
        declare_hmi = "\n".join(map(lambda x:"%s = None" % x,
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    67
                                hmi_frames.keys()))
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    68
        global_hmi = "global "+",".join(hmi_frames.keys())
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    69
        init_hmi = "\n".join(map(lambda x: """\
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    70
def OnCloseFrame(evt):
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    71
    wx.MessageBox(_("Please stop PLC to close"))
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    72
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    73
%(name)s = %(class)s(None)
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    74
%(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame)
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    75
%(name)s.Show()
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    76
""" % {"name": x[0], "class": x[1]}, hmi_frames.items()))
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    77
        cleanup_hmi = "\n".join(map(lambda x:"if %s is not None: %s.Destroy()" % (x,x), hmi_frames.keys()))
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1062
diff changeset
    78
        
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    79
        self.PreSectionsTexts = {
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    80
            "globals":define_hmi + declare_hmi,
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    81
            "start":global_hmi,
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    82
            "stop":global_hmi
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    83
        }
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    84
        self.PostSectionsTexts = {
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    85
            "start":init_hmi,
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    86
        }
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1062
diff changeset
    87
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1131
diff changeset
    88
        return PythonFileCTNMixin.CTNGenerate_C(self, buildpath, locations)
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    89
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    90
    def _editWXGLADE(self):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    91
        wxg_filename = self._getWXGLADEpath()
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
    92
        open_wxglade = True
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    93
        if not self.GetCTRoot().CheckProjectPathPerm():
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    94
            dialog = wx.MessageDialog(self.GetCTRoot().AppFrame,
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
    95
                                      _("You don't have write permissions.\nOpen wxGlade anyway ?"),
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
    96
                                      _("Open wxGlade"),
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
    97
                                      wx.YES_NO|wx.ICON_QUESTION)
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
    98
            open_wxglade = dialog.ShowModal() == wx.ID_YES
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
    99
            dialog.Destroy()
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   100
        if open_wxglade:
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   101
            if not os.path.exists(wxg_filename):
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   102
                hmi_name = self.BaseParams.getName()
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   103
                open(wxg_filename,"w").write("""<?xml version="1.0"?>
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   104
    <application path="" name="" class="" option="0" language="python" top_window="%(name)s" encoding="UTF-8" use_gettext="0" overwrite="0" use_new_namespace="1" for_version="2.8" is_template="0">
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   105
        <object class="%(class)s" name="%(name)s" base="EditFrame">
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   106
            <style>wxDEFAULT_FRAME_STYLE</style>
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   107
            <title>frame_1</title>
834
d613696aad01 Adding default sizer in main frame in default wxGlade GUI file
laurent
parents: 734
diff changeset
   108
            <object class="wxBoxSizer" name="sizer_1" base="EditBoxSizer">
d613696aad01 Adding default sizer in main frame in default wxGlade GUI file
laurent
parents: 734
diff changeset
   109
                <orient>wxVERTICAL</orient>
d613696aad01 Adding default sizer in main frame in default wxGlade GUI file
laurent
parents: 734
diff changeset
   110
            <object class="sizerslot" />
d613696aad01 Adding default sizer in main frame in default wxGlade GUI file
laurent
parents: 734
diff changeset
   111
        </object>
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   112
        </object>
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   113
    </application>
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   114
    """ % {"name": hmi_name, "class": "Class_%s" % hmi_name})
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   115
            if wx.Platform == '__WXMSW__':
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   116
                wxg_filename = "\"%s\""%wxg_filename
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   117
            self.launch_wxglade([wxg_filename])
1061
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
   118