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: # Edit Transition Content Dialog laurent@409: #------------------------------------------------------------------------------- laurent@409: laurent@409: class SFCTransitionDialog(wx.Dialog): laurent@409: Laurent@714: def __init__(self, parent, controller, connection): Laurent@714: self.Connection = connection Laurent@714: Laurent@714: wx.Dialog.__init__(self, parent, Laurent@714: size=wx.Size(350, 300), title=_('Edit transition')) Laurent@714: Laurent@714: main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) Laurent@714: main_sizer.AddGrowableCol(0) Laurent@714: main_sizer.AddGrowableRow(0) Laurent@714: Laurent@714: column_sizer = wx.BoxSizer(wx.HORIZONTAL) Laurent@714: main_sizer.AddSizer(column_sizer, border=20, Laurent@714: flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) Laurent@714: Laurent@714: left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=8, vgap=5) Laurent@714: left_gridsizer.AddGrowableCol(0) Laurent@714: column_sizer.AddSizer(left_gridsizer, 1, border=5, Laurent@714: flag=wx.GROW|wx.RIGHT) Laurent@714: Laurent@714: type_label = wx.StaticText(self, label=_('Type:')) Laurent@714: left_gridsizer.AddWindow(type_label, flag=wx.GROW) Laurent@714: Laurent@714: self.ReferenceRadioButton = wx.RadioButton(self, Laurent@714: label=_('Reference'), style=wx.RB_GROUP) Laurent@714: self.ReferenceRadioButton.SetValue(True) Laurent@714: self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.ReferenceRadioButton) Laurent@714: left_gridsizer.AddWindow(self.ReferenceRadioButton, flag=wx.GROW) Laurent@714: Laurent@714: self.Reference = wx.ComboBox(self, style=wx.CB_READONLY) Laurent@714: self.Bind(wx.EVT_COMBOBOX, self.OnReferenceChanged, self.Reference) Laurent@714: left_gridsizer.AddWindow(self.Reference, flag=wx.GROW) Laurent@714: Laurent@714: self.InlineRadioButton = wx.RadioButton(self, label=_('Inline')) Laurent@714: self.InlineRadioButton.SetValue(False) Laurent@714: self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.InlineRadioButton) Laurent@714: left_gridsizer.AddWindow(self.InlineRadioButton, flag=wx.GROW) Laurent@714: Laurent@714: self.Inline = wx.TextCtrl(self) laurent@409: self.Inline.Enable(False) Laurent@714: self.Bind(wx.EVT_TEXT, self.OnInlineChanged, self.Inline) Laurent@714: left_gridsizer.AddWindow(self.Inline, flag=wx.GROW) Laurent@714: Laurent@714: self.ConnectionRadioButton = wx.RadioButton(self, label=_('Connection')) Laurent@714: self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.ConnectionRadioButton) Laurent@714: self.ConnectionRadioButton.SetValue(False) laurent@409: if not self.Connection: Laurent@714: self.ConnectionRadioButton.Hide() Laurent@714: left_gridsizer.AddWindow(self.ConnectionRadioButton, flag=wx.GROW) Laurent@714: Laurent@714: priority_label = wx.StaticText(self, label=_('Priority:')) Laurent@714: left_gridsizer.AddWindow(priority_label, flag=wx.GROW) Laurent@714: Laurent@714: self.Priority = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS) Laurent@714: self.Bind(wx.EVT_TEXT, self.OnPriorityChanged, self.Priority) Laurent@714: left_gridsizer.AddWindow(self.Priority, flag=wx.GROW) Laurent@714: Laurent@714: right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5) Laurent@714: right_gridsizer.AddGrowableCol(0) Laurent@714: right_gridsizer.AddGrowableRow(1) Laurent@714: column_sizer.AddSizer(right_gridsizer, 1, border=5, Laurent@714: flag=wx.GROW|wx.LEFT) Laurent@714: Laurent@714: preview_label = wx.StaticText(self, label=_('Preview:')) Laurent@714: right_gridsizer.AddWindow(preview_label, flag=wx.GROW) Laurent@714: Laurent@714: self.Preview = wx.Panel(self, Laurent@714: 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, "RefreshTransitionModel", lambda x:None) laurent@409: setattr(self.Preview, "GetScaling", lambda:None) Laurent@714: setattr(self.Preview, "IsOfType", controller.IsOfType) Laurent@714: self.Preview.Bind(wx.EVT_PAINT, self.OnPaint) Laurent@714: right_gridsizer.AddWindow(self.Preview, flag=wx.GROW) Laurent@714: Laurent@714: button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) Laurent@714: self.Bind(wx.EVT_BUTTON, self.OnOK, Laurent@714: button_sizer.GetAffirmativeButton()) Laurent@714: main_sizer.AddSizer(button_sizer, border=20, Laurent@714: flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) Laurent@714: Laurent@714: self.SetSizer(main_sizer) Laurent@714: laurent@409: self.Transition = None laurent@409: self.MinTransitionSize = None laurent@409: laurent@409: self.Element = SFC_Transition(self.Preview) laurent@577: Laurent@714: self.ReferenceRadioButton.SetFocus() laurent@409: laurent@409: def SetPreviewFont(self, font): laurent@409: self.Preview.SetFont(font) laurent@409: laurent@409: def SetElementSize(self, size): laurent@409: min_width, min_height = self.Element.GetMinSize() laurent@409: width, height = max(min_width, size[0]), max(min_height, size[1]) laurent@409: self.Element.SetSize(width, height) laurent@409: laurent@409: def OnOK(self, event): laurent@409: error = [] Laurent@714: if self.ReferenceRadioButton.GetValue() and self.Reference.GetStringSelection() == "": laurent@409: error.append(_("Reference")) Laurent@714: if self.InlineRadioButton.GetValue() and self.Inline.GetValue() == "": laurent@409: error.append(_("Inline")) laurent@409: if len(error) > 0: laurent@409: text = "" laurent@409: for i, item in enumerate(error): laurent@409: if i == 0: laurent@409: text += item laurent@409: elif i == len(error) - 1: laurent@409: text += _(" and %s")%item laurent@409: else: laurent@409: text += _(", %s")%item Laurent@714: dialog = wx.MessageDialog(self, _("Form isn't complete. %s must be filled!")%text, _("Error"), wx.OK|wx.ICON_ERROR) Laurent@714: dialog.ShowModal() Laurent@714: dialog.Destroy() laurent@409: else: laurent@409: self.EndModal(wx.ID_OK) laurent@409: laurent@409: def OnTypeChanged(self, event): Laurent@714: if self.ReferenceRadioButton.GetValue(): laurent@409: self.Element.SetType("reference", self.Reference.GetStringSelection()) laurent@409: self.Reference.Enable(True) laurent@409: self.Inline.Enable(False) Laurent@714: elif self.InlineRadioButton.GetValue(): laurent@409: self.Element.SetType("inline", self.Inline.GetValue()) laurent@409: self.Reference.Enable(False) laurent@409: self.Inline.Enable(True) laurent@409: else: laurent@409: self.Element.SetType("connection") laurent@409: self.Reference.Enable(False) laurent@409: self.Inline.Enable(False) laurent@409: self.RefreshPreview() laurent@409: event.Skip() laurent@409: laurent@409: def OnReferenceChanged(self, event): laurent@409: self.Element.SetType("reference", self.Reference.GetStringSelection()) laurent@409: self.RefreshPreview() laurent@409: event.Skip() laurent@409: laurent@409: def OnInlineChanged(self, event): laurent@409: self.Element.SetType("inline", self.Inline.GetValue()) laurent@409: self.RefreshPreview() laurent@409: event.Skip() laurent@409: laurent@409: def OnPriorityChanged(self, event): laurent@409: self.Element.SetPriority(int(self.Priority.GetValue())) laurent@409: self.RefreshPreview() laurent@409: event.Skip() laurent@409: laurent@409: def SetTransitions(self, transitions): laurent@409: self.Reference.Append("") laurent@409: for transition in transitions: laurent@409: self.Reference.Append(transition) laurent@409: laurent@409: def SetValues(self, values): laurent@409: if values["type"] == "reference": Laurent@714: self.ReferenceRadioButton.SetValue(True) Laurent@714: self.InlineRadioButton.SetValue(False) Laurent@714: self.ConnectionRadioButton.SetValue(False) laurent@409: self.Reference.Enable(True) laurent@409: self.Inline.Enable(False) laurent@409: self.Reference.SetStringSelection(values["value"]) laurent@409: self.Element.SetType("reference", values["value"]) laurent@409: elif values["type"] == "inline": Laurent@714: self.ReferenceRadioButton.SetValue(False) Laurent@714: self.InlineRadioButton.SetValue(True) Laurent@714: self.ConnectionRadioButton.SetValue(False) laurent@409: self.Reference.Enable(False) laurent@409: self.Inline.Enable(True) laurent@409: self.Inline.SetValue(values["value"]) laurent@409: self.Element.SetType("inline", values["value"]) laurent@409: elif values["type"] == "connection" and self.Connection: Laurent@714: self.ReferenceRadioButton.SetValue(False) Laurent@714: self.InlineRadioButton.SetValue(False) Laurent@714: self.ConnectionRadioButton.SetValue(True) laurent@409: self.Reference.Enable(False) laurent@409: self.Inline.Enable(False) laurent@409: self.Element.SetType("connection") laurent@409: self.Priority.SetValue(values["priority"]) laurent@409: self.Element.SetPriority(values["priority"]) laurent@409: self.RefreshPreview() laurent@409: laurent@409: def GetValues(self): laurent@409: values = {"priority" : int(self.Priority.GetValue())} Laurent@714: if self.ReferenceRadioButton.GetValue(): laurent@409: values["type"] = "reference" laurent@409: values["value"] = self.Reference.GetStringSelection() Laurent@714: elif self.InlineRadioButton.GetValue(): laurent@409: values["type"] = "inline" laurent@409: values["value"] = self.Inline.GetValue() laurent@409: else: laurent@409: values["type"] = "connection" laurent@409: values["value"] = None laurent@409: return values 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: clientsize = self.Preview.GetClientSize() laurent@409: posx, posy = self.Element.GetPosition() laurent@409: rect = self.Element.GetBoundingBox() laurent@409: diffx, diffy = posx - rect.x, posy - rect.y laurent@409: self.Element.SetPosition((clientsize.width - rect.width) / 2 + diffx, (clientsize.height - rect.height) / 2 + diffy) laurent@409: self.Element.Draw(dc) laurent@409: laurent@409: def OnPaint(self, event): laurent@409: self.RefreshPreview() laurent@409: event.Skip()