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) 2007: 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.grid Laurent@814: import wx.lib.buttons Laurent@814: Laurent@814: from controls import CustomGrid, CustomTable Laurent@814: from util.BitmapLibrary import GetBitmap Laurent@814: Laurent@814: #------------------------------------------------------------------------------- Laurent@814: # Helpers Laurent@814: #------------------------------------------------------------------------------- Laurent@814: Laurent@814: def GetActionTableColnames(): Laurent@814: _ = lambda x: x Laurent@814: return [_("Qualifier"), _("Duration"), _("Type"), _("Value"), _("Indicator")] Laurent@814: Laurent@814: def GetTypeList(): Laurent@814: _ = lambda x: x Laurent@814: return [_("Action"), _("Variable"), _("Inline")] Laurent@814: Laurent@814: #------------------------------------------------------------------------------- Laurent@814: # Action Table Laurent@814: #------------------------------------------------------------------------------- Laurent@814: Laurent@814: class ActionTable(CustomTable): Laurent@814: Laurent@814: def GetValue(self, row, col): Laurent@814: if row < self.GetNumberRows(): Laurent@814: colname = self.GetColLabelValue(col, False) Laurent@814: name = str(self.data[row].get(colname, "")) Laurent@814: if colname == "Type": Laurent@814: return _(name) Laurent@814: return name Laurent@814: Laurent@814: def SetValue(self, row, col, value): Laurent@814: if col < len(self.colnames): Laurent@814: colname = self.GetColLabelValue(col, False) Laurent@814: if colname == "Type": Laurent@814: value = self.Parent.TranslateType[value] Laurent@814: self.data[row][colname] = value Laurent@814: Laurent@814: def _updateColAttrs(self, grid): Laurent@814: """ Laurent@814: wx.Grid -> update the column attributes to add the Laurent@814: appropriate renderer given the column name. Laurent@814: Laurent@814: Otherwise default to the default renderer. Laurent@814: """ Laurent@814: Laurent@814: for row in range(self.GetNumberRows()): Laurent@814: for col in range(self.GetNumberCols()): Laurent@814: editor = None Laurent@814: renderer = None Laurent@814: readonly = False Laurent@814: colname = self.GetColLabelValue(col, False) Laurent@814: if colname == "Qualifier": Laurent@814: editor = wx.grid.GridCellChoiceEditor() Laurent@814: editor.SetParameters(self.Parent.QualifierList) Laurent@814: if colname == "Duration": Laurent@814: editor = wx.grid.GridCellTextEditor() Laurent@814: renderer = wx.grid.GridCellStringRenderer() Laurent@814: if self.Parent.DurationList[self.data[row]["Qualifier"]]: Laurent@814: readonly = False Laurent@814: else: Laurent@814: readonly = True Laurent@814: self.data[row]["Duration"] = "" Laurent@814: elif colname == "Type": Laurent@814: editor = wx.grid.GridCellChoiceEditor() Laurent@814: editor.SetParameters(self.Parent.TypeList) Laurent@814: elif colname == "Value": Laurent@814: type = self.data[row]["Type"] Laurent@814: if type == "Action": Laurent@814: editor = wx.grid.GridCellChoiceEditor() Laurent@814: editor.SetParameters(self.Parent.ActionList) Laurent@814: elif type == "Variable": Laurent@814: editor = wx.grid.GridCellChoiceEditor() Laurent@814: editor.SetParameters(self.Parent.VariableList) Laurent@814: elif type == "Inline": Laurent@814: editor = wx.grid.GridCellTextEditor() Laurent@814: renderer = wx.grid.GridCellStringRenderer() Laurent@814: elif colname == "Indicator": Laurent@814: editor = wx.grid.GridCellChoiceEditor() Laurent@814: editor.SetParameters(self.Parent.VariableList) Laurent@814: Laurent@814: grid.SetCellEditor(row, col, editor) Laurent@814: grid.SetCellRenderer(row, col, renderer) Laurent@814: grid.SetReadOnly(row, col, readonly) Laurent@814: Laurent@814: grid.SetCellBackgroundColour(row, col, wx.WHITE) Laurent@814: self.ResizeRow(grid, row) Laurent@814: Laurent@814: #------------------------------------------------------------------------------- Laurent@814: # Action Block Dialog Laurent@814: #------------------------------------------------------------------------------- Laurent@814: Laurent@814: class ActionBlockDialog(wx.Dialog): Laurent@814: Laurent@814: def __init__(self, parent): Laurent@814: wx.Dialog.__init__(self, parent, Laurent@814: size=wx.Size(500, 300), title=_('Edit action block properties')) Laurent@814: Laurent@814: main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) Laurent@814: main_sizer.AddGrowableCol(0) Laurent@814: main_sizer.AddGrowableRow(1) Laurent@814: Laurent@814: top_sizer = wx.FlexGridSizer(cols=5, hgap=5, rows=1, vgap=0) Laurent@814: top_sizer.AddGrowableCol(0) Laurent@814: top_sizer.AddGrowableRow(0) Laurent@814: main_sizer.AddSizer(top_sizer, border=20, Laurent@814: flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) Laurent@814: Laurent@814: actions_label = wx.StaticText(self, label=_('Actions:')) Laurent@814: top_sizer.AddWindow(actions_label, flag=wx.ALIGN_BOTTOM) Laurent@814: Laurent@814: for name, bitmap, help in [ Laurent@814: ("AddButton", "add_element", _("Add action")), Laurent@814: ("DeleteButton", "remove_element", _("Remove action")), Laurent@814: ("UpButton", "up", _("Move action up")), Laurent@814: ("DownButton", "down", _("Move action down"))]: Laurent@814: button = wx.lib.buttons.GenBitmapButton(self, bitmap=GetBitmap(bitmap), Laurent@814: size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@814: button.SetToolTipString(help) Laurent@814: setattr(self, name, button) Laurent@814: top_sizer.AddWindow(button) Laurent@814: Laurent@814: self.ActionsGrid = CustomGrid(self, size=wx.Size(0, 0), style=wx.VSCROLL) Laurent@814: self.ActionsGrid.DisableDragGridSize() Laurent@814: self.ActionsGrid.EnableScrolling(False, True) Laurent@814: self.ActionsGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, Laurent@814: self.OnActionsGridCellChange) Laurent@814: main_sizer.AddSizer(self.ActionsGrid, border=20, Laurent@814: flag=wx.GROW|wx.LEFT|wx.RIGHT) Laurent@814: Laurent@814: button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) Laurent@814: self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton()) Laurent@814: main_sizer.AddSizer(button_sizer, border=20, Laurent@814: flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) Laurent@814: Laurent@814: self.SetSizer(main_sizer) Laurent@814: Laurent@814: self.Table = ActionTable(self, [], GetActionTableColnames()) Laurent@814: typelist = GetTypeList() Laurent@814: self.TypeList = ",".join(map(_,typelist)) Laurent@814: self.TranslateType = dict([(_(value), value) for value in typelist]) Laurent@814: self.ColSizes = [60, 90, 80, 110, 80] Laurent@814: self.ColAlignements = [wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT] Laurent@814: Laurent@814: self.ActionsGrid.SetTable(self.Table) Laurent@814: self.ActionsGrid.SetDefaultValue({"Qualifier" : "N", Laurent@814: "Duration" : "", Laurent@814: "Type" : "Action", Laurent@814: "Value" : "", Laurent@814: "Indicator" : ""}) Laurent@814: self.ActionsGrid.SetButtons({"Add": self.AddButton, Laurent@814: "Delete": self.DeleteButton, Laurent@814: "Up": self.UpButton, Laurent@814: "Down": self.DownButton}) Laurent@814: self.ActionsGrid.SetRowLabelSize(0) Laurent@814: Laurent@814: for col in range(self.Table.GetNumberCols()): Laurent@814: attr = wx.grid.GridCellAttr() Laurent@814: attr.SetAlignment(self.ColAlignements[col], wx.ALIGN_CENTRE) Laurent@814: self.ActionsGrid.SetColAttr(col, attr) Laurent@814: self.ActionsGrid.SetColMinimalWidth(col, self.ColSizes[col]) Laurent@814: self.ActionsGrid.AutoSizeColumn(col, False) Laurent@814: Laurent@814: self.Table.ResetView(self.ActionsGrid) Laurent@814: self.ActionsGrid.SetFocus() Laurent@814: self.ActionsGrid.RefreshButtons() Laurent@814: Laurent@814: def OnOK(self, event): Laurent@814: self.ActionsGrid.CloseEditControl() Laurent@814: self.EndModal(wx.ID_OK) Laurent@814: Laurent@814: def OnActionsGridCellChange(self, event): Laurent@814: wx.CallAfter(self.Table.ResetView, self.ActionsGrid) Laurent@814: event.Skip() Laurent@814: Laurent@814: def SetQualifierList(self, list): Laurent@814: self.QualifierList = "," + ",".join(list) Laurent@814: self.DurationList = list Laurent@814: Laurent@814: def SetVariableList(self, list): Laurent@814: self.VariableList = "," + ",".join([variable["Name"] for variable in list]) Laurent@814: Laurent@814: def SetActionList(self, list): Laurent@814: self.ActionList = "," + ",".join(list) Laurent@814: Laurent@814: def SetValues(self, actions): Laurent@814: for action in actions: Laurent@814: row = {"Qualifier" : action["qualifier"], "Value" : action["value"]} Laurent@814: if action["type"] == "reference": Laurent@814: if action["value"] in self.ActionList: Laurent@814: row["Type"] = "Action" Laurent@814: elif action["value"] in self.VariableList: Laurent@814: row["Type"] = "Variable" Laurent@814: else: Laurent@814: row["Type"] = "Inline" Laurent@814: else: Laurent@814: row["Type"] = "Inline" Laurent@814: if "duration" in action: Laurent@814: row["Duration"] = action["duration"] Laurent@814: else: Laurent@814: row["Duration"] = "" Laurent@814: if "indicator" in action: Laurent@814: row["Indicator"] = action["indicator"] Laurent@814: else: Laurent@814: row["Indicator"] = "" Laurent@814: self.Table.AppendRow(row) Laurent@814: self.Table.ResetView(self.ActionsGrid) Laurent@814: if len(actions) > 0: Laurent@814: self.ActionsGrid.SetGridCursor(0, 0) Laurent@814: self.ActionsGrid.RefreshButtons() Laurent@814: Laurent@814: def GetValues(self): Laurent@814: values = [] Laurent@814: for data in self.Table.GetData(): Laurent@814: action = {"qualifier" : data["Qualifier"], "value" : data["Value"]} Laurent@814: if data["Type"] in ["Action", "Variable"]: Laurent@814: action["type"] = "reference" Laurent@814: else: Laurent@814: action["type"] = "inline" Laurent@814: if data["Duration"] != "": Laurent@814: action["duration"] = data["Duration"] Laurent@814: if data["Indicator"] != "": Laurent@814: action["indicator"] = data["Indicator"] Laurent@814: values.append(action) Laurent@814: return values