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          with self._db.transaction("Save container") as t:
     50              t.save_container(self._immutable_value, self.ensure_id())
     51          return self
     52
     53 -    def delete(self):
     54          with self._db.transaction("Delete container") as t:
     55              t.delete_container(self.id)
     56          self.id = None
     57
     58 -    def get_time_period(self):
     59          if len(self.subevents) == 0:
     60              return self._immutable_value.time_period
     61          else:
     62              return TimePeriod(
     63                  min([event.get_start_time() for event in self.subevents]),
     64                  max([event.get_end_time() for event in self.subevents])
     65              )
     66
     67 -    def set_time_period(self, value):
     68          self._immutable_value = self._immutable_value.update(time_period=value)
     69          return self
     70
     71      time_period = property(get_time_period, set_time_period)
     72
     73 -    def get_sort_order(self):
     74          if len(self.subevents) == 0:
     75              return 0
     76          else:
     77              return min(
     78                  self.subevents,
     79                  key=lambda event: event.sort_order
     80              ).sort_order
     81
     82 -    def set_sort_order(self, sort_order):
     83          # Don't save it
     84          return self
     85
     86      sort_order = property(get_sort_order, set_sort_order)
     87
     88 -    def __eq__(self, other):
     89          return (isinstance(other, Container) and
     90                  super(Container, self).__eq__(other))
     91
     92 -    def is_container(self):
     93          return True
     94
     95 -    def is_subevent(self):
     96          return False
     97
     98 -    def register_subevent(self, subevent):
     99          self.strategy.register_subevent(subevent)
    100
    101 -    def unregister_subevent(self, subevent):
    102          self.strategy.unregister_subevent(subevent)
    103
    104 -    def update_container(self, subevent):
    105          if self._is_in_update:
    106              return
    107          try:
    108              self._is_in_update = True
    109              self.strategy.update(subevent)
    110          finally:
    111              self._is_in_update = False
    112
    113 -    def update_properties(self, text, category=None):
    114          self.set_text(text)
    115          self.set_category(category)
    116
    117 -    def allow_ends_today_on_subevents(self):
    118          return self.strategy.allow_ends_today_on_subevents()
    119
    120
    121  create_noop_property(Container, "fuzzy", False)
    122  create_noop_property(Container, "locked", False)
    123  create_noop_property(Container, "ends_today", False)
    124  create_noop_property(Container, "icon", None)
    125  create_noop_property(Container, "hyperlink", None)
    126  create_noop_property(Container, "alert", None)
    127  create_noop_property(Container, "progress", None)
    128  create_noop_property(Container, "default_color", DEFAULT_COLOR)
    129