Laurent@814: #!/usr/bin/env python Laurent@814: # -*- coding: utf-8 -*- Laurent@814: Laurent@814: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor Laurent@814: #based on the plcopen standard. Laurent@814: # Laurent@814: #Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD Laurent@814: # Laurent@814: #See COPYING file for copyrights details. Laurent@814: # Laurent@814: #This library is free software; you can redistribute it and/or Laurent@814: #modify it under the terms of the GNU General Public Laurent@814: #License as published by the Free Software Foundation; either Laurent@814: #version 2.1 of the License, or (at your option) any later version. Laurent@814: # Laurent@814: #This library is distributed in the hope that it will be useful, Laurent@814: #but WITHOUT ANY WARRANTY; without even the implied warranty of Laurent@814: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Laurent@814: #General Public License for more details. Laurent@814: # Laurent@814: #You should have received a copy of the GNU General Public Laurent@814: #License along with this library; if not, write to the Free Software Laurent@814: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Laurent@814: Laurent@814: import wx Laurent@814: import wx.lib.buttons Laurent@814: import wx.lib.agw.customtreectrl as CT Laurent@814: laurent@826: from PLCControler import ITEMS_VARIABLE, ITEM_CONFIGURATION, ITEM_RESOURCE, ITEM_POU, ITEM_TRANSITION, ITEM_ACTION Laurent@814: from util.BitmapLibrary import GetBitmap Laurent@814: Laurent@814: class PouInstanceVariablesPanel(wx.Panel): Laurent@814: Laurent@814: def __init__(self, parent, window, controller, debug): Laurent@814: wx.Panel.__init__(self, name='PouInstanceTreePanel', Laurent@814: parent=parent, pos=wx.Point(0, 0), Laurent@814: size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL) Laurent@814: Laurent@814: self.ParentButton = wx.lib.buttons.GenBitmapButton(self, Laurent@814: bitmap=GetBitmap("top"), size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@814: self.ParentButton.SetToolTipString(_("Parent instance")) Laurent@814: self.Bind(wx.EVT_BUTTON, self.OnParentButtonClick, Laurent@814: self.ParentButton) Laurent@814: laurent@821: self.InstanceChoice = wx.ComboBox(self, size=wx.Size(0, 0), style=wx.CB_READONLY) Laurent@814: self.Bind(wx.EVT_COMBOBOX, self.OnInstanceChoiceChanged, Laurent@814: self.InstanceChoice) Laurent@814: self.InstanceChoice.Bind(wx.EVT_LEFT_DOWN, self.OnInstanceChoiceLeftDown) Laurent@814: Laurent@814: self.DebugButton = wx.lib.buttons.GenBitmapButton(self, Laurent@814: bitmap=GetBitmap("debug_instance"), size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@814: self.ParentButton.SetToolTipString(_("Debug instance")) Laurent@814: self.Bind(wx.EVT_BUTTON, self.OnDebugButtonClick, Laurent@814: self.DebugButton) Laurent@814: Laurent@814: self.VariablesList = CT.CustomTreeCtrl(self, Laurent@814: style=wx.SUNKEN_BORDER, Laurent@814: agwStyle=CT.TR_NO_BUTTONS| Laurent@814: CT.TR_SINGLE| Laurent@814: CT.TR_HAS_VARIABLE_ROW_HEIGHT| Laurent@814: CT.TR_HIDE_ROOT| Laurent@814: CT.TR_NO_LINES| Laurent@814: getattr(CT, "TR_ALIGN_WINDOWS_RIGHT", CT.TR_ALIGN_WINDOWS)) Laurent@814: self.VariablesList.SetIndent(0) Laurent@814: self.VariablesList.SetSpacing(5) Laurent@814: self.VariablesList.DoSelectItem = lambda *x,**y:True Laurent@814: self.VariablesList.Bind(CT.EVT_TREE_ITEM_ACTIVATED, Laurent@814: self.OnVariablesListItemActivated) Laurent@814: self.VariablesList.Bind(wx.EVT_LEFT_DOWN, self.OnVariablesListLeftDown) Laurent@814: Laurent@814: buttons_sizer = wx.FlexGridSizer(cols=3, hgap=0, rows=1, vgap=0) Laurent@814: buttons_sizer.AddWindow(self.ParentButton) Laurent@814: buttons_sizer.AddWindow(self.InstanceChoice, flag=wx.GROW) Laurent@814: buttons_sizer.AddWindow(self.DebugButton) Laurent@814: buttons_sizer.AddGrowableCol(1) Laurent@814: buttons_sizer.AddGrowableRow(0) Laurent@814: Laurent@814: main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) Laurent@814: main_sizer.AddSizer(buttons_sizer, flag=wx.GROW) Laurent@814: main_sizer.AddWindow(self.VariablesList, flag=wx.GROW) Laurent@814: main_sizer.AddGrowableCol(0) Laurent@814: main_sizer.AddGrowableRow(1) Laurent@814: Laurent@814: self.SetSizer(main_sizer) Laurent@814: Laurent@814: self.ParentWindow = window Laurent@814: self.Controller = controller Laurent@814: self.Debug = debug Laurent@814: if not self.Debug: Laurent@814: self.DebugButton.Hide() Laurent@814: Laurent@814: self.PouTagName = None Laurent@814: self.PouInfos = None Laurent@814: self.PouInstance = None Laurent@814: Laurent@814: def __del__(self): Laurent@814: self.Controller = None Laurent@814: Laurent@814: def SetTreeImageList(self, tree_image_list): Laurent@814: self.VariablesList.SetImageList(tree_image_list) Laurent@814: Laurent@814: def SetController(self, controller): Laurent@814: self.Controller = controller Laurent@814: Laurent@814: self.RefreshView() Laurent@814: Laurent@814: def SetPouType(self, tagname, pou_instance=None): Laurent@814: self.PouTagName = tagname Laurent@814: if pou_instance is not None: Laurent@814: self.PouInstance = pou_instance Laurent@814: Laurent@814: self.RefreshView() Laurent@814: Laurent@814: def ResetView(self): Laurent@814: self.Controller = None Laurent@814: Laurent@814: self.PouTagName = None Laurent@814: self.PouInfos = None Laurent@814: self.PouInstance = None Laurent@814: Laurent@814: self.RefreshView() Laurent@814: Laurent@814: def RefreshView(self): Laurent@814: self.VariablesList.DeleteAllItems() Laurent@814: self.InstanceChoice.Clear() Laurent@814: self.InstanceChoice.SetValue("") Laurent@814: Laurent@814: if self.Controller is not None and self.PouTagName is not None: Laurent@814: self.PouInfos = self.Controller.GetPouVariables(self.PouTagName, self.Debug) Laurent@814: else: Laurent@814: self.PouInfos = None Laurent@814: if self.PouInfos is not None: Laurent@814: root = self.VariablesList.AddRoot("") Laurent@814: for var_infos in self.PouInfos["variables"]: Laurent@814: if var_infos.get("type", None) is not None: Laurent@814: text = "%(name)s (%(type)s)" % var_infos Laurent@814: else: Laurent@814: text = var_infos["name"] Laurent@814: Laurent@814: panel = wx.Panel(self.VariablesList) Laurent@814: Laurent@814: buttons = [] Laurent@814: if var_infos["class"] in ITEMS_VARIABLE: Laurent@814: if (var_infos["debug"] and self.Debug and Laurent@814: (self.Controller.IsOfType(var_infos["type"], "ANY_NUM", True) or Laurent@814: self.Controller.IsOfType(var_infos["type"], "ANY_BIT", True))): Laurent@814: graph_button = wx.lib.buttons.GenBitmapButton(panel, Laurent@814: bitmap=GetBitmap("instance_graph"), Laurent@814: size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@814: self.Bind(wx.EVT_BUTTON, self.GenGraphButtonCallback(var_infos), graph_button) Laurent@814: buttons.append(graph_button) Laurent@814: elif var_infos["edit"]: Laurent@814: edit_button = wx.lib.buttons.GenBitmapButton(panel, Laurent@814: bitmap=GetBitmap("edit"), Laurent@814: size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@814: self.Bind(wx.EVT_BUTTON, self.GenEditButtonCallback(var_infos), edit_button) Laurent@814: buttons.append(edit_button) Laurent@814: Laurent@814: if var_infos["debug"] and self.Debug: Laurent@814: debug_button = wx.lib.buttons.GenBitmapButton(panel, Laurent@814: bitmap=GetBitmap("debug_instance"), Laurent@814: size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@814: self.Bind(wx.EVT_BUTTON, self.GenDebugButtonCallback(var_infos), debug_button) Laurent@814: buttons.append(debug_button) Laurent@814: Laurent@814: button_num = len(buttons) Laurent@814: if button_num > 0: Laurent@814: panel.SetSize(wx.Size(button_num * 32, 28)) Laurent@814: panel.SetBackgroundColour(self.VariablesList.GetBackgroundColour()) Laurent@814: panel_sizer = wx.BoxSizer(wx.HORIZONTAL) Laurent@814: panel.SetSizer(panel_sizer) Laurent@814: Laurent@814: for button in buttons: Laurent@814: panel_sizer.AddWindow(button, 0, border=4, flag=wx.LEFT) Laurent@814: panel_sizer.Layout() Laurent@814: Laurent@814: else: Laurent@814: panel.Destroy() Laurent@814: panel = None Laurent@814: Laurent@814: item = self.VariablesList.AppendItem(root, text, wnd=panel) Laurent@814: self.VariablesList.SetItemImage(item, self.ParentWindow.GetTreeImage(var_infos["class"])) Laurent@814: self.VariablesList.SetPyData(item, var_infos) Laurent@814: Laurent@814: instances = self.Controller.SearchPouInstances(self.PouTagName, self.Debug) Laurent@814: for instance in instances: Laurent@814: self.InstanceChoice.Append(instance) Laurent@814: if len(instances) == 1: Laurent@814: self.PouInstance = instances[0] Laurent@814: if self.PouInfos["class"] in [ITEM_CONFIGURATION, ITEM_RESOURCE]: Laurent@814: self.PouInstance = None Laurent@814: self.InstanceChoice.SetSelection(0) Laurent@814: elif self.PouInstance in instances: Laurent@814: self.InstanceChoice.SetStringSelection(self.PouInstance) Laurent@814: else: Laurent@814: self.PouInstance = None Laurent@814: self.InstanceChoice.SetValue(_("Select an instance")) Laurent@814: Laurent@814: self.RefreshButtons() Laurent@814: Laurent@814: def RefreshButtons(self): Laurent@814: enabled = self.InstanceChoice.GetSelection() != -1 Laurent@814: self.ParentButton.Enable(enabled and self.PouInfos["class"] != ITEM_CONFIGURATION) Laurent@814: self.DebugButton.Enable(enabled and self.PouInfos["debug"] and self.Debug) Laurent@814: Laurent@814: root = self.VariablesList.GetRootItem() Laurent@814: if root is not None and root.IsOk(): Laurent@814: item, item_cookie = self.VariablesList.GetFirstChild(root) Laurent@814: while item is not None and item.IsOk(): Laurent@814: panel = self.VariablesList.GetItemWindow(item) Laurent@814: if panel is not None: Laurent@814: for child in panel.GetChildren(): Laurent@814: if child.GetName() != "edit": Laurent@814: child.Enable(enabled) Laurent@814: item, item_cookie = self.VariablesList.GetNextChild(root, item_cookie) Laurent@814: Laurent@814: def GenEditButtonCallback(self, infos): Laurent@814: def EditButtonCallback(event): Laurent@814: var_class = infos["class"] Laurent@814: if var_class == ITEM_RESOURCE: Laurent@814: tagname = self.Controller.ComputeConfigurationResourceName( Laurent@814: self.InstanceChoice.GetStringSelection(), Laurent@814: infos["name"]) laurent@826: elif var_class == ITEM_TRANSITION: laurent@826: tagname = self.Controller.ComputePouTransitionName( laurent@826: self.PouTagName.split("::")[1], laurent@826: infos["name"]) laurent@826: elif var_class == ITEM_ACTION: laurent@826: tagname = self.Controller.ComputePouActionName( laurent@826: self.PouTagName.split("::")[1], laurent@826: infos["name"]) Laurent@814: else: Laurent@814: var_class = ITEM_POU Laurent@814: tagname = self.Controller.ComputePouName(infos["type"]) Laurent@814: self.ParentWindow.EditProjectElement(var_class, tagname) Laurent@814: event.Skip() Laurent@814: return EditButtonCallback Laurent@814: Laurent@814: def GenDebugButtonCallback(self, infos): Laurent@814: def DebugButtonCallback(event): Laurent@814: if self.InstanceChoice.GetSelection() != -1: Laurent@814: var_class = infos["class"] Laurent@814: var_path = "%s.%s" % (self.InstanceChoice.GetStringSelection(), Laurent@814: infos["name"]) Laurent@814: if var_class in ITEMS_VARIABLE: Laurent@814: self.ParentWindow.AddDebugVariable(var_path, force=True) laurent@826: elif var_class == ITEM_TRANSITION: laurent@826: self.ParentWindow.OpenDebugViewer( laurent@826: var_class, laurent@826: var_path, laurent@826: self.Controller.ComputePouTransitionName( laurent@826: self.PouTagName.split("::")[1], laurent@826: infos["name"])) laurent@826: elif var_class == ITEM_ACTION: laurent@826: self.ParentWindow.OpenDebugViewer( laurent@826: var_class, laurent@826: var_path, laurent@826: self.Controller.ComputePouActionName( laurent@826: self.PouTagName.split("::")[1], laurent@826: infos["name"])) Laurent@814: else: Laurent@814: self.ParentWindow.OpenDebugViewer( laurent@826: var_class, Laurent@814: var_path, Laurent@814: self.Controller.ComputePouName(infos["type"])) Laurent@814: event.Skip() Laurent@814: return DebugButtonCallback Laurent@814: Laurent@814: def GenGraphButtonCallback(self, infos): Laurent@814: def GraphButtonCallback(event): Laurent@814: if self.InstanceChoice.GetSelection() != -1: Laurent@814: if infos["class"] in ITEMS_VARIABLE: Laurent@814: var_path = "%s.%s" % (self.InstanceChoice.GetStringSelection(), Laurent@814: infos["name"]) Laurent@885: self.ParentWindow.OpenDebugViewer(infos["class"], var_path, infos["type"]) Laurent@814: event.Skip() Laurent@814: return GraphButtonCallback Laurent@814: Laurent@814: def ShowInstanceChoicePopup(self): Laurent@814: self.InstanceChoice.SetFocusFromKbd() Laurent@814: size = self.InstanceChoice.GetSize() Laurent@814: event = wx.MouseEvent(wx.EVT_LEFT_DOWN._getEvtType()) Laurent@814: event.m_x = size.width / 2 Laurent@814: event.m_y = size.height / 2 Laurent@814: event.SetEventObject(self.InstanceChoice) Laurent@814: #event = wx.KeyEvent(wx.EVT_KEY_DOWN._getEvtType()) Laurent@814: #event.m_keyCode = wx.WXK_SPACE Laurent@814: self.InstanceChoice.GetEventHandler().ProcessEvent(event) Laurent@814: Laurent@814: def OnParentButtonClick(self, event): Laurent@814: if self.InstanceChoice.GetSelection() != -1: Laurent@814: parent_path = self.InstanceChoice.GetStringSelection().rsplit(".", 1)[0] Laurent@814: tagname = self.Controller.GetPouInstanceTagName(parent_path, self.Debug) Laurent@814: if tagname is not None: Laurent@814: wx.CallAfter(self.SetPouType, tagname, parent_path) Laurent@814: wx.CallAfter(self.ParentWindow.SelectProjectTreeItem, tagname) Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnInstanceChoiceChanged(self, event): Laurent@814: self.RefreshButtons() Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnDebugButtonClick(self, event): Laurent@814: if self.InstanceChoice.GetSelection() != -1: Laurent@814: self.ParentWindow.OpenDebugViewer( Laurent@814: self.PouInfos["class"], Laurent@814: self.InstanceChoice.GetStringSelection(), Laurent@814: self.PouTagName) Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnVariablesListItemActivated(self, event): Laurent@814: if self.InstanceChoice.GetSelection() != -1: Laurent@814: instance_path = self.InstanceChoice.GetStringSelection() Laurent@814: selected_item = event.GetItem() Laurent@814: if selected_item is not None and selected_item.IsOk(): Laurent@814: item_infos = self.VariablesList.GetPyData(selected_item) Laurent@814: if item_infos is not None and item_infos["class"] not in ITEMS_VARIABLE: Laurent@814: if item_infos["class"] == ITEM_RESOURCE: Laurent@814: tagname = self.Controller.ComputeConfigurationResourceName( Laurent@814: instance_path, Laurent@814: item_infos["name"]) Laurent@814: else: Laurent@814: tagname = self.Controller.ComputePouName(item_infos["type"]) Laurent@814: item_path = "%s.%s" % (instance_path, item_infos["name"]) Laurent@814: wx.CallAfter(self.SetPouType, tagname, item_path) Laurent@814: wx.CallAfter(self.ParentWindow.SelectProjectTreeItem, tagname) Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnVariablesListLeftDown(self, event): Laurent@814: if self.InstanceChoice.GetSelection() == -1: Laurent@814: wx.CallAfter(self.ShowInstanceChoicePopup) Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnInstanceChoiceLeftDown(self, event): Laurent@814: event.Skip()