Package Gnumed :: Package timelinelib :: Package canvas :: Package data :: Module category |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.canvas.data.category

      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.canvas.data.base import ItemBase
     20  from timelinelib.canvas.data.immutable import ImmutableCategory
     21  from timelinelib.canvas.drawing.drawers import get_progress_color
     22
     23
     24  EXPORTABLE_FIELDS = FIELDS = (_("Name"), _("Color"), _("Progress Color"), _("Done Color"), _("Parent"))
     25
     26
     27 -class Category(ItemBase):
     28
     29 -    def __init__(self, db=None, id_=None, immutable_value=ImmutableCategory()):
     30          ItemBase.__init__(self, db, id_, immutable_value)
     31          self._parent = None
     32
     33 -    def duplicate(self, target_db=None):
     34          duplicate = ItemBase.duplicate(self, target_db=target_db)
     35          if duplicate.db is self.db:
     36              duplicate.parent = self.parent
     37          return duplicate
     38
     39 -    def update(self, name, color, font_color, parent=None):
     40          self.parent = None
     41          self.name = name
     42          self.color = color
     43          self.progress_color = get_progress_color(color)
     44          self.done_color = get_progress_color(color)
     45          if font_color is None:
     46              self.font_color = (0, 0, 0)
     47          else:
     48              self.font_color = font_color
     49          self.parent = parent
     50          return self
     51
     52 -    def save(self):
     53          self._update_parent_id()
     54          with self._db.transaction("Save category") as t:
     55              t.save_category(self._immutable_value, self.ensure_id())
     56          return self
     57
     58 -    def _update_parent_id(self):
     59          if self.parent is None:
     60              self._immutable_value = self._immutable_value.update(
     61                  parent_id=None
     62              )
     63          elif self.parent.id is None:
     64              raise Exception("Unknown parent")
     65          else:
     66              self._immutable_value = self._immutable_value.update(
     67                  parent_id=self.parent.id
     68              )
     69
     70 -    def delete(self):
     71          with self._db.transaction("Delete category") as t:
     72              t.delete_category(self.id)
     73          self.id = None
     74
     75 -    def reload(self):
     76          return self._db.get_category_by_id(self.id)
     77
     78 -    def get_name(self):
     79          return self._immutable_value.name
     80
     81 -    def set_name(self, name):
     82          self._immutable_value = self._immutable_value.update(name=name)
     83          return self
     84
     85      name = property(get_name, set_name)
     86
     87 -    def get_color(self):
     88          return self._immutable_value.color
     89
     90 -    def set_color(self, color):
     91          self._immutable_value = self._immutable_value.update(color=color)
     92          return self
     93
     94      color = property(get_color, set_color)
     95
     96 -    def get_progress_color(self):
     97          return self._immutable_value.progress_color
     98
     99 -    def set_progress_color(self, color):
    100          self._immutable_value = self._immutable_value.update(progress_color=color)
    101          return self
    102
    103      progress_color = property(get_progress_color, set_progress_color)
    104
    105 -    def get_done_color(self):
    106          return self._immutable_value.done_color
    107
    108 -    def set_done_color(self, color):
    109          self._immutable_value = self._immutable_value.update(done_color=color)
    110          return self
    111
    112      done_color = property(get_done_color, set_done_color)
    113
    114 -    def get_font_color(self):
    115          return self._immutable_value.font_color
    116
    117 -    def set_font_color(self, font_color):
    118          self._immutable_value = self._immutable_value.update(font_color=font_color)
    119          return self
    120
    121      font_color = property(get_font_color, set_font_color)
    122
    123 -    def _get_parent(self):
    124          return self._parent
    125
    126 -    def set_parent(self, parent):
    127          self._parent = parent
    128          return self
    129
    130      parent = property(_get_parent, set_parent)
    131
    132 -    def get_exportable_fields(self):
    133          return EXPORTABLE_FIELDS
    134
    135 -    def __repr__(self):
    136          return "Category<id=%r, name=%r, color=%r, font_color=%r>" % (
    137              self.get_id(), self.get_name(), self.get_color(),
    138              self.get_font_color())
    139
    140 -    def __eq__(self, other):
    141          if self is other:
    142              return True
    143          return (isinstance(other, Category) and
    144                  self.get_id() == other.get_id() and
    145                  self.get_name() == other.get_name() and
    146                  self.get_color() == other.get_color() and
    147                  self.get_progress_color() == other.get_progress_color() and
    148                  self.get_done_color() == other.get_done_color() and
    149                  self.get_font_color() == other.get_font_color() and
    150                  self._get_parent() == other._get_parent())
    151
    152 -    def __ne__(self, other):
    153          return not (self == other)
    154
    155
    156 -def sort_categories(categories):
    157      return sorted(list(categories), key=lambda category: category.get_name().lower())
    158