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 Transition Content Dialog
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814: class SFCTransitionDialog(wx.Dialog):
Laurent@814:     
Laurent@814:     def __init__(self, parent, controller, connection):
Laurent@814:         self.Connection = connection
Laurent@814:         
Laurent@814:         wx.Dialog.__init__(self, parent, 
Laurent@814:               size=wx.Size(350, 300), title=_('Edit transition'))
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=8, 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:         type_label = wx.StaticText(self, label=_('Type:'))
Laurent@814:         left_gridsizer.AddWindow(type_label, flag=wx.GROW)
Laurent@814:         
Laurent@814:         self.ReferenceRadioButton = wx.RadioButton(self,
Laurent@814:               label=_('Reference'), style=wx.RB_GROUP)
Laurent@814:         self.ReferenceRadioButton.SetValue(True)
Laurent@814:         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.ReferenceRadioButton)
Laurent@814:         left_gridsizer.AddWindow(self.ReferenceRadioButton, flag=wx.GROW)
Laurent@814:         
Laurent@814:         self.Reference = wx.ComboBox(self, style=wx.CB_READONLY)
Laurent@814:         self.Bind(wx.EVT_COMBOBOX, self.OnReferenceChanged, self.Reference)
Laurent@814:         left_gridsizer.AddWindow(self.Reference, flag=wx.GROW)
Laurent@814:         
Laurent@814:         self.InlineRadioButton = wx.RadioButton(self, label=_('Inline'))
Laurent@814:         self.InlineRadioButton.SetValue(False)
Laurent@814:         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.InlineRadioButton)
Laurent@814:         left_gridsizer.AddWindow(self.InlineRadioButton, flag=wx.GROW)
Laurent@814:         
Laurent@814:         self.Inline = wx.TextCtrl(self)
Laurent@814:         self.Inline.Enable(False)
Laurent@814:         self.Bind(wx.EVT_TEXT, self.OnInlineChanged, self.Inline)
Laurent@814:         left_gridsizer.AddWindow(self.Inline, flag=wx.GROW)
Laurent@814:         
Laurent@814:         self.ConnectionRadioButton = wx.RadioButton(self, label=_('Connection'))
Laurent@814:         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.ConnectionRadioButton)
Laurent@814:         self.ConnectionRadioButton.SetValue(False)
Laurent@814:         if not self.Connection:
Laurent@814:             self.ConnectionRadioButton.Hide()
Laurent@814:         left_gridsizer.AddWindow(self.ConnectionRadioButton, flag=wx.GROW)
Laurent@814:         
Laurent@814:         priority_label = wx.StaticText(self, label=_('Priority:'))
Laurent@814:         left_gridsizer.AddWindow(priority_label, flag=wx.GROW)
Laurent@814:         
Laurent@814:         self.Priority = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS)
Laurent@814:         self.Bind(wx.EVT_TEXT, self.OnPriorityChanged, self.Priority)
Laurent@814:         left_gridsizer.AddWindow(self.Priority, 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, "RefreshTransitionModel", 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(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.Transition = None
Laurent@814:         self.MinTransitionSize = None
Laurent@814:         
Laurent@814:         self.Element = SFC_Transition(self.Preview)
Laurent@814:         
Laurent@814:         self.ReferenceRadioButton.SetFocus()
Laurent@814:     
Laurent@814:     def SetPreviewFont(self, font):
Laurent@814:         self.Preview.SetFont(font)
Laurent@814:     
Laurent@814:     def SetElementSize(self, size):
Laurent@814:         min_width, min_height = self.Element.GetMinSize()
Laurent@814:         width, height = max(min_width, size[0]), max(min_height, size[1])
Laurent@814:         self.Element.SetSize(width, height)
Laurent@814:     
Laurent@814:     def OnOK(self, event):
Laurent@814:         error = []
Laurent@814:         if self.ReferenceRadioButton.GetValue() and self.Reference.GetStringSelection() == "":
Laurent@814:             error.append(_("Reference"))
Laurent@814:         if self.InlineRadioButton.GetValue() and self.Inline.GetValue() == "":
Laurent@814:             error.append(_("Inline"))
Laurent@814:         if len(error) > 0:
Laurent@814:             text = ""
Laurent@814:             for i, item in enumerate(error):
Laurent@814:                 if i == 0:
Laurent@814:                     text += item
Laurent@814:                 elif i == len(error) - 1:
Laurent@814:                     text += _(" and %s")%item
Laurent@814:                 else:
Laurent@814:                     text += _(", %s")%item 
Laurent@814:             dialog = wx.MessageDialog(self, _("Form isn't complete. %s must be filled!")%text, _("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 OnTypeChanged(self, event):
Laurent@814:         if self.ReferenceRadioButton.GetValue():
Laurent@814:             self.Element.SetType("reference", self.Reference.GetStringSelection())
Laurent@814:             self.Reference.Enable(True)
Laurent@814:             self.Inline.Enable(False)
Laurent@814:         elif self.InlineRadioButton.GetValue():
Laurent@814:             self.Element.SetType("inline", self.Inline.GetValue())
Laurent@814:             self.Reference.Enable(False)
Laurent@814:             self.Inline.Enable(True)
Laurent@814:         else:
Laurent@814:             self.Element.SetType("connection")
Laurent@814:             self.Reference.Enable(False)
Laurent@814:             self.Inline.Enable(False)
Laurent@814:         self.RefreshPreview()
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def OnReferenceChanged(self, event):
Laurent@814:         self.Element.SetType("reference", self.Reference.GetStringSelection())
Laurent@814:         self.RefreshPreview()
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def OnInlineChanged(self, event):
Laurent@814:         self.Element.SetType("inline", self.Inline.GetValue())
Laurent@814:         self.RefreshPreview()
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def OnPriorityChanged(self, event):
Laurent@814:         self.Element.SetPriority(int(self.Priority.GetValue()))
Laurent@814:         self.RefreshPreview()
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def SetTransitions(self, transitions):
Laurent@814:         self.Reference.Append("")
Laurent@814:         for transition in transitions:
Laurent@814:             self.Reference.Append(transition)
Laurent@814: 
Laurent@814:     def SetValues(self, values):
Laurent@814:         if values["type"] == "reference":
Laurent@814:             self.ReferenceRadioButton.SetValue(True)
Laurent@814:             self.InlineRadioButton.SetValue(False)
Laurent@814:             self.ConnectionRadioButton.SetValue(False)
Laurent@814:             self.Reference.Enable(True)
Laurent@814:             self.Inline.Enable(False)
Laurent@814:             self.Reference.SetStringSelection(values["value"])
Laurent@814:             self.Element.SetType("reference", values["value"])
Laurent@814:         elif values["type"] == "inline":
Laurent@814:             self.ReferenceRadioButton.SetValue(False)
Laurent@814:             self.InlineRadioButton.SetValue(True)
Laurent@814:             self.ConnectionRadioButton.SetValue(False)
Laurent@814:             self.Reference.Enable(False)
Laurent@814:             self.Inline.Enable(True)
Laurent@814:             self.Inline.SetValue(values["value"])
Laurent@814:             self.Element.SetType("inline", values["value"])
Laurent@814:         elif values["type"] == "connection" and self.Connection:
Laurent@814:             self.ReferenceRadioButton.SetValue(False)
Laurent@814:             self.InlineRadioButton.SetValue(False)
Laurent@814:             self.ConnectionRadioButton.SetValue(True)
Laurent@814:             self.Reference.Enable(False)
Laurent@814:             self.Inline.Enable(False)
Laurent@814:             self.Element.SetType("connection")
Laurent@814:         self.Priority.SetValue(values["priority"])
Laurent@814:         self.Element.SetPriority(values["priority"])
Laurent@814:         self.RefreshPreview()
Laurent@814:         
Laurent@814:     def GetValues(self):
Laurent@814:         values = {"priority" : int(self.Priority.GetValue())}
Laurent@814:         if self.ReferenceRadioButton.GetValue():
Laurent@814:             values["type"] = "reference"
Laurent@814:             values["value"] = self.Reference.GetStringSelection()
Laurent@814:         elif self.InlineRadioButton.GetValue():
Laurent@814:             values["type"] = "inline"
Laurent@814:             values["value"] = self.Inline.GetValue()
Laurent@814:         else:
Laurent@814:             values["type"] = "connection"
Laurent@814:             values["value"] = None
Laurent@814:         return values
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:         clientsize = self.Preview.GetClientSize()
Laurent@814:         posx, posy = self.Element.GetPosition()
Laurent@814:         rect = self.Element.GetBoundingBox()
Laurent@814:         diffx, diffy = posx - rect.x, posy - rect.y
Laurent@814:         self.Element.SetPosition((clientsize.width - rect.width) / 2 + diffx, (clientsize.height - rect.height) / 2 + diffy)
Laurent@814:         self.Element.Draw(dc)
Laurent@814: 
Laurent@814:     def OnPaint(self, event):
Laurent@814:         self.RefreshPreview()
Laurent@814:         event.Skip()