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  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.db.exceptions import TimelineIOError
     22  from timelinelib.db.objects.category import sort_categories
     23  from timelinelib.wxgui.dialogs.categorieseditor import CategoriesEditor
     24  from timelinelib.wxgui.dialogs.categoryeditor import WxCategoryEdtiorDialog
     25  import timelinelib.wxgui.utils as gui_utils
     26
     27
     28 -class CategoryChoice(wx.Choice):
     29
     30 -    def __init__(self, parent, timeline):
     31          wx.Choice.__init__(self, parent, wx.ID_ANY)
     32          self.timeline = timeline
     33
     34 -    def select(self, select_category):
     35          # We can not do error handling here since this method is also called
     36          # from the constructor (and then error handling is done by the code
     37          # calling the constructor).
     38          self.Clear()
     39          self.Append("", None) # The None-category
     40          selection_set = False
     41          current_item_index = 1
     42          for cat in sort_categories(self.timeline.get_categories()):
     43              self.Append(cat.name, cat)
     44              if cat == select_category:
     45                  self.SetSelection(current_item_index)
     46                  selection_set = True
     47              current_item_index += 1
     48          self.last_real_category_index = current_item_index - 1
     49          self.add_category_item_index = self.last_real_category_index + 2
     50          self.edit_categoris_item_index = self.last_real_category_index + 3
     51          self.Append("", None)
     52          self.Append(_("Add new"), None)
     53          self.Append(_("Edit categories"), None)
     54          if not selection_set:
     55              self.SetSelection(0)
     56          self.current_category_selection = self.GetSelection()
     57
     58 -    def get(self):
     59          selection = self.GetSelection()
     60          category = self.GetClientData(selection)
     61          return category
     62
     63 -    def on_choice(self, e):
     64          new_selection_index = e.GetSelection()
     65          if new_selection_index > self.last_real_category_index:
     66              self.SetSelection(self.current_category_selection)
     67              if new_selection_index == self.add_category_item_index:
     68                  self._add_category()
     69              elif new_selection_index == self.edit_categoris_item_index:
     70                  self._edit_categories()
     71          else:
     72              self.current_category_selection = new_selection_index
     73
     74 -    def _add_category(self):
     75          def create_category_editor():
     76              return WxCategoryEdtiorDialog(self, _("Add Category"),
     77                                            self.timeline, None)
     78          def handle_success(dialog):
     79              if dialog.GetReturnCode() == wx.ID_OK:
     80                  try:
     81                      self.select(dialog.get_edited_category())
     82                  except TimelineIOError, e:
     83                      gui_utils.handle_db_error_in_dialog(self, e)
     84          gui_utils.show_modal(create_category_editor,
     85                               gui_utils.create_dialog_db_error_handler(self),
     86                               handle_success)
     87
     88 -    def _edit_categories(self):
     89          def create_categories_editor():
     90              return CategoriesEditor(self, self.timeline)
     91          def handle_success(dialog):
     92              try:
     93                  prev_index = self.GetSelection()
     94                  prev_category = self.GetClientData(prev_index)
     95                  self.select(prev_category)
     96              except TimelineIOError, e:
     97                  gui_utils.handle_db_error_in_dialog(self, e)
     98          gui_utils.show_modal(create_categories_editor,
     99                               gui_utils.create_dialog_db_error_handler(self),
    100                               handle_success)
    101