Laurent@814: #!/usr/bin/env python 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: Laurent@814: from graphics import * Laurent@814: from controls.LibraryPanel import LibraryPanel Laurent@814: Laurent@814: #------------------------------------------------------------------------------- Laurent@814: # Create New Block Dialog Laurent@814: #------------------------------------------------------------------------------- Laurent@814: Laurent@814: class FBDBlockDialog(wx.Dialog): Laurent@814: Laurent@814: def __init__(self, parent, controller): Laurent@814: wx.Dialog.__init__(self, parent, Laurent@814: size=wx.Size(600, 450), title=_('Block Properties')) Laurent@814: Laurent@814: main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=4, vgap=10) Laurent@814: main_sizer.AddGrowableCol(0) Laurent@814: main_sizer.AddGrowableRow(0) Laurent@814: Laurent@814: column_sizer = wx.BoxSizer(wx.HORIZONTAL) Laurent@814: main_sizer.AddSizer(column_sizer, border=20, Laurent@814: flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) Laurent@814: Laurent@814: type_staticbox = wx.StaticBox(self, label=_('Type:')) Laurent@814: left_staticboxsizer = wx.StaticBoxSizer(type_staticbox, wx.VERTICAL) Laurent@814: column_sizer.AddSizer(left_staticboxsizer, 1, border=5, Laurent@814: flag=wx.GROW|wx.RIGHT) Laurent@814: Laurent@814: self.LibraryPanel = LibraryPanel(self) Laurent@814: setattr(self.LibraryPanel, "_OnTreeItemSelected", Laurent@814: self.OnLibraryTreeItemSelected) Laurent@814: left_staticboxsizer.AddWindow(self.LibraryPanel, 1, border=5, Laurent@814: flag=wx.GROW|wx.TOP) Laurent@814: Laurent@814: right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=5) Laurent@814: right_gridsizer.AddGrowableCol(0) Laurent@814: right_gridsizer.AddGrowableRow(2) Laurent@814: column_sizer.AddSizer(right_gridsizer, 1, border=5, Laurent@814: flag=wx.GROW|wx.LEFT) Laurent@814: Laurent@814: top_right_gridsizer = wx.FlexGridSizer(cols=2, hgap=0, rows=4, vgap=5) Laurent@814: top_right_gridsizer.AddGrowableCol(1) Laurent@814: right_gridsizer.AddSizer(top_right_gridsizer, flag=wx.GROW) Laurent@814: Laurent@814: name_label = wx.StaticText(self, label=_('Name:')) Laurent@814: top_right_gridsizer.AddWindow(name_label, Laurent@814: flag=wx.ALIGN_CENTER_VERTICAL) Laurent@814: Laurent@814: self.BlockName = wx.TextCtrl(self) Laurent@814: self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.BlockName) Laurent@814: top_right_gridsizer.AddWindow(self.BlockName, flag=wx.GROW) Laurent@814: Laurent@814: inputs_label = wx.StaticText(self, label=_('Inputs:')) Laurent@814: top_right_gridsizer.AddWindow(inputs_label, Laurent@814: flag=wx.ALIGN_CENTER_VERTICAL) Laurent@814: Laurent@814: self.Inputs = wx.SpinCtrl(self, min=2, max=20, Laurent@814: style=wx.SP_ARROW_KEYS) Laurent@814: self.Bind(wx.EVT_SPINCTRL, self.OnInputsChanged, self.Inputs) Laurent@814: top_right_gridsizer.AddWindow(self.Inputs, flag=wx.GROW) Laurent@814: Laurent@814: execution_order_label = wx.StaticText(self, label=_('Execution Order:')) Laurent@814: top_right_gridsizer.AddWindow(execution_order_label, Laurent@814: flag=wx.ALIGN_CENTER_VERTICAL) Laurent@814: Laurent@814: self.ExecutionOrder = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS) Laurent@814: self.Bind(wx.EVT_SPINCTRL, self.OnExecutionOrderChanged, self.ExecutionOrder) Laurent@814: top_right_gridsizer.AddWindow(self.ExecutionOrder, flag=wx.GROW) Laurent@814: Laurent@814: execution_control_label = wx.StaticText(self, label=_('Execution Control:')) Laurent@814: top_right_gridsizer.AddWindow(execution_control_label, Laurent@814: flag=wx.ALIGN_CENTER_VERTICAL) Laurent@814: Laurent@814: self.ExecutionControl = wx.CheckBox(self) Laurent@814: self.Bind(wx.EVT_CHECKBOX, self.OnExecutionOrderChanged, self.ExecutionControl) Laurent@814: top_right_gridsizer.AddWindow(self.ExecutionControl, flag=wx.GROW) Laurent@814: Laurent@814: preview_label = wx.StaticText(self, label=_('Preview:')) Laurent@814: right_gridsizer.AddWindow(preview_label, flag=wx.GROW) Laurent@814: Laurent@814: self.Preview = wx.Panel(self, Laurent@814: style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER) Laurent@814: self.Preview.SetBackgroundColour(wx.Colour(255,255,255)) Laurent@814: setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE) Laurent@814: setattr(self.Preview, "GetScaling", lambda:None) Laurent@814: setattr(self.Preview, "GetBlockType", controller.GetBlockType) Laurent@814: setattr(self.Preview, "IsOfType", controller.IsOfType) Laurent@814: self.Preview.Bind(wx.EVT_PAINT, self.OnPaint) Laurent@814: right_gridsizer.AddWindow(self.Preview, flag=wx.GROW) 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.Controller = controller Laurent@814: Laurent@814: self.BlockName.SetValue("") Laurent@814: self.BlockName.Enable(False) Laurent@814: self.Inputs.Enable(False) Laurent@814: self.Block = None Laurent@814: self.MinBlockSize = None Laurent@814: self.First = True Laurent@814: Laurent@814: self.PouNames = [] Laurent@814: self.PouElementNames = [] Laurent@814: Laurent@814: self.LibraryPanel.SetFocus() Laurent@814: Laurent@814: def __del__(self): Laurent@814: self.Controller = None Laurent@814: Laurent@814: def SetBlockList(self, blocklist): Laurent@814: self.LibraryPanel.SetBlockList(blocklist) Laurent@814: Laurent@814: def SetPreviewFont(self, font): Laurent@814: self.Preview.SetFont(font) Laurent@814: Laurent@814: def OnOK(self, event): Laurent@814: message = None Laurent@814: selected = self.LibraryPanel.GetSelectedBlock() Laurent@814: block_name = self.BlockName.GetValue() Laurent@814: name_enabled = self.BlockName.IsEnabled() Laurent@814: if selected is None: Laurent@814: message = _("Form isn't complete. Valid block type must be selected!") Laurent@814: elif name_enabled and block_name == "": Laurent@814: message = _("Form isn't complete. Name must be filled!") Laurent@814: elif name_enabled and not TestIdentifier(block_name): Laurent@814: message = _("\"%s\" is not a valid identifier!") % block_name Laurent@814: elif name_enabled and block_name.upper() in IEC_KEYWORDS: Laurent@814: message = _("\"%s\" is a keyword. It can't be used!") % block_name Laurent@814: elif name_enabled and block_name.upper() in self.PouNames: Laurent@814: message = _("\"%s\" pou already exists!") % block_name Laurent@814: elif name_enabled and block_name.upper() in self.PouElementNames: Laurent@814: message = _("\"%s\" element for this pou already exists!") % block_name Laurent@814: if message is not None: Laurent@814: dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) Laurent@814: dialog.ShowModal() Laurent@814: dialog.Destroy() Laurent@814: else: Laurent@814: self.EndModal(wx.ID_OK) Laurent@814: Laurent@814: def SetMinBlockSize(self, size): Laurent@814: self.MinBlockSize = size Laurent@814: Laurent@814: def SetPouNames(self, pou_names): Laurent@814: self.PouNames = [pou_name.upper() for pou_name in pou_names] Laurent@814: Laurent@814: def SetPouElementNames(self, element_names): Laurent@814: self.PouElementNames = [element_name.upper() for element_name in element_names] Laurent@814: Laurent@814: def SetValues(self, values): Laurent@814: blocktype = values.get("type", None) Laurent@814: if blocktype is not None: Laurent@814: self.LibraryPanel.SelectTreeItem(blocktype, values.get("inputs", None)) Laurent@814: for name, value in values.items(): Laurent@814: if name == "name": Laurent@814: self.BlockName.SetValue(value) Laurent@814: elif name == "extension": Laurent@814: self.Inputs.SetValue(value) Laurent@814: elif name == "executionOrder": Laurent@814: self.ExecutionOrder.SetValue(value) Laurent@814: elif name == "executionControl": Laurent@814: self.ExecutionControl.SetValue(value) Laurent@814: self.RefreshPreview() Laurent@814: Laurent@814: def GetValues(self): Laurent@814: values = self.LibraryPanel.GetSelectedBlock() Laurent@814: if self.BlockName.IsEnabled() and self.BlockName.GetValue() != "": Laurent@814: values["name"] = self.BlockName.GetValue() Laurent@814: values["width"], values["height"] = self.Block.GetSize() Laurent@814: values["extension"] = self.Inputs.GetValue() Laurent@814: values["executionOrder"] = self.ExecutionOrder.GetValue() Laurent@814: values["executionControl"] = self.ExecutionControl.GetValue() Laurent@814: return values Laurent@814: Laurent@814: def OnLibraryTreeItemSelected(self, event): Laurent@814: values = self.LibraryPanel.GetSelectedBlock() Laurent@814: if values is not None: Laurent@814: blocktype = self.Controller.GetBlockType(values["type"], values["inputs"]) Laurent@814: else: Laurent@814: blocktype = None Laurent@814: if blocktype is not None: Laurent@814: self.Inputs.SetValue(len(blocktype["inputs"])) Laurent@814: self.Inputs.Enable(blocktype["extensible"]) Laurent@814: self.BlockName.Enable(blocktype["type"] != "function") Laurent@814: wx.CallAfter(self.RefreshPreview) Laurent@814: else: Laurent@814: self.BlockName.Enable(False) Laurent@814: self.Inputs.Enable(False) Laurent@814: self.Inputs.SetValue(2) Laurent@814: wx.CallAfter(self.ErasePreview) Laurent@814: Laurent@814: def OnNameChanged(self, event): Laurent@814: if self.BlockName.IsEnabled(): Laurent@814: self.RefreshPreview() Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnInputsChanged(self, event): Laurent@814: if self.Inputs.IsEnabled(): Laurent@814: self.RefreshPreview() Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnExecutionOrderChanged(self, event): Laurent@814: self.RefreshPreview() Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnExecutionControlChanged(self, event): Laurent@814: self.RefreshPreview() Laurent@814: event.Skip() Laurent@814: Laurent@814: def ErasePreview(self): Laurent@814: dc = wx.ClientDC(self.Preview) Laurent@814: dc.Clear() Laurent@814: self.Block = None Laurent@814: Laurent@814: def RefreshPreview(self): Laurent@814: dc = wx.ClientDC(self.Preview) Laurent@814: dc.SetFont(self.Preview.GetFont()) Laurent@814: dc.Clear() Laurent@814: values = self.LibraryPanel.GetSelectedBlock() Laurent@814: if values is not None: Laurent@814: if self.BlockName.IsEnabled(): Laurent@814: blockname = self.BlockName.GetValue() Laurent@814: else: Laurent@814: blockname = "" Laurent@814: self.Block = FBD_Block(self.Preview, values["type"], Laurent@814: blockname, Laurent@814: extension = self.Inputs.GetValue(), Laurent@814: inputs = values["inputs"], Laurent@814: executionControl = self.ExecutionControl.GetValue(), Laurent@814: executionOrder = self.ExecutionOrder.GetValue()) Laurent@814: width, height = self.MinBlockSize Laurent@814: min_width, min_height = self.Block.GetMinSize() Laurent@814: width, height = max(min_width, width), max(min_height, height) Laurent@814: self.Block.SetSize(width, height) Laurent@814: clientsize = self.Preview.GetClientSize() Laurent@814: x = (clientsize.width - width) / 2 Laurent@814: y = (clientsize.height - height) / 2 Laurent@814: self.Block.SetPosition(x, y) Laurent@814: self.Block.Draw(dc) Laurent@814: else: Laurent@814: self.Block = None Laurent@814: Laurent@814: def OnPaint(self, event): Laurent@814: if self.Block is not None: Laurent@814: self.RefreshPreview() Laurent@814: event.Skip()