dialogs/FBDVariableDialog.py
author Laurent Bessard
Wed, 12 Jun 2013 09:36:59 +0200
changeset 1249 b956c924cbbd
parent 1246 101625efb1c1
child 1250 7e6de17c687a
permissions -rw-r--r--
Rewrite LDPowerRailDialog
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     3
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     5
#based on the plcopen standard. 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     6
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     8
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     9
#See COPYING file for copyrights details.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    10
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    15
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    19
#General Public License for more details.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    20
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    24
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    25
import wx
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    26
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    27
from graphics.GraphicCommons import INPUT, INOUT, OUTPUT
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    28
from graphics.FBD_Objects import FBD_Variable
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    29
from BlockPreviewDialog import BlockPreviewDialog
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    30
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    31
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    32
#                                    Helpers
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    33
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    34
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    35
# Dictionaries containing correspondence between variable block class and string
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    36
# to be shown in Class combo box in both sense
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    37
VARIABLE_CLASSES_DICT = {INPUT : _("Input"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    38
                         INOUT : _("InOut"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    39
                         OUTPUT : _("Output")}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    40
VARIABLE_CLASSES_DICT_REVERSE = dict(
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    41
    [(value, key) for key, value in VARIABLE_CLASSES_DICT.iteritems()])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    42
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    43
#-------------------------------------------------------------------------------
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    44
#                        Set Variable Parameters Dialog
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    45
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    46
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    47
"""
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    48
Class that implements a dialog for defining parameters of a FBD variable graphic
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    49
element
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    50
"""
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    51
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    52
class FBDVariableDialog(BlockPreviewDialog):
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    53
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    54
    def __init__(self, parent, controller, tagname):
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    55
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    56
        Constructor
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    57
        @param parent: Parent wx.Window of dialog for modal
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    58
        @param controller: Reference to project controller
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    59
        @param tagname: Tagname of project POU edited
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    60
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    61
        BlockPreviewDialog.__init__(self, parent, controller, tagname,
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    62
              size=wx.Size(400, 380), title=_('Variable Properties'))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    63
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    64
        # Create dialog main sizer
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    65
        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=4, vgap=10)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    66
        main_sizer.AddGrowableCol(0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    67
        main_sizer.AddGrowableRow(2)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    68
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    69
        # Create a sizer for dividing FBD variable parameters in two columns
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    70
        column_sizer = wx.BoxSizer(wx.HORIZONTAL)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    71
        main_sizer.AddSizer(column_sizer, border=20, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    72
              flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    73
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    74
        # Create a sizer for left column
1182
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
    75
        left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=4, vgap=5)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    76
        left_gridsizer.AddGrowableCol(0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    77
        column_sizer.AddSizer(left_gridsizer, 1, border=5, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    78
              flag=wx.GROW|wx.RIGHT)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    79
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    80
        # Create label for variable class
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    81
        class_label = wx.StaticText(self, label=_('Class:'))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    82
        left_gridsizer.AddWindow(class_label, flag=wx.GROW)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    83
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    84
        # Create a combo box for defining variable class
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    85
        self.Class = wx.ComboBox(self, style=wx.CB_READONLY)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    86
        self.Bind(wx.EVT_COMBOBOX, self.OnClassChanged, self.Class)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    87
        left_gridsizer.AddWindow(self.Class, flag=wx.GROW)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    88
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    89
        # Create label for variable execution order
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    90
        execution_order_label = wx.StaticText(self, 
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    91
              label=_('Execution Order:'))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    92
        left_gridsizer.AddWindow(execution_order_label, flag=wx.GROW)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    93
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    94
        # Create spin control for defining variable execution order
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    95
        self.ExecutionOrder = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS)
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    96
        self.Bind(wx.EVT_SPINCTRL, self.OnExecutionOrderChanged, 
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
    97
                  self.ExecutionOrder)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    98
        left_gridsizer.AddWindow(self.ExecutionOrder, flag=wx.GROW)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    99
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   100
        # Create a sizer for right column
1182
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   101
        right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=0)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   102
        right_gridsizer.AddGrowableCol(0)
1182
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   103
        right_gridsizer.AddGrowableRow(2)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   104
        column_sizer.AddSizer(right_gridsizer, 1, border=5, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   105
              flag=wx.GROW|wx.LEFT)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   106
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   107
        # Create label for variable expression
1182
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   108
        name_label = wx.StaticText(self, label=_('Expression:'))
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   109
        right_gridsizer.AddWindow(name_label, border=5, flag=wx.GROW|wx.BOTTOM)
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   110
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   111
        # Create text control for defining variable expression
1182
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   112
        self.Expression = wx.TextCtrl(self)
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   113
        self.Bind(wx.EVT_TEXT, self.OnExpressionChanged, self.Expression)
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   114
        right_gridsizer.AddWindow(self.Expression, flag=wx.GROW)
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   115
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   116
        # Create a list box to selected variable expression in the list of
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   117
        # variables defined in POU
1182
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   118
        self.VariableName = wx.ListBox(self, size=wx.Size(0, 120), 
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   119
              style=wx.LB_SINGLE|wx.LB_SORT)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   120
        self.Bind(wx.EVT_LISTBOX, self.OnNameChanged, self.VariableName)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   121
        right_gridsizer.AddWindow(self.VariableName, flag=wx.GROW)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   122
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   123
        # Add preview panel and associated label to sizers
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   124
        main_sizer.AddWindow(self.PreviewLabel, border=20,
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   125
              flag=wx.GROW|wx.LEFT|wx.RIGHT)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   126
        main_sizer.AddWindow(self.Preview, border=20,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   127
              flag=wx.GROW|wx.LEFT|wx.RIGHT)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   128
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   129
        # Add buttons sizer to sizers
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   130
        main_sizer.AddSizer(self.ButtonSizer, border=20, 
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   131
              flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   132
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   133
        self.SetSizer(main_sizer)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   134
        
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   135
        # Set options that can be selected in class combo box
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   136
        for choice in VARIABLE_CLASSES_DICT.itervalues():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   137
            self.Class.Append(choice)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   138
        self.Class.SetStringSelection(VARIABLE_CLASSES_DICT[INPUT])
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   139
        
1246
101625efb1c1 Rewrite LDElementDialog
Laurent Bessard
parents: 1244
diff changeset
   140
        # Extract list of variables defined in POU
101625efb1c1 Rewrite LDElementDialog
Laurent Bessard
parents: 1244
diff changeset
   141
        self.RefreshVariableList()
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   142
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   143
        # Refresh values in name list box
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   144
        self.RefreshNameList()
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   145
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   146
        # Class combo box is default control having keyboard focus
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   147
        self.Class.SetFocus()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   148
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   149
    def RefreshNameList(self):
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   150
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   151
        Called to refresh names in name list box
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   152
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   153
        # Get variable class to select POU variable applicable
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   154
        var_class = VARIABLE_CLASSES_DICT_REVERSE[
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   155
                            self.Class.GetStringSelection()]
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   156
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   157
        # Refresh names in name list box by selecting variables in POU variables
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   158
        # list that can be applied to variable class
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   159
        self.VariableName.Clear()
1246
101625efb1c1 Rewrite LDElementDialog
Laurent Bessard
parents: 1244
diff changeset
   160
        for name, (var_type, value_type) in self.VariableList.iteritems():
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   161
            if var_type != "Input" or var_class == INPUT:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   162
                self.VariableName.Append(name)
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   163
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   164
        # Get variable expression and select corresponding value in name list
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   165
        # box if it exists
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   166
        selected = self.Expression.GetValue()
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   167
        if (selected != "" and 
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   168
            self.VariableName.FindString(selected) != wx.NOT_FOUND):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   169
            self.VariableName.SetStringSelection(selected)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   170
        else:
1182
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   171
            self.VariableName.SetSelection(wx.NOT_FOUND)
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   172
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   173
        # Disable name list box if no name present inside
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   174
        self.VariableName.Enable(self.VariableName.GetCount() > 0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   175
            
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   176
    def SetValues(self, values):
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   177
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   178
        Set default variable parameters
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   179
        @param values: Variable parameters values
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   180
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   181
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   182
        # Get class parameter value
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   183
        var_class = values.get("class", None)
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   184
        if var_class is not None:
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   185
            # Set class selected in class combo box
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   186
            self.Class.SetStringSelection(VARIABLE_CLASSES_DICT[var_class])
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   187
            # Refresh names in name list box according to var class
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   188
            self.RefreshNameList()
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   189
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   190
        # For each parameters defined, set corresponding control value
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   191
        for name, value in values.items():
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   192
            
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   193
            # Parameter is variable expression
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   194
            if name == "expression":
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   195
                # Set expression text control value
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   196
                self.Expression.ChangeValue(value)
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   197
                # Select corresponding text in name list box if it exists
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   198
                if self.VariableName.FindString(value) != wx.NOT_FOUND:
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   199
                    self.VariableName.SetStringSelection(value)
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   200
                else:
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   201
                    self.VariableName.SetSelection(wx.NOT_FOUND)
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   202
            
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   203
            # Parameter is variable execution order
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   204
            elif name == "executionOrder":
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   205
                self.ExecutionOrder.SetValue(value)
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   206
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   207
        # Refresh preview panel
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   208
        self.RefreshPreview()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   209
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   210
    def GetValues(self):
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   211
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   212
        Return block parameters defined in dialog
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   213
        @return: {parameter_name: parameter_value,...}
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   214
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   215
        expression = self.Expression.GetValue()
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   216
        values = {
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   217
            "class": VARIABLE_CLASSES_DICT_REVERSE[
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   218
                        self.Class.GetStringSelection()],
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   219
            "expression": expression,
1246
101625efb1c1 Rewrite LDElementDialog
Laurent Bessard
parents: 1244
diff changeset
   220
            "var_type": self.VariableList.get(expression, (None, None))[1],
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   221
            "executionOrder": self.ExecutionOrder.GetValue()}
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   222
        values["width"], values["height"] = self.Element.GetSize()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   223
        return values
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   224
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   225
    def OnOK(self, event):
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   226
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   227
        Called when dialog OK button is pressed
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   228
        Test if parameters defined are valid
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   229
        @param event: wx.Event from OK button
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   230
        """
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   231
        message = None
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   232
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   233
        # Test that an expression have been selected or typed by user
1182
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   234
        value = self.Expression.GetValue()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   235
        if value == "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   236
            message = _("At least a variable or an expression must be selected!")
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   237
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   238
        # Show error message if an error is detected
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   239
        if message is not None:
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   240
            self.ShowErrorMessage(message)
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   241
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   242
        else:
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   243
            # Call BlockPreviewDialog function
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   244
            BlockPreviewDialog.OnOK(self, event)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   245
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   246
    def OnClassChanged(self, event):
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   247
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   248
        Called when variable class value changed
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   249
        @param event: wx.ComboBoxEvent
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   250
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   251
        # Refresh name list box values
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   252
        self.RefreshNameList()
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   253
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   254
        self.RefreshPreview()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   255
        event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   256
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   257
    def OnNameChanged(self, event):
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   258
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   259
        Called when name selected in name list box changed
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   260
        @param event: wx.ListBoxEvent
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   261
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   262
        # Change expression test control value to the value selected in name
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   263
        # list box if value selected is valid
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   264
        if self.VariableName.GetSelection() != wx.NOT_FOUND:
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   265
            self.Expression.ChangeValue(self.VariableName.GetStringSelection())
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   266
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   267
        self.RefreshPreview()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   268
        event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   269
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   270
    def OnExpressionChanged(self, event):
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   271
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   272
        Called when expression text control is changed by user
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   273
        @param event: wx.ListBoxEvent
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   274
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   275
        # Select the corresponding value in name list box if it exists
1182
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   276
        self.VariableName.SetSelection(
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   277
            self.VariableName.FindString(self.Expression.GetValue()))
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   278
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   279
        self.RefreshPreview()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   280
        event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   281
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   282
    def OnExecutionOrderChanged(self, event):
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   283
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   284
        Called when block execution control value changed
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   285
        @param event: wx.SpinEvent
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   286
        """
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   287
        self.RefreshPreview()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   288
        event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   289
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   290
    def RefreshPreview(self):
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   291
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   292
        Refresh preview panel of graphic element
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   293
        Override BlockPreviewDialog function
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   294
        """
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   295
        # Get expression value to put in FBD variable element
1182
208103fd9ad0 Improved FBDVariable dialog
Laurent Bessard
parents: 814
diff changeset
   296
        name = self.Expression.GetValue()
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   297
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   298
        # Set graphic element displayed, creating a FBD variable element
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   299
        self.Element = FBD_Variable(self.Preview, 
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   300
                    VARIABLE_CLASSES_DICT_REVERSE[
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   301
                        self.Class.GetStringSelection()], 
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   302
                    name, 
1246
101625efb1c1 Rewrite LDElementDialog
Laurent Bessard
parents: 1244
diff changeset
   303
                    self.VariableList.get(name, ("", ""))[1], 
1244
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   304
                    executionOrder = self.ExecutionOrder.GetValue())
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   305
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   306
        # Call BlockPreviewDialog function
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   307
        BlockPreviewDialog.RefreshPreview(self)
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   308
        
336d515096b1 Rewrite FBDVariableDialog
Laurent Bessard
parents: 1187
diff changeset
   309