controls/PouInstanceVariablesPanel.py
author Laurent Bessard
Wed, 02 Oct 2013 17:23:44 +0200
changeset 1339 6adf05c4508d
parent 1281 47131e3388f4
child 1343 a76a020b8822
permissions -rw-r--r--
Fixed bug in actionBlock actions editing
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
#based on the plcopen standard. 
#
#Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD
#
#See COPYING file for copyrights details.
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#General Public License for more details.
#
#You should have received a copy of the GNU General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import wx
import wx.lib.agw.customtreectrl as CT

import wx.lib.imageutils
import wx.lib.buttons
GrayedBitmapCache = {}
def SetBitmapLabel(self, bitmap, createOthers=True):
    """
    Set the bitmap to display normally.
    This is the only one that is required. If
    createOthers is True, then the other bitmaps
    will be generated on the fly.  Currently,
    only the disabled bitmap is generated.
    """
    self.bmpLabel = bitmap
    if bitmap is not None and createOthers:
        GrayBitmap=GrayedBitmapCache.get(bitmap,None)
        if GrayBitmap is None:
            image = wx.ImageFromBitmap(bitmap)
            wx.lib.imageutils.grayOut(image)
            GrayBitmap = wx.BitmapFromImage(image)
            GrayedBitmapCache[bitmap] = GrayBitmap
        self.SetBitmapDisabled(GrayBitmap)
wx.lib.buttons.GenBitmapButton.SetBitmapLabel = SetBitmapLabel

try:
    import matplotlib
    matplotlib.use('WX')
    USE_MPL = True
except:
    USE_MPL = False

from PLCControler import ITEMS_VARIABLE, ITEM_CONFIGURATION, ITEM_RESOURCE, ITEM_POU, ITEM_TRANSITION, ITEM_ACTION
from util.BitmapLibrary import GetBitmap

