Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
authorLaurent Bessard
Thu, 18 Oct 2012 01:22:52 +0200
changeset 855 b30421d07e8c
parent 854 c10f2092c43a
child 856 b64e436f000e
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
controls/LocationCellEditor.py
dialogs/BrowseLocationsDialog.py
--- a/controls/LocationCellEditor.py	Tue Oct 16 19:01:17 2012 +0200
+++ b/controls/LocationCellEditor.py	Thu Oct 18 01:22:52 2012 +0200
@@ -54,16 +54,22 @@
         
         self.SetSizer(main_sizer)
 
-        self.Locations = None
+        self.Controller = None
         self.VarType = None
         self.Default = False
 
-    def SetLocations(self, locations):
-        self.Locations = locations
+    def __del__(self):
+        self.Controller = None
+
+    def SetController(self, controller):
+        self.Controller = controller
 
     def SetVarType(self, vartype):
         self.VarType = vartype
 
+    def GetVarType(self):
+        return self.VarType
+
     def SetValue(self, value):
         self.Default = value
         self.Location.SetValue(value)
@@ -76,12 +82,13 @@
 
     def OnBrowseButtonClick(self, event):
         # pop up the location browser dialog
-        dialog = BrowseLocationsDialog(self, self.VarType, self.Locations)
+        dialog = BrowseLocationsDialog(self, self.VarType, self.Controller)
         if dialog.ShowModal() == wx.ID_OK:
             infos = dialog.GetValues()
 
             # set the location
             self.Location.SetValue(infos["location"])
+            self.VarType = infos["IEC_type"]
 
         dialog.Destroy()
 
@@ -115,6 +122,7 @@
 
     def __del__(self):
         self.CellControl = None
+        self.Controller = None
     
     def Create(self, parent, id, evt_handler):
         self.CellControl = LocationCellControl(parent)
@@ -124,10 +132,10 @@
 
     def BeginEdit(self, row, col, grid):
         self.CellControl.Enable()
-        self.CellControl.SetLocations(self.Controller.GetVariableLocationTree())
+        self.CellControl.SetController(self.Controller)
         self.CellControl.SetValue(self.Table.GetValueByName(row, 'Location'))
         if isinstance(self.CellControl, LocationCellControl):
-            self.CellControl.SetVarType(self.Controller.GetBaseType(self.Table.GetValueByName(row, 'Type')))
+            self.CellControl.SetVarType(self.Table.GetValueByName(row, 'Type'))
         self.CellControl.SetFocus()
 
     def EndEdit(self, row, col, grid):
@@ -136,6 +144,7 @@
         changed = loc != old_loc
         if changed:
             self.Table.SetValueByName(row, 'Location', loc)
+            self.Table.SetValueByName(row, 'Type', self.CellControl.GetVarType())
         self.CellControl.Disable()
         return changed
     
--- a/dialogs/BrowseLocationsDialog.py	Tue Oct 16 19:01:17 2012 +0200
+++ b/dialogs/BrowseLocationsDialog.py	Thu Oct 18 01:22:52 2012 +0200
@@ -29,13 +29,19 @@
 #                                   Helpers
 #-------------------------------------------------------------------------------
 
-def GetDirChoiceOptions():
+def GetDirFilterChoiceOptions():
     _ = lambda x : x
     return [(_("All"), [LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY]), 
             (_("Input"), [LOCATION_VAR_INPUT]), 
             (_("Output"), [LOCATION_VAR_OUTPUT]), 
             (_("Memory"), [LOCATION_VAR_MEMORY])]
-DIRCHOICE_OPTIONS_FILTER = dict([(_(option), filter) for option, filter in GetDirChoiceOptions()])
+DIRFILTERCHOICE_OPTIONS = dict([(_(option), filter) for option, filter in GetDirFilterChoiceOptions()])
+
+def GetTypeFilterChoiceOptions():
+    _ = lambda x : x
+    return [_("All"), 
+            _("Type and derivated"), 
+            _("Type strict")]
 
 # turn LOCATIONDATATYPES inside-out
 LOCATION_SIZES = {}
@@ -49,9 +55,10 @@
 
 class BrowseLocationsDialog(wx.Dialog):
     
-    def __init__(self, parent, var_type, locations):
+    def __init__(self, parent, var_type, controller):
         wx.Dialog.__init__(self, parent,  
-              size=wx.Size(600, 400), title=_('Browse Locations'))
+              size=wx.Size(600, 400), title=_('Browse Locations'),
+              style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
         
         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
         main_sizer.AddGrowableCol(0)
@@ -68,8 +75,9 @@
         main_sizer.AddWindow(self.LocationsTree, border=20, 
               flag=wx.LEFT|wx.RIGHT|wx.GROW)
         
-        button_gridsizer = wx.FlexGridSizer(cols=3, hgap=5, rows=1, vgap=0)
-        button_gridsizer.AddGrowableCol(2)
+        button_gridsizer = wx.FlexGridSizer(cols=5, hgap=5, rows=1, vgap=0)
+        button_gridsizer.AddGrowableCol(1)
+        button_gridsizer.AddGrowableCol(3)
         button_gridsizer.AddGrowableRow(0)
         main_sizer.AddSizer(button_gridsizer, border=20, 
               flag=wx.BOTTOM|wx.LEFT|wx.RIGHT|wx.GROW)
