dialogs/ConnectionDialog.py
changeset 714 131ea7f237b9
parent 577 9dbb79722fbc
equal deleted inserted replaced
713:95a0a427f3ef 714:131ea7f237b9
    28 
    28 
    29 #-------------------------------------------------------------------------------
    29 #-------------------------------------------------------------------------------
    30 #                          Create New Connection Dialog
    30 #                          Create New Connection Dialog
    31 #-------------------------------------------------------------------------------
    31 #-------------------------------------------------------------------------------
    32 
    32 
    33 [ID_CONNECTIONDIALOG, ID_CONNECTIONDIALOGSPACER, 
       
    34  ID_CONNECTIONDIALOGNAME, ID_CONNECTIONDIALOGRADIOBUTTON1, 
       
    35  ID_CONNECTIONDIALOGRADIOBUTTON2, ID_CONNECTIONDIALOGPREVIEW,
       
    36  ID_CONNECTIONDIALOGSTATICTEXT1, ID_CONNECTIONDIALOGSTATICTEXT2, 
       
    37  ID_CONNECTIONDIALOGSTATICTEXT3, 
       
    38 ] = [wx.NewId() for _init_ctrls in range(9)]
       
    39 
       
    40 class ConnectionDialog(wx.Dialog):
    33 class ConnectionDialog(wx.Dialog):
    41     
    34     
    42     if wx.VERSION < (2, 6, 0):
    35     def __init__(self, parent, controller):
    43         def Bind(self, event, function, id = None):
    36         wx.Dialog.__init__(self, parent,
    44             if id is not None:
    37               size=wx.Size(350, 220), title=_('Connection Properties'))
    45                 event(self, id, function)
       
    46             else:
       
    47                 event(self, function)
       
    48     
       
    49     def _init_coll_flexGridSizer1_Items(self, parent):
       
    50         parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
       
    51         parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
       
    52         
    38         
    53     def _init_coll_flexGridSizer1_Growables(self, parent):
    39         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
    54         parent.AddGrowableCol(0)
    40         main_sizer.AddGrowableCol(0)
    55         parent.AddGrowableRow(0)
    41         main_sizer.AddGrowableRow(0)
    56     
       
    57     def _init_coll_MainSizer_Items(self, parent):
       
    58         parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT)
       
    59         parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT)
       
    60     
       
    61     def _init_coll_LeftGridSizer_Items(self, parent):
       
    62         parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
       
    63         parent.AddWindow(self.radioButton1, 0, border=0, flag=wx.GROW)
       
    64         parent.AddWindow(self.radioButton2, 0, border=0, flag=wx.GROW)
       
    65         parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW)
       
    66         parent.AddWindow(self.ConnectionName, 0, border=0, flag=wx.GROW)
       
    67         parent.AddWindow(self.Spacer, 0, border=0, flag=wx.GROW)
       
    68     
       
    69     def _init_coll_LeftGridSizer_Growables(self, parent):
       
    70         parent.AddGrowableCol(0)
       
    71         parent.AddGrowableRow(5)
       
    72             
       
    73     def _init_coll_RightGridSizer_Items(self, parent):
       
    74         parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW)
       
    75         parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW)
       
    76         
    42         
    77     def _init_coll_RightGridSizer_Growables(self, parent):
    43         column_sizer = wx.BoxSizer(wx.HORIZONTAL)
    78         parent.AddGrowableCol(0)
    44         main_sizer.AddSizer(column_sizer, border=20, 
    79         parent.AddGrowableRow(1)
    45               flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
    80 
       
    81     def _init_sizers(self):
       
    82         self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
       
    83         self.MainSizer = wx.BoxSizer(wx.HORIZONTAL)
       
    84         self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=6, vgap=5)
       
    85         self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)
       
    86 
       
    87         self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
       
    88         self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
       
    89         self._init_coll_MainSizer_Items(self.MainSizer)
       
    90         self._init_coll_LeftGridSizer_Items(self.LeftGridSizer)
       
    91         self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer)
       
    92         self._init_coll_RightGridSizer_Items(self.RightGridSizer)
       
    93         self._init_coll_RightGridSizer_Growables(self.RightGridSizer)
       
    94 
       
    95         self.SetSizer(self.flexGridSizer1)
       
    96 
       
    97     def _init_ctrls(self, prnt, ctrler):
       
    98         wx.Dialog.__init__(self, id=ID_CONNECTIONDIALOG,
       
    99               name='ConnectionDialog', parent=prnt,
       
   100               size=wx.Size(350, 220), style=wx.DEFAULT_DIALOG_STYLE,
       
   101               title=_('Connection Properties'))
       
   102         self.SetClientSize(wx.Size(350, 220))
       
   103 
       
   104         self.staticText1 = wx.StaticText(id=ID_CONNECTIONDIALOGSTATICTEXT1,
       
   105               label=_('Type:'), name='staticText1', parent=self,
       
   106               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   107 
       
   108         self.staticText2 = wx.StaticText(id=ID_CONNECTIONDIALOGSTATICTEXT2,
       
   109               label=_('Name:'), name='staticText2', parent=self,
       
   110               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   111 
       
   112         self.staticText3 = wx.StaticText(id=ID_CONNECTIONDIALOGSTATICTEXT3,
       
   113               label=_('Preview:'), name='staticText3', parent=self,
       
   114               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   115 
       
   116         self.radioButton1 = wx.RadioButton(id=ID_CONNECTIONDIALOGRADIOBUTTON1,
       
   117               label=_('Connector'), name='radioButton1', parent=self,
       
   118               pos=wx.Point(0, 0), size=wx.Size(0, 24), style=wx.RB_GROUP)
       
   119         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_CONNECTIONDIALOGRADIOBUTTON1)
       
   120         self.radioButton1.SetValue(True)
       
   121 
       
   122         self.radioButton2 = wx.RadioButton(id=ID_CONNECTIONDIALOGRADIOBUTTON2,
       
   123               label=_('Continuation'), name='radioButton2', parent=self, 
       
   124               pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
       
   125         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_CONNECTIONDIALOGRADIOBUTTON2)
       
   126         self.radioButton2.SetValue(False)
       
   127         
    46         
   128         self.ConnectionName = wx.TextCtrl(id=ID_CONNECTIONDIALOGNAME,
    47         left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=5, vgap=5)
   129               name='Name', parent=self, pos=wx.Point(0, 0),
    48         left_gridsizer.AddGrowableCol(0)
   130               size=wx.Size(0, 24), style=0)
    49         column_sizer.AddSizer(left_gridsizer, 1, border=5, 
   131         self.Bind(wx.EVT_TEXT, self.OnNameChanged, id=ID_CONNECTIONDIALOGNAME)
    50               flag=wx.GROW|wx.RIGHT)
   132 
    51         
   133         self.Preview = wx.Panel(id=ID_CONNECTIONDIALOGPREVIEW,
    52         type_label = wx.StaticText(self, label=_('Type:'))
   134               name='Preview', parent=self, pos=wx.Point(0, 0),
    53         left_gridsizer.AddWindow(type_label, flag=wx.GROW)
   135               size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
    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)
   136         self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
    83         self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
   137         setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
    84         setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
   138         setattr(self.Preview, "GetScaling", lambda:None)
    85         setattr(self.Preview, "GetScaling", lambda:None)
   139         setattr(self.Preview, "IsOfType", ctrler.IsOfType)
    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)
   140         
    89         
   141         self.Spacer = wx.Panel(id=ID_CONNECTIONDIALOGSPACER,
    90         button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
   142               name='Spacer', parent=self, pos=wx.Point(0, 0),
    91         self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
   143               size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)
    92         main_sizer.AddSizer(button_sizer, border=20, 
       
    93               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
   144         
    94         
   145         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
    95         self.SetSizer(main_sizer)
   146         if wx.VERSION >= (2, 5, 0):
       
   147             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
       
   148             self.Preview.Bind(wx.EVT_PAINT, self.OnPaint)
       
   149         else:
       
   150             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId())
       
   151             wx.EVT_PAINT(self.Preview, self.OnPaint)
       
   152         
    96         
   153         self._init_sizers()
       
   154 
       
   155     def __init__(self, parent, controler):
       
   156         self._init_ctrls(parent, controler)
       
   157         self.Connection = None
    97         self.Connection = None
   158         self.MinConnectionSize = None
    98         self.MinConnectionSize = None
   159         
    99         
   160         self.PouNames = []
   100         self.PouNames = []
   161         self.PouElementNames = []
   101         self.PouElementNames = []
   162         
   102         
   163         self.radioButton1.SetFocus()
   103         self.ConnectorRadioButton.SetFocus()
   164     
   104     
   165     def SetPreviewFont(self, font):
   105     def SetPreviewFont(self, font):
   166         self.Preview.SetFont(font)
   106         self.Preview.SetFont(font)
   167     
   107     
   168     def SetMinConnectionSize(self, size):
   108     def SetMinConnectionSize(self, size):
   170     
   110     
   171     def SetValues(self, values):
   111     def SetValues(self, values):
   172         for name, value in values.items():
   112         for name, value in values.items():
   173             if name == "type":
   113             if name == "type":
   174                 if value == CONNECTOR:
   114                 if value == CONNECTOR:
   175                     self.radioButton1.SetValue(True)
   115                     self.ConnectorRadioButton.SetValue(True)
   176                 elif value == CONTINUATION:
   116                 elif value == CONTINUATION:
   177                     self.radioButton2.SetValue(True)
   117                     self.ConnectionRadioButton.SetValue(True)
   178             elif name == "name":
   118             elif name == "name":
   179                 self.ConnectionName.SetValue(value)
   119                 self.ConnectionName.SetValue(value)
   180         self.RefreshPreview()
   120         self.RefreshPreview()
   181     
   121     
   182     def GetValues(self):
   122     def GetValues(self):
   183         values = {}
   123         values = {}
   184         if self.radioButton1.GetValue():
   124         if self.ConnectorRadioButton.GetValue():
   185             values["type"] = CONNECTOR
   125             values["type"] = CONNECTOR
   186         else:
   126         else:
   187             values["type"] = CONTINUATION
   127             values["type"] = CONTINUATION
   188         values["name"] = self.ConnectionName.GetValue()
   128         values["name"] = self.ConnectionName.GetValue()
   189         values["width"], values["height"] = self.Connection.GetSize()
   129         values["width"], values["height"] = self.Connection.GetSize()
   194         
   134         
   195     def SetPouElementNames(self, element_names):
   135     def SetPouElementNames(self, element_names):
   196         self.PouElementNames = [element_name.upper() for element_name in element_names]
   136         self.PouElementNames = [element_name.upper() for element_name in element_names]
   197     
   137     
   198     def OnOK(self, event):
   138     def OnOK(self, event):
       
   139         message = None
   199         connection_name = self.ConnectionName.GetValue()
   140         connection_name = self.ConnectionName.GetValue()
   200         if connection_name == "":
   141         if connection_name == "":
   201             message = wx.MessageDialog(self, _("Form isn't complete. Name must be filled!"), _("Error"), wx.OK|wx.ICON_ERROR)
   142             message = _("Form isn't complete. Name must be filled!")
   202             message.ShowModal()
       
   203             message.Destroy()
       
   204         elif not TestIdentifier(connection_name):
   143         elif not TestIdentifier(connection_name):
   205             message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%connection_name, _("Error"), wx.OK|wx.ICON_ERROR)
   144             message = _("\"%s\" is not a valid identifier!") % connection_name
   206             message.ShowModal()
       
   207             message.Destroy()
       
   208         elif connection_name.upper() in IEC_KEYWORDS:
   145         elif connection_name.upper() in IEC_KEYWORDS:
   209             message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%connection_name, _("Error"), wx.OK|wx.ICON_ERROR)
   146             message = _("\"%s\" is a keyword. It can't be used!") % connection_name
   210             message.ShowModal()
       
   211             message.Destroy()
       
   212         elif connection_name.upper() in self.PouNames:
   147         elif connection_name.upper() in self.PouNames:
   213             message = wx.MessageDialog(self, _("\"%s\" pou already exists!")%connection_name, _("Error"), wx.OK|wx.ICON_ERROR)
   148             message = _("\"%s\" pou already exists!") % connection_name
   214             message.ShowModal()
       
   215             message.Destroy()
       
   216         elif connection_name.upper() in self.PouElementNames:
   149         elif connection_name.upper() in self.PouElementNames:
   217             message = wx.MessageDialog(self, _("\"%s\" element for this pou already exists!")%connection_name, _("Error"), wx.OK|wx.ICON_ERROR)
   150             message = _("\"%s\" element for this pou already exists!") % connection_name
   218             message.ShowModal()
   151         if message is not None:
   219             message.Destroy()
   152             dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR)
       
   153             dialog.ShowModal()
       
   154             dialog.Destroy()
   220         else:
   155         else:
   221             self.EndModal(wx.ID_OK)
   156             self.EndModal(wx.ID_OK)
   222 
   157 
   223     def OnTypeChanged(self, event):
   158     def OnTypeChanged(self, event):
   224         self.RefreshPreview()
   159         self.RefreshPreview()
   230         
   165         
   231     def RefreshPreview(self):
   166     def RefreshPreview(self):
   232         dc = wx.ClientDC(self.Preview)
   167         dc = wx.ClientDC(self.Preview)
   233         dc.SetFont(self.Preview.GetFont())
   168         dc.SetFont(self.Preview.GetFont())
   234         dc.Clear()
   169         dc.Clear()
   235         if self.radioButton1.GetValue():
   170         if self.ConnectorRadioButton.GetValue():
   236             self.Connection = FBD_Connector(self.Preview, CONNECTOR, self.ConnectionName.GetValue())
   171             self.Connection = FBD_Connector(self.Preview, CONNECTOR, self.ConnectionName.GetValue())
   237         else:
   172         else:
   238             self.Connection = FBD_Connector(self.Preview, CONTINUATION, self.ConnectionName.GetValue())
   173             self.Connection = FBD_Connector(self.Preview, CONTINUATION, self.ConnectionName.GetValue())
   239         width, height = self.MinConnectionSize
   174         width, height = self.MinConnectionSize
   240         min_width, min_height = self.Connection.GetMinSize()
   175         min_width, min_height = self.Connection.GetMinSize()