Gnumed.timelinelib.view.move

Package Gnumed :: Package timelinelib :: Package view :: Module move |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.view.move

      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.view.scrollbase import ScrollViewInputHandler
     20
     21
     22 -class MoveByDragInputHandler(ScrollViewInputHandler):
     23
     24 -    def __init__(self, drawing_area, status_bar_adapter, event, start_drag_time):
     25          ScrollViewInputHandler.__init__(self, drawing_area)
     26          self.drawing_area = drawing_area
     27          self.status_bar_adapter = status_bar_adapter
     28          self.start_drag_time = start_drag_time
     29          self._store_event_periods(event)
     30
     31 -    def _store_event_periods(self, event_being_dragged):
     32          self.event_periods = []
     33          selected_events = self.drawing_area.get_selected_events()
     34          if not event_being_dragged in selected_events:
     35              return
     36          for event in selected_events:
     37              period_pair = (event, event.time_period)
     38              if event == event_being_dragged:
     39                  self.event_periods.insert(0, period_pair)
     40              else:
     41                  self.event_periods.append(period_pair)
     42          if event.is_container():
     43              for subevent in event.events:
     44                  period_pair = (subevent, subevent.time_period)
     45                  self.event_periods.append(period_pair)
     46          assert self.event_periods[0][0] == event_being_dragged
     47
     48 -    def mouse_moved(self, x, y, alt_down=False):
     49          ScrollViewInputHandler.mouse_moved(self, x, y, alt_down)
     50          self._move_event()
     51
     52 -    def left_mouse_up(self):
     53          ScrollViewInputHandler.left_mouse_up(self)
     54          self.status_bar_adapter.set_text("")
     55          self.drawing_area.change_input_handler_to_no_op()
     56
     57 -    def view_scrolled(self):
     58          self._move_event()
     59
     60 -    def _move_event(self):
     61          if len(self.event_periods) == 0:
     62              return
     63          if self._any_event_locked():
     64              self.status_bar_adapter.set_text(_("Can't move locked event"))
     65              return
     66          self._move_selected_events()
     67          self.drawing_area.redraw_timeline()
     68
     69 -    def _any_event_locked(self):
     70          for (event, original_period) in self.event_periods:
     71              if event.locked:
     72                  return True
     73          return False
     74
     75 -    def _move_selected_events(self):
     76          total_move_delta = self._get_total_move_delta()
     77          for (event, original_period) in self.event_periods:
     78              event.update_period_o(original_period.move_delta(total_move_delta))
     79
     80 -    def _get_total_move_delta(self):
     81          moved_delta = self._get_moved_delta()
     82          if self.drawing_area.event_is_period(self.event_periods[0][0]):
     83              new_period = self.event_periods[0][1].move_delta(moved_delta)
     84              snapped_period = self._snap(new_period)
     85              return snapped_period.start_time - self.event_periods[0][1].start_time
     86          else:
     87              return moved_delta
     88
     89 -    def _get_moved_delta(self):
     90          current_time = self.drawing_area.get_time(self.last_x)
     91          delta = current_time - self.start_drag_time
     92          return delta
     93
     94 -    def _snap(self, period):
     95          start = period.start_time
     96          end = period.end_time
     97          start_snapped = self.drawing_area.snap(start)
     98          end_snapped = self.drawing_area.snap(end)
     99          if start_snapped != start:
    100              return period.move_delta(start_snapped - start)
    101          elif end_snapped != end:
    102              return period.move_delta(end_snapped - end)
    103          else:
    104              return period
    105