Package Gnumed :: Package timelinelib :: Package calendar :: Package num :: Module timetype |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.calendar.num.timetype

      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  """Contains the class NumTimeType."""
     20
     21
     22  import math
     23  import re
     24
     25  from timelinelib.calendar.num.time import NumDelta
     26  from timelinelib.calendar.num.time import NumTime
     27  from timelinelib.calendar.timetype import TimeType
     28  from timelinelib.canvas.data import TimePeriod
     29  from timelinelib.canvas.data import time_period_center
     30  from timelinelib.canvas.drawing.interface import Strip
     31
     32
     33 -class NumTimeType(TimeType):
     34
     35      """The class."""
     36
     37 -    def __eq__(self, other):
     38          return isinstance(other, NumTimeType)
     39
     40 -    def __ne__(self, other):
     41          return not (self == other)
     42
     43 -    def time_string(self, time):
     44          return "%s" % (time.value)
     45
     46 -    def parse_time(self, time_string):
     47          match = re.search(r"^([-]?\d+(.\d+)?(e[+]\d+)?)$", time_string)
     48          if match:
     49              if '.' in time_string or 'e' in time_string:
     50                  time = float(match.group(1))
     51              else:
     52                  time = int(match.group(1))
     53              try:
     54                  return NumTime(time)
     55              except ValueError:
     56                  raise ValueError("Invalid time, time string = '%s'" % time_string)
     57          else:
     58              raise ValueError("Time not on correct format = '%s'" % time_string)
     59
     68
     69 -    def format_period(self, time_period):
     70          """Returns a unicode string describing the time period."""
     71          if time_period.is_period():
     72              label = u"%s to %s" % (
     73                  time_period.start_time.value,
     74                  time_period.end_time.value
     75              )
     76          else:
     77              label = u"%s" % time_period.start_time.value
     78          return label
     79
     80 -    def format_delta(self, delta):
     81          return "%d" % delta.value
     82
     83 -    def get_min_time(self):
     84          return None
     85
     86 -    def get_max_time(self):
     87          return None
     88
     89 -    def choose_strip(self, metrics, appearance):
     90          # Choose an exponent that will make the minor strip just larger than
     91          # the displayed period:
     92          #
     93          #     10**x > period_delta   =>
     94          #     x > log(period_delta)
     95          exponent = int(math.log(metrics.time_period.delta().value, 10)) + 1
     96          # Keep decreasing the exponent until the minor strip is small enough.
     97          while True:
     98              if exponent == 0:
     99                  break
    100              next_minor_strip_with_px = metrics.calc_exact_width(
    101                  TimePeriod(
    102                      NumTime(0),
    103                      NumTime(10 ** (exponent - 1))
    104                  )
    105              )
    106              if next_minor_strip_with_px > 30:
    107                  exponent -= 1
    108              else:
    109                  break
    110          return (NumStrip(10 ** (exponent + 1)), NumStrip(10 ** exponent))
    111
    112 -    def get_default_time_period(self):
    113          return time_period_center(NumTime(0), NumDelta(100))
    114
    115 -    def now(self):
    116          return NumTime(0)
    117
    118 -    def get_min_zoom_delta(self):
    119          return (NumDelta(5), _("Can't zoom deeper than 5"))
    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 supports_saved_now(self):
    133          return False
    134
    135 -    def create_time_picker(self, parent, *args, **kwargs):
    136          from timelinelib.calendar.num.timepicker import NumTimePicker
    137          return NumTimePicker(parent, *args, **kwargs)
    138
    139 -    def create_period_picker(self, parent, *args, **kwargs):
    140          from timelinelib.calendar.num.periodpicker import NumPeriodPicker
    141          return NumPeriodPicker(parent, *args, **kwargs)
    142
    143
    144 -class NumStrip(Strip):
    145
    146 -    def __init__(self, size):
    147          self.size = size
    148
    149 -    def label(self, time, major=False):
    150          return "%s" % time.value
    151
    152 -    def start(self, time):
    153          start = int((time.value / self.size)) * self.size
    154          if time < NumTime(0):
    155              start -= self.size
    156          return NumTime(start)
    157
    158 -    def increment(self, time):
    159          return time + NumDelta(self.size)
    160
    161
    162 -def go_to_zero_fn(main_frame, current_period, navigation_fn):
    163      navigation_fn(lambda tp: tp.center(NumTime(0)))
    164
    165
    166 -def go_to_time_fn(main_frame, current_period, navigation_fn):
    167      def navigate_to(time):
    168          navigation_fn(lambda tp: tp.center(time))
    169      main_frame.display_time_editor_dialog(
    170          NumTimeType(),
    171          current_period.mean_time(),
    172          navigate_to,
    173          _("Go to Time")
    174      )
    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, num):
    188      delta = NumDelta(num)
    189      start_time = period.start_time + delta
    190      end_time = period.end_time + delta
    191      return TimePeriod(start_time, end_time)
    192