Package Gnumed :: Package timelinelib :: Package wxgui :: Package dialogs :: Package duplicateevent :: Module controller
[frames] | no frames]

Source Code for Module Gnumed.timelinelib.wxgui.dialogs.duplicateevent.controller

  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.wxgui.framework import Controller 
 20   
 21   
 22  FORWARD = 0 
 23  BACKWARD = 1 
 24  BOTH = 2 
 25   
 26   
27 -class DuplicateEventDialogController(Controller):
28
29 - def on_init(self, event, event_duplicator=None):
30 if event_duplicator is None: 31 self.event_duplicator = EventDuplicator() 32 else: 33 self.event_duplicator = event_duplicator 34 self.event = event 35 self._set_default_values()
36
37 - def on_ok(self, evt):
38 self._create_duplicates() 39 self.view.Close()
40
41 - def _set_default_values(self):
46
47 - def _create_duplicates(self):
48 missing_dates_count, periods = self._repeat_period( 49 self.event.time_period 50 ) 51 self.event_duplicator.duplicate(self.event, periods) 52 if missing_dates_count > 0: 53 self.view.HandleDateErrors(missing_dates_count)
54
55 - def _repeat_period(self, period):
56 missing_dates_count = 0 57 periods = [] 58 for index in self._calculate_indicies(): 59 new_period = self.view.GetMovePeriodFn()( 60 period, 61 index * self.view.GetFrequency() 62 ) 63 if new_period is None: 64 missing_dates_count += 1 65 else: 66 periods.append(new_period) 67 return (missing_dates_count, periods)
68
69 - def _calculate_indicies(self):
70 direction = self.view.GetDirection() 71 repetitions = self.view.GetCount() 72 if direction == FORWARD: 73 return range(1, repetitions + 1) 74 elif direction == BACKWARD: 75 return range(-repetitions, 0) 76 elif direction == BOTH: 77 indicies = list(range(-repetitions, repetitions + 1)) 78 indicies.remove(0) 79 return indicies 80 else: 81 raise Exception("Invalid direction.")
82 83
84 -class EventDuplicator(object):
85
86 - def duplicate(self, event, periods):
87 with event.db.transaction("Duplicate event"): 88 self._duplicate_events(event, periods)
89
90 - def _duplicate_events(self, event, periods):
91 for period in periods: 92 self._duplicate_event(event, period)
93
94 - def _duplicate_event(self, event, period):
95 duplicate = event.duplicate() 96 duplicate.time_period = period 97 duplicate.container = event.container 98 duplicate.save() 99 if event.is_container(): 100 delta = period.get_start_time() - event.get_start_time() 101 for subevent in event.subevents: 102 duplicate_subevent = subevent.duplicate() 103 duplicate_subevent.time_period = subevent.time_period.move_delta(delta) 104 # It is important to set time period before container, because 105 # otherwise the strategy might move events unexpectedly. 106 duplicate_subevent.container = duplicate 107 duplicate_subevent.save()
108