Package Gnumed :: Package timelinelib :: Package wxgui :: Package components :: Module categorychoice
[frames] | no frames]

Source Code for Module Gnumed.timelinelib.wxgui.components.categorychoice

 1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg 
 2  # 
 3  # This file is part of Timeline. 
 4  # 
 5  # Timeline is free software: you can redistribute it and/or modify 
 6  # it under the terms of the GNU General Public License as published by 
 7  # the Free Software Foundation, either version 3 of the License, or 
 8  # (at your option) any later version. 
 9  # 
10  # Timeline is distributed in the hope that it will be useful, 
11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
13  # GNU General Public License for more details. 
14  # 
15  # You should have received a copy of the GNU General Public License 
16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>. 
17   
18   
19  import wx 
20   
21  from timelinelib.canvas.data.exceptions import TimelineIOError 
22  from timelinelib.repositories.dbwrapper import DbWrapperCategoryRepository 
23  from timelinelib.wxgui.dialogs.editcategory.view import EditCategoryDialog 
24  import timelinelib.wxgui.utils as gui_utils 
25   
26   
27 -class CategoryChoice(wx.Choice):
28
29 - def __init__(self, parent, timeline, allow_add=False, **kwargs):
30 wx.Choice.__init__(self, parent, wx.ID_ANY, **kwargs) 31 self.timeline = timeline 32 self.category_repository = DbWrapperCategoryRepository(self.timeline) 33 self.allow_add = allow_add 34 self.Bind(wx.EVT_CHOICE, self._on_choice) 35 self._clear()
36
37 - def Populate(self, exclude=None, select=None):
38 self.exclude = exclude 39 self._clear() 40 self._populate_tree(self.category_repository.get_tree(remove=exclude)) 41 self.SetSelectedCategory(select)
42
43 - def GetSelectedCategory(self):
44 if self.GetSelection() == wx.NOT_FOUND: 45 return None 46 return self.GetClientData(self.GetSelection())
47
48 - def SetSelectedCategory(self, category):
49 self.SetSelection(self._get_index(category)) 50 self.current_category_selection = self.GetSelection()
51
52 - def _clear(self):
53 self.Clear() 54 self.add_category_item_index = None 55 self.edit_categoris_item_index = None 56 self.last_real_category_index = None 57 self.current_category_selection = self.GetSelection()
58
59 - def _get_index(self, category):
60 for index in range(self.GetCount()): 61 if self.GetClientData(index) == category: 62 return index 63 return 0
64
65 - def _populate_tree(self, tree):
66 self.Append("", None) 67 self._append_tree(tree) 68 self.last_real_category_index = self.GetCount() - 1 69 if self.allow_add: 70 self.Append("", None) 71 if self.allow_add: 72 self.add_category_item_index = self.GetCount() 73 self.Append(_("Add new"), None)
74
75 - def _append_tree(self, tree, indent=""):
76 for (category, subtree) in tree: 77 self.Append(indent + category.name, category) 78 self._append_tree(subtree, indent + " ")
79
80 - def _on_choice(self, event):
81 new_selection_index = event.GetSelection() 82 if new_selection_index > self.last_real_category_index: 83 self.SetSelection(self.current_category_selection) 84 if new_selection_index == self.add_category_item_index: 85 self._add_category() 86 else: 87 self.current_category_selection = new_selection_index
88
89 - def _add_category(self):
90 dialog = EditCategoryDialog(self, _("Add Category"), self.timeline, None) 91 if dialog.ShowModal() == wx.ID_OK: 92 self.Populate(select=dialog.GetEditedCategory(), exclude=self.exclude) 93 dialog.Destroy()
94