dialogs/ConnectionDialog.py
changeset 814 5743cbdff669
child 856 b64e436f000e
equal deleted inserted replaced
813:1460273f40ed 814:5743cbdff669
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
       
     5 #based on the plcopen standard. 
       
     6 #
       
     7 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU General Public
       
    22 #License along with this library; if not, write to the Free Software
       
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    24 
       
    25 import wx
       
    26 
       
    27 from graphics import *
       
    28 
       
    29 #-------------------------------------------------------------------------------
       
    30 #                          Create New Connection Dialog
       
    31 #-------------------------------------------------------------------------------
       
    32 
       
    33 class ConnectionDialog(wx.Dialog):
       
    34     
       
    35     def __init__(self, parent, controller):
       
    36         wx.Dialog.__init__(self, parent,
       
    37               size=wx.Size(350, 220), title=_('Connection Properties'))
       
    38         
       
    39         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
       
    40         main_sizer.AddGrowableCol(0)
       
    41         main_sizer.AddGrowableRow(0)
       
    42         
       
    43         column_sizer = wx.BoxSizer(wx.HORIZONTAL)
       
    44         main_sizer.AddSizer(column_sizer, border=20, 
       
    45               flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
       
    46         
       
    47         left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=5, vgap=5)
       
    48         left_gridsizer.AddGrowableCol(0)
       
    49         column_sizer.AddSizer(left_gridsizer, 1, border=5, 
       
    50               flag=wx.GROW|wx.RIGHT)
       
    51         
       
    52         type_label = wx.StaticText(self, label=_('Type:'))
       
    53         left_gridsizer.AddWindow(type_label, flag=wx.GROW)
       
    54         
       
    55         self.ConnectorRadioButton = wx.RadioButton(self, 
       
    56               label=_('Connector'), style=wx.RB_GROUP)
       
    57         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.ConnectorRadioButton)
       
    58         self.ConnectorRadioButton.SetValue(True)
       
    59         left_gridsizer.AddWindow(self.ConnectorRadioButton, flag=wx.GROW)
       
    60         
       
    61         self.ConnectionRadioButton = wx.RadioButton(self, label=_('Continuation'))
       
    62         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.ConnectionRadioButton)
       
    63         left_gridsizer.AddWindow(self.ConnectionRadioButton, flag=wx.GROW)
       
    64         
       
    65         name_label = wx.StaticText(self, label=_('Name:'))
       
    66         left_gridsizer.AddWindow(name_label, flag=wx.GROW)
       
    67         
       
    68         self.ConnectionName = wx.TextCtrl(self)
       
    69         self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.ConnectionName)
       
    70         left_gridsizer.AddWindow(self.ConnectionName, flag=wx.GROW)
       
    71         
       
    72         right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)
       
    73         right_gridsizer.AddGrowableCol(0)
       
    74         right_gridsizer.AddGrowableRow(1)
       
    75         column_sizer.AddSizer(right_gridsizer, 1, border=5, 
       
    76               flag=wx.GROW|wx.LEFT)
       
    77         
       
    78         preview_label = wx.StaticText(self, label=_('Preview:'))
       
    79         right_gridsizer.AddWindow(preview_label, flag=wx.GROW)
       
    80         
       
    81         self.Preview = wx.Panel(self, 
       
    82               style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
       
    83         self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
       
    84         setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
       
    85         setattr(self.Preview, "GetScaling", lambda:None)
       
    86         setattr(self.Preview, "IsOfType", controller.IsOfType)
       
    87         self.Preview.Bind(wx.EVT_PAINT, self.OnPaint)
       
    88         right_gridsizer.AddWindow(self.Preview, flag=wx.GROW)
       
    89         
       
    90         button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
       
    91         self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
       
    92         main_sizer.AddSizer(button_sizer, border=20, 
       
    93               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
       
    94         
       
    95         self.SetSizer(main_sizer)
       
    96         
       
    97         self.Connection = None
       
    98         self.MinConnectionSize = None
       
    99         
       
   100         self.PouNames = []
       
   101         self.PouElementNames = []
       
   102         
       
   103         self.ConnectorRadioButton.SetFocus()
       
   104     
       
   105     def SetPreviewFont(self, font):
       
   106         self.Preview.SetFont(font)
       
   107     
       
   108     def SetMinConnectionSize(self, size):
       
   109         self.MinConnectionSize = size
       
   110     
       
   111     def SetValues(self, values):
       
   112         for name, value in values.items():
       
   113             if name == "type":
       
   114                 if value == CONNECTOR:
       
   115                     self.ConnectorRadioButton.SetValue(True)
       
   116                 elif value == CONTINUATION:
       
   117                     self.ConnectionRadioButton.SetValue(True)
       
   118             elif name == "name":
       
   119                 self.ConnectionName.SetValue(value)
       
   120         self.RefreshPreview()
       
   121     
       
   122     def GetValues(self):
       
   123         values = {}
       
   124         if self.ConnectorRadioButton.GetValue():
       
   125             values["type"] = CONNECTOR
       
   126         else:
       
   127             values["type"] = CONTINUATION
       
   128         values["name"] = self.ConnectionName.GetValue()
       
   129         values["width"], values["height"] = self.Connection.GetSize()
       
   130         return values
       
   131 
       
   132     def SetPouNames(self, pou_names):
       
   133         self.PouNames = [pou_name.upper() for pou_name in pou_names]
       
   134         
       
   135     def SetPouElementNames(self, element_names):
       
   136         self.PouElementNames = [element_name.upper() for element_name in element_names]
       
   137     
       
   138     def OnOK(self, event):
       
   139         message = None
       
   140         connection_name = self.ConnectionName.GetValue()
       
   141         if connection_name == "":
       
   142             message = _("Form isn't complete. Name must be filled!")
       
   143         elif not TestIdentifier(connection_name):
       
   144             message = _("\"%s\" is not a valid identifier!") % connection_name
       
   145         elif connection_name.upper() in IEC_KEYWORDS:
       
   146             message = _("\"%s\" is a keyword. It can't be used!") % connection_name
       
   147         elif connection_name.upper() in self.PouNames:
       
   148             message = _("\"%s\" pou already exists!") % connection_name
       
   149         elif connection_name.upper() in self.PouElementNames:
       
   150             message = _("\"%s\" element for this pou already exists!") % connection_name
       
   151         if message is not None:
       
   152             dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR)
       
   153             dialog.ShowModal()
       
   154             dialog.Destroy()
       
   155         else:
       
   156             self.EndModal(wx.ID_OK)
       
   157 
       
   158     def OnTypeChanged(self, event):
       
   159         self.RefreshPreview()
       
   160         event.Skip()
       
   161 
       
   162     def OnNameChanged(self, event):
       
   163         self.RefreshPreview()
       
   164         event.Skip()
       
   165         
       
   166     def RefreshPreview(self):
       
   167         dc = wx.ClientDC(self.Preview)
       
   168         dc.SetFont(self.Preview.GetFont())
       
   169         dc.Clear()
       
   170         if self.ConnectorRadioButton.GetValue():
       
   171             self.Connection = FBD_Connector(self.Preview, CONNECTOR, self.ConnectionName.GetValue())
       
   172         else:
       
   173             self.Connection = FBD_Connector(self.Preview, CONTINUATION, self.ConnectionName.GetValue())
       
   174         width, height = self.MinConnectionSize
       
   175         min_width, min_height = self.Connection.GetMinSize()
       
   176         width, height = max(min_width, width), max(min_height, height)
       
   177         self.Connection.SetSize(width, height)
       
   178         clientsize = self.Preview.GetClientSize()
       
   179         x = (clientsize.width - width) / 2
       
   180         y = (clientsize.height - height) / 2
       
   181         self.Connection.SetPosition(x, y)
       
   182         self.Connection.Draw(dc)
       
   183 
       
   184     def OnPaint(self, event):
       
   185         self.RefreshPreview()
       
   186         event.Skip()