Gnumed.timelinelib.editors.category

Package Gnumed :: Package timelinelib :: Package editors :: Module category |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.editors.category

     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  from timelinelib.db.exceptions import TimelineIOError
    20  from timelinelib.db.objects import Category
    21
    22
    23 -class CategoryEditor(object):
    24
    25 -    def __init__(self, view):
    26          self.view = view
    27
    28 -    def edit(self, category, category_repository):
    29          self.category = category
    30          self.category_repository = category_repository
    31          try:
    32              tree = self.category_repository.get_tree(remove=self.category)
    33          except TimelineIOError, e:
    34              self.view.handle_db_error(e)
    35          else:
    36              self.view.set_category_tree(tree)
    37              if self.category is None:
    38                  self.view.set_name("")
    39                  self.view.set_color((255, 0, 0))
    40                  self.view.set_font_color((0, 0, 0))
    41                  self.view.set_parent(None)
    42              else:
    43                  self.view.set_name(self.category.name)
    44                  self.view.set_color(self.category.color)
    45                  self.view.set_font_color(self.category.font_color)
    46                  self.view.set_parent(self.category.parent)
    47
    48 -    def save(self):
    49          try:
    50              new_name = self.view.get_name()
    51              new_color = self.view.get_color()
    52              new_font_color = self.view.get_font_color()
    53              new_parent = self.view.get_parent()
    54              if not self._name_valid(new_name):
    55                  self.view.handle_invalid_name(new_name)
    56                  return
    57              if self._name_in_use(new_name):
    58                  self.view.handle_used_name(new_name)
    59                  return
    60              if self.category is None:
    61                  self.category = Category(new_name, new_color, new_font_color,
    62                                           True, parent=new_parent)
    63              else:
    64                  self.category.name = new_name
    65                  self.category.color = new_color
    66                  self.category.font_color = new_font_color
    67                  self.category.parent = new_parent
    68              self.category_repository.save(self.category)
    69              self.view.close()
    70          except TimelineIOError, e:
    71              self.view.handle_db_error(e)
    72
    73 -    def _name_valid(self, name):
    74          return len(name) > 0
    75
    76 -    def _name_in_use(self, name):
    77          for cat in self.category_repository.get_all():
    78              if cat != self.category and cat.name == name:
    79                  return True
    80          return False
    81