Gnumed.timelinelib.time.numtime

Package Gnumed :: Package timelinelib :: Package time :: Module numtime |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.time.numtime

      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 re
     20
     21  from timelinelib.time.typeinterface import TimeType
     22  from timelinelib.db.objects import time_period_center
     23  from timelinelib.drawing.interface import Strip
     24  from timelinelib.drawing.utils import get_default_font
     25  from timelinelib.db.objects import TimePeriod
     26
     27
     28 -class NumTimeType(TimeType):
     29
     30 -    def time_string(self, time):
     31          return "%s" % (time)
     32
     33 -    def parse_time(self, time_string):
     34          match = re.search(r"^([-]?\d+(.\d+)?)$", time_string)
     35          if match:
     36              time = float(match.group(1))
     37              try:
     38                  return time
     39              except ValueError:
     40                  raise ValueError("Invalid time, time string = '%s'" % time_string)
     41          else:
     42              raise ValueError("Time not on correct format = '%s'" % time_string)
     43
     52
     53 -    def is_date_time_type(self):
     54          return False
     55
     56 -    def format_period(self, time_period):
     57          """Returns a unicode string describing the time period."""
     58          if time_period.is_period():
     59              label = u"%s to %s" % (time_period.start_time, time_period.end_time)
     60          else:
     61              label = u"%s" % time_period.start_time
     62          return label
     63
     64 -    def format_delta(self, delta):
     65          return "%d" % delta
     66
     67 -    def get_min_time(self):
     68          return(None, None)
     69
     70 -    def get_max_time(self):
     71          return(None, None)
     72
     73 -    def choose_strip(self, metrics, config):
     74          start_time = 1
     75          end_time = 2
     76          limit = 30
     77          period = TimePeriod(self, start_time, end_time)
     78          period_width = metrics.calc_exact_width(period)
     79          while period_width == 0:
     80              start_time *= 10
     81              end_time *= 10
     82              limit /= 10
     83              period = TimePeriod(self, start_time, end_time)
     84              period_width = metrics.calc_exact_width(period)
     85          nbr_of_units = metrics.width / period_width
     86          size = 1
     87          while nbr_of_units > limit:
     88              size *= 10
     89              nbr_of_units /= 10
     90          return (NumStrip(size * 10), NumStrip(size))
     91
     92 -    def mult_timedelta(self, delta, num):
     93          return delta * num
     94
     95 -    def get_default_time_period(self):
     96          return time_period_center(self, 0, 100)
     97
     98 -    def now(self):
     99          return 0
    100
    101 -    def get_time_at_x(self, time_period, x_percent_of_width):
    102          """Return the time at pixel `x`."""
    103          delta = time_period.end_time - time_period.start_time
    104          return time_period.start_time + delta * x_percent_of_width
    105
    106 -    def div_timedeltas(self, delta1, delta2):
    107          return delta1 / delta2
    108
    109 -    def get_max_zoom_delta(self):
    110          return (None, None)
    111
    112 -    def get_min_zoom_delta(self):
    113          return (5, _("Can't zoom deeper than 5"))
    114
    115 -    def get_zero_delta(self):
    116          return 0
    117
    118 -    def time_period_has_nonzero_time(self, time_period):
    119          return False
    120
    121 -    def get_name(self):
    122          return u"numtime"
    123
    124 -    def get_duplicate_functions(self):
    125          return [
    126              (_("1-period"), lambda p, d : move_period(p, d)),
    127              (_("10-period"), lambda p, d : move_period(p, d * 10)),
    128              (_("100-period"), lambda p, d : move_period(p, d * 100)),
    129              (_("1000-period"), lambda p, d : move_period(p, d * 1000)),
    130          ]
    131
    132 -    def zoom_is_ok(self, delta):
    133          return (delta >= 5)
    134
    135 -    def half_delta(self, delta):
    136          return delta / 2
    137
    138 -    def margin_delta(self, delta):
    139          return delta / 24
    140
    141 -    def eventtimes_equals(self, time1, time2):
    142          return time_string(time1) == time_string(time2)
    143
    144
    145 -class NumStrip(Strip):
    146
    147 -    def __init__(self, size):
    148          self.size = size
    149
    150 -    def label(self, time, major=False):
    151          return "%s" % (time)
    152
    153 -    def start(self, time):
    154          start = int((time / self.size)) * self.size
    155          if time < 0:
    156              start -= self.size
    157          return start
    158
    159 -    def increment(self, time):
    160          return time + self.size
    161
    162 -    def get_font(self, time_period):
    163          return get_default_font(8)
    164
    165
    166 -def go_to_zero_fn(main_frame, current_period, navigation_fn):
    167      navigation_fn(lambda tp: tp.center(0))
    168
    169
    170 -def go_to_time_fn(main_frame, current_period, navigation_fn):
    171      def navigate_to(time):
    172          navigation_fn(lambda tp: tp.center(time))
    173      main_frame.display_time_editor_dialog(
    174          NumTimeType(), current_period.mean_time(), navigate_to, _("Go to Time"))
    175
    176
    177 -def backward_fn(main_frame, current_period, navigation_fn):
    178      delta = current_period.start_time - current_period.end_time
    179      navigation_fn(lambda tp: tp.move_delta(delta))
    180
    181
    182 -def forward_fn(main_frame, current_period, navigation_fn):
    183      delta = current_period.end_time - current_period.start_time
    184      navigation_fn(lambda tp: tp.move_delta(delta))
    185
    186
    187 -def move_period(period, delta):
    188      start_time = period.start_time + delta
    189      end_time = period.end_time + delta
    190      return TimePeriod(period.time_type, start_time, end_time)
    191