class PouInstanceVariablesPanel(wx.Panel):
    
    def __init__(self, parent, window, controller, debug):
        wx.Panel.__init__(self, name='PouInstanceTreePanel', 
                parent=parent, pos=wx.Point(0, 0), 
                size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)
        
        self.ParentButton = wx.lib.buttons.GenBitmapButton(self,
              bitmap=GetBitmap("top"), size=wx.Size(28, 28), style=wx.NO_BORDER)
        self.ParentButton.SetToolTipString(_("Parent instance"))
        self.Bind(wx.EVT_BUTTON, self.OnParentButtonClick, 
                self.ParentButton)
        
        self.InstanceChoice = wx.ComboBox(self, size=wx.Size(0, 0), style=wx.CB_READONLY)
        self.Bind(wx.EVT_COMBOBOX, self.OnInstanceChoiceChanged,
                self.InstanceChoice)
        
        self.DebugButton = wx.lib.buttons.GenBitmapButton(self, 
              bitmap=GetBitmap("debug_instance"), size=wx.Size(28, 28), style=wx.NO_BORDER)
        self.DebugButton.SetToolTipString(_("Debug instance"))
        self.Bind(wx.EVT_BUTTON, self.OnDebugButtonClick, 
                self.DebugButton)
        
        self.VariablesList = CT.CustomTreeCtrl(self,
              style=wx.SUNKEN_BORDER,
              agwStyle=CT.TR_NO_BUTTONS|
                       CT.TR_SINGLE|
                       CT.TR_HAS_VARIABLE_ROW_HEIGHT|
                       CT.TR_HIDE_ROOT|
                       CT.TR_NO_LINES|
                       getattr(CT, "TR_ALIGN_WINDOWS_RIGHT", CT.TR_ALIGN_WINDOWS))
        self.VariablesList.SetIndent(0)
        self.VariablesList.SetSpacing(5)
        self.VariablesList.DoSelectItem = lambda *x,**y:True
        self.VariablesList.Bind(CT.EVT_TREE_ITEM_ACTIVATED,
                self.OnVariablesListItemActivated)
        self.VariablesList.Bind(wx.EVT_LEFT_DOWN, self.OnVariablesListLeftDown)
        self.VariablesList.Bind(wx.EVT_KEY_DOWN, self.OnVariablesListKeyDown)
        
        buttons_sizer = wx.FlexGridSizer(cols=3, hgap=0, rows=1, vgap=0)
        buttons_sizer.AddWindow(self.ParentButton)
        buttons_sizer.AddWindow(self.InstanceChoice, flag=wx.GROW)
        buttons_sizer.AddWindow(self.DebugButton)
        buttons_sizer.AddGrowableCol(1)
        buttons_sizer.AddGrowableRow(0)
        
        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
        main_sizer.AddSizer(buttons_sizer, flag=wx.GROW)
        main_sizer.AddWindow(self.VariablesList, flag=wx.GROW)
        main_sizer.AddGrowableCol(0)
        main_sizer.AddGrowableRow(1)
        
        self.SetSizer(main_sizer)
        
        self.ParentWindow = window
        self.Controller = controller
        self.Debug = debug
        if not self.Debug:
            self.DebugButton.Hide()
        
        self.PouTagName = None
        self.PouInfos = None
        self.PouInstance = None
        
    def __del__(self):
        self.Controller = None
    
    def SetTreeImageList(self, tree_image_list):
        self.VariablesList.SetImageList(tree_image_list)
    
    def SetController(self, controller):
        self.Controller = controller
    
        self.RefreshView()
    
    def SetPouType(self, tagname, pou_instance=None):
        if self.Controller is not None:
            if tagname == "Project":
                config_name = self.Controller.GetProjectMainConfigurationName()
                if config_name is not None:
                    tagname = self.Controller.ComputeConfigurationName(config_name)
            if pou_instance is not None:
                self.PouInstance = pou_instance
            
            if self.PouTagName != tagname:
                self.PouTagName = tagname
                self.RefreshView()
            else:
                self.RefreshInstanceChoice()
        else:
            self.RefreshView()
    
    def ResetView(self):
        self.Controller = None
        
        self.PouTagName = None
        self.PouInfos = None
        self.PouInstance = None
        
        self.RefreshView()
    
    def RefreshView(self):
        self.Freeze()
        self.VariablesList.DeleteAllItems()
        
        if self.Controller is not None and self.PouTagName is not None:
            self.PouInfos = self.Controller.GetPouVariables(self.PouTagName, self.Debug)
        else:
            self.PouInfos = None
        if self.PouInfos is not None:
            root = self.VariablesList.AddRoot("")
            for var_infos in self.PouInfos["variables"]:
                if var_infos.get("type", None) is not None:
                    text = "%(name)s (%(type)s)" % var_infos
                else:
                    text = var_infos["name"]
                
                panel = wx.Panel(self.VariablesList)
                    
                buttons = []
                if var_infos["class"] in ITEMS_VARIABLE:
                    if (not USE_MPL and var_infos["debug"] and self.Debug and
                        (self.Controller.IsOfType(var_infos["type"], "ANY_NUM", True) or
                         self.Controller.IsOfType(var_infos["type"], "ANY_BIT", True))):
                        graph_button = wx.lib.buttons.GenBitmapButton(panel, 
                              bitmap=GetBitmap("instance_graph"), 
                              size=wx.Size(28, 28), style=wx.NO_BORDER)
                        self.Bind(wx.EVT_BUTTON, self.GenGraphButtonCallback(var_infos), graph_button)
                        buttons.append(graph_button)
                elif var_infos["edit"]:
                    edit_button = wx.lib.buttons.GenBitmapButton(panel, 
                          bitmap=GetBitmap("edit"), 
                          size=wx.Size(28, 28), style=wx.NO_BORDER)
                    self.Bind(wx.EVT_BUTTON, self.GenEditButtonCallback(var_infos), edit_button)
                    buttons.append(edit_button)
                
                if var_infos["debug"] and self.Debug:
                    debug_button = wx.lib.buttons.GenBitmapButton(panel, 
                          bitmap=GetBitmap("debug_instance"), 
                          size=wx.Size(28, 28), style=wx.NO_BORDER)
                    self.Bind(wx.EVT_BUTTON, self.GenDebugButtonCallback(var_infos), debug_button)
                    debug_button.Bind(wx.EVT_LEFT_DCLICK, self.GenDebugButtonDClickCallback(var_infos))
                    buttons.append(debug_button)
                
                button_num = len(buttons)
                if button_num > 0:
                    panel.SetSize(wx.Size(button_num * 32, 28))
                    panel.SetBackgroundColour(self.VariablesList.GetBackgroundColour())
                    panel_sizer = wx.BoxSizer(wx.HORIZONTAL)
                    panel.SetSizer(panel_sizer)
                    
                    for button in buttons:
                        panel_sizer.AddWindow(button, 0, border=4, flag=wx.LEFT)
                    panel_sizer.Layout()
                    
                else:
                    panel.Destroy()
                    panel = None
                
                item = self.VariablesList.AppendItem(root, text, wnd=panel)
                self.VariablesList.SetItemImage(item, self.ParentWindow.GetTreeImage(var_infos["class"]))
                self.VariablesList.SetPyData(item, var_infos)
            
        self.RefreshInstanceChoice()
        self.RefreshButtons()
        
        self.Thaw()
    
    def RefreshInstanceChoice(self):
        self.InstanceChoice.Clear()
        self.InstanceChoice.SetValue("")
        if self.Controller is not None and self.PouInfos is not None:
            instances = self.Controller.SearchPouInstances(self.PouTagName, self.Debug)
            for instance in instances:
                self.InstanceChoice.Append(instance)
            if len(instances) == 1:
                self.PouInstance = instances[0]
            if self.PouInfos["class"] in [ITEM_CONFIGURATION, ITEM_RESOURCE]:
                self.PouInstance = None
                self.InstanceChoice.SetSelection(0)
            elif self.PouInstance in instances:
                self.InstanceChoice.SetStringSelection(self.PouInstance)
            else:
                self.PouInstance = None
                self.InstanceChoice.SetValue(_("Select an instance"))
    
    def RefreshButtons(self):
        enabled = self.InstanceChoice.GetSelection() != -1
        self.ParentButton.Enable(enabled and self.PouInfos["class"] != ITEM_CONFIGURATION)
        self.DebugButton.Enable(enabled and self.PouInfos["debug"] and self.Debug)
        
        root = self.VariablesList.GetRootItem()
        if root is not None and root.IsOk():
            item, item_cookie = self.VariablesList.GetFirstChild(root)
            while item is not None and item.IsOk():
                panel = self.VariablesList.GetItemWindow(item)
                if panel is not None:
                    for child in panel.GetChildren():
                        if child.GetName() != "edit":
                            child.Enable(enabled)
                item, item_cookie = self.VariablesList.GetNextChild(root, item_cookie)
    
    def GenEditButtonCallback(self, infos):
        def EditButtonCallback(event):
            var_class = infos["class"]
            if var_class == ITEM_RESOURCE:
                tagname = self.Controller.ComputeConfigurationResourceName(
                    self.InstanceChoice.GetStringSelection(), 
                    infos["name"])
            elif var_class == ITEM_TRANSITION:
                tagname = self.Controller.ComputePouTransitionName(
                    self.PouTagName.split("::")[1],
                    infos["name"])
            elif var_class == ITEM_ACTION:
                tagname = self.Controller.ComputePouActionName(
                    self.PouTagName.split("::")[1],
                    infos["name"])
            else:
                var_class = ITEM_POU
                tagname = self.Controller.ComputePouName(infos["type"])
            self.ParentWindow.EditProjectElement(var_class, tagname)
            event.Skip()
        return EditButtonCallback
    
    def GenDebugButtonCallback(self, infos):
        def DebugButtonCallback(event):
            if self.InstanceChoice.GetSelection() != -1:
                var_class = infos["class"]
                var_path = "%s.%s" % (self.InstanceChoice.GetStringSelection(), 
                                      infos["name"])
                if var_class in ITEMS_VARIABLE:
                    self.ParentWindow.AddDebugVariable(var_path, force=True)
                elif var_class == ITEM_TRANSITION:
                    self.ParentWindow.OpenDebugViewer(
                        var_class,
                        var_path,
                        self.Controller.ComputePouTransitionName(
                            self.PouTagName.split("::")[1],
                            infos["name"]))
                elif var_class == ITEM_ACTION:
                    self.ParentWindow.OpenDebugViewer(
                        var_class,
                        var_path,
                        self.Controller.ComputePouActionName(
                            self.PouTagName.split("::")[1],
                            infos["name"]))
                else:
                    self.ParentWindow.OpenDebugViewer(
                        var_class,
                        var_path,
                        self.Controller.ComputePouName(infos["type"]))
            event.Skip()
        return DebugButtonCallback
    
    def GenDebugButtonDClickCallback(self, infos):
        def DebugButtonDClickCallback(event):
            if self.InstanceChoice.GetSelection() != -1:
                if infos["class"] in ITEMS_VARIABLE:
                    self.ParentWindow.AddDebugVariable(
                        "%s.%s" % (self.InstanceChoice.GetStringSelection(), 
                                   infos["name"]), 
                        force=True,
                        graph=True)
            event.Skip()
        return DebugButtonDClickCallback
    
    def GenGraphButtonCallback(self, infos):
        def GraphButtonCallback(event):
            if self.InstanceChoice.GetSelection() != -1:
                if infos["class"] in ITEMS_VARIABLE:
                    var_path = "%s.%s" % (self.InstanceChoice.GetStringSelection(), 
                                          infos["name"])
                    self.ParentWindow.OpenDebugViewer(infos["class"], var_path, infos["type"])
            event.Skip()
        return GraphButtonCallback
    
    def ShowInstanceChoicePopup(self):
        self.InstanceChoice.SetFocusFromKbd()
        size = self.InstanceChoice.GetSize()
        event = wx.MouseEvent(wx.EVT_LEFT_DOWN._getEvtType())
        event.m_x = size.width / 2
        event.m_y = size.height / 2
        event.SetEventObject(self.InstanceChoice)
        #event = wx.KeyEvent(wx.EVT_KEY_DOWN._getEvtType())
        #event.m_keyCode = wx.WXK_SPACE
        self.InstanceChoice.GetEventHandler().ProcessEvent(event)
    
    def OnParentButtonClick(self, event):
        if self.InstanceChoice.GetSelection() != -1:
            parent_path = self.InstanceChoice.GetStringSelection().rsplit(".", 1)[0]
            tagname = self.Controller.GetPouInstanceTagName(parent_path, self.Debug)
            if tagname is not None:
                wx.CallAfter(self.SetPouType, tagname, parent_path)
                wx.CallAfter(self.ParentWindow.SelectProjectTreeItem, tagname)
        event.Skip()
        
    def OnInstanceChoiceChanged(self, event):
        self.RefreshButtons()
        event.Skip()
        
    def OnDebugButtonClick(self, event):
        if self.InstanceChoice.GetSelection() != -1:
            self.ParentWindow.OpenDebugViewer(
                self.PouInfos["class"],
                self.InstanceChoice.GetStringSelection(),
                self.PouTagName)
        event.Skip()
        
    def OnVariablesListItemActivated(self, event):
        selected_item = event.GetItem()
        if selected_item is not None and selected_item.IsOk():
            item_infos = self.VariablesList.GetPyData(selected_item)
            if item_infos is not None and item_infos["class"] not in ITEMS_VARIABLE:
                instance_path = self.InstanceChoice.GetStringSelection()
                if item_infos["class"] == ITEM_RESOURCE:
                    if instance_path != "":
                        tagname = self.Controller.ComputeConfigurationResourceName(
                                       instance_path, 
                                       item_infos["name"])
                    else:
                        tagname = None
                else:
                    tagname = self.Controller.ComputePouName(item_infos["type"])
                if tagname is not None:
                    if instance_path != "":
                        item_path = "%s.%s" % (instance_path, item_infos["name"])
                    else:
                        item_path = None
                    self.SetPouType(tagname, item_path)
                    self.ParentWindow.SelectProjectTreeItem(tagname)
        event.Skip()
    
    def OnVariablesListLeftDown(self, event):
        if self.InstanceChoice.GetSelection() == -1:
            wx.CallAfter(self.ShowInstanceChoicePopup)
        else:
            instance_path = self.InstanceChoice.GetStringSelection()
            item, flags = self.VariablesList.HitTest(event.GetPosition())
            if item is not None and flags & CT.TREE_HITTEST_ONITEMLABEL:
                item_infos = self.VariablesList.GetPyData(item)
                if item_infos is not None and item_infos["class"] in ITEMS_VARIABLE:
                    self.ParentWindow.EnsureTabVisible(
                        self.ParentWindow.DebugVariablePanel)
                    item_path = "%s.%s" % (instance_path, item_infos["name"])
                    data = wx.TextDataObject(str((item_path, "debug")))
                    dragSource = wx.DropSource(self.VariablesList)
                    dragSource.SetData(data)
                    dragSource.DoDragDrop()
        event.Skip()

    def OnVariablesListKeyDown(self, event):
        keycode = event.GetKeyCode()
        if keycode != wx.WXK_LEFT:
            event.Skip()