Gnumed.timelinelib.canvas.data.container

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

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

      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 create_noop_property
     20  from timelinelib.canvas.data.event import Event
     21  from timelinelib.canvas.data.event import DEFAULT_COLOR
     22  from timelinelib.canvas.data.immutable import ImmutableContainer
     23  from timelinelib.canvas.data.timeperiod import TimePeriod
     24  from timelinelib.features.experimental.experimentalfeatures import EXTENDED_CONTAINER_STRATEGY
     25
     26
     27 -class Container(Event):
     28
     29 -    def __init__(self, db=None, id_=None, immutable_value=ImmutableContainer()):
     30          Event.__init__(self, db=db, id_=id_, immutable_value=immutable_value)
     31          self._subevents = []
     32          self._is_in_update = False
     33          import timelinelib.db.strategies
     34          if EXTENDED_CONTAINER_STRATEGY.enabled():
     35              self.strategy = timelinelib.db.strategies.ExtendedContainerStrategy(self)
     36          else:
     37              self.strategy = timelinelib.db.strategies.DefaultContainerStrategy(self)
     38
     39      @property
     40 -    def subevents(self):
     41          return self._subevents
     42
     43      @subevents.setter
     44 -    def subevents(self, value):
     45          self._subevents = value
     46
     47 -    def save(self):
     48          self._update_category_id()
     49          self._update_category_ids()
     50          with self._db.transaction("Save container") as t:
     51              t.save_container(self._immutable_value, self.ensure_id())
     52          return self
     53
     54 -    def delete(self):
     55          with self._db.transaction("Delete container") as t:
     56              t.delete_container(self.id)
     57          self.id = None
     58
     59 -    def get_time_period(self):
     60          if len(self.subevents) == 0:
     61              return self._immutable_value.time_period
     62          else:
     63              return TimePeriod(
     64                  min([event.get_start_time() for event in self.subevents]),
     65                  max([event.get_end_time() for event in self.subevents])
     66              )
     67
     68 -    def set_time_period(self, value):
     69          self._immutable_value = self._immutable_value.update(time_period=value)
     70          return self
     71
     72      time_period = property(get_time_period, set_time_period)
     73
     74 -    def get_sort_order(self):
     75          if len(self.subevents) == 0:
     76              return 0
     77          else:
     78              return min(
     79                  self.subevents,
     80                  key=lambda event: event.sort_order
     81              ).sort_order
     82
     83 -    def set_sort_order(self, sort_order):
     84          # Don't save it
     85          return self
     86
     87      sort_order = property(get_sort_order, set_sort_order)
     88
     89 -    def __eq__(self, other):
     90          return (isinstance(other, Container) and
     91                  super(Container, self).__eq__(other))
     92
     93 -    def is_container(self):
     94          return True
     95
     96 -    def is_subevent(self):
     97          return False
     98
     99 -    def register_subevent(self, subevent):
    100          self.strategy.register_subevent(subevent)
    101
    102 -    def unregister_subevent(self, subevent):
    103          self.strategy.unregister_subevent(subevent)
    104
    105 -    def update_container(self, subevent):
    106          if self._is_in_update:
    107              return
    108          try:
    109              self._is_in_update = True
    110              self.strategy.update(subevent)
    111          finally:
    112              self._is_in_update = False
    113
    114 -    def update_properties(self, text, category=None):
    115          self.set_text(text)
    116          self.set_category(category)
    117
    118 -    def allow_ends_today_on_subevents(self):
    119          return self.strategy.allow_ends_today_on_subevents()
    120
    121
    122  create_noop_property(Container, "fuzzy", False)
    123  create_noop_property(Container, "locked", False)
    124  create_noop_property(Container, "ends_today", False)
    125  create_noop_property(Container, "icon", None)
    126  create_noop_property(Container, "hyperlink", None)
    127  create_noop_property(Container, "alert", None)
    128  create_noop_property(Container, "progress", None)
    129  create_noop_property(Container, "default_color", DEFAULT_COLOR)
    130