Gnumed.timelinelib.editors.event

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

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

      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.objects import Event
     20  from timelinelib.db.objects import PeriodTooLongError
     21  from timelinelib.db.objects import Subevent
     22  from timelinelib.db.objects import TimePeriod
     23  from timelinelib.utils import ex_msg
     24
     25
     26 -class EventEditor(object):
     27
     28 -    def __init__(self, view):
     29          self.view = view
     30
     31 -    def edit(self, time_type, event_repository, timeline, start, end, event):
     32          self.timeline = timeline
     33          self.time_type = time_type
     34          self.event_repository = event_repository
     35          self._set_values(start, end, event)
     36          self._set_view_content()
     37
     38 -    def create_or_update_event(self):
     39          try:
     40              self._get_and_verify_input()
     41              self._save_event()
     42              if self.view.get_show_add_more():
     43                  self.view.clear_dialog()
     44              else:
     45                  self.view.close()
     46          except ValueError:
     47              pass
     48
     49 -    def clear(self):
     50          self.name = ""
     51          self.event = None
     52          self.view.set_name(self.name)
     53          self.view.set_focus("start")
     54
     55 -    def start_is_in_history(self):
     56          if self.start is None:
     57              return False
     58          return self.start < self.timeline.time_type.now()
     59
     60 -    def _set_values(self, start, end, event):
     61          self.event = event
     62          if self.event != None:
     63              self.start = self.event.time_period.start_time
     64              self.end = self.event.time_period.end_time
     65              self.name = self.event.text
     66              self.category = self.event.category
     67              self.fuzzy = self.event.fuzzy
     68              self.locked = self.event.locked
     69              self.ends_today = self.event.ends_today
     70          else:
     71              self.start = start
     72              self.end = end
     73              self.name = ""
     74              self.category = None
     75              self.fuzzy = False
     76              self.locked = False
     77              self.ends_today = False
     78
     79 -    def _set_view_content(self):
     80          if self.event != None:
     81              self.view.set_event_data(self.event.data)
     82              if self.event.is_subevent():
     83                  self.view.set_container(self.event.container)
     84              else:
     85                  self.view.set_container(None)
     86          else:
     87              self.view.set_container(None)
     88          self.view.set_start(self.start)
     89          self.view.set_end(self.end)
     90          self.view.set_name(self.name)
     91          self.view.set_category(self.category)
     92          self.view.set_show_period(self.end > self.start)
     93          self.view.set_show_time(self._event_has_nonzero_time())
     94          self.view.set_show_add_more(self.event == None)
     95          self.view.set_fuzzy(self.fuzzy)
     96          self.view.set_locked(self.locked)
     97          self.view.set_ends_today(self.ends_today)
     98          if self.start != self.end:
     99              self.view.set_focus("text")
    100          else:
    101              self.view.set_focus("start")
    102
    103 -    def _get_and_verify_input(self):
    104          self.name = self._get_name(self.view.get_name())
    105          self.fuzzy = self.view.get_fuzzy()
    106          self.locked = self.view.get_locked()
    107          self.ends_today = self.view.get_ends_today()
    108          self.category = self.view.get_category()
    109          start = self.get_start_from_view()
    110          if self._dialog_has_signalled_invalid_input(start):
    111              raise ValueError()
    112          end = self.get_end_from_view()
    113          if self._dialog_has_signalled_invalid_input(end):
    114              raise ValueError()
    115          if self.event != None and self.locked:
    116              self._verify_that_time_has_not_been_changed(start, end)
    117          self.start = self._validate_and_save_start(self.get_start_from_view())
    118          self.end = self._validate_and_save_end(self.get_end_from_view())
    119          self._validate_period()
    120          self.container = self.view.get_container()
    121
    122 -    def get_start_from_view(self):
    123          try:
    124              return self.view.get_start()
    125          except ValueError, ex:
    126              self.view.display_invalid_start("%s" % ex_msg(ex))
    127
    128 -    def get_end_from_view(self):
    129          if self.view.get_show_period():
    130              try:
    131                  return self.view.get_end()
    132              except ValueError, ex:
    133                  self.view.display_invalid_end("%s" % ex_msg(ex))
    134          else:
    135              return self.get_start_from_view()
    136
    137 -    def _dialog_has_signalled_invalid_input(self, time):
    138          return time == None
    139
    140 -    def _verify_that_time_has_not_been_changed(self, start, end):
    141          self._exception_if_start_has_changed(start)
    142          if not self.ends_today:
    143              self._exception_if_end_has_changed(end)
    144
    145 -    def _exception_if_start_has_changed(self, start):
    146          if not self.time_type.eventtimes_equals(self.start, start):
    147              self.view.set_start(self.start)
    148              self._exception_when_start_or_end_has_changed()
    149
    150 -    def _exception_if_end_has_changed(self, end):
    151          if not self.time_type.eventtimes_equals(self.end, end):
    152              self.view.set_end(self.end)
    153              self._exception_when_start_or_end_has_changed()
    154
    155 -    def _exception_when_start_or_end_has_changed(self):
    156          error_message = _("You can't change time when the Event is locked")
    157          self.view.display_invalid_start(error_message)
    158          raise ValueError()
    159
    160 -    def _save_event(self):
    161          if self.event == None:
    162              self._create_new_event()
    163          else:
    164              self._update_event()
    165          self.event.data = self.view.get_event_data()
    166          self._save_event_to_db()
    167
    168 -    def _update_event(self):
    169          container_selected = (self.container != None)
    170          if container_selected:
    171              if self.event.is_subevent():
    172                  if self.event.container == self.container:
    173                      self.event.update(self.start, self.end, self.name,
    174                                        self.category, self.fuzzy, self.locked,
    175                                        self.ends_today)
    176                  else:
    177                      self._change_container()
    178              else:
    179                  self._add_event_to_container()
    180          else:
    181              if self.event.is_subevent():
    182                  self._remove_event_from_container()
    183                  pass
    184              else:
    185                  self.event.update(self.start, self.end, self.name,
    186                                    self.category, self.fuzzy, self.locked,
    187                                    self.ends_today)
    188
    189 -    def _remove_event_from_container(self):
    190          self.event.container.unregister_subevent(self.event)
    191          self.timeline.delete_event(self.event)
    192          self._create_new_event()
    193
    194 -    def _add_event_to_container(self):
    195          self.timeline.delete_event(self.event)
    196          self._create_subevent()
    197
    198 -    def _change_container(self):
    199          self.event.container.unregister_subevent(self.event)
    200          self.container.register_subevent(self.event)
    201
    202 -    def _create_new_event(self):
    203          if self.container != None:
    204              self._create_subevent()
    205          else:
    206              self.event = Event(self.time_type, self.start, self.end, self.name,
    207                                 self.category, self.fuzzy, self.locked,
    208                                 self.ends_today)
    209
    210 -    def _create_subevent(self):
    211          if self.is_new_container(self.container):
    212              self.add_new_container()
    213          self.event = Subevent(self.time_type, self.start, self.end, self.name,
    214                                self.category, self.container)
    215
    216 -    def is_new_container(self, container):
    217          return container not in self.timeline.get_containers()
    218
    219 -    def add_new_container(self):
    220          max_id = 0
    221          for container in self.timeline.get_containers():
    222              if container.cid() > max_id:
    223                  max_id = container.cid()
    224          max_id += 1
    225          self.container.set_cid(max_id)
    226          self._save_container_to_db()
    227
    228 -    def _validate_and_save_start(self, start):
    229          if start == None:
    230              raise ValueError()
    231          return start
    232
    233 -    def _validate_and_save_end(self, end):
    234          if end == None:
    235              raise ValueError()
    236          if end < self.start:
    237              self.view.display_invalid_start(_("End must be > Start"))
    238              raise ValueError()
    239          return end
    240
    241 -    def _validate_period(self):
    242          try:
    243              TimePeriod(self.time_type, self.start, self.end)
    244          except PeriodTooLongError:
    245              self.view.display_error_message(_("Entered period is too long."))
    246              raise ValueError()
    247
    248 -    def _get_name(self, name):
    249          if name == "":
    250              msg = _("Field '%s' can't be empty.") % _("Text")
    251              self.view.display_invalid_name(msg)
    252              raise ValueError()
    253          return name
    254
    255 -    def _save_event_to_db(self):
    256          try:
    257              self.event_repository.save(self.event)
    258          except Exception, e:
    259              self.view.display_db_exception(e)
    260
    261 -    def _save_container_to_db(self):
    262          try:
    263              self.event_repository.save(self.container)
    264          except Exception, e:
    265              self.view.display_db_exception(e)
    266
    267 -    def _event_has_nonzero_time(self):
    268          try:
    269              time_type = self.time_type
    270              time_period = TimePeriod(time_type, self.start, self.end)
    271              return time_period.has_nonzero_time()
    272          except Exception:
    273              return False
    274