#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.lib.agw.customtreectrl as CT
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.
if bitmap is not None and createOthers:
GrayBitmap=GrayedBitmapCache.get(bitmap,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
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.InstanceChoice = wx.ComboBox(self, size=wx.Size(0, 0), style=wx.CB_READONLY)
self.Bind(wx.EVT_COMBOBOX, self.OnInstanceChoiceChanged,
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.VariablesList = CT.CustomTreeCtrl(self,
agwStyle=CT.TR_NO_BUTTONS|
CT.TR_HAS_VARIABLE_ROW_HEIGHT|
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
def SetTreeImageList(self, tree_image_list):
self.VariablesList.SetImageList(tree_image_list)
def SetController(self, controller):
self.Controller = controller
def SetPouType(self, tagname, pou_instance=None):
if self.Controller is not None:
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.RefreshInstanceChoice()
self.VariablesList.DeleteAllItems()
if self.Controller is not None and self.PouTagName is not None:
self.PouInfos = self.Controller.GetPouVariables(self.PouTagName, self.Debug)
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
panel = wx.Panel(self.VariablesList)
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)
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)
panel.SetSize(wx.Size(button_num * 32, 28))
panel.SetBackgroundColour(self.VariablesList.GetBackgroundColour())
panel_sizer = wx.BoxSizer(wx.HORIZONTAL)
panel.SetSizer(panel_sizer)
panel_sizer.AddWindow(button, 0, border=4, flag=wx.LEFT)
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()
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)
self.PouInstance = instances[0]
if self.PouInfos["class"] in [ITEM_CONFIGURATION, ITEM_RESOURCE]:
self.InstanceChoice.SetSelection(0)
elif self.PouInstance in instances:
self.InstanceChoice.SetStringSelection(self.PouInstance)
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)
for child in panel.GetChildren():
if child.GetName() != "edit":
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(),
elif var_class == ITEM_TRANSITION:
tagname = self.Controller.ComputePouTransitionName(
self.PouTagName.split("::")[1],
elif var_class == ITEM_ACTION:
tagname = self.Controller.ComputePouActionName(
self.PouTagName.split("::")[1],
tagname = self.Controller.ComputePouName(infos["type"])
self.ParentWindow.EditProjectElement(var_class, tagname)
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(),
if var_class in ITEMS_VARIABLE:
self.ParentWindow.AddDebugVariable(var_path, force=True)
elif var_class == ITEM_TRANSITION:
self.ParentWindow.OpenDebugViewer(
self.Controller.ComputePouTransitionName(
self.PouTagName.split("::")[1],
elif var_class == ITEM_ACTION:
self.ParentWindow.OpenDebugViewer(
self.Controller.ComputePouActionName(
self.PouTagName.split("::")[1],
self.ParentWindow.OpenDebugViewer(
self.Controller.ComputePouName(infos["type"]))
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(),
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(),
self.ParentWindow.OpenDebugViewer(infos["class"], var_path, infos["type"])
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)
wx.CallAfter(self.SetPouType, tagname, parent_path)
wx.CallAfter(self.ParentWindow.SelectProjectTreeItem, tagname)
def OnInstanceChoiceChanged(self, event):
def OnDebugButtonClick(self, event):
if self.InstanceChoice.GetSelection() != -1:
self.ParentWindow.OpenDebugViewer(
self.InstanceChoice.GetStringSelection(),
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:
tagname = self.Controller.ComputeConfigurationResourceName(
tagname = self.Controller.ComputePouName(item_infos["type"])
item_path = "%s.%s" % (instance_path, item_infos["name"])
self.SetPouType(tagname, item_path)
self.ParentWindow.SelectProjectTreeItem(tagname)
def OnVariablesListLeftDown(self, event):
if self.InstanceChoice.GetSelection() == -1:
wx.CallAfter(self.ShowInstanceChoicePopup)
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)
def OnVariablesListKeyDown(self, event):
keycode = event.GetKeyCode()
if keycode != wx.WXK_LEFT: