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
     28 -    def __init__(self, parent):
     29          TextPatternControl.__init__(self, parent, fit_text="00:00")
     30          self.SetSeparators([":"])
     31          self.SetValidator(self._is_time_valid)
     32          self.SetUpHandler(self.HOUR_GROUP, self._increment_hour)
     33          self.SetUpHandler(self.MINUTE_GROUP, self._increment_minute)
     34          self.SetDownHandler(self.HOUR_GROUP, self._decrement_hour)
     35          self.SetDownHandler(self.MINUTE_GROUP, self._decrement_minute)
     36
     37 -    def GetGregorianTime(self):
     38          return parts_to_gregorian_time(self.GetParts())
     39
     40 -    def SetGregorianTime(self, time):
     41          self.SetParts(gregorian_time_to_parts(time))
     42
     43 -    def _is_time_valid(self):
     44          try:
     45              self.GetGregorianTime()
     46          except ValueError:
     47              return False
     48          else:
     49              return True
     50
     51 -    def _increment_hour(self):
     52          self.SetGregorianTime(increment_hour(self.GetGregorianTime()))
     53
     54 -    def _increment_minute(self):
     55          self.SetGregorianTime(increment_minute(self.GetGregorianTime()))
     56
     57 -    def _decrement_hour(self):
     58          self.SetGregorianTime(decrement_hour(self.GetGregorianTime()))
     59
     60 -    def _decrement_minute(self):
     61          self.SetGregorianTime(decrement_minute(self.GetGregorianTime()))
     62
     63
     64 -def gregorian_time_to_parts(time):
     65      (hour, minute, second) = time
     66      return [
     67          "%02d" % hour,
     68          "%02d" % minute,
     69      ]
     70
     71
     72 -def parts_to_gregorian_time(parts):
     73      [hour_str, minute_str] = parts
     74      hour = int(hour_str)
     75      minute = int(minute_str)
     76      if not is_valid_time(hour, minute, 0):
     77          raise ValueError()
     78      return (hour, minute, 0)
     79
     80
     81 -def increment_hour(time):
     82      hour, minute, second = time
     83      new_hour = hour + 1
     84      if new_hour > 23:
     85          new_hour = 0
     86      return (new_hour, minute, second)
     87
     88
     89 -def increment_minute(time):
     90      hour, minute, second = time
     91      new_hour = hour
     92      new_minute = minute + 1
     93      if new_minute > 59:
     94          new_minute = 0
     95          new_hour = hour + 1
     96          if new_hour > 23:
     97              new_hour = 0
     98      return (new_hour, new_minute, second)
     99
    100
    101 -def decrement_hour(time):
    102      hour, minute, second = time
    103      new_hour = hour - 1
    104      if new_hour < 0:
    105          new_hour = 23
    106      return (new_hour, minute, second)
    107
    108
    109 -def decrement_minute(time):
    110      hour, minute, second = time
    111      new_hour = hour
    112      new_minute = minute - 1
    113      if new_minute < 0:
    114          new_minute = 59
    115          new_hour = hour - 1
    116          if new_hour < 0:
    117              new_hour = 23
    118      return (new_hour, new_minute, second)
    119