@@ -78,19 +86,31 @@
         button_gridsizer.AddWindow(direction_label,
               flag=wx.ALIGN_CENTER_VERTICAL)
         
-        self.DirChoice = wx.ComboBox(self, style=wx.CB_READONLY)
-        self.Bind(wx.EVT_COMBOBOX, self.OnDirChoice, self.DirChoice)
-        button_gridsizer.AddWindow(self.DirChoice,
+        self.DirFilterChoice = wx.ComboBox(self, size=wx.Size(0, -1), style=wx.CB_READONLY)
+        self.Bind(wx.EVT_COMBOBOX, self.OnFilterChoice, self.DirFilterChoice)
+        button_gridsizer.AddWindow(self.DirFilterChoice,
+              flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL)
+        
+        filter_label = wx.StaticText(self, label=_('Type:'))
+        button_gridsizer.AddWindow(filter_label,
               flag=wx.ALIGN_CENTER_VERTICAL)
         
+        self.TypeFilterChoice = wx.ComboBox(self, size=wx.Size(0, -1), style=wx.CB_READONLY)
+        self.Bind(wx.EVT_COMBOBOX, self.OnFilterChoice, self.TypeFilterChoice)
+        button_gridsizer.AddWindow(self.TypeFilterChoice,
+              flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL)
+        
         button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
         self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
-        button_gridsizer.AddWindow(button_sizer, flag=wx.ALIGN_RIGHT)
+        button_gridsizer.AddSizer(button_sizer, flag=wx.ALIGN_RIGHT)
         
         self.SetSizer(main_sizer)
         
+        self.Controller = controller
         self.VarType = var_type
-        self.Locations = locations
+        self.BaseVarType = self.Controller.GetBaseType(self.VarType)
+        self.VarTypeSize = LOCATION_SIZES[self.BaseVarType]
+        self.Locations = self.Controller.GetVariableLocationTree()
         
         # Define Tree item icon list
         self.TreeImageList = wx.ImageList(16, 16)
@@ -110,15 +130,19 @@
         self.LocationsTree.SetImageList(self.TreeImageList)
         
         # Set a options for the choice
-        for option, filter in GetDirChoiceOptions():
-            self.DirChoice.Append(_(option))
-        self.DirChoice.SetStringSelection(_("All"))
-        self.RefreshFilter()
+        for option, filter in GetDirFilterChoiceOptions():
+            self.DirFilterChoice.Append(_(option))
+        self.DirFilterChoice.SetStringSelection(_("All"))
+        for option in GetTypeFilterChoiceOptions():
+            self.TypeFilterChoice.Append(_(option))
+        self.TypeFilterChoice.SetStringSelection(_("All"))
+        self.RefreshFilters()
         
         self.RefreshLocationsTree()
     
-    def RefreshFilter(self):
-        self.Filter = DIRCHOICE_OPTIONS_FILTER[self.DirChoice.GetStringSelection()]
+    def RefreshFilters(self):
+        self.DirFilter = DIRFILTERCHOICE_OPTIONS[self.DirFilterChoice.GetStringSelection()]
+        self.TypeFilter = self.TypeFilterChoice.GetSelection()
     
     def RefreshLocationsTree(self):
         root = self.LocationsTree.GetRootItem()
@@ -126,14 +150,27 @@
             root = self.LocationsTree.AddRoot("")
         self.GenerateLocationsTreeBranch(root, self.Locations)
     
+    def FilterType(self, location_type, location_size):
+        if self.TypeFilter == 0:
+            return True
+        
+        if location_size != self.VarTypeSize:
+            return False
+        
+        if self.TypeFilter == 1:
+            return self.Controller.IsOfType(location_type, self.BaseVarType)
+        elif self.TypeFilter == 2:
+            return location_type == self.VarType
+        
+        return True
+    
     def GenerateLocationsTreeBranch(self, root, locations):
         to_delete = []
         item, root_cookie = self.LocationsTree.GetFirstChild(root)
         for loc_infos in locations:
             infos = loc_infos.copy()
             if infos["type"] in [LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP] or\
-               infos["type"] in self.Filter and (infos["IEC_type"] == self.VarType or
-               infos["IEC_type"] is None and LOCATION_SIZES[self.VarType] == infos["size"]):
+               infos["type"] in self.DirFilter and self.FilterType(infos["IEC_type"], infos["size"]):
                 children = [child for child in infos.pop("children")]
                 if not item.IsOk():
                     item = self.LocationsTree.AppendItem(root, infos["name"])
@@ -157,10 +194,10 @@
             wx.CallAfter(self.EndModal, wx.ID_OK)
         event.Skip()
     
-    def OnDirChoice(self, event):
-        self.RefreshFilter()
+    def OnFilterChoice(self, event):
+        self.RefreshFilters()
         self.RefreshLocationsTree()
-        
+    
     def GetValues(self):
         selected = self.LocationsTree.GetSelection()
         return self.LocationsTree.GetPyData(selected)