#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
from types import TupleType
from editors.DebugViewer import DebugViewer
from controls import CustomGrid, CustomTable
from dialogs.ForceVariableDialog import ForceVariableDialog
from util.BitmapLibrary import GetBitmap
from DebugVariableItem import DebugVariableItem
def GetDebugVariablesTableColnames():
return [_("Variable"), _("Value")]
class DebugVariableTable(CustomTable):
def GetValue(self, row, col):
if row < self.GetNumberRows():
return self.GetValueByName(row, self.GetColLabelValue(col, False))
def SetValue(self, row, col, value):
if col < len(self.colnames):
self.SetValueByName(row, self.GetColLabelValue(col, False), value)
def GetValueByName(self, row, colname):
if row < self.GetNumberRows():
if colname == "Variable":
return self.data[row].GetVariable()
return self.data[row].GetValue()
def SetValueByName(self, row, colname, value):
if row < self.GetNumberRows():
if colname == "Variable":
self.data[row].SetVariable(value)
self.data[row].SetValue(value)
if row < self.GetNumberRows():
return self.data[row].IsForced()
def IsNumVariable(self, row):
if row < self.GetNumberRows():
return self.data[row].IsNumVariable()
def _updateColAttrs(self, grid):
wx.grid.Grid -> update the column attributes to add the
appropriate renderer given the column name.
Otherwise default to the default renderer.
for row in range(self.GetNumberRows()):
for col in range(self.GetNumberCols()):
colname = self.GetColLabelValue(col, False)
grid.SetCellTextColour(row, col, wx.BLUE)
grid.SetCellTextColour(row, col, wx.BLACK)
grid.SetReadOnly(row, col, True)
self.ResizeRow(grid, row)
def AppendItem(self, data):
def InsertItem(self, idx, data):
self.data.insert(idx, data)
def RemoveItem(self, idx):
def MoveItem(self, idx, new_idx):
self.data.insert(new_idx, self.data.pop(idx))
class DebugVariableTableDropTarget(wx.TextDropTarget):
def __init__(self, parent):
wx.TextDropTarget.__init__(self)
self.ParentWindow = parent
def OnDropText(self, x, y, data):
message = _("Invalid value \"%s\" for debug variable")%data
if not isinstance(values, TupleType):
message = _("Invalid value \"%s\" for debug variable")%data
wx.CallAfter(self.ShowMessage, message)
elif values is not None and values[1] == "debug":
grid = self.ParentWindow.VariablesGrid
x, y = grid.CalcUnscrolledPosition(x, y)
row = grid.YToRow(y - grid.GetColLabelSize())
row = self.ParentWindow.Table.GetNumberRows()
self.ParentWindow.InsertValue(values[0], row, force=True)
def ShowMessage(self, message):
dialog = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK|wx.ICON_ERROR)
class DebugVariableTablePanel(wx.Panel, DebugViewer):
def __init__(self, parent, producer, window):
wx.Panel.__init__(self, parent, style=wx.SP_3D|wx.TAB_TRAVERSAL)
self.ParentWindow = window
DebugViewer.__init__(self, producer, True)
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
main_sizer.AddGrowableCol(0)
main_sizer.AddGrowableRow(1)
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
main_sizer.AddSizer(button_sizer, border=5,
flag=wx.ALIGN_RIGHT|wx.ALL)
for name, bitmap, help in [
("DeleteButton", "remove_element", _("Remove debug variable")),
("UpButton", "up", _("Move debug variable up")),
("DownButton", "down", _("Move debug variable down"))]:
button = wx.lib.buttons.GenBitmapButton(self, bitmap=GetBitmap(bitmap),
size=wx.Size(28, 28), style=wx.NO_BORDER)
button.SetToolTipString(help)
setattr(self, name, button)
button_sizer.AddWindow(button, border=5, flag=wx.LEFT)
self.VariablesGrid = CustomGrid(self, size=wx.Size(-1, 150), style=wx.VSCROLL)
self.VariablesGrid.SetDropTarget(DebugVariableTableDropTarget(self))
self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,
self.OnVariablesGridCellRightClick)
self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK,
self.OnVariablesGridCellLeftClick)
main_sizer.AddWindow(self.VariablesGrid, flag=wx.GROW)
self.Table = DebugVariableTable(self, [], GetDebugVariablesTableColnames())
self.VariablesGrid.SetTable(self.Table)
self.VariablesGrid.SetButtons({"Delete": self.DeleteButton,
"Down": self.DownButton})
def _AddVariable(new_row):
return self.VariablesGrid.GetGridCursorRow()
setattr(self.VariablesGrid, "_AddRow", _AddVariable)
def _DeleteVariable(row):
item = self.Table.GetItem(row)
self.RemoveDataConsumer(item)
self.Table.RemoveItem(row)
setattr(self.VariablesGrid, "_DeleteRow", _DeleteVariable)
def _MoveVariable(row, move):
new_row = max(0, min(row + move, self.Table.GetNumberRows() - 1))
self.Table.MoveItem(row, new_row)
setattr(self.VariablesGrid, "_MoveRow", _MoveVariable)
self.VariablesGrid.SetRowLabelSize(0)
self.GridColSizes = [200, 100]
for col in range(self.Table.GetNumberCols()):
attr = wx.grid.GridCellAttr()
attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTER)
self.VariablesGrid.SetColAttr(col, attr)
self.VariablesGrid.SetColSize(col, self.GridColSizes[col])
self.Table.ResetView(self.VariablesGrid)
self.VariablesGrid.RefreshButtons()
self.SetSizer(main_sizer)
def RefreshNewData(self, *args, **kwargs):
self.RefreshView(only_values=True)
DebugViewer.RefreshNewData(self, *args, **kwargs)
def RefreshView(self, only_values=False):
for col in xrange(self.Table.GetNumberCols()):
if self.Table.GetColLabelValue(col, False) == "Value":
for row in xrange(self.Table.GetNumberRows()):
self.VariablesGrid.SetCellValue(row, col, str(self.Table.GetValueByName(row, "Value")))
if self.Table.IsForced(row):
self.VariablesGrid.SetCellTextColour(row, col, wx.BLUE)
self.VariablesGrid.SetCellTextColour(row, col, wx.BLACK)
self.Table.ResetView(self.VariablesGrid)
self.VariablesGrid.RefreshButtons()
def UnsubscribeObsoleteData(self):
self.SubscribeAllDataConsumers()
items = [(idx, item) for idx, item in enumerate(self.Table.GetData())]
iec_path = item.GetVariable()
if self.GetDataType(iec_path) is None:
self.RemoveDataConsumer(item)
self.Table.RemoveItem(idx)
self.AddDataConsumer(iec_path.upper(), item)
item.RefreshVariableType()
self.Table.ResetView(self.VariablesGrid)
self.VariablesGrid.RefreshButtons()
self.UnsubscribeAllDataConsumers()
self.Table.ResetView(self.VariablesGrid)
self.VariablesGrid.RefreshButtons()
def GetForceVariableMenuFunction(self, iec_path, item):
iec_type = self.GetDataType(iec_path)
def ForceVariableFunction(event):
dialog = ForceVariableDialog(self, iec_type, str(item.GetValue()))
if dialog.ShowModal() == wx.ID_OK:
self.ForceDataValue(iec_path.upper(), dialog.GetValue())
return ForceVariableFunction
def GetReleaseVariableMenuFunction(self, iec_path):
def ReleaseVariableFunction(event):
self.ReleaseDataValue(iec_path)
return ReleaseVariableFunction
def OnVariablesGridCellLeftClick(self, event):
data = wx.TextDataObject(str((self.Table.GetValueByName(row, "Variable"), "debug")))
dragSource = wx.DropSource(self.VariablesGrid)
def OnVariablesGridCellRightClick(self, event):
row, col = event.GetRow(), event.GetCol()
if self.Table.GetColLabelValue(col, False) == "Value":
iec_path = self.Table.GetValueByName(row, "Variable").upper()
menu.Append(help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Force value"))
self.Bind(wx.EVT_MENU, self.GetForceVariableMenuFunction(iec_path.upper(), self.Table.GetItem(row)), id=new_id)
menu.Append(help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Release value"))
self.Bind(wx.EVT_MENU, self.GetReleaseVariableMenuFunction(iec_path.upper()), id=new_id)
if self.Table.IsForced(row):
menu.Enable(new_id, True)
menu.Enable(new_id, False)
def InsertValue(self, iec_path, idx = None, force=False):
for item in self.Table.GetData():
if iec_path == item.GetVariable():
idx = self.Table.GetNumberRows()
item = DebugVariableItem(self, iec_path)
result = self.AddDataConsumer(iec_path.upper(), item)
if result is not None or force:
self.Table.InsertItem(idx, item)
def ResetGraphicsValues(self):