Gnumed.timelinelib.editors.duplicateevent

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

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

     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
    21
    22  FORWARD = 0
    23  BACKWARD = 1
    24  BOTH = 2
    25
    26
    27 -class DuplicateEventEditor(object):
    28
    29 -    def __init__(self, view, db, event):
    30          self.view = view
    31          self.db = db
    32          self.event = event
    33
    34 -    def initialize(self):
    35          self.view.set_count(1)
    36          self.view.set_frequency(1)
    37          self.view.select_move_period_fn_at_index(0)
    38          self.view.set_direction(FORWARD)
    39
    40 -    def create_duplicates_and_save(self):
    41          (periods, nbr_of_missing_dates) = self._repeat_period(
    42              self.event.time_period,
    43              self.view.get_move_period_fn(),
    44              self.view.get_frequency(),
    45              self.view.get_count(),
    46              self.view.get_direction())
    47          try:
    48              for period in periods:
    49                  event = self.event.clone()
    50                  event.update_period(period.start_time, period.end_time)
    51                  self.db.save_event(event)
    52              if nbr_of_missing_dates > 0:
    53                  self.view.handle_date_errors(nbr_of_missing_dates)
    54              self.view.close()
    55          except TimelineIOError, e:
    56              self.view.handle_db_error(e)
    57
    58 -    def _repeat_period(self, period, move_period_fn, frequency,
    59                         repetitions, direction):
    60          periods = []
    61          nbr_of_missing_dates = 0
    62          for index in self._calc_indicies(direction, repetitions):
    63              new_period = move_period_fn(period, index*frequency)
    64              if new_period == None:
    65                  nbr_of_missing_dates += 1
    66              else:
    67                  periods.append(new_period)
    68          return (periods, nbr_of_missing_dates)
    69
    70 -    def _calc_indicies(self, direction, repetitions):
    71          if direction == FORWARD:
    72              return range(1, repetitions + 1)
    73          elif direction == BACKWARD:
    74              return range(-repetitions, 0)
    75          elif direction == BOTH:
    76              indicies = range(-repetitions, repetitions + 1)
    77              indicies.remove(0)
    78              return indicies
    79          else:
    80              raise Exception("Invalid direction.")
    81