laurent@409: # -*- coding: utf-8 -*- laurent@409: laurent@409: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor laurent@409: #based on the plcopen standard. laurent@409: # laurent@409: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD laurent@409: # laurent@409: #See COPYING file for copyrights details. laurent@409: # laurent@409: #This library is free software; you can redistribute it and/or laurent@409: #modify it under the terms of the GNU General Public laurent@409: #License as published by the Free Software Foundation; either laurent@409: #version 2.1 of the License, or (at your option) any later version. laurent@409: # laurent@409: #This library is distributed in the hope that it will be useful, laurent@409: #but WITHOUT ANY WARRANTY; without even the implied warranty of laurent@409: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU laurent@409: #General Public License for more details. laurent@409: # laurent@409: #You should have received a copy of the GNU General Public laurent@409: #License along with this library; if not, write to the Free Software laurent@409: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA laurent@409: laurent@409: import wx laurent@409: import wx.grid Laurent@714: import wx.lib.buttons laurent@409: laurent@604: from controls import CustomGrid, CustomTable Laurent@714: from utils.BitmapLibrary import GetBitmap Laurent@714: Laurent@714: #------------------------------------------------------------------------------- Laurent@714: # Helpers laurent@409: #------------------------------------------------------------------------------- laurent@409: laurent@409: def GetActionTableColnames(): laurent@409: _ = lambda x: x laurent@409: return [_("Qualifier"), _("Duration"), _("Type"), _("Value"), _("Indicator")] laurent@409: laurent@409: def GetTypeList(): laurent@409: _ = lambda x: x laurent@409: return [_("Action"), _("Variable"), _("Inline")] laurent@409: Laurent@714: #------------------------------------------------------------------------------- Laurent@714: # Action Table Laurent@714: #------------------------------------------------------------------------------- Laurent@714: laurent@604: class ActionTable(CustomTable): laurent@604: laurent@409: def GetValue(self, row, col): laurent@409: if row < self.GetNumberRows(): laurent@409: colname = self.GetColLabelValue(col, False) laurent@409: name = str(self.data[row].get(colname, "")) laurent@409: if colname == "Type": laurent@409: return _(name) laurent@409: return name laurent@409: laurent@409: def SetValue(self, row, col, value): laurent@409: if col < len(self.colnames): laurent@409: colname = self.GetColLabelValue(col, False) laurent@409: if colname == "Type": laurent@409: value = self.Parent.TranslateType[value] laurent@409: self.data[row][colname] = value laurent@409: laurent@409: def _updateColAttrs(self, grid): laurent@409: """ laurent@409: wx.Grid -> update the column attributes to add the laurent@409: appropriate renderer given the column name. laurent@409: laurent@409: Otherwise default to the default renderer. laurent@409: """ laurent@409: laurent@409: for row in range(self.GetNumberRows()): laurent@409: for col in range(self.GetNumberCols()): laurent@409: editor = None laurent@409: renderer = None laurent@409: readonly = False laurent@409: colname = self.GetColLabelValue(col, False) laurent@409: if colname == "Qualifier": laurent@409: editor = wx.grid.GridCellChoiceEditor() laurent@409: editor.SetParameters(self.Parent.QualifierList) laurent@409: if colname == "Duration": laurent@409: editor = wx.grid.GridCellTextEditor() laurent@409: renderer = wx.grid.GridCellStringRenderer() laurent@409: if self.Parent.DurationList[self.data[row]["Qualifier"]]: laurent@409: readonly = False laurent@409: else: laurent@409: readonly = True laurent@409: self.data[row]["Duration"] = "" laurent@409: elif colname == "Type": laurent@409: editor = wx.grid.GridCellChoiceEditor() laurent@409: editor.SetParameters(self.Parent.TypeList) laurent@409: elif colname == "Value": laurent@409: type = self.data[row]["Type"] laurent@409: if type == "Action": laurent@409: editor = wx.grid.GridCellChoiceEditor() laurent@409: editor.SetParameters(self.Parent.ActionList) laurent@409: elif type == "Variable": laurent@409: editor = wx.grid.GridCellChoiceEditor() laurent@409: editor.SetParameters(self.Parent.VariableList) laurent@409: elif type == "Inline": laurent@409: editor = wx.grid.GridCellTextEditor() laurent@409: renderer = wx.grid.GridCellStringRenderer() laurent@409: elif colname == "Indicator": laurent@409: editor = wx.grid.GridCellChoiceEditor() laurent@409: editor.SetParameters(self.Parent.VariableList) laurent@409: laurent@409: grid.SetCellEditor(row, col, editor) laurent@409: grid.SetCellRenderer(row, col, renderer) laurent@409: grid.SetReadOnly(row, col, readonly) laurent@409: laurent@409: grid.SetCellBackgroundColour(row, col, wx.WHITE) laurent@604: self.ResizeRow(grid, row) laurent@604: Laurent@714: #------------------------------------------------------------------------------- Laurent@714: # Action Block Dialog Laurent@714: #------------------------------------------------------------------------------- laurent@409: laurent@409: class ActionBlockDialog(wx.Dialog): laurent@409: Laurent@714: def __init__(self, parent): Laurent@714: wx.Dialog.__init__(self, parent, Laurent@714: size=wx.Size(500, 300), title=_('Edit action block properties')) Laurent@714: Laurent@714: main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) Laurent@714: main_sizer.AddGrowableCol(0) Laurent@714: main_sizer.AddGrowableRow(1) Laurent@714: Laurent@714: top_sizer = wx.FlexGridSizer(cols=5, hgap=5, rows=1, vgap=0) Laurent@714: top_sizer.AddGrowableCol(0) Laurent@714: top_sizer.AddGrowableRow(0) Laurent@714: main_sizer.AddSizer(top_sizer, border=20, Laurent@714: flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) Laurent@714: Laurent@714: actions_label = wx.StaticText(self, label=_('Actions:')) Laurent@714: top_sizer.AddWindow(actions_label, flag=wx.ALIGN_BOTTOM) Laurent@714: Laurent@714: for name, bitmap, help in [ Laurent@714: ("AddButton", "add_element", _("Add action")), Laurent@714: ("DeleteButton", "remove_element", _("Remove action")), Laurent@714: ("UpButton", "up", _("Move action up")), Laurent@714: ("DownButton", "down", _("Move action down"))]: Laurent@714: button = wx.lib.buttons.GenBitmapButton(self, bitmap=GetBitmap(bitmap), Laurent@714: size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@714: button.SetToolTipString(help) Laurent@714: setattr(self, name, button) Laurent@714: top_sizer.AddWindow(button) Laurent@714: Laurent@714: self.ActionsGrid = CustomGrid(self, style=wx.VSCROLL) laurent@409: self.ActionsGrid.DisableDragGridSize() laurent@409: self.ActionsGrid.EnableScrolling(False, True) Laurent@714: self.ActionsGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, Laurent@714: self.OnActionsGridCellChange) Laurent@714: main_sizer.AddSizer(self.ActionsGrid, border=20, Laurent@714: flag=wx.GROW|wx.LEFT|wx.RIGHT) Laurent@714: Laurent@714: button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) Laurent@714: self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton()) Laurent@714: main_sizer.AddSizer(button_sizer, border=20, Laurent@714: flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) Laurent@714: Laurent@714: self.SetSizer(main_sizer) laurent@409: laurent@409: self.Table = ActionTable(self, [], GetActionTableColnames()) laurent@409: typelist = GetTypeList() laurent@409: self.TypeList = ",".join(map(_,typelist)) laurent@409: self.TranslateType = dict([(_(value), value) for value in typelist]) laurent@409: self.ColSizes = [60, 90, 80, 110, 80] laurent@409: self.ColAlignements = [wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT] laurent@409: laurent@409: self.ActionsGrid.SetTable(self.Table) laurent@577: self.ActionsGrid.SetDefaultValue({"Qualifier" : "N", laurent@577: "Duration" : "", laurent@577: "Type" : "Action", laurent@577: "Value" : "", laurent@577: "Indicator" : ""}) laurent@577: self.ActionsGrid.SetButtons({"Add": self.AddButton, laurent@577: "Delete": self.DeleteButton, laurent@577: "Up": self.UpButton, laurent@577: "Down": self.DownButton}) laurent@409: self.ActionsGrid.SetRowLabelSize(0) laurent@409: laurent@409: for col in range(self.Table.GetNumberCols()): laurent@409: attr = wx.grid.GridCellAttr() laurent@409: attr.SetAlignment(self.ColAlignements[col], wx.ALIGN_CENTRE) laurent@409: self.ActionsGrid.SetColAttr(col, attr) laurent@409: self.ActionsGrid.SetColMinimalWidth(col, self.ColSizes[col]) laurent@409: self.ActionsGrid.AutoSizeColumn(col, False) laurent@409: laurent@409: self.Table.ResetView(self.ActionsGrid) laurent@577: self.ActionsGrid.SetFocus() laurent@577: self.ActionsGrid.RefreshButtons() laurent@577: laurent@409: def OnOK(self, event): laurent@577: self.ActionsGrid.CloseEditControl() laurent@409: self.EndModal(wx.ID_OK) laurent@409: laurent@577: def OnActionsGridCellChange(self, event): laurent@577: wx.CallAfter(self.Table.ResetView, self.ActionsGrid) laurent@409: event.Skip() laurent@577: laurent@409: def SetQualifierList(self, list): laurent@409: self.QualifierList = "," + ",".join(list) laurent@409: self.DurationList = list laurent@409: laurent@409: def SetVariableList(self, list): laurent@409: self.VariableList = "," + ",".join([variable["Name"] for variable in list]) laurent@409: laurent@409: def SetActionList(self, list): laurent@409: self.ActionList = "," + ",".join(list) laurent@409: laurent@409: def SetValues(self, actions): laurent@409: for action in actions: laurent@409: row = {"Qualifier" : action["qualifier"], "Value" : action["value"]} laurent@409: if action["type"] == "reference": laurent@409: if action["value"] in self.ActionList: laurent@409: row["Type"] = "Action" laurent@409: elif action["value"] in self.VariableList: laurent@409: row["Type"] = "Variable" laurent@409: else: laurent@409: row["Type"] = "Inline" laurent@409: else: laurent@409: row["Type"] = "Inline" laurent@409: if "duration" in action: laurent@409: row["Duration"] = action["duration"] laurent@409: else: laurent@409: row["Duration"] = "" laurent@409: if "indicator" in action: laurent@409: row["Indicator"] = action["indicator"] laurent@409: else: laurent@409: row["Indicator"] = "" laurent@409: self.Table.AppendRow(row) laurent@409: self.Table.ResetView(self.ActionsGrid) laurent@577: if len(actions) > 0: laurent@577: self.ActionsGrid.SetGridCursor(0, 0) laurent@577: self.ActionsGrid.RefreshButtons() laurent@409: laurent@409: def GetValues(self): laurent@409: values = [] laurent@409: for data in self.Table.GetData(): laurent@409: action = {"qualifier" : data["Qualifier"], "value" : data["Value"]} laurent@409: if data["Type"] in ["Action", "Variable"]: laurent@409: action["type"] = "reference" laurent@409: else: laurent@409: action["type"] = "inline" laurent@409: if data["Duration"] != "": laurent@409: action["duration"] = data["Duration"] laurent@409: if data["Indicator"] != "": laurent@409: action["indicator"] = data["Indicator"] laurent@409: values.append(action) laurent@409: return values