dialogs/ConnectionDialog.py
branch1.1 Korean release
changeset 968 eee7625de1f7
parent 856 b64e436f000e
child 1245 d34ba528346b
equal deleted inserted replaced
808:6e205c1f05a0 968:eee7625de1f7
       
     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, apply_button=False):
       
    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         if apply_button:
       
    96             self.ApplyToAllButton = wx.Button(self, label=_("Propagate Name"))
       
    97             self.ApplyToAllButton.SetToolTipString(
       
    98                 _("Apply name modification to all continuations with the same name"))
       
    99             self.Bind(wx.EVT_BUTTON, self.OnApplyToAll, self.ApplyToAllButton)
       
   100             button_sizer.AddWindow(self.ApplyToAllButton)
       
   101         
       
   102         self.SetSizer(main_sizer)
       
   103         
       
   104         self.Connection = None
       
   105         self.MinConnectionSize = None
       
   106         
       
   107         self.PouNames = []
       
   108         self.PouElementNames = []
       
   109         
       
   110         self.ConnectorRadioButton.SetFocus()
       
   111     
       
   112     def SetPreviewFont(self, font):
       
   113         self.Preview.SetFont(font)
       
   114     
       
   115     def SetMinConnectionSize(self, size):
       
   116         self.MinConnectionSize = size
       
   117     
       
   118     def SetValues(self, values):
       
   119         for name, value in values.items():
       
   120             if name == "type":
       
   121                 if value == CONNECTOR:
       
   122                     self.ConnectorRadioButton.SetValue(True)
       
   123                 elif value == CONTINUATION:
       
   124                     self.ConnectionRadioButton.SetValue(True)
       
   125             elif name == "name":
       
   126                 self.ConnectionName.SetValue(value)
       
   127         self.RefreshPreview()
       
   128     
       
   129     def GetValues(self):
       
   130         values = {}
       
   131         if self.ConnectorRadioButton.GetValue():
       
   132             values["type"] = CONNECTOR
       
   133         else:
       
   134             values["type"] = CONTINUATION
       
   135         values["name"] = self.ConnectionName.GetValue()
       
   136         values["width"], values["height"] = self.Connection.GetSize()
       
   137         return values
       
   138 
       
   139     def SetPouNames(self, pou_names):
       
   140         self.PouNames = [pou_name.upper() for pou_name in pou_names]
       
   141         
       
   142     def SetPouElementNames(self, element_names):
       
   143         self.PouElementNames = [element_name.upper() for element_name in element_names]
       
   144     
       
   145     def TestName(self):
       
   146         message = None
       
   147         connection_name = self.ConnectionName.GetValue()
       
   148         if connection_name == "":
       
   149             message = _("Form isn't complete. Name must be filled!")
       
   150         elif not TestIdentifier(connection_name):
       
   151             message = _("\"%s\" is not a valid identifier!") % connection_name
       
   152         elif connection_name.upper() in IEC_KEYWORDS:
       
   153             message = _("\"%s\" is a keyword. It can't be used!") % connection_name
       
   154         elif connection_name.upper() in self.PouNames:
       
   155             message = _("\"%s\" pou already exists!") % connection_name
       
   156         elif connection_name.upper() in self.PouElementNames:
       
   157             message = _("\"%s\" element for this pou already exists!") % connection_name
       
   158         if message is not None:
       
   159             dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR)
       
   160             dialog.ShowModal()
       
   161             dialog.Destroy()
       
   162             return False
       
   163         return True
       
   164         
       
   165     def OnOK(self, event):
       
   166         if self.TestName():
       
   167             self.EndModal(wx.ID_OK)
       
   168 
       
   169     def OnApplyToAll(self, event):
       
   170         if self.TestName():
       
   171             self.EndModal(wx.ID_YESTOALL)
       
   172 
       
   173     def OnTypeChanged(self, event):
       
   174         self.RefreshPreview()
       
   175         event.Skip()
       
   176 
       
   177     def OnNameChanged(self, event):
       
   178         self.RefreshPreview()
       
   179         event.Skip()
       
   180         
       
   181     def RefreshPreview(self):
       
   182         dc = wx.ClientDC(self.Preview)
       
   183         dc.SetFont(self.Preview.GetFont())
       
   184         dc.Clear()
       
   185         if self.ConnectorRadioButton.GetValue():
       
   186             self.Connection = FBD_Connector(self.Preview, CONNECTOR, self.ConnectionName.GetValue())
       
   187         else:
       
   188             self.Connection = FBD_Connector(self.Preview, CONTINUATION, self.ConnectionName.GetValue())
       
   189         width, height = self.MinConnectionSize
       
   190         min_width, min_height = self.Connection.GetMinSize()
       
   191         width, height = max(min_width, width), max(min_height, height)
       
   192         self.Connection.SetSize(width, height)
       
   193         clientsize = self.Preview.GetClientSize()
       
   194         x = (clientsize.width - width) / 2
       
   195         y = (clientsize.height - height) / 2
       
   196         self.Connection.SetPosition(x, y)
       
   197         self.Connection.Draw(dc)
       
   198 
       
   199     def OnPaint(self, event):
       
   200         self.RefreshPreview()
       
   201         event.Skip()