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@409: laurent@604: from controls import CustomGrid, CustomTable laurent@577: laurent@409: #------------------------------------------------------------------------------- laurent@409: # Action Block Dialog 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@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@409: laurent@409: [ID_ACTIONBLOCKDIALOG, ID_ACTIONBLOCKDIALOGVARIABLESGRID, laurent@409: ID_ACTIONBLOCKDIALOGSTATICTEXT1, ID_ACTIONBLOCKDIALOGADDBUTTON, laurent@409: ID_ACTIONBLOCKDIALOGDELETEBUTTON, ID_ACTIONBLOCKDIALOGUPBUTTON, laurent@409: ID_ACTIONBLOCKDIALOGDOWNBUTTON, laurent@409: ] = [wx.NewId() for _init_ctrls in range(7)] laurent@409: laurent@409: class ActionBlockDialog(wx.Dialog): laurent@409: laurent@409: if wx.VERSION < (2, 6, 0): laurent@409: def Bind(self, event, function, id = None): laurent@409: if id is not None: laurent@409: event(self, id, function) laurent@409: else: laurent@409: event(self, function) laurent@409: laurent@409: def _init_coll_flexGridSizer1_Items(self, parent): laurent@409: parent.AddSizer(self.TopSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) laurent@409: parent.AddSizer(self.GridButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.LEFT|wx.RIGHT) laurent@409: parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) laurent@409: laurent@409: def _init_coll_flexGridSizer1_Growables(self, parent): laurent@409: parent.AddGrowableCol(0) laurent@409: parent.AddGrowableRow(0) laurent@409: laurent@409: def _init_coll_TopSizer_Items(self, parent): laurent@409: parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW) laurent@409: parent.AddWindow(self.ActionsGrid, 0, border=0, flag=wx.GROW) laurent@409: laurent@409: def _init_coll_TopSizer_Growables(self, parent): laurent@409: parent.AddGrowableCol(0) laurent@409: parent.AddGrowableRow(1) laurent@409: laurent@409: def _init_coll_GridButtonSizer_Items(self, parent): laurent@409: parent.AddWindow(self.AddButton, 0, border=10, flag=wx.GROW|wx.LEFT) laurent@409: parent.AddWindow(self.DeleteButton, 0, border=10, flag=wx.GROW|wx.LEFT) laurent@409: parent.AddWindow(self.UpButton, 0, border=10, flag=wx.GROW|wx.LEFT) laurent@409: parent.AddWindow(self.DownButton, 0, border=10, flag=wx.GROW|wx.LEFT) laurent@409: laurent@409: def _init_sizers(self): laurent@409: self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) laurent@409: self.TopSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5) laurent@409: self.GridButtonSizer = wx.BoxSizer(wx.HORIZONTAL) laurent@409: laurent@409: self._init_coll_flexGridSizer1_Items(self.flexGridSizer1) laurent@409: self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1) laurent@409: self._init_coll_TopSizer_Items(self.TopSizer) laurent@409: self._init_coll_TopSizer_Growables(self.TopSizer) laurent@409: self._init_coll_GridButtonSizer_Items(self.GridButtonSizer) laurent@409: laurent@409: self.SetSizer(self.flexGridSizer1) laurent@409: laurent@409: def _init_ctrls(self, prnt): laurent@409: wx.Dialog.__init__(self, id=ID_ACTIONBLOCKDIALOG, laurent@534: name='ActionBlockDialog', parent=prnt, laurent@409: size=wx.Size(500, 300), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER, laurent@409: title=_('Edit action block properties')) laurent@409: self.SetClientSize(wx.Size(500, 300)) laurent@409: laurent@409: self.staticText1 = wx.StaticText(id=ID_ACTIONBLOCKDIALOGSTATICTEXT1, laurent@409: label=_('Actions:'), name='staticText1', parent=self, laurent@409: pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) laurent@409: laurent@577: self.ActionsGrid = CustomGrid(id=ID_ACTIONBLOCKDIALOGVARIABLESGRID, laurent@409: name='ActionsGrid', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.Size(0, 0), style=wx.VSCROLL) laurent@409: self.ActionsGrid.DisableDragGridSize() laurent@409: self.ActionsGrid.EnableScrolling(False, True) laurent@577: if wx.VERSION >= (2, 6, 0): laurent@577: self.ActionsGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnActionsGridCellChange) laurent@577: else: laurent@577: wx.grid.EVT_GRID_CELL_CHANGE(self.ActionsGrid, self.OnActionsGridCellChange) laurent@577: laurent@409: self.AddButton = wx.Button(id=ID_ACTIONBLOCKDIALOGADDBUTTON, label=_('Add'), laurent@409: name='AddButton', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.DefaultSize, style=0) laurent@577: laurent@409: self.DeleteButton = wx.Button(id=ID_ACTIONBLOCKDIALOGDELETEBUTTON, label=_('Delete'), laurent@409: name='DeleteButton', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.DefaultSize, style=0) laurent@577: laurent@409: self.UpButton = wx.Button(id=ID_ACTIONBLOCKDIALOGUPBUTTON, label='^', laurent@409: name='UpButton', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.Size(32, 32), style=0) laurent@577: laurent@409: self.DownButton = wx.Button(id=ID_ACTIONBLOCKDIALOGDOWNBUTTON, label='v', laurent@409: name='DownButton', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.Size(32, 32), style=0) laurent@577: laurent@409: self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) laurent@409: if wx.VERSION >= (2, 5, 0): laurent@409: self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) laurent@409: else: laurent@409: self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) laurent@409: laurent@409: self._init_sizers() laurent@409: laurent@409: def __init__(self, parent): laurent@409: self._init_ctrls(parent) 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