wxglade_hmi/wxglade_hmi.py
author Edouard Tisserant
Sat, 12 May 2012 11:21:10 +0200
changeset 728 e0424e96e3fd
parent 725 py_ext/modules/wxglade_hmi/wxglade_hmi.py@31dade089db5
child 734 5c42cafaee15
permissions -rw-r--r--
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     1
import wx
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     2
import os, sys
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 util import opjimg
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
     6
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
     7
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
     8
class WxGladeHMI(PythonFileCTNMixin):
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     9
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 427
diff changeset
    10
    ConfNodeMethods = [
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    11
        {"bitmap" : opjimg("editWXGLADE"),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    12
         "name" : _("WXGLADE GUI"),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    13
         "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
    14
         "method" : "_editWXGLADE"},
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    15
    ]
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    16
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    17
    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
    18
        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
    19
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    20
    def _getWXGLADEpath(self):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    21
        # define name for IEC raw code file
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    22
        return os.path.join(self.CTNPath(), "hmi.wxg")
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    23
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    24
    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
    25
        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
    26
        path = os.path.dirname(fileName)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    27
        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
    28
        if wx.Platform == '__WXMSW__':
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    29
            glade = "\"%s\""%glade
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    30
        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
    31
        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
    32
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    33
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    34
    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
    35
        """
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    36
        Return C code generated by iec2c compiler 
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    37
        when _generate_softPLC have been called
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    38
        @param locations: ignored
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    39
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    40
        """
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
        current_location = self.GetCurrentLocation()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    43
        # define a unique name for the generated C file
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    44
        location_str = "_".join(map(lambda x:str(x), current_location))
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    45
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    46
        runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    47
        runtimefile = open(runtimefile_path, 'w')
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    48
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    49
        hmi_frames = {}
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    50
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    51
        wxgfile_path=self._getWXGLADEpath()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    52
        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
    53
            wxgfile = open(wxgfile_path, 'r')
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    54
            wxgtree = minidom.parse(wxgfile)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    55
            wxgfile.close()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    56
            
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    57
            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
    58
                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
    59
                    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
    60
                    
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    61
            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
    62
            if wx.Platform == '__WXMSW__':
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    63
                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
    64
                wxghmipyfile_path = "\"%s\""%hmipyfile_path
50f2069a06b3 Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents: 367
diff changeset
    65
            else:
50f2069a06b3 Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents: 367
diff changeset
    66
                wxghmipyfile_path = hmipyfile_path
50f2069a06b3 Bug on windows when trying to open wxglade generated 'hmi.py' file fixed
laurent
parents: 367
diff changeset
    67
            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
    68
            
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    69
            hmipyfile = open(hmipyfile_path, 'r')
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    70
            runtimefile.write(hmipyfile.read())
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    71
            hmipyfile.close()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    72
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    73
        runtimefile.write(self.GetPythonCode())
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    74
        runtimefile.write("""
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    75
%(declare)s
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    76
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    77
def _runtime_%(location)s_begin():
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    78
    global %(global)s
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    79
    
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    80
    def OnCloseFrame(evt):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    81
        wx.MessageBox(_("Please stop PLC to close"))
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    82
    
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    83
    %(init)s
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    84
    
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    85
def _runtime_%(location)s_cleanup():
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    86
    global %(global)s
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    87
    
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    88
    %(cleanup)s
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
""" % {"location": location_str,
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    91
       "declare": "\n".join(map(lambda x:"%s = None" % x, hmi_frames.keys())),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    92
       "global": ",".join(hmi_frames.keys()),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    93
       "init": "\n".join(map(lambda x: """
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    94
    %(name)s = %(class)s(None)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    95
    %(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    96
    %(name)s.Show()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    97
""" % {"name": x[0], "class": x[1]},
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    98
                             hmi_frames.items())),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    99
       "cleanup": "\n    ".join(map(lambda x:"%s.Destroy()" % x, hmi_frames.keys()))})
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   100
        runtimefile.close()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   101
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   102
        return [], "", False, ("runtime_%s.py"%location_str, file(runtimefile_path,"rb"))
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   103
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   104
    def _editWXGLADE(self):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   105
        wxg_filename = self._getWXGLADEpath()
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   106
        open_wxglade = True
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   107
        if not self.GetCTRoot().CheckProjectPathPerm():
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   108
            dialog = wx.MessageDialog(self.GetCTRoot().AppFrame,
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   109
                                      _("You don't have write permissions.\nOpen wxGlade anyway ?"),
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   110
                                      _("Open wxGlade"),
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   111
                                      wx.YES_NO|wx.ICON_QUESTION)
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   112
            open_wxglade = dialog.ShowModal() == wx.ID_YES
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   113
            dialog.Destroy()
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   114
        if open_wxglade:
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   115
            if not os.path.exists(wxg_filename):
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   116
                hmi_name = self.BaseParams.getName()
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   117
                open(wxg_filename,"w").write("""<?xml version="1.0"?>
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   118
    <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
   119
        <object class="%(class)s" name="%(name)s" base="EditFrame">
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   120
            <style>wxDEFAULT_FRAME_STYLE</style>
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   121
            <title>frame_1</title>
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   122
        </object>
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   123
    </application>
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   124
    """ % {"name": hmi_name, "class": "Class_%s" % hmi_name})
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   125
            if wx.Platform == '__WXMSW__':
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   126
                wxg_filename = "\"%s\""%wxg_filename
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 384
diff changeset
   127
            self.launch_wxglade([wxg_filename])