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

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

     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  import time
    20
    21  from timelinelib.view.inputhandler import InputHandler
    22
    23
    24 -class ScrollByDragInputHandler(InputHandler):
    25
    26 -    def __init__(self, controller, start_time):
    27          self.controller = controller
    28          self.start_time = start_time
    29          self.last_clock_time = time.clock()
    30          self.last_x = 0
    31          self.last_x_distance = 0
    32          self.speed_px_per_sec = 0
    33          self.INERTIAL_SCROLLING_SPEED_THRESHOLD = 200
    34
    35 -    def mouse_moved(self, x, y, alt_down=False):
    36          self._calculate_sped(x)
    37          self._scroll_timeline(x)
    38
    39 -    def left_mouse_up(self):
    40          self.controller.change_input_handler_to_no_op()
    41          if self.controller.config.use_inertial_scrolling:
    42              if self.speed_px_per_sec > self.INERTIAL_SCROLLING_SPEED_THRESHOLD:
    43                  self._inertial_scrolling()
    44
    45 -    def _calculate_sped(self, x):
    46          MAX_SPEED = 10000
    47          self.last_x_distance = x - self.last_x
    48          self.last_x = x
    49          current_clock_time = time.clock()
    50          elapsed_clock_time = current_clock_time - self.last_clock_time
    51          if elapsed_clock_time == 0:
    52              self.speed_px_per_sec = MAX_SPEED
    53          else:
    54              self.speed_px_per_sec = min(MAX_SPEED, abs(self.last_x_distance /
    55                                          elapsed_clock_time))
    56          self.last_clock_time = current_clock_time
    57
    58 -    def _scroll_timeline(self, x):
    59          self.current_time = self.controller.get_time(x)
    60          delta = (self.current_time - self.start_time)
    61          self.controller._scroll_timeline(delta)
    62
    63 -    def _inertial_scrolling(self):
    64          frame_time = self._calculate_frame_time()
    65          value_factor = self._calculate_scroll_factor()
    66          inertial_func = (0.20, 0.15, 0.10, 0.10, 0.10, 0.08, 0.06, 0.06, 0.05)
    67          #inertial_func = (0.20, 0.15, 0.10, 0.10, 0.07, 0.05, 0.02, 0.05)
    68          self.controller.use_fast_draw(True)
    69          next_frame_time = time.clock()
    70          for value in inertial_func:
    71              self.controller._scroll_timeline_view(value * value_factor)
    72              next_frame_time += frame_time
    73              sleep_time = next_frame_time - time.clock()
    74              if sleep_time >= 0:
    75                  time.sleep(sleep_time)
    76          self.controller.use_fast_draw(False)
    77          self.controller._redraw_timeline()
    78
    79 -    def _calculate_frame_time(self):
    80          MAX_FRAME_RATE = 26.0
    81          frames_per_second = (MAX_FRAME_RATE * self.speed_px_per_sec /
    82                               (100 + self.speed_px_per_sec))
    83          frame_time = 1.0 / frames_per_second
    84          return frame_time
    85
    86 -    def _calculate_scroll_factor(self):
    87          if self.current_time > self.start_time:
    88              direction = 1
    89          else:
    90              direction = -1
    91          scroll_factor = (direction * self.speed_px_per_sec /
    92                          self.INERTIAL_SCROLLING_SPEED_THRESHOLD)
    93          return scroll_factor
    94