controls/PouInstanceVariablesPanel.py
author Laurent Bessard
Mon, 11 Jun 2012 01:25:32 +0200
changeset 709 2507f2736a3e
parent 701 25fbbb005a30
child 714 131ea7f237b9
permissions -rw-r--r--
Fix bug editor toolbar wrong size after restoring perspective
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
     1
#!/usr/bin/env python
f10449b18dbe refactoring
laurent
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
f10449b18dbe refactoring
laurent
parents:
diff changeset
     3
f10449b18dbe refactoring
laurent
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
f10449b18dbe refactoring
laurent
parents:
diff changeset
     5
#based on the plcopen standard. 
f10449b18dbe refactoring
laurent
parents:
diff changeset
     6
#
f10449b18dbe refactoring
laurent
parents:
diff changeset
     7
#Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD
f10449b18dbe refactoring
laurent
parents:
diff changeset
     8
#
f10449b18dbe refactoring
laurent
parents:
diff changeset
     9
#See COPYING file for copyrights details.
f10449b18dbe refactoring
laurent
parents:
diff changeset
    10
#
f10449b18dbe refactoring
laurent
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
f10449b18dbe refactoring
laurent
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
f10449b18dbe refactoring
laurent
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
f10449b18dbe refactoring
laurent
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
f10449b18dbe refactoring
laurent
parents:
diff changeset
    15
#
f10449b18dbe refactoring
laurent
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
f10449b18dbe refactoring
laurent
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
f10449b18dbe refactoring
laurent
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
f10449b18dbe refactoring
laurent
parents:
diff changeset
    19
#General Public License for more details.
f10449b18dbe refactoring
laurent
parents:
diff changeset
    20
#
f10449b18dbe refactoring
laurent
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
f10449b18dbe refactoring
laurent
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
f10449b18dbe refactoring
laurent
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
f10449b18dbe refactoring
laurent
parents:
diff changeset
    24
f10449b18dbe refactoring
laurent
parents:
diff changeset
    25
import wx
f10449b18dbe refactoring
laurent
parents:
diff changeset
    26
import wx.lib.buttons
f10449b18dbe refactoring
laurent
parents:
diff changeset
    27
import wx.lib.agw.customtreectrl as CT
f10449b18dbe refactoring
laurent
parents:
diff changeset
    28
f10449b18dbe refactoring
laurent
parents:
diff changeset
    29
from PLCControler import ITEMS_VARIABLE, ITEM_CONFIGURATION, ITEM_RESOURCE, ITEM_POU
f10449b18dbe refactoring
laurent
parents:
diff changeset
    30
f10449b18dbe refactoring
laurent
parents:
diff changeset
    31
class PouInstanceVariablesPanel(wx.Panel):
f10449b18dbe refactoring
laurent
parents:
diff changeset
    32
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
    33
    def __init__(self, parent, window, controller, debug):
