VariablePanel.py
changeset 484 acef952101a5
parent 483 779a519f78f2
child 491 d22a4a95fd5e
equal deleted inserted replaced
483:779a519f78f2 484:acef952101a5
    50     _ = lambda x : x
    50     _ = lambda x : x
    51     if location:
    51     if location:
    52     	return ["#", _("Name"), _("Class"), _("Type"), _("Location"), _("Initial Value"), _("Option"), _("Documentation")]
    52     	return ["#", _("Name"), _("Class"), _("Type"), _("Location"), _("Initial Value"), _("Option"), _("Documentation")]
    53     return ["#", _("Name"), _("Class"), _("Type"), _("Initial Value"), _("Option"), _("Documentation")]
    53     return ["#", _("Name"), _("Class"), _("Type"), _("Initial Value"), _("Option"), _("Documentation")]
    54 
    54 
    55 def GetOptions():
    55 def GetOptions(constant=True, retain=True, non_retain=True):
    56     _ = lambda x : x
    56     _ = lambda x : x
    57     return ["", _("Constant"), _("Retain"), _("Non-Retain")]
    57     options = [""]
       
    58     if constant:
       
    59         options.append(_("Constant"))
       
    60     if retain:
       
    61         options.append(_("Retain"))
       
    62     if non_retain:
       
    63         options.append(_("Non-Retain"))
       
    64     return options
    58 OPTIONS_DICT = dict([(_(option), option) for option in GetOptions()])
    65 OPTIONS_DICT = dict([(_(option), option) for option in GetOptions()])
    59 
    66 
    60 def GetFilterChoiceTransfer():
    67 def GetFilterChoiceTransfer():
    61     _ = lambda x : x
    68     _ = lambda x : x
    62     return {_("All"): _("All"), _("Interface"): _("Interface"), 
    69     return {_("All"): _("All"), _("Interface"): _("Interface"), 
    63             _("   Input"): _("Input"), _("   Output"): _("Output"), _("   InOut"): _("InOut"), 
    70             _("   Input"): _("Input"), _("   Output"): _("Output"), _("   InOut"): _("InOut"), 
    64             _("   External"): _("External"), _("Variables"): _("Variables"), _("   Local"): _("Local"),
    71             _("   External"): _("External"), _("Variables"): _("Variables"), _("   Local"): _("Local"),
    65             _("   Temp"): _("Temp"), _("Global"): _("Global")}#, _("Access") : _("Access")}
    72             _("   Temp"): _("Temp"), _("Global"): _("Global")}#, _("Access") : _("Access")}
    66 VARIABLE_CLASSES_DICT = dict([(_(_class), _class) for _class in GetFilterChoiceTransfer().itervalues()])
    73 VARIABLE_CLASSES_DICT = dict([(_(_class), _class) for _class in GetFilterChoiceTransfer().itervalues()])
       
    74 
       
    75 CheckOptionForClass = {"Local": lambda x: x,
       
    76                        "Temp": lambda x: "",
       
    77                        "Input": lambda x: {"Retain": "Retain", "Non-Retain": "Non-Retain"}.get(x, ""),
       
    78                        "InOut": lambda x: "",
       
    79                        "Output": lambda x: {"Retain": "Retain", "Non-Retain": "Non-Retain"}.get(x, ""),
       
    80                        "Global": lambda x: {"Constant": "Constant", "Retain": "Retain"}.get(x, ""),
       
    81                        "External": lambda x: {"Constant": "Constant"}.get(x, "")
       
    82                       }
    67 
    83 
    68 class VariableTable(wx.grid.PyGridTableBase):
    84 class VariableTable(wx.grid.PyGridTableBase):
    69     
    85     
    70     """
    86     """
    71     A custom wx.grid.Grid Table using user supplied data
    87     A custom wx.grid.Grid Table using user supplied data
   114             colname = self.GetColLabelValue(col, False)
   130             colname = self.GetColLabelValue(col, False)
   115             if colname == "Name":
   131             if colname == "Name":
   116                 self.old_value = self.data[row][colname]
   132                 self.old_value = self.data[row][colname]
   117             elif colname == "Class":
   133             elif colname == "Class":
   118                 value = VARIABLE_CLASSES_DICT[value]
   134                 value = VARIABLE_CLASSES_DICT[value]
       
   135                 self.SetValueByName(row, "Option", CheckOptionForClass[value](self.GetValueByName(row, "Option")))
       
   136                 if value == "External":
       
   137                     self.SetValueByName(row, "Initial Value", "")
   119             elif colname == "Option":
   138             elif colname == "Option":
   120                 value = OPTIONS_DICT[value]
   139                 value = OPTIONS_DICT[value]
   121             self.data[row][colname] = value
   140             self.data[row][colname] = value
   122     
   141     
   123     def GetValueByName(self, row, colname):
   142     def GetValueByName(self, row, colname):
   171         appropriate renderer given the column name.
   190         appropriate renderer given the column name.
   172 
   191 
   173         Otherwise default to the default renderer.
   192         Otherwise default to the default renderer.
   174         """
   193         """
   175         for row in range(self.GetNumberRows()):
   194         for row in range(self.GetNumberRows()):
       
   195             var_class = self.GetValueByName(row, "Class")
   176             for col in range(self.GetNumberCols()):
   196             for col in range(self.GetNumberCols()):
   177                 editor = None
   197                 editor = None
   178                 renderer = None
   198                 renderer = None
   179                 colname = self.GetColLabelValue(col, False)
   199                 colname = self.GetColLabelValue(col, False)
   180                 if colname == "Option":
   200                 if colname == "Option":
   181                     editor = wx.grid.GridCellChoiceEditor()
   201                     options = GetOptions(constant = var_class in ["Local", "External", "Global"],
   182                     editor.SetParameters(",".join(map(_, self.Parent.OptionList)))
   202                                          retain = self.Parent.ElementType != "function" and var_class in ["Local", "Input", "Output", "Global"],
       
   203                                          non_retain = self.Parent.ElementType != "function" and var_class in ["Local", "Input", "Output"])
       
   204                     if len(options) > 1:
       
   205                         editor = wx.grid.GridCellChoiceEditor()
       
   206                         editor.SetParameters(",".join(map(_, options)))
       
   207                     else:
       
   208                         grid.SetReadOnly(row, col, True)
   183                 elif col != 0 and self.GetValueByName(row, "Edit"):
   209                 elif col != 0 and self.GetValueByName(row, "Edit"):
   184                     grid.SetReadOnly(row, col, False)
   210                     grid.SetReadOnly(row, col, False)
   185                     if colname == "Name":
   211                     if colname == "Name":
   186                         if self.Parent.PouIsUsed and self.GetValueByName(row, "Class") in ["Input", "Output", "InOut"]:
   212                         if self.Parent.PouIsUsed and var_class in ["Input", "Output", "InOut"]:
   187                             grid.SetReadOnly(row, col, True)
   213                             grid.SetReadOnly(row, col, True)
   188                         else:
   214                         else:
   189                             editor = wx.grid.GridCellTextEditor()
   215                             editor = wx.grid.GridCellTextEditor()
   190                             renderer = wx.grid.GridCellStringRenderer()
   216                             renderer = wx.grid.GridCellStringRenderer()
   191                     elif colname == "Initial Value":
   217                     elif colname == "Initial Value":
   192                         editor = wx.grid.GridCellTextEditor()
   218                         if var_class != "External":
   193                         renderer = wx.grid.GridCellStringRenderer()
   219                             editor = wx.grid.GridCellTextEditor()
       
   220                             renderer = wx.grid.GridCellStringRenderer()
       
   221                         else:
       
   222                             grid.SetReadOnly(row, col, True)
   194                     elif colname == "Location":
   223                     elif colname == "Location":
   195                         if self.GetValueByName(row, "Class") in ["Local", "Global"]:
   224                         if var_class in ["Local", "Global"]:
   196                             editor = LocationCellEditor(self, self.Parent.Controler)
   225                             editor = LocationCellEditor(self, self.Parent.Controler)
   197                             renderer = wx.grid.GridCellStringRenderer()
   226                             renderer = wx.grid.GridCellStringRenderer()
   198                         else:
   227                         else:
   199                             grid.SetReadOnly(row, col, True)
   228                             grid.SetReadOnly(row, col, True)
   200                     elif colname == "Class":
   229                     elif colname == "Class":
   201                         if len(self.Parent.ClassList) == 1 or self.Parent.PouIsUsed and self.GetValueByName(row, "Class") in ["Input", "Output", "InOut"]:
   230                         if len(self.Parent.ClassList) == 1 or self.Parent.PouIsUsed and var_class in ["Input", "Output", "InOut"]:
   202                             grid.SetReadOnly(row, col, True)
   231                             grid.SetReadOnly(row, col, True)
   203                         else:
   232                         else:
   204                             editor = wx.grid.GridCellChoiceEditor()
   233                             editor = wx.grid.GridCellChoiceEditor()
   205                             excluded = []
   234                             excluded = []
   206                             if self.Parent.PouIsUsed:
   235                             if self.Parent.PouIsUsed:
   518             self.Table = VariableTable(self, [], GetVariableTableColnames(False))
   547             self.Table = VariableTable(self, [], GetVariableTableColnames(False))
   519 
   548 
   520             if element_type == "function":
   549             if element_type == "function":
   521                 self.FilterChoices = ["All",
   550                 self.FilterChoices = ["All",
   522                                         "Interface", "   Input", "   Output", "   InOut",
   551                                         "Interface", "   Input", "   Output", "   InOut",
   523                                         "Variables", "   Local", "   Temp"]
   552                                         "Variables", "   Local"]
   524             else:
   553             else:
   525                 self.FilterChoices = ["All",
   554                 self.FilterChoices = ["All",
   526                                         "Interface", "   Input", "   Output", "   InOut", "   External",
   555                                         "Interface", "   Input", "   Output", "   InOut", "   External",
   527                                         "Variables", "   Local", "   Temp"]
   556                                         "Variables", "   Local", "   Temp"]
   528 
   557 
   541         for filter, choice in self.FilterChoiceTransfer.items():
   570         for filter, choice in self.FilterChoiceTransfer.items():
   542             reverse_transfer[choice] = filter
   571             reverse_transfer[choice] = filter
   543         self.ClassFilter.SetStringSelection(_(reverse_transfer[self.Filter]))
   572         self.ClassFilter.SetStringSelection(_(reverse_transfer[self.Filter]))
   544         self.RefreshTypeList()
   573         self.RefreshTypeList()
   545 
   574 
   546         self.OptionList = GetOptions()
       
   547         
       
   548         if element_type == "function":
   575         if element_type == "function":
   549             for base_type in self.Controler.GetBaseTypes():
   576             for base_type in self.Controler.GetBaseTypes():
   550                 self.ReturnType.Append(base_type)
   577                 self.ReturnType.Append(base_type)
   551             self.ReturnType.Enable(True)
   578             self.ReturnType.Enable(True)
   552         else:
   579         else:
   730 
   757 
   731             type_menu.AppendMenu(wx.NewId(), _("Base Types"), base_menu)
   758             type_menu.AppendMenu(wx.NewId(), _("Base Types"), base_menu)
   732 
   759 
   733             # build a submenu containing user-defined types
   760             # build a submenu containing user-defined types
   734             datatype_menu = wx.Menu(title='')
   761             datatype_menu = wx.Menu(title='')
   735             datatypes = self.Controler.GetDataTypes(basetypes = False)
   762             
       
   763             # TODO : remove complextypes argument when matiec can manage complex types in pou interface
       
   764             datatypes = self.Controler.GetDataTypes(basetypes = False, 
       
   765                                                     complextypes = self.Table.GetValueByName(row, "Class") not in ["Input", "Ouput", "InOut"])
   736             for datatype in datatypes:
   766             for datatype in datatypes:
   737                 new_id = wx.NewId()
   767                 new_id = wx.NewId()
   738                 AppendMenu(datatype_menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=datatype)
   768                 AppendMenu(datatype_menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=datatype)
   739                 self.Bind(wx.EVT_MENU, self.GetVariableTypeFunction(datatype), id=new_id)
   769                 self.Bind(wx.EVT_MENU, self.GetVariableTypeFunction(datatype), id=new_id)
   740 
   770