Package Gnumed :: Package timelinelib :: Package wxgui :: Package dialogs :: Package editcategory :: Module controller |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.wxgui.dialogs.editcategory.controller

     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  from timelinelib.wxgui.framework import Controller
    20
    21
    22 -class EditCategoryDialogController(Controller):
    23
    24 -    def on_init(self, db, category):
    25          self._db = db
    26          self._create_category(category)
    27          self.view.PopulateCategories(exclude=category)
    28          self._populate_view()
    29
    30 -    def on_ok_clicked(self, event):
    31          if self._validate():
    32              self._populate_category()
    33              self._category.save()
    34              self.view.EndModalOk()
    35
    36 -    def get_edited_category(self):
    37          return self._category
    38
    39 -    def _create_category(self, category):
    40          if category is None:
    41              self._category = self._db.new_category()
    42          else:
    43              self._category = category
    44
    45 -    def _populate_view(self):
    46          self.view.SetName(self._category.name)
    47          self.view.SetColor(self._category.color)
    48          self.view.SetProgressColor(self._category.progress_color)
    49          self.view.SetDoneColor(self._category.done_color)
    50          self.view.SetFontColor(self._category.font_color)
    51          self.view.SetParent(self._category.parent)
    52
    53 -    def _validate(self):
    54          new_name = self.view.GetName()
    55          if not self._is_name_valid(new_name):
    56              self.view.HandleInvalidName(new_name)
    57              return False
    58          if self._is_name_in_use(new_name):
    59              self.view.HandleUsedName(new_name)
    60              return False
    61          return True
    62
    63 -    def _is_name_valid(self, name):
    64          return len(name) > 0
    65
    66 -    def _is_name_in_use(self, name):
    67          for cat in self._db.get_categories():
    68              if cat != self._category and cat.get_name() == name:
    69                  return True
    70          return False
    71
    72 -    def _populate_category(self):
    73          self._category.name = self.view.GetName()
    74          self._category.color = self.view.GetColor()
    75          self._category.progress_color = self.view.GetProgressColor()
    76          self._category.done_color = self.view.GetDoneColor()
    77          self._category.font_color = self.view.GetFontColor()
    78          self._category.parent = self.view.GetParent()
    79