Gnumed.timelinelib.calendar.gregorian.timepicker.time

Package Gnumed :: Package timelinelib :: Package calendar :: Package gregorian :: Package timepicker :: Module time |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.calendar.gregorian.timepicker.time

      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.calendar.gregorian.gregorian import is_valid_time
     20  from timelinelib.wxgui.components import TextPatternControl
     21
     22
     23 -class GregorianTimePicker(TextPatternControl):
     24
     25      HOUR_GROUP = 0
     26      MINUTE_GROUP = 1
     27      SECOND_GROUP = 2
     28
     29 -    def __init__(self, parent, config):
     30          self.config = config
     31          if (self.config.use_second):
     32              TextPatternControl.__init__(self, parent, fit_text="00:00:00")
     33              self.SetSeparators([":",":"])
     34          else:
     35              TextPatternControl.__init__(self, parent, fit_text="00:00")
     36              self.SetSeparators([":"])
     37          self.SetValidator(self._is_time_valid)
     38          self.SetUpHandler(self.HOUR_GROUP, self._increment_hour)
     39          self.SetUpHandler(self.MINUTE_GROUP, self._increment_minute)
     40          self.SetDownHandler(self.HOUR_GROUP, self._decrement_hour)
     41          self.SetDownHandler(self.MINUTE_GROUP, self._decrement_minute)
     42
     43          if (self.config.use_second):
     44              self.SetUpHandler(self.SECOND_GROUP, self._increment_second)
     45              self.SetDownHandler(self.SECOND_GROUP, self._decrement_second)
     46
     47 -    def GetGregorianTime(self):
     48          return self.parts_to_gregorian_time(self.GetParts())
     49
     50 -    def SetGregorianTime(self, time):
     51          self.SetParts(self.gregorian_time_to_parts(time))
     52
     53 -    def _is_time_valid(self):
     54          try:
     55              self.GetGregorianTime()
     56          except ValueError:
     57              return False
     58          else:
     59              return True
     60
     61 -    def _increment_hour(self):
     62          self.SetGregorianTime(increment_hour(self.GetGregorianTime()))
     63
     64 -    def _increment_minute(self):
     65          self.SetGregorianTime(increment_minute(self.GetGregorianTime()))
     66
     67 -    def _increment_second(self):
     68          self.SetGregorianTime(increment_second(self.GetGregorianTime()))
     69
     70 -    def _decrement_hour(self):
     71          self.SetGregorianTime(decrement_hour(self.GetGregorianTime()))
     72
     73 -    def _decrement_minute(self):
     74          self.SetGregorianTime(decrement_minute(self.GetGregorianTime()))
     75
     76 -    def _decrement_second(self):
     77          self.SetGregorianTime(decrement_second(self.GetGregorianTime()))
     78
     79 -    def gregorian_time_to_parts(self, time):
     80          (hour, minute, second) = time
     81          if (self.config.use_second):
     82              return [
     83                  "%02d" % hour,
     84                  "%02d" % minute,
     85                  "%02d" % second,
     86              ]
     87          else:
     88              return [
     89                  "%02d" % hour,
     90                  "%02d" % minute
     91              ]
     92
     93
     94 -    def parts_to_gregorian_time(self, parts):
     95          hour = minute = second = 0
     96          if not self.config.use_second:
     97              [hour_str, minute_str] = parts
     98              hour = int(hour_str)
     99              minute = int(minute_str)
    100
    101          if self.config.use_second:
    102              [hour_str, minute_str, second_str] = parts
    103              hour = int(hour_str)
    104              minute = int(minute_str)
    105              second = int(second_str)
    106
    107          if not is_valid_time(hour, minute, second):
    108              raise ValueError()
    109          return (hour, minute, second)
    110
    111
    112 -def increment_hour(time):
    113      hour, minute, second = time
    114      new_hour = hour + 1
    115      if new_hour > 23:
    116          new_hour = 0
    117      return (new_hour, minute, second)
    118
    119
    120 -def increment_minute(time):
    121      hour, minute, second = time
    122      new_hour = hour
    123      new_minute = minute + 1
    124      if new_minute > 59:
    125          new_minute = 0
    126          new_hour = hour + 1
    127          if new_hour > 23:
    128              new_hour = 0
    129      return (new_hour, new_minute, second)
    130
    131
    132 -def increment_second(time):
    133      hour, minute, second = time
    134      new_hour = hour
    135      new_minute = minute
    136      new_second = second
    137      if new_second > 59:
    138          new_second = 0
    139          new_minute = minute + 1
    140          if new_minute > 59:
    141              new_minute = 0
    142              new_hour = hour + 1
    143              if new_hour > 23:
    144                  new_hour = 0
    145      return (new_hour, new_minute, new_second)
    146
    147 -def decrement_hour(time):
    148      hour, minute, second = time
    149      new_hour = hour - 1
    150      if new_hour < 0:
    151          new_hour = 23
    152      return (new_hour, minute, second)
    153
    154
    155 -def decrement_minute(time):
    156      hour, minute, second = time
    157      new_hour = hour
    158      new_minute = minute - 1
    159      if new_minute < 0:
    160          new_minute = 59
    161          new_hour = hour - 1
    162          if new_hour < 0:
    163              new_hour = 23
    164      return (new_hour, new_minute, second)
    165
    166 -def decrement_second(time):
    167      hour, minute, second = time
    168      new_hour = hour
    169      new_minute = minute
    170      new_second = second
    171      if new_second < 0:
    172          new_second = 59
    173          new_minute = minute - 1
    174          if new_minute < 0:
    175              new_minute = 59
    176              new_hour = hour - 1
    177              if new_hour < 0:
    178                  new_hour = 23
    179      return (new_hour, new_minute, new_second)
    180