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: Laurent@814: #------------------------------------------------------------------------------- Laurent@814: # Edit Step Content Dialog Laurent@814: #------------------------------------------------------------------------------- Laurent@814: Laurent@814: class SFCStepDialog(wx.Dialog): Laurent@814: Laurent@814: def __init__(self, parent, controller, initial = False): Laurent@814: wx.Dialog.__init__(self, parent, title=_('Edit Step'), Laurent@814: size=wx.Size(400, 250)) Laurent@814: Laurent@814: main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, 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: left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=6, vgap=5) Laurent@814: left_gridsizer.AddGrowableCol(0) Laurent@814: column_sizer.AddSizer(left_gridsizer, 1, border=5, Laurent@814: flag=wx.GROW|wx.RIGHT) Laurent@814: Laurent@814: name_label = wx.StaticText(self, label=_('Name:')) Laurent@814: left_gridsizer.AddWindow(name_label, flag=wx.GROW) Laurent@814: Laurent@814: self.StepName = wx.TextCtrl(self) Laurent@814: self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.StepName) Laurent@814: left_gridsizer.AddWindow(self.StepName, flag=wx.GROW) Laurent@814: Laurent@814: connectors_label = wx.StaticText(self, label=_('Connectors:')) Laurent@814: left_gridsizer.AddWindow(connectors_label, flag=wx.GROW) Laurent@814: Laurent@814: self.Input = wx.CheckBox(self, label=_("Input")) Laurent@814: self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, self.Input) Laurent@814: left_gridsizer.AddWindow(self.Input, flag=wx.GROW) Laurent@814: Laurent@814: self.Output = wx.CheckBox(self, label=_("Output")) Laurent@814: self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, self.Output) Laurent@814: left_gridsizer.AddWindow(self.Output, flag=wx.GROW) Laurent@814: Laurent@814: self.Action = wx.CheckBox(self, label=_("Action")) Laurent@814: self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, self.Action) Laurent@814: left_gridsizer.AddWindow(self.Action, flag=wx.GROW) Laurent@814: Laurent@814: right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5) Laurent@814: right_gridsizer.AddGrowableCol(0) Laurent@814: right_gridsizer.AddGrowableRow(1) Laurent@814: column_sizer.AddSizer(right_gridsizer, 1, border=5, Laurent@814: flag=wx.GROW|wx.LEFT) 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, "RefreshStepModel", lambda x:None) Laurent@814: setattr(self.Preview, "GetScaling", lambda:None) 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( Laurent@814: wx.OK|wx.CANCEL|wx.CENTRE) Laurent@814: self.Bind(wx.EVT_BUTTON, self.OnOK, Laurent@814: 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.Step = None Laurent@814: self.Initial = initial Laurent@814: self.MinStepSize = None Laurent@814: Laurent@814: self.PouNames = [] Laurent@814: self.Variables = [] Laurent@814: self.StepNames = [] Laurent@814: Laurent@814: self.StepName.SetFocus() 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: step_name = self.StepName.GetValue() Laurent@814: if step_name == "": Laurent@814: message = _("You must type a name!") Laurent@814: elif not TestIdentifier(step_name): Laurent@814: message = _("\"%s\" is not a valid identifier!") % step_name Laurent@814: elif step_name.upper() in IEC_KEYWORDS: Laurent@814: message = _("\"%s\" is a keyword. It can't be used!") % step_name Laurent@814: elif step_name.upper() in self.PouNames: Laurent@814: message = _("A POU named \"%s\" already exists!") % step_name Laurent@814: elif step_name.upper() in self.Variables: Laurent@814: message = _("A variable with \"%s\" as name already exists in this pou!") % step_name Laurent@814: elif step_name.upper() in self.StepNames: Laurent@814: message = _("\"%s\" step already exists!") % step_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 SetMinStepSize(self, size): Laurent@814: self.MinStepSize = 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 SetVariables(self, variables): Laurent@814: self.Variables = [var["Name"].upper() for var in variables] Laurent@814: Laurent@814: def SetStepNames(self, step_names): Laurent@814: self.StepNames = [step_name.upper() for step_name in step_names] Laurent@814: Laurent@814: def SetValues(self, values): Laurent@814: value_name = values.get("name", None) Laurent@814: if value_name: Laurent@814: self.StepName.SetValue(value_name) Laurent@814: else: Laurent@814: self.StepName.SetValue("") Laurent@814: self.Input.SetValue(values.get("input", False)) Laurent@814: self.Output.SetValue(values.get("output", False)) Laurent@814: self.Action.SetValue(values.get("action", False)) Laurent@814: self.RefreshPreview() Laurent@814: Laurent@814: def GetValues(self): Laurent@814: values = {} Laurent@814: values["name"] = self.StepName.GetValue() Laurent@814: values["input"] = self.Input.IsChecked() Laurent@814: values["output"] = self.Output.IsChecked() Laurent@814: values["action"] = self.Action.IsChecked() Laurent@814: values["width"], values["height"] = self.Step.GetSize() Laurent@814: return values Laurent@814: Laurent@814: def OnConnectorsChanged(self, event): Laurent@814: self.RefreshPreview() Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnNameChanged(self, event): Laurent@814: self.RefreshPreview() Laurent@814: event.Skip() 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: self.Step = SFC_Step(self.Preview, self.StepName.GetValue(), self.Initial) Laurent@814: if self.Input.IsChecked(): Laurent@814: self.Step.AddInput() Laurent@814: else: Laurent@814: self.Step.RemoveInput() Laurent@814: if self.Output.IsChecked(): Laurent@814: self.Step.AddOutput() Laurent@814: else: Laurent@814: self.Step.RemoveOutput() Laurent@814: if self.Action.IsChecked(): Laurent@814: self.Step.AddAction() Laurent@814: else: Laurent@814: self.Step.RemoveAction() Laurent@814: width, height = self.MinStepSize Laurent@814: min_width, min_height = self.Step.GetMinSize() Laurent@814: width, height = max(min_width, width), max(min_height, height) Laurent@814: self.Step.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.Step.SetPosition(x, y) Laurent@814: self.Step.Draw(dc) Laurent@814: Laurent@814: def OnPaint(self, event): Laurent@814: self.RefreshPreview() Laurent@814: event.Skip()