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, allow_edit=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.allow_edit = allow_edit
     35          self.Bind(wx.EVT_CHOICE, self._on_choice)
     36          self._clear()
     37
     38 -    def Populate(self, exclude=None, select=None):
     39          self.exclude = exclude
     40          self._clear()
     41          self._populate_tree(self.category_repository.get_tree(remove=exclude))
     42          self.SetSelectedCategory(select)
     43
     44 -    def GetSelectedCategory(self):
     45          if self.GetSelection() == wx.NOT_FOUND:
     46              return None
     47          return self.GetClientData(self.GetSelection())
     48
     49 -    def SetSelectedCategory(self, category):
     50          self.SetSelection(self._get_index(category))
     51          self.current_category_selection = self.GetSelection()
     52
     53 -    def _clear(self):
     54          self.Clear()
     55          self.add_category_item_index = None
     56          self.edit_categoris_item_index = None
     57          self.last_real_category_index = None
     58          self.current_category_selection = self.GetSelection()
     59
     60 -    def _get_index(self, category):
     61          for index in range(self.GetCount()):
     62              if self.GetClientData(index) == category:
     63                  return index
     64          return 0
     65
     66 -    def _populate_tree(self, tree):
     67          self.Append("", None)
     68          self._append_tree(tree)
     69          self.last_real_category_index = self.GetCount() - 1
     70          if self.allow_add or self.allow_edit:
     71              self.Append("", None)
     72          if self.allow_add:
     73              self.add_category_item_index = self.GetCount()
     74              self.Append(_("Add new"), None)
     75          if self.allow_edit:
     76              self.edit_categoris_item_index = self.GetCount()
     77              self.Append(_("Edit categories"), None)
     78
     79 -    def _append_tree(self, tree, indent=""):
     80          for (category, subtree) in tree:
     81              self.Append(indent + category.name, category)
     82              self._append_tree(subtree, indent + "    ")
     83
     84 -    def _on_choice(self, event):
     85          new_selection_index = event.GetSelection()
     86          if new_selection_index > self.last_real_category_index:
     87              self.SetSelection(self.current_category_selection)
     88              if new_selection_index == self.add_category_item_index:
     89                  self._add_category()
     90              elif new_selection_index == self.edit_categoris_item_index:
     91                  self._edit_categories()
     92          else:
     93              self.current_category_selection = new_selection_index
     94
     95 -    def _add_category(self):
     96          dialog = EditCategoryDialog(self,
     97                                      _("Add Category"),
     98                                      self.timeline,
     99                                      None)
    100          if dialog.ShowModal() == wx.ID_OK:
    101              self.Populate(select=dialog.GetEditedCategory(),
    102                            exclude=self.exclude)
    103          dialog.Destroy()
    104
    105 -    def _edit_categories(self):
    106          gui_utils.display_categories_editor_moved_message(self)
    107