dialogs/BrowseLocationsDialog.py
changeset 1730 64d8f52bc8c8
parent 1696 8043f32de7b8
child 1736 7e61baa047f0
equal deleted inserted replaced
1726:d51af006fa6b 1730:64d8f52bc8c8
    35 #                                   Helpers
    35 #                                   Helpers
    36 #-------------------------------------------------------------------------------
    36 #-------------------------------------------------------------------------------
    37 
    37 
    38 def GetDirFilterChoiceOptions():
    38 def GetDirFilterChoiceOptions():
    39     _ = lambda x : x
    39     _ = lambda x : x
    40     return [(_("All"), [LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY]), 
    40     return [(_("All"), [LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY]),
    41             (_("Input"), [LOCATION_VAR_INPUT]), 
    41             (_("Input"), [LOCATION_VAR_INPUT]),
    42             (_("Output"), [LOCATION_VAR_OUTPUT]), 
    42             (_("Output"), [LOCATION_VAR_OUTPUT]),
    43             (_("Memory"), [LOCATION_VAR_MEMORY])]
    43             (_("Memory"), [LOCATION_VAR_MEMORY])]
    44 DIRFILTERCHOICE_OPTIONS = dict([(_(option), filter) for option, filter in GetDirFilterChoiceOptions()])
    44 DIRFILTERCHOICE_OPTIONS = dict([(_(option), filter) for option, filter in GetDirFilterChoiceOptions()])
    45 
    45 
    46 def GetTypeFilterChoiceOptions():
    46 def GetTypeFilterChoiceOptions():
    47     _ = lambda x : x
    47     _ = lambda x : x
    48     return [_("All"), 
    48     return [_("All"),
    49             _("Type and derivated"), 
    49             _("Type and derivated"),
    50             _("Type strict")]
    50             _("Type strict")]
    51 
    51 
    52 # turn LOCATIONDATATYPES inside-out
    52 # turn LOCATIONDATATYPES inside-out
    53 LOCATION_SIZES = {}
    53 LOCATION_SIZES = {}
    54 for size, types in LOCATIONDATATYPES.iteritems():
    54 for size, types in LOCATIONDATATYPES.iteritems():
    58 #-------------------------------------------------------------------------------
    58 #-------------------------------------------------------------------------------
    59 #                            Browse Locations Dialog
    59 #                            Browse Locations Dialog
    60 #-------------------------------------------------------------------------------
    60 #-------------------------------------------------------------------------------
    61 
    61 
    62 class BrowseLocationsDialog(wx.Dialog):
    62 class BrowseLocationsDialog(wx.Dialog):
    63     
    63 
    64     def __init__(self, parent, var_type, controller):
    64     def __init__(self, parent, var_type, controller):
    65         wx.Dialog.__init__(self, parent, title=_('Browse Locations'),
    65         wx.Dialog.__init__(self, parent, title=_('Browse Locations'),
    66               style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
    66               style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
    67         
    67 
    68         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
    68         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
    69         main_sizer.AddGrowableCol(0)
    69         main_sizer.AddGrowableCol(0)
    70         main_sizer.AddGrowableRow(1)
    70         main_sizer.AddGrowableRow(1)
    71         
    71 
    72         locations_label = wx.StaticText(self, label=_('Locations available:'))
    72         locations_label = wx.StaticText(self, label=_('Locations available:'))
    73         main_sizer.AddWindow(locations_label, border=20, 
    73         main_sizer.AddWindow(locations_label, border=20,
    74               flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW)
    74               flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW)
    75         
    75 
    76         self.LocationsTree = wx.TreeCtrl(self, 
    76         self.LocationsTree = wx.TreeCtrl(self,
    77               style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER|wx.TR_HIDE_ROOT|wx.TR_LINES_AT_ROOT)
    77               style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER|wx.TR_HIDE_ROOT|wx.TR_LINES_AT_ROOT)
    78         self.LocationsTree.SetInitialSize(wx.Size(-1, 300))
    78         self.LocationsTree.SetInitialSize(wx.Size(-1, 300))
    79         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnLocationsTreeItemActivated, 
    79         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnLocationsTreeItemActivated,
    80                   self.LocationsTree)
    80                   self.LocationsTree)
    81         main_sizer.AddWindow(self.LocationsTree, border=20, 
    81         main_sizer.AddWindow(self.LocationsTree, border=20,
    82               flag=wx.LEFT|wx.RIGHT|wx.GROW)
    82               flag=wx.LEFT|wx.RIGHT|wx.GROW)
    83         
    83 
    84         button_gridsizer = wx.FlexGridSizer(cols=5, hgap=5, rows=1, vgap=0)
    84         button_gridsizer = wx.FlexGridSizer(cols=5, hgap=5, rows=1, vgap=0)
    85         button_gridsizer.AddGrowableCol(1)
    85         button_gridsizer.AddGrowableCol(1)
    86         button_gridsizer.AddGrowableCol(3)
    86         button_gridsizer.AddGrowableCol(3)
    87         button_gridsizer.AddGrowableRow(0)
    87         button_gridsizer.AddGrowableRow(0)
    88         main_sizer.AddSizer(button_gridsizer, border=20, 
    88         main_sizer.AddSizer(button_gridsizer, border=20,
    89               flag=wx.BOTTOM|wx.LEFT|wx.RIGHT|wx.GROW)
    89               flag=wx.BOTTOM|wx.LEFT|wx.RIGHT|wx.GROW)
    90         
    90 
    91         direction_label = wx.StaticText(self, label=_('Direction:'))
    91         direction_label = wx.StaticText(self, label=_('Direction:'))
    92         button_gridsizer.AddWindow(direction_label,
    92         button_gridsizer.AddWindow(direction_label,
    93               flag=wx.ALIGN_CENTER_VERTICAL)
    93               flag=wx.ALIGN_CENTER_VERTICAL)
    94         
    94 
    95         self.DirFilterChoice = wx.ComboBox(self, style=wx.CB_READONLY)
    95         self.DirFilterChoice = wx.ComboBox(self, style=wx.CB_READONLY)
    96         self.Bind(wx.EVT_COMBOBOX, self.OnFilterChoice, self.DirFilterChoice)
    96         self.Bind(wx.EVT_COMBOBOX, self.OnFilterChoice, self.DirFilterChoice)
    97         button_gridsizer.AddWindow(self.DirFilterChoice,
    97         button_gridsizer.AddWindow(self.DirFilterChoice,
    98               flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL)
    98               flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL)
    99         
    99 
   100         filter_label = wx.StaticText(self, label=_('Type:'))
   100         filter_label = wx.StaticText(self, label=_('Type:'))
   101         button_gridsizer.AddWindow(filter_label,
   101         button_gridsizer.AddWindow(filter_label,
   102               flag=wx.ALIGN_CENTER_VERTICAL)
   102               flag=wx.ALIGN_CENTER_VERTICAL)
   103         
   103 
   104         self.TypeFilterChoice = wx.ComboBox(self, style=wx.CB_READONLY)
   104         self.TypeFilterChoice = wx.ComboBox(self, style=wx.CB_READONLY)
   105         self.Bind(wx.EVT_COMBOBOX, self.OnFilterChoice, self.TypeFilterChoice)
   105         self.Bind(wx.EVT_COMBOBOX, self.OnFilterChoice, self.TypeFilterChoice)
   106         button_gridsizer.AddWindow(self.TypeFilterChoice,
   106         button_gridsizer.AddWindow(self.TypeFilterChoice,
   107               flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL)
   107               flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL)
   108         
   108 
   109         button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
   109         button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
   110         self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
   110         self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
   111         button_gridsizer.AddSizer(button_sizer, flag=wx.ALIGN_RIGHT)
   111         button_gridsizer.AddSizer(button_sizer, flag=wx.ALIGN_RIGHT)
   112         
   112 
   113         self.SetSizer(main_sizer)
   113         self.SetSizer(main_sizer)
   114         
   114 
   115         self.Controller = controller
   115         self.Controller = controller
   116         self.VarType = var_type
   116         self.VarType = var_type
   117         self.BaseVarType = self.Controller.GetBaseType(self.VarType)
   117         self.BaseVarType = self.Controller.GetBaseType(self.VarType)
   118         self.VarTypeSize = LOCATION_SIZES[self.BaseVarType]
   118         self.VarTypeSize = LOCATION_SIZES[self.BaseVarType]
   119         self.Locations = self.Controller.GetVariableLocationTree()
   119         self.Locations = self.Controller.GetVariableLocationTree()
   120         
   120 
   121         # Define Tree item icon list
   121         # Define Tree item icon list
   122         self.TreeImageList = wx.ImageList(16, 16)
   122         self.TreeImageList = wx.ImageList(16, 16)
   123         self.TreeImageDict = {}
   123         self.TreeImageDict = {}
   124         
   124 
   125         # Icons for items
   125         # Icons for items
   126         for imgname, itemtype in [
   126         for imgname, itemtype in [
   127             ("CONFIGURATION", LOCATION_CONFNODE), 
   127             ("CONFIGURATION", LOCATION_CONFNODE),
   128             ("RESOURCE",      LOCATION_MODULE), 
   128             ("RESOURCE",      LOCATION_MODULE),
   129             ("PROGRAM",       LOCATION_GROUP), 
   129             ("PROGRAM",       LOCATION_GROUP),
   130             ("VAR_INPUT",     LOCATION_VAR_INPUT), 
   130             ("VAR_INPUT",     LOCATION_VAR_INPUT),
   131             ("VAR_OUTPUT",    LOCATION_VAR_OUTPUT), 
   131             ("VAR_OUTPUT",    LOCATION_VAR_OUTPUT),
   132             ("VAR_LOCAL",     LOCATION_VAR_MEMORY)]:
   132             ("VAR_LOCAL",     LOCATION_VAR_MEMORY)]:
   133             self.TreeImageDict[itemtype]=self.TreeImageList.Add(GetBitmap(imgname))
   133             self.TreeImageDict[itemtype]=self.TreeImageList.Add(GetBitmap(imgname))
   134         
   134 
   135         # Assign icon list to TreeCtrls
   135         # Assign icon list to TreeCtrls
   136         self.LocationsTree.SetImageList(self.TreeImageList)
   136         self.LocationsTree.SetImageList(self.TreeImageList)
   137         
   137 
   138         # Set a options for the choice
   138         # Set a options for the choice
   139         for option, filter in GetDirFilterChoiceOptions():
   139         for option, filter in GetDirFilterChoiceOptions():
   140             self.DirFilterChoice.Append(_(option))
   140             self.DirFilterChoice.Append(_(option))
   141         self.DirFilterChoice.SetStringSelection(_("All"))
   141         self.DirFilterChoice.SetStringSelection(_("All"))
   142         for option in GetTypeFilterChoiceOptions():
   142         for option in GetTypeFilterChoiceOptions():
   143             self.TypeFilterChoice.Append(_(option))
   143             self.TypeFilterChoice.Append(_(option))
   144         self.TypeFilterChoice.SetStringSelection(_("All"))
   144         self.TypeFilterChoice.SetStringSelection(_("All"))
   145         self.RefreshFilters()
   145         self.RefreshFilters()
   146         
   146 
   147         self.RefreshLocationsTree()
   147         self.RefreshLocationsTree()
   148         self.Fit()
   148         self.Fit()
   149     
   149 
   150     def RefreshFilters(self):
   150     def RefreshFilters(self):
   151         self.DirFilter = DIRFILTERCHOICE_OPTIONS[self.DirFilterChoice.GetStringSelection()]
   151         self.DirFilter = DIRFILTERCHOICE_OPTIONS[self.DirFilterChoice.GetStringSelection()]
   152         self.TypeFilter = self.TypeFilterChoice.GetSelection()
   152         self.TypeFilter = self.TypeFilterChoice.GetSelection()
   153     
   153 
   154     def RefreshLocationsTree(self):
   154     def RefreshLocationsTree(self):
   155         root = self.LocationsTree.GetRootItem()
   155         root = self.LocationsTree.GetRootItem()
   156         if not root.IsOk():
   156         if not root.IsOk():
   157             root = self.LocationsTree.AddRoot("")
   157             root = self.LocationsTree.AddRoot("")
   158         self.GenerateLocationsTreeBranch(root, self.Locations)
   158         self.GenerateLocationsTreeBranch(root, self.Locations)
   159     
   159 
   160     def FilterType(self, location_type, location_size):
   160     def FilterType(self, location_type, location_size):
   161         if self.TypeFilter == 0:
   161         if self.TypeFilter == 0:
   162             return True
   162             return True
   163         
   163 
   164         if location_size != self.VarTypeSize:
   164         if location_size != self.VarTypeSize:
   165             return False
   165             return False
   166         
   166 
   167         if self.TypeFilter == 1:
   167         if self.TypeFilter == 1:
   168             return self.Controller.IsOfType(location_type, self.BaseVarType)
   168             return self.Controller.IsOfType(location_type, self.BaseVarType)
   169         elif self.TypeFilter == 2:
   169         elif self.TypeFilter == 2:
   170             return location_type == self.VarType
   170             return location_type == self.VarType
   171         
   171 
   172         return True
   172         return True
   173     
   173 
   174     def GenerateLocationsTreeBranch(self, root, locations):
   174     def GenerateLocationsTreeBranch(self, root, locations):
   175         to_delete = []
   175         to_delete = []
   176         item, root_cookie = self.LocationsTree.GetFirstChild(root)
   176         item, root_cookie = self.LocationsTree.GetFirstChild(root)
   177         for loc_infos in locations:
   177         for loc_infos in locations:
   178             infos = loc_infos.copy()
   178             infos = loc_infos.copy()
   192         while item.IsOk():
   192         while item.IsOk():
   193             to_delete.append(item)
   193             to_delete.append(item)
   194             item, root_cookie = self.LocationsTree.GetNextChild(root, root_cookie)
   194             item, root_cookie = self.LocationsTree.GetNextChild(root, root_cookie)
   195         for item in to_delete:
   195         for item in to_delete:
   196             self.LocationsTree.Delete(item)
   196             self.LocationsTree.Delete(item)
   197     
   197 
   198     def OnLocationsTreeItemActivated(self, event):
   198     def OnLocationsTreeItemActivated(self, event):
   199         infos = self.LocationsTree.GetPyData(event.GetItem())
   199         infos = self.LocationsTree.GetPyData(event.GetItem())
   200         if infos["type"] not in [LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP]:
   200         if infos["type"] not in [LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP]:
   201             wx.CallAfter(self.EndModal, wx.ID_OK)
   201             wx.CallAfter(self.EndModal, wx.ID_OK)
   202         event.Skip()
   202         event.Skip()
   203     
   203 
   204     def OnFilterChoice(self, event):
   204     def OnFilterChoice(self, event):
   205         self.RefreshFilters()
   205         self.RefreshFilters()
   206         self.RefreshLocationsTree()
   206         self.RefreshLocationsTree()
   207     
   207 
   208     def GetValues(self):
   208     def GetValues(self):
   209         selected = self.LocationsTree.GetSelection()
   209         selected = self.LocationsTree.GetSelection()
   210         return self.LocationsTree.GetPyData(selected)
   210         return self.LocationsTree.GetPyData(selected)
   211         
   211 
   212     def OnOK(self, event):
   212     def OnOK(self, event):
   213         selected = self.LocationsTree.GetSelection()
   213         selected = self.LocationsTree.GetSelection()
   214         var_infos = None
   214         var_infos = None
   215         if selected.IsOk():
   215         if selected.IsOk():
   216             var_infos = self.LocationsTree.GetPyData(selected)
   216             var_infos = self.LocationsTree.GetPyData(selected)