Package Gnumed :: Package timelinelib :: Package wxgui :: Package components :: Package maincanvas :: Module movebydrag |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.wxgui.components.maincanvas.movebydrag

      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.components.maincanvas.scrollbase import ScrollViewInputHandler
     20
     21
     22 -class MoveByDragInputHandler(ScrollViewInputHandler):
     23
     24 -    def __init__(self, state, timeline_canvas, event, start_drag_time):
     25          ScrollViewInputHandler.__init__(self, timeline_canvas)
     26          self._state = state
     27          self.start_drag_time = start_drag_time
     28          self._store_event_periods(event)
     29          self._transaction = self.timeline_canvas.GetDb().transaction("Move events")
     30
     31 -    def _store_event_periods(self, event_being_dragged):
     32          self.event_periods = []
     33          selected_events = self.timeline_canvas.GetSelectedEvents()
     34          if event_being_dragged not in selected_events:
     35              return
     36          for event in selected_events:
     37              period_pair = (event, event.get_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.subevents:
     44                      period_pair = (subevent, subevent.get_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, cursor, keyboard):
     49          ScrollViewInputHandler.mouse_moved(self, cursor, keyboard)
     50          self._move_event()
     51
     52 -    def left_mouse_up(self):
     53          ScrollViewInputHandler.left_mouse_up(self)
     54          self._state.display_status("")
     55          self._transaction.commit()
     56          self._state.edit_ends()
     57          self._state.change_to_no_op()
     58
     59 -    def view_scrolled(self):
     60          self._move_event()
     61
     62 -    def _move_event(self):
     63          if len(self.event_periods) == 0:
     64              return
     65          if self._any_event_locked():
     66              self._state.display_status(_("Can't move locked event"))
     67              return
     68          self._move_selected_events()
     69          self.timeline_canvas.Redraw()
     70
     71 -    def _any_event_locked(self):
     72          for (event, _) in self.event_periods:
     73              if event.get_locked():
     74                  return True
     75          return False
     76
     77 -    def _move_selected_events(self):
     78          try:
     79              total_move_delta = self._get_total_move_delta()
     80              for (event, original_period) in self.event_periods:
     81                  event.update_period_o(original_period.move_delta(total_move_delta))
     82                  event.save()
     83          except ValueError as ex:
     84              self.status_bar.set_text("%s" % ex)
     85
     86 -    def _get_total_move_delta(self):
     87          moved_delta = self._get_moved_delta()
     88          if self.timeline_canvas.EventIsPeriod(self.event_periods[0][0]):
     89              new_period = self.event_periods[0][1].move_delta(moved_delta)
     90              snapped_period = self._snap(new_period)
     91              return snapped_period.start_time - self.event_periods[0][1].start_time
     92          else:
     93              return moved_delta
     94
     95 -    def _get_moved_delta(self):
     96          current_time = self.timeline_canvas.GetTimeAt(self.last_x)
     97          return current_time - self.start_drag_time
     98
     99 -    def _snap(self, period):
    100          start = period.start_time
    101          end = period.end_time
    102          start_snapped = self.timeline_canvas.Snap(start)
    103          end_snapped = self.timeline_canvas.Snap(end)
    104          if start_snapped != start:
    105              return period.move_delta(start_snapped - start)
    106          elif end_snapped != end:
    107              return period.move_delta(end_snapped - end)
    108          else:
    109              return period
    110