814
|
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) 2012: 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 plcopen.structures import TestIdentifier, IEC_KEYWORDS
|
|
28 |
|
|
29 |
#-------------------------------------------------------------------------------
|
|
30 |
# POU Transition Dialog
|
|
31 |
#-------------------------------------------------------------------------------
|
|
32 |
|
|
33 |
def GetTransitionLanguages():
|
|
34 |
_ = lambda x : x
|
|
35 |
return [_("IL"), _("ST"), _("LD"), _("FBD")]
|
|
36 |
TRANSITION_LANGUAGES_DICT = dict([(_(language), language) for language in GetTransitionLanguages()])
|
|
37 |
|
|
38 |
class PouTransitionDialog(wx.Dialog):
|
|
39 |
|
|
40 |
def __init__(self, parent):
|
|
41 |
wx.Dialog.__init__(self, parent, size=wx.Size(350, 160),
|
|
42 |
title=_('Create a new transition'))
|
|
43 |
|
|
44 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
|
|
45 |
main_sizer.AddGrowableCol(0)
|
|
46 |
main_sizer.AddGrowableRow(0)
|
|
47 |
|
|
48 |
infos_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=15)
|
|
49 |
infos_sizer.AddGrowableCol(1)
|
|
50 |
main_sizer.AddSizer(infos_sizer, border=20,
|
|
51 |
flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
|
|
52 |
|
|
53 |
transitionname_label = wx.StaticText(self, label=_('Transition Name:'))
|
|
54 |
infos_sizer.AddWindow(transitionname_label, border=4,
|
|
55 |
flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP)
|
|
56 |
|
|
57 |
self.TransitionName = wx.TextCtrl(self)
|
|
58 |
infos_sizer.AddWindow(self.TransitionName, flag=wx.GROW)
|
|
59 |
|
|
60 |
language_label = wx.StaticText(self, label=_('Language:'))
|
|
61 |
infos_sizer.AddWindow(language_label, border=4,
|
|
62 |
flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP)
|
|
63 |
|
|
64 |
self.Language = wx.ComboBox(self, style=wx.CB_READONLY)
|
|
65 |
infos_sizer.AddWindow(self.Language, flag=wx.GROW)
|
|
66 |
|
|
67 |
button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
|
|
68 |
self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
|
|
69 |
main_sizer.AddSizer(button_sizer, border=20,
|
|
70 |
flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
|
|
71 |
|
|
72 |
self.SetSizer(main_sizer)
|
|
73 |
|
|
74 |
for language in GetTransitionLanguages():
|
|
75 |
self.Language.Append(_(language))
|
|
76 |
|
|
77 |
self.PouNames = []
|
|
78 |
self.PouElementNames = []
|
|
79 |
|
|
80 |
def OnOK(self, event):
|
|
81 |
error = []
|
|
82 |
transition_name = self.TransitionName.GetValue()
|
|
83 |
if self.TransitionName.GetValue() == "":
|
|
84 |
error.append(_("Transition Name"))
|
|
85 |
if self.Language.GetSelection() == -1:
|
|
86 |
error.append(_("Language"))
|
|
87 |
message = None
|
|
88 |
if len(error) > 0:
|
|
89 |
text = ""
|
|
90 |
for i, item in enumerate(error):
|
|
91 |
if i == 0:
|
|
92 |
text += item
|
|
93 |
elif i == len(error) - 1:
|
|
94 |
text += _(" and %s")%item
|
|
95 |
else:
|
|
96 |
text += _(", %s")%item
|
|
97 |
message = _("Form isn't complete. %s must be filled!") % text
|
|
98 |
elif not TestIdentifier(transition_name):
|
|
99 |
message = _("\"%s\" is not a valid identifier!") % transition_name
|
|
100 |
elif transition_name.upper() in IEC_KEYWORDS:
|
|
101 |
message = _("\"%s\" is a keyword. It can't be used!") % transition_name
|
|
102 |
elif transition_name.upper() in self.PouNames:
|
|
103 |
message = _("A POU named \"%s\" already exists!") % transition_name
|
|
104 |
elif transition_name.upper() in self.PouElementNames:
|
|
105 |
message = _("\"%s\" element for this pou already exists!") % transition_name
|
|
106 |
if message is not None:
|
|
107 |
dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR)
|
|
108 |
dialog.ShowModal()
|
|
109 |
dialog.Destroy()
|
|
110 |
else:
|
|
111 |
self.EndModal(wx.ID_OK)
|
|
112 |
|
|
113 |
def SetPouNames(self, pou_names):
|
|
114 |
self.PouNames = [pou_name.upper() for pou_name in pou_names]
|
|
115 |
|
|
116 |
def SetPouElementNames(self, pou_names):
|
|
117 |
self.PouElementNames = [pou_name.upper() for pou_name in pou_names]
|
|
118 |
|
|
119 |
def SetValues(self, values):
|
|
120 |
for item, value in values.items():
|
|
121 |
if item == "transitionName":
|
|
122 |
self.TransitionName.SetValue(value)
|
|
123 |
elif item == "language":
|
|
124 |
self.Language.SetSelection(_(value))
|
|
125 |
|
|
126 |
def GetValues(self):
|
|
127 |
values = {}
|
|
128 |
values["transitionName"] = self.TransitionName.GetValue()
|
|
129 |
values["language"] = TRANSITION_LANGUAGES_DICT[self.Language.GetStringSelection()]
|
|
130 |
return values
|