laurent@409: #!/usr/bin/env python 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: laurent@409: from graphics import * laurent@409: laurent@409: #------------------------------------------------------------------------------- laurent@409: # Create New Block Dialog laurent@409: #------------------------------------------------------------------------------- laurent@409: laurent@409: [ID_FBDBLOCKDIALOG, ID_FBDBLOCKDIALOGNAME, laurent@409: ID_FBDBLOCKDIALOGTYPETREE, ID_FBDBLOCKDIALOGTYPEDESC, laurent@409: ID_FBDBLOCKDIALOGINPUTS, ID_FBDBLOCKDIALOGPREVIEW, laurent@409: ID_FBDBLOCKDIALOGEXECUTIONORDER, ID_FBDBLOCKDIALOGEXECUTIONCONTROL, laurent@409: ID_FBDBLOCKDIALOGSTATICTEXT1, ID_FBDBLOCKDIALOGSTATICTEXT2, laurent@409: ID_FBDBLOCKDIALOGSTATICTEXT3, ID_FBDBLOCKDIALOGSTATICTEXT4, laurent@409: ID_FBDBLOCKDIALOGSTATICTEXT5, ID_FBDBLOCKDIALOGSTATICTEXT6, laurent@409: ] = [wx.NewId() for _init_ctrls in range(14)] laurent@409: laurent@409: [CATEGORY, BLOCK] = range(2) laurent@409: laurent@409: class FBDBlockDialog(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.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|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_MainSizer_Items(self, parent): laurent@409: parent.AddSizer(self.LeftBoxSizer, 1, border=5, flag=wx.GROW|wx.RIGHT) laurent@409: parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT) laurent@409: laurent@409: def _init_coll_LeftBoxSizer_Items(self, parent): laurent@409: parent.AddWindow(self.TypeTree, 3, border=5, flag=wx.GROW|wx.BOTTOM) laurent@409: parent.AddWindow(self.TypeDesc, 1, border=0, flag=wx.GROW) laurent@409: laurent@409: def _init_coll_RightGridSizer_Items(self, parent): laurent@409: parent.AddSizer(self.RightUpGridSizer, 0, border=0, flag=wx.GROW) laurent@409: parent.AddWindow(self.staticText6, 0, border=0, flag=wx.GROW) laurent@409: parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW) laurent@409: laurent@409: def _init_coll_RightGridSizer_Growables(self, parent): laurent@409: parent.AddGrowableCol(0) laurent@409: parent.AddGrowableRow(2) laurent@409: laurent@409: def _init_coll_RightUpGridSizer_Items(self, parent): laurent@409: parent.AddWindow(self.staticText2, 0, border=4, flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) laurent@409: parent.AddWindow(self.BlockName, 0, border=0, flag=wx.GROW) laurent@409: parent.AddWindow(self.staticText3, 0, border=4, flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) laurent@409: parent.AddWindow(self.Inputs, 0, border=0, flag=wx.GROW) laurent@409: parent.AddWindow(self.staticText4, 0, border=4, flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) laurent@409: parent.AddWindow(self.ExecutionOrder, 0, border=0, flag=wx.GROW) laurent@409: parent.AddWindow(self.staticText5, 0, border=4, flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) laurent@409: parent.AddWindow(self.ExecutionControl, 0, border=0, flag=wx.GROW) laurent@409: laurent@409: def _init_coll_RightUpGridSizer_Growables(self, parent): laurent@409: parent.AddGrowableCol(1) laurent@409: laurent@409: def _init_sizers(self): laurent@409: self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) laurent@409: self.MainSizer = wx.BoxSizer(wx.HORIZONTAL) laurent@409: self.LeftBoxSizer = wx.StaticBoxSizer(self.staticbox1, wx.VERTICAL) laurent@409: self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=5) laurent@409: self.RightUpGridSizer = wx.FlexGridSizer(cols=2, hgap=5, rows=4, vgap=5) 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_MainSizer_Items(self.MainSizer) laurent@409: self._init_coll_LeftBoxSizer_Items(self.LeftBoxSizer) laurent@409: self._init_coll_RightGridSizer_Items(self.RightGridSizer) laurent@409: self._init_coll_RightGridSizer_Growables(self.RightGridSizer) laurent@409: self._init_coll_RightUpGridSizer_Items(self.RightUpGridSizer) laurent@409: self._init_coll_RightUpGridSizer_Growables(self.RightUpGridSizer) 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_FBDBLOCKDIALOG, laurent@534: name='FBDBlockDialog', parent=prnt, laurent@409: size=wx.Size(600, 400), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER, laurent@409: title=_('Block Properties')) laurent@409: self.SetClientSize(wx.Size(600, 400)) laurent@409: laurent@409: self.staticbox1 = wx.StaticBox(id=ID_FBDBLOCKDIALOGSTATICTEXT1, laurent@409: label=_('Type:'), name='staticBox1', parent=self, laurent@409: pos=wx.Point(0, 0), size=wx.Size(0, 0), style=0) laurent@409: laurent@409: self.staticText2 = wx.StaticText(id=ID_FBDBLOCKDIALOGSTATICTEXT2, laurent@409: label=_('Name:'), name='staticText2', parent=self, laurent@409: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) laurent@409: laurent@409: self.staticText3 = wx.StaticText(id=ID_FBDBLOCKDIALOGSTATICTEXT2, laurent@409: label=_('Inputs:'), name='staticText4', parent=self, laurent@409: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) laurent@409: laurent@409: self.staticText4 = wx.StaticText(id=ID_FBDBLOCKDIALOGSTATICTEXT4, laurent@409: label=_('Execution Order:'), name='staticText4', parent=self, laurent@409: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) laurent@409: laurent@409: self.staticText5 = wx.StaticText(id=ID_FBDBLOCKDIALOGSTATICTEXT5, laurent@409: label=_('Execution Control:'), name='staticText5', parent=self, laurent@409: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) laurent@409: laurent@409: self.staticText6 = wx.StaticText(id=ID_FBDBLOCKDIALOGSTATICTEXT6, laurent@409: label=_('Preview:'), name='staticText6', parent=self, laurent@409: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) laurent@409: laurent@409: self.TypeTree = wx.TreeCtrl(id=ID_FBDBLOCKDIALOGTYPETREE, laurent@409: name='TypeTree', parent=self, pos=wx.Point(0, 0), laurent@596: size=wx.Size(0, 0), style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER|wx.TR_HIDE_ROOT|wx.TR_LINES_AT_ROOT) laurent@409: self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTypeTreeItemSelected, laurent@409: id=ID_FBDBLOCKDIALOGTYPETREE) laurent@409: laurent@409: self.TypeDesc = wx.TextCtrl(id=ID_FBDBLOCKDIALOGTYPEDESC, laurent@409: name='TypeDesc', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.Size(0, 0), style=wx.TE_READONLY|wx.TE_MULTILINE) laurent@409: laurent@409: self.BlockName = wx.TextCtrl(id=ID_FBDBLOCKDIALOGNAME, value='', laurent@409: name='BlockName', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.Size(0, 24), style=0) laurent@409: self.Bind(wx.EVT_TEXT, self.OnNameChanged, id=ID_FBDBLOCKDIALOGNAME) laurent@409: laurent@409: self.Inputs = wx.SpinCtrl(id=ID_FBDBLOCKDIALOGINPUTS, laurent@409: name='Inputs', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.Size(0, 24), style=wx.SP_ARROW_KEYS, min=2, max=20) laurent@409: self.Bind(wx.EVT_SPINCTRL, self.OnInputsChanged, id=ID_FBDBLOCKDIALOGINPUTS) laurent@409: laurent@409: self.ExecutionOrder = wx.SpinCtrl(id=ID_FBDBLOCKDIALOGEXECUTIONORDER, laurent@409: name='ExecutionOrder', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.Size(0, 24), style=wx.SP_ARROW_KEYS, min=0) laurent@409: self.Bind(wx.EVT_SPINCTRL, self.OnExecutionOrderChanged, id=ID_FBDBLOCKDIALOGEXECUTIONORDER) laurent@409: laurent@409: self.ExecutionControl = wx.CheckBox(id=ID_FBDBLOCKDIALOGEXECUTIONCONTROL, laurent@409: name='ExecutionControl', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.Size(0, 24), style=0) laurent@409: self.Bind(wx.EVT_CHECKBOX, self.OnExecutionOrderChanged, id=ID_FBDBLOCKDIALOGEXECUTIONCONTROL) laurent@409: laurent@409: self.Preview = wx.Panel(id=ID_FBDBLOCKDIALOGPREVIEW, laurent@409: name='Preview', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER) laurent@409: self.Preview.SetBackgroundColour(wx.Colour(255,255,255)) laurent@409: setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE) laurent@409: setattr(self.Preview, "GetScaling", lambda:None) laurent@409: setattr(self.Preview, "GetBlockType", self.Controler.GetBlockType) laurent@409: setattr(self.Preview, "IsOfType", self.Controler.IsOfType) laurent@409: 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: self.Preview.Bind(wx.EVT_PAINT, self.OnPaint) laurent@409: else: laurent@409: self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) laurent@409: wx.EVT_PAINT(self.Preview, self.OnPaint) laurent@409: laurent@409: self._init_sizers() laurent@409: laurent@409: def __init__(self, parent, controler): laurent@409: self.Controler = controler laurent@409: self._init_ctrls(parent) laurent@409: self.BlockName.SetValue("") laurent@409: self.BlockName.Enable(False) laurent@409: self.Inputs.Enable(False) laurent@409: self.Block = None laurent@409: self.MinBlockSize = None laurent@409: self.First = True laurent@409: laurent@409: self.PouNames = [] laurent@409: self.PouElementNames = [] laurent@409: laurent@577: self.TypeTree.SetFocus() laurent@577: laurent@409: def SetPreviewFont(self, font): laurent@409: self.Preview.SetFont(font) laurent@409: laurent@409: def FindTreeItem(self, root, name, inputs = None): laurent@409: if root.IsOk(): laurent@409: pydata = self.TypeTree.GetPyData(root) laurent@596: if pydata is not None: laurent@596: type_inputs = pydata.get("inputs", None) laurent@596: type_extension = pydata.get("extension", None) laurent@596: if inputs is not None and type_inputs is not None: laurent@596: if type_extension is not None: laurent@596: same_inputs = type_inputs == inputs[:type_extension] laurent@596: else: laurent@596: same_inputs = type_inputs == inputs laurent@409: else: laurent@596: same_inputs = True laurent@596: if pydata is not None and self.TypeTree.GetItemText(root) == name and same_inputs: laurent@409: return root laurent@409: else: laurent@409: if wx.VERSION < (2, 6, 0): laurent@409: item, root_cookie = self.TypeTree.GetFirstChild(root, 0) laurent@409: else: laurent@409: item, root_cookie = self.TypeTree.GetFirstChild(root) laurent@409: while item.IsOk(): laurent@409: result = self.FindTreeItem(item, name, inputs) laurent@409: if result: laurent@409: return result laurent@409: item, root_cookie = self.TypeTree.GetNextChild(root, root_cookie) laurent@409: return None laurent@409: laurent@409: def OnOK(self, event): laurent@409: selected = self.TypeTree.GetSelection() laurent@409: block_name = self.BlockName.GetValue() laurent@409: name_enabled = self.BlockName.IsEnabled() laurent@409: if not selected.IsOk() or self.TypeTree.GetItemParent(selected) == self.TypeTree.GetRootItem() or selected == self.TypeTree.GetRootItem(): laurent@409: message = wx.MessageDialog(self, _("Form isn't complete. Valid block type must be selected!"), _("Error"), wx.OK|wx.ICON_ERROR) laurent@409: message.ShowModal() laurent@409: message.Destroy() laurent@409: elif name_enabled and block_name == "": laurent@409: message = wx.MessageDialog(self, _("Form isn't complete. Name must be filled!"), _("Error"), wx.OK|wx.ICON_ERROR) laurent@409: message.ShowModal() laurent@409: message.Destroy() laurent@409: elif name_enabled and not TestIdentifier(block_name): laurent@409: message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%block_name, _("Error"), wx.OK|wx.ICON_ERROR) laurent@409: message.ShowModal() laurent@409: message.Destroy() laurent@409: elif name_enabled and block_name.upper() in IEC_KEYWORDS: laurent@409: message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%block_name, _("Error"), wx.OK|wx.ICON_ERROR) laurent@409: message.ShowModal() laurent@409: message.Destroy() laurent@409: elif name_enabled and block_name.upper() in self.PouNames: laurent@409: message = wx.MessageDialog(self, _("\"%s\" pou already exists!")%block_name, _("Error"), wx.OK|wx.ICON_ERROR) laurent@409: message.ShowModal() laurent@409: message.Destroy() laurent@409: elif name_enabled and block_name.upper() in self.PouElementNames: laurent@409: message = wx.MessageDialog(self, _("\"%s\" element for this pou already exists!")%block_name, _("Error"), wx.OK|wx.ICON_ERROR) laurent@409: message.ShowModal() laurent@409: message.Destroy() laurent@409: else: laurent@409: self.EndModal(wx.ID_OK) laurent@409: laurent@409: def SetBlockList(self, blocktypes): laurent@596: root = self.TypeTree.AddRoot("") laurent@409: for category in blocktypes: laurent@409: category_name = category["name"] laurent@409: category_item = self.TypeTree.AppendItem(root, _(category_name)) laurent@409: self.TypeTree.SetPyData(category_item, {"type" : CATEGORY}) laurent@409: for blocktype in category["list"]: laurent@409: blocktype_item = self.TypeTree.AppendItem(category_item, blocktype["name"]) laurent@409: block_data = {"type" : BLOCK, laurent@409: "inputs" : tuple([type for name, type, modifier in blocktype["inputs"]]), laurent@409: "extension" : None} laurent@409: if blocktype["extensible"]: laurent@409: block_data["extension"] = len(blocktype["inputs"]) laurent@409: self.TypeTree.SetPyData(blocktype_item, block_data) laurent@596: laurent@409: def SetMinBlockSize(self, size): laurent@409: self.MinBlockSize = size laurent@409: laurent@409: def SetPouNames(self, pou_names): laurent@409: self.PouNames = [pou_name.upper() for pou_name in pou_names] laurent@409: laurent@409: def SetPouElementNames(self, element_names): laurent@409: self.PouElementNames = [element_name.upper() for element_name in element_names] laurent@409: laurent@409: def SetValues(self, values): laurent@409: blocktype = values.get("type", None) laurent@409: if blocktype is not None: laurent@409: inputs = values.get("inputs", None) laurent@409: item = self.FindTreeItem(self.TypeTree.GetRootItem(), blocktype, inputs) laurent@409: if item: laurent@409: self.TypeTree.SelectItem(item) laurent@596: self.TypeTree.EnsureVisible(item) laurent@409: for name, value in values.items(): laurent@409: if name == "name": laurent@409: self.BlockName.SetValue(value) laurent@409: elif name == "extension": laurent@409: self.Inputs.SetValue(value) laurent@409: elif name == "executionOrder": laurent@409: self.ExecutionOrder.SetValue(value) laurent@409: elif name == "executionControl": laurent@409: self.ExecutionControl.SetValue(value) laurent@409: self.RefreshPreview() laurent@409: laurent@409: def GetValues(self): laurent@409: values = {} laurent@409: item = self.TypeTree.GetSelection() laurent@409: values["type"] = self.TypeTree.GetItemText(item) laurent@409: values["inputs"] = self.TypeTree.GetPyData(item)["inputs"] laurent@409: if self.BlockName.GetValue() != "": laurent@409: values["name"] = self.BlockName.GetValue() laurent@409: values["width"], values["height"] = self.Block.GetSize() laurent@409: values["extension"] = self.Inputs.GetValue() laurent@409: values["executionOrder"] = self.ExecutionOrder.GetValue() laurent@409: values["executionControl"] = self.ExecutionControl.GetValue() laurent@409: return values laurent@409: laurent@409: def OnTypeTreeItemSelected(self, event): laurent@409: self.BlockName.SetValue("") laurent@409: selected = event.GetItem() laurent@409: pydata = self.TypeTree.GetPyData(selected) laurent@409: if pydata["type"] != CATEGORY: laurent@409: blocktype = self.Controler.GetBlockType(self.TypeTree.GetItemText(selected), pydata["inputs"]) laurent@409: if blocktype: laurent@409: self.Inputs.SetValue(len(blocktype["inputs"])) laurent@409: self.Inputs.Enable(blocktype["extensible"]) laurent@409: self.BlockName.Enable(blocktype["type"] != "function") laurent@409: comment = blocktype["comment"] laurent@409: self.TypeDesc.SetValue(_(comment) + blocktype.get("usage", "")) laurent@409: wx.CallAfter(self.RefreshPreview) laurent@409: else: laurent@409: self.BlockName.Enable(False) laurent@409: self.Inputs.Enable(False) laurent@409: self.Inputs.SetValue(2) laurent@409: self.TypeDesc.SetValue("") laurent@409: wx.CallAfter(self.ErasePreview) laurent@409: else: laurent@409: self.BlockName.Enable(False) laurent@409: self.Inputs.Enable(False) laurent@409: self.Inputs.SetValue(2) laurent@409: self.TypeDesc.SetValue("") laurent@409: wx.CallAfter(self.ErasePreview) laurent@409: event.Skip() laurent@409: laurent@409: def OnNameChanged(self, event): laurent@409: if self.BlockName.IsEnabled(): laurent@409: self.RefreshPreview() laurent@409: event.Skip() laurent@409: laurent@409: def OnInputsChanged(self, event): laurent@409: if self.Inputs.IsEnabled(): laurent@409: self.RefreshPreview() laurent@409: event.Skip() laurent@409: laurent@409: def OnExecutionOrderChanged(self, event): laurent@409: self.RefreshPreview() laurent@409: event.Skip() laurent@409: laurent@409: def OnExecutionControlChanged(self, event): laurent@409: self.RefreshPreview() laurent@409: event.Skip() laurent@409: laurent@409: def ErasePreview(self): laurent@409: dc = wx.ClientDC(self.Preview) laurent@409: dc.Clear() laurent@409: self.Block = None laurent@409: laurent@409: def RefreshPreview(self): laurent@409: dc = wx.ClientDC(self.Preview) laurent@409: dc.SetFont(self.Preview.GetFont()) laurent@409: dc.Clear() laurent@409: item = self.TypeTree.GetSelection() laurent@409: if item.IsOk(): laurent@409: pydata = self.TypeTree.GetPyData(item) laurent@409: if pydata["type"] == CATEGORY: laurent@409: self.Block = None laurent@409: else: laurent@409: blocktype = self.TypeTree.GetItemText(item) laurent@409: if blocktype: laurent@409: self.Block = FBD_Block(self.Preview, blocktype, laurent@409: self.BlockName.GetValue(), laurent@409: extension = self.Inputs.GetValue(), laurent@409: inputs = pydata["inputs"], laurent@409: executionControl = self.ExecutionControl.GetValue(), laurent@409: executionOrder = self.ExecutionOrder.GetValue()) laurent@409: width, height = self.MinBlockSize laurent@409: min_width, min_height = self.Block.GetMinSize() laurent@409: width, height = max(min_width, width), max(min_height, height) laurent@409: self.Block.SetSize(width, height) laurent@409: clientsize = self.Preview.GetClientSize() laurent@409: x = (clientsize.width - width) / 2 laurent@409: y = (clientsize.height - height) / 2 laurent@409: self.Block.SetPosition(x, y) laurent@409: self.Block.Draw(dc) laurent@409: else: laurent@409: self.Block = None laurent@409: laurent@409: def OnPaint(self, event): laurent@409: if self.Block is not None: laurent@409: self.RefreshPreview() laurent@409: event.Skip()