f10449b18dbe refactoring
laurent
parents:
diff changeset
    34
        wx.Panel.__init__(self, name='PouInstanceTreePanel', 
f10449b18dbe refactoring
laurent
parents:
diff changeset
    35
                parent=parent, pos=wx.Point(0, 0), 
f10449b18dbe refactoring
laurent
parents:
diff changeset
    36
                size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    37
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    38
        self.ParentButton = wx.lib.buttons.GenBitmapButton(
f10449b18dbe refactoring
laurent
parents:
diff changeset
    39
              name='ParentButton', parent=self, 
f10449b18dbe refactoring
laurent
parents:
diff changeset
    40
              bitmap=window.GenerateBitmap("up"), 
f10449b18dbe refactoring
laurent
parents:
diff changeset
    41
              pos=wx.Point(0, 0), size=wx.Size(28, 28), 
f10449b18dbe refactoring
laurent
parents:
diff changeset
    42
              style=wx.NO_BORDER)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    43
        self.Bind(wx.EVT_BUTTON, self.OnParentButtonClick, 
f10449b18dbe refactoring
laurent
parents:
diff changeset
    44
                self.ParentButton)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    45
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    46
        self.InstanceChoice = wx.ComboBox(name='InstanceChoice', 
f10449b18dbe refactoring
laurent
parents:
diff changeset
    47
              parent=self, pos=wx.Point(0, 0),
f10449b18dbe refactoring
laurent
parents:
diff changeset
    48
              size=wx.Size(0, 28), style=wx.CB_READONLY)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    49
        self.Bind(wx.EVT_COMBOBOX, self.OnInstanceChoiceChanged,
f10449b18dbe refactoring
laurent
parents:
diff changeset
    50
                self.InstanceChoice)
687
629680fb0582 refactoring
laurent
parents: 686
diff changeset
    51
        self.InstanceChoice.Bind(wx.EVT_LEFT_DOWN, self.OnInstanceChoiceLeftDown)
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
    52
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    53
        self.DebugButton = wx.lib.buttons.GenBitmapButton(
f10449b18dbe refactoring
laurent
parents:
diff changeset
    54
              name='DebugButton', parent=self, 
f10449b18dbe refactoring
laurent
parents:
diff changeset
    55
              bitmap=window.GenerateBitmap("debug"), 
f10449b18dbe refactoring
laurent
parents:
diff changeset
    56
              pos=wx.Point(0, 0), size=wx.Size(28, 28), 
f10449b18dbe refactoring
laurent
parents:
diff changeset
    57
              style=wx.NO_BORDER)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    58
        self.Bind(wx.EVT_BUTTON, self.OnDebugButtonClick, 
f10449b18dbe refactoring
laurent
parents:
diff changeset
    59
                self.DebugButton)
687
629680fb0582 refactoring
laurent
parents: 686
diff changeset
    60
        
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
    61
        self.VariablesList = CT.CustomTreeCtrl(
f10449b18dbe refactoring
laurent
parents:
diff changeset
    62
              name='VariablesList', parent=self,
f10449b18dbe refactoring
laurent
parents:
diff changeset
    63
              pos=wx.Point(0, 0), size=wx.Size(0, 0), 
f10449b18dbe refactoring
laurent
parents:
diff changeset
    64
              style=wx.SUNKEN_BORDER,
f10449b18dbe refactoring
laurent
parents:
diff changeset
    65
              agwStyle=CT.TR_NO_BUTTONS|
f10449b18dbe refactoring
laurent
parents:
diff changeset
    66
                       CT.TR_SINGLE|
f10449b18dbe refactoring
laurent
parents:
diff changeset
    67
                       CT.TR_HAS_VARIABLE_ROW_HEIGHT|
f10449b18dbe refactoring
laurent
parents:
diff changeset
    68
                       CT.TR_HIDE_ROOT|
686
3216bf5f711d Fixed instances treecontrol display
Edouard Tisserant
parents: 684
diff changeset
    69
                       CT.TR_NO_LINES|
687
629680fb0582 refactoring
laurent
parents: 686
diff changeset
    70
                       getattr(CT, "TR_ALIGN_WINDOWS_RIGHT", CT.TR_ALIGN_WINDOWS))
686
3216bf5f711d Fixed instances treecontrol display
Edouard Tisserant
parents: 684
diff changeset
    71
        self.VariablesList.SetIndent(0)
3216bf5f711d Fixed instances treecontrol display
Edouard Tisserant
parents: 684
diff changeset
    72
        self.VariablesList.SetSpacing(5)
3216bf5f711d Fixed instances treecontrol display
Edouard Tisserant
parents: 684
diff changeset
    73
        self.VariablesList.DoSelectItem = lambda *x,**y:True
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
    74
        self.VariablesList.Bind(CT.EVT_TREE_ITEM_ACTIVATED,
f10449b18dbe refactoring
laurent
parents:
diff changeset
    75
                self.OnVariablesListItemActivated)
687
629680fb0582 refactoring
laurent
parents: 686
diff changeset
    76
        self.VariablesList.Bind(wx.EVT_LEFT_DOWN, self.OnVariablesListLeftDown)
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
    77
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    78
        buttons_sizer = wx.FlexGridSizer(cols=3, hgap=0, rows=1, vgap=0)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    79
        buttons_sizer.AddWindow(self.ParentButton, 0, border=0, flag=0)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    80
        buttons_sizer.AddWindow(self.InstanceChoice, 0, border=0, flag=wx.GROW)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    81
        buttons_sizer.AddWindow(self.DebugButton, 0, border=0, flag=0)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    82
        buttons_sizer.AddGrowableCol(1)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    83
        buttons_sizer.AddGrowableRow(0)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    84
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    85
        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    86
        main_sizer.AddSizer(buttons_sizer, 0, border=0, flag=wx.GROW)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    87
        main_sizer.AddWindow(self.VariablesList, 0, border=0, flag=wx.GROW)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    88
        main_sizer.AddGrowableCol(0)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    89
        main_sizer.AddGrowableRow(1)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    90
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    91
        self.SetSizer(main_sizer)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    92
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    93
        self.ParentWindow = window
f10449b18dbe refactoring
laurent
parents:
diff changeset
    94
        self.Controller = controller
f10449b18dbe refactoring
laurent
parents:
diff changeset
    95
        self.Debug = debug
f10449b18dbe refactoring
laurent
parents:
diff changeset
    96
        if not self.Debug:
f10449b18dbe refactoring
laurent
parents:
diff changeset
    97
            self.DebugButton.Hide()
f10449b18dbe refactoring
laurent
parents:
diff changeset
    98
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    99
        self.PouTagName = None
f10449b18dbe refactoring
laurent
parents:
diff changeset
   100
        self.PouInfos = None
f10449b18dbe refactoring
laurent
parents:
diff changeset
   101
        self.PouInstance = None
f10449b18dbe refactoring
laurent
parents:
diff changeset
   102
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
   103
    def __del__(self):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   104
        self.Controller = None
f10449b18dbe refactoring
laurent
parents:
diff changeset
   105
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
   106
    def SetTreeImageList(self, tree_image_list):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   107
        self.VariablesList.SetImageList(tree_image_list)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   108
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
   109
    def SetController(self, controller):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   110
        self.Controller = controller
687
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   111
    
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   112
        self.RefreshView()
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   113
    
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
   114
    def SetPouType(self, tagname, pou_instance=None):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   115
        self.PouTagName = tagname
f10449b18dbe refactoring
laurent
parents:
diff changeset
   116
        if pou_instance is not None:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   117
            self.PouInstance = pou_instance
f10449b18dbe refactoring
laurent
parents:
diff changeset
   118
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
   119
        self.RefreshView()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   120
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
   121
    def ResetView(self):
687
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   122
        self.Controller = None
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   123
        
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
   124
        self.PouTagName = None
f10449b18dbe refactoring
laurent
parents:
diff changeset
   125
        self.PouInfos = None
f10449b18dbe refactoring
laurent
parents:
diff changeset
   126
        self.PouInstance = None
f10449b18dbe refactoring
laurent
parents:
diff changeset
   127
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
   128
        self.RefreshView()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   129
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
   130
    def RefreshView(self):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   131
        self.VariablesList.DeleteAllItems()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   132
        self.InstanceChoice.Clear()
687
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   133
        self.InstanceChoice.SetValue("")
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
   134
        
701
25fbbb005a30 Fix bug in POUinstanceVariablesPanel when no tagname defined
Laurent Bessard
parents: 692
diff changeset
   135
        if self.Controller is not None and self.PouTagName is not None:
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
   136
            self.PouInfos = self.Controller.GetPouVariables(self.PouTagName, self.Debug)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   137
        else:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   138
            self.PouInfos = None
f10449b18dbe refactoring
laurent
parents:
diff changeset
   139
        if self.PouInfos is not None:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   140
            root = self.VariablesList.AddRoot("")
f10449b18dbe refactoring
laurent
parents:
diff changeset
   141
            for var_infos in self.PouInfos["variables"]:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   142
                if var_infos.get("type", None) is not None:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   143
                    text = "%(name)s (%(type)s)" % var_infos
f10449b18dbe refactoring
laurent
parents:
diff changeset
   144
                else:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   145
                    text = var_infos["name"]
f10449b18dbe refactoring
laurent
parents:
diff changeset
   146
                
f10449b18dbe refactoring
laurent
parents:
diff changeset
   147
                panel = wx.Panel(self.VariablesList)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   148
                    
f10449b18dbe refactoring
laurent
parents:
diff changeset
   149
                buttons = []
f10449b18dbe refactoring
laurent
parents:
diff changeset
   150
                if var_infos["class"] in ITEMS_VARIABLE:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   151
                    if (var_infos["debug"] and self.Debug and
f10449b18dbe refactoring
laurent
parents:
diff changeset
   152
                        (self.Controller.IsOfType(var_infos["type"], "ANY_NUM", True) or
f10449b18dbe refactoring
laurent
parents:
diff changeset
   153
                         self.Controller.IsOfType(var_infos["type"], "ANY_BIT", True))):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   154
                        graph_button = wx.lib.buttons.GenBitmapButton(name="graph", 
f10449b18dbe refactoring
laurent
parents:
diff changeset
   155
                              parent=panel, bitmap=self.ParentWindow.GenerateBitmap("graph"), 
f10449b18dbe refactoring
laurent
parents:
diff changeset
   156
                              pos=wx.Point(0, 0), size=wx.Size(28, 28), style=wx.NO_BORDER)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   157
                        self.Bind(wx.EVT_BUTTON, self.GenGraphButtonCallback(var_infos), graph_button)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   158
                        buttons.append(graph_button)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   159
                elif var_infos["edit"]:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   160
                    edit_button = wx.lib.buttons.GenBitmapButton(name="edit", 
f10449b18dbe refactoring
laurent
parents:
diff changeset
   161
                          parent=panel, bitmap=self.ParentWindow.GenerateBitmap("edit"), 
f10449b18dbe refactoring
laurent
parents:
diff changeset
   162
                          pos=wx.Point(0, 0), size=wx.Size(28, 28), style=wx.NO_BORDER)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   163
                    self.Bind(wx.EVT_BUTTON, self.GenEditButtonCallback(var_infos), edit_button)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   164
                    buttons.append(edit_button)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   165
                
f10449b18dbe refactoring
laurent
parents:
diff changeset
   166
                if var_infos["debug"] and self.Debug:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   167
                    debug_button = wx.lib.buttons.GenBitmapButton(name="debug", 
f10449b18dbe refactoring
laurent
parents:
diff changeset
   168
                          parent=panel, bitmap=self.ParentWindow.GenerateBitmap("debug"), 
f10449b18dbe refactoring
laurent
parents:
diff changeset
   169
                          pos=wx.Point(0, 0), size=wx.Size(28, 28), style=wx.NO_BORDER)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   170
                    self.Bind(wx.EVT_BUTTON, self.GenDebugButtonCallback(var_infos), debug_button)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   171
                    buttons.append(debug_button)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   172
                
f10449b18dbe refactoring
laurent
parents:
diff changeset
   173
                button_num = len(buttons)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   174
                if button_num > 0:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   175
                    panel.SetSize(wx.Size(button_num * 32, 28))
f10449b18dbe refactoring
laurent
parents:
diff changeset
   176
                    panel.SetBackgroundColour(self.VariablesList.GetBackgroundColour())
f10449b18dbe refactoring
laurent
parents:
diff changeset
   177
                    panel_sizer = wx.BoxSizer(wx.HORIZONTAL)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   178
                    panel.SetSizer(panel_sizer)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   179
                    
f10449b18dbe refactoring
laurent
parents:
diff changeset
   180
                    for button in buttons:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   181
                        panel_sizer.AddWindow(button, 0, border=4, flag=wx.LEFT)
692
e00ce7077bfc Fix bugs on PouInstanceVariablesPanel
laurent
parents: 687
diff changeset
   182
                    panel_sizer.Layout()
e00ce7077bfc Fix bugs on PouInstanceVariablesPanel
laurent
parents: 687
diff changeset
   183
                    
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
   184
                else:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   185
                    panel.Destroy()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   186
                    panel = None
f10449b18dbe refactoring
laurent
parents:
diff changeset
   187
                
f10449b18dbe refactoring
laurent
parents:
diff changeset
   188
                item = self.VariablesList.AppendItem(root, text, wnd=panel)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   189
                self.VariablesList.SetItemImage(item, self.ParentWindow.GetTreeImage(var_infos["class"]))
f10449b18dbe refactoring
laurent
parents:
diff changeset
   190
                self.VariablesList.SetPyData(item, var_infos)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   191
            
f10449b18dbe refactoring
laurent
parents:
diff changeset
   192
            instances = self.Controller.SearchPouInstances(self.PouTagName, self.Debug)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   193
            for instance in instances:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   194
                self.InstanceChoice.Append(instance)
687
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   195
            if len(instances) == 1:
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   196
                self.PouInstance = instances[0]
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
   197
            if self.PouInfos["class"] in [ITEM_CONFIGURATION, ITEM_RESOURCE]:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   198
                self.PouInstance = None
f10449b18dbe refactoring
laurent
parents:
diff changeset
   199
                self.InstanceChoice.SetSelection(0)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   200
            elif self.PouInstance in instances:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   201
                self.InstanceChoice.SetStringSelection(self.PouInstance)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   202
            else:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   203
                self.PouInstance = None
f10449b18dbe refactoring
laurent
parents:
diff changeset
   204
                self.InstanceChoice.SetValue(_("Select an instance"))
f10449b18dbe refactoring
laurent
parents:
diff changeset
   205
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
   206
        self.RefreshButtons()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   207
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
   208
    def RefreshButtons(self):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   209
        enabled = self.InstanceChoice.GetSelection() != -1
f10449b18dbe refactoring
laurent
parents:
diff changeset
   210
        self.ParentButton.Enable(enabled and self.PouInfos["class"] != ITEM_CONFIGURATION)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   211
        self.DebugButton.Enable(enabled and self.PouInfos["debug"] and self.Debug)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   212
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
   213
        root = self.VariablesList.GetRootItem()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   214
        if root is not None and root.IsOk():
f10449b18dbe refactoring
laurent
parents:
diff changeset
   215
            item, item_cookie = self.VariablesList.GetFirstChild(root)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   216
            while item is not None and item.IsOk():
f10449b18dbe refactoring
laurent
parents:
diff changeset
   217
                panel = self.VariablesList.GetItemWindow(item)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   218
                if panel is not None:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   219
                    for child in panel.GetChildren():
f10449b18dbe refactoring
laurent
parents:
diff changeset
   220
                        if child.GetName() != "edit":
f10449b18dbe refactoring
laurent
parents:
diff changeset
   221
                            child.Enable(enabled)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   222
                item, item_cookie = self.VariablesList.GetNextChild(root, item_cookie)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   223
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
   224
    def GenEditButtonCallback(self, infos):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   225
        def EditButtonCallback(event):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   226
            var_class = infos["class"]
f10449b18dbe refactoring
laurent
parents:
diff changeset
   227
            if var_class == ITEM_RESOURCE:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   228
                tagname = self.Controller.ComputeConfigurationResourceName(
f10449b18dbe refactoring
laurent
parents:
diff changeset
   229
                    self.InstanceChoice.GetStringSelection(), 
f10449b18dbe refactoring
laurent
parents:
diff changeset
   230
                    infos["name"])
f10449b18dbe refactoring
laurent
parents:
diff changeset
   231
            else:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   232
                var_class = ITEM_POU
f10449b18dbe refactoring
laurent
parents:
diff changeset
   233
                tagname = self.Controller.ComputePouName(infos["type"])
f10449b18dbe refactoring
laurent
parents:
diff changeset
   234
            self.ParentWindow.EditProjectElement(var_class, tagname)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   235
            event.Skip()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   236
        return EditButtonCallback
f10449b18dbe refactoring
laurent
parents:
diff changeset
   237
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
   238
    def GenDebugButtonCallback(self, infos):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   239
        def DebugButtonCallback(event):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   240
            if self.InstanceChoice.GetSelection() != -1:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   241
                var_class = infos["class"]
f10449b18dbe refactoring
laurent
parents:
diff changeset
   242
                var_path = "%s.%s" % (self.InstanceChoice.GetStringSelection(), 
f10449b18dbe refactoring
laurent
parents:
diff changeset
   243
                                      infos["name"])
f10449b18dbe refactoring
laurent
parents:
diff changeset
   244
                if var_class in ITEMS_VARIABLE:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   245
                    self.ParentWindow.AddDebugVariable(var_path, force=True)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   246
                else:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   247
                    self.ParentWindow.OpenDebugViewer(
f10449b18dbe refactoring
laurent
parents:
diff changeset
   248
                        infos["class"],
f10449b18dbe refactoring
laurent
parents:
diff changeset
   249
                        var_path,
f10449b18dbe refactoring
laurent
parents:
diff changeset
   250
                        self.Controller.ComputePouName(infos["type"]))
f10449b18dbe refactoring
laurent
parents:
diff changeset
   251
            event.Skip()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   252
        return DebugButtonCallback
f10449b18dbe refactoring
laurent
parents:
diff changeset
   253
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
   254
    def GenGraphButtonCallback(self, infos):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   255
        def GraphButtonCallback(event):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   256
            if self.InstanceChoice.GetSelection() != -1:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   257
                if infos["class"] in ITEMS_VARIABLE:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   258
                    var_path = "%s.%s" % (self.InstanceChoice.GetStringSelection(), 
f10449b18dbe refactoring
laurent
parents:
diff changeset
   259
                                          infos["name"])
f10449b18dbe refactoring
laurent
parents:
diff changeset
   260
                    self.ParentWindow.OpenGraphicViewer(var_path)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   261
            event.Skip()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   262
        return GraphButtonCallback
f10449b18dbe refactoring
laurent
parents:
diff changeset
   263
    
687
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   264
    def ShowInstanceChoicePopup(self):
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   265
        self.InstanceChoice.SetFocusFromKbd()
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   266
        size = self.InstanceChoice.GetSize()
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   267
        event = wx.MouseEvent(wx.EVT_LEFT_DOWN._getEvtType())
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   268
        event.m_x = size.width / 2
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   269
        event.m_y = size.height / 2
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   270
        event.SetEventObject(self.InstanceChoice)
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   271
        #event = wx.KeyEvent(wx.EVT_KEY_DOWN._getEvtType())
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   272
        #event.m_keyCode = wx.WXK_SPACE
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   273
        self.InstanceChoice.GetEventHandler().ProcessEvent(event)
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   274
    
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
   275
    def OnParentButtonClick(self, event):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   276
        if self.InstanceChoice.GetSelection() != -1:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   277
            parent_path = self.InstanceChoice.GetStringSelection().rsplit(".", 1)[0]
687
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   278
            tagname = self.Controller.GetPouInstanceTagName(parent_path, self.Debug)
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
   279
            if tagname is not None:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   280
                wx.CallAfter(self.SetPouType, tagname, parent_path)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   281
                wx.CallAfter(self.ParentWindow.SelectProjectTreeItem, tagname)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   282
        event.Skip()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   283
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
   284
    def OnInstanceChoiceChanged(self, event):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   285
        self.RefreshButtons()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   286
        event.Skip()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   287
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
   288
    def OnDebugButtonClick(self, event):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   289
        if self.InstanceChoice.GetSelection() != -1:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   290
            self.ParentWindow.OpenDebugViewer(
f10449b18dbe refactoring
laurent
parents:
diff changeset
   291
                self.PouInfos["class"],
f10449b18dbe refactoring
laurent
parents:
diff changeset
   292
                self.InstanceChoice.GetStringSelection(),
f10449b18dbe refactoring
laurent
parents:
diff changeset
   293
                self.PouTagName)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   294
        event.Skip()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   295
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
   296
    def OnVariablesListItemActivated(self, event):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   297
        if self.InstanceChoice.GetSelection() != -1:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   298
            instance_path = self.InstanceChoice.GetStringSelection()
686
3216bf5f711d Fixed instances treecontrol display
Edouard Tisserant
parents: 684
diff changeset
   299
            selected_item = event.GetItem()
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
   300
            if selected_item is not None and selected_item.IsOk():
f10449b18dbe refactoring
laurent
parents:
diff changeset
   301
                item_infos = self.VariablesList.GetPyData(selected_item)
687
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   302
                if item_infos is not None and item_infos["class"] not in ITEMS_VARIABLE:
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
   303
                    if item_infos["class"] == ITEM_RESOURCE:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   304
                        tagname = self.Controller.ComputeConfigurationResourceName(
f10449b18dbe refactoring
laurent
parents:
diff changeset
   305
                                       instance_path, 
f10449b18dbe refactoring
laurent
parents:
diff changeset
   306
                                       item_infos["name"])
f10449b18dbe refactoring
laurent
parents:
diff changeset
   307
                    else:
f10449b18dbe refactoring
laurent
parents:
diff changeset
   308
                        tagname = self.Controller.ComputePouName(item_infos["type"])
f10449b18dbe refactoring
laurent
parents:
diff changeset
   309
                    item_path = "%s.%s" % (instance_path, item_infos["name"])
f10449b18dbe refactoring
laurent
parents:
diff changeset
   310
                    wx.CallAfter(self.SetPouType, tagname, item_path)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   311
                    wx.CallAfter(self.ParentWindow.SelectProjectTreeItem, tagname)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   312
        event.Skip()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   313
    
687
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   314
    def OnVariablesListLeftDown(self, event):
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   315
        if self.InstanceChoice.GetSelection() == -1:
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   316
            wx.CallAfter(self.ShowInstanceChoicePopup)
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   317
        event.Skip()
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   318
        
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   319
    def OnInstanceChoiceLeftDown(self, event):
629680fb0582 refactoring
laurent
parents: 686
diff changeset
   320
        event.Skip()