Gnumed.timelinelib.calendar.gregorian.timepicker.datecontroller

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

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

      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  import humblewx
     20  import wx
     21
     22
     23 -class GregorianDatePickerController(humblewx.Controller):
     24
     25 -    def on_init(self, date_formatter, date_modifier):
     26          self._date_formatter = date_formatter
     27          self._date_modifier = date_modifier
     28
     29 -    def on_text(self, event):
     30          self._validate()
     31
     32 -    def on_char(self, event):
     33          skip = True
     34          if event.GetKeyCode() == wx.WXK_TAB:
     35              if event.ShiftDown():
     36                  tab_handled = self.on_shift_tab()
     37              else:
     38                  tab_handled = self.on_tab()
     39              skip = not tab_handled
     40          elif event.GetKeyCode() == wx.WXK_UP:
     41              self.on_key_up()
     42              skip = False
     43          elif event.GetKeyCode() == wx.WXK_DOWN:
     44              self.on_key_down()
     45              skip = False
     46          event.Skip(skip)
     47
     48 -    def _validate(self):
     49          try:
     50              self.get_gregorian_date()
     51          except ValueError:
     52              self.view.SetBackgroundColour("pink")
     53          else:
     54              self.view.SetBackgroundColour(wx.NullColour)
     55
     56 -    def on_tab(self):
     57          return self._select_region(self._date_formatter.get_next_region)
     58
     59 -    def on_shift_tab(self):
     60          return self._select_region(self._date_formatter.get_previous_region)
     61
     62 -    def _select_region(self, get_region_fn):
     63          region = get_region_fn(
     64              self.view.GetText(),
     65              self.view.GetCursorPosition()
     66          )
     67          if region is None:
     68              return False
     69          else:
     70              self.view.SetSelection(region)
     71              return True
     72
     73 -    def on_key_up(self):
     74          try:
     75              date = self.get_gregorian_date()
     76          except ValueError:
     77              pass
     78          else:
     79              self.set_gregorian_date(self._get_incrementer()(date))
     80
     81 -    def on_key_down(self):
     82          try:
     83              date = self.get_gregorian_date()
     84          except ValueError:
     85              pass
     86          else:
     87              self.set_gregorian_date(self._get_decrementer()(date))
     88
     89 -    def set_gregorian_date(self, date):
     90          (formatted_date, is_bc) = self._date_formatter.format(date)
     91          self.view.SetText(formatted_date)
     92          self.view.SetIsBc(is_bc)
     93
     94 -    def get_gregorian_date(self):
     95          return self._date_formatter.parse((
     96              self.view.GetText(),
     97              self.view.GetIsBc(),
     98          ))
     99
    100 -    def _get_incrementer(self):
    101          return {
    102              self._date_formatter.YEAR: self._date_modifier.increment_year,
    103              self._date_formatter.MONTH: self._date_modifier.increment_month,
    104              self._date_formatter.DAY: self._date_modifier.increment_day,
    105          }[self._get_region_type()]
    106
    107 -    def _get_decrementer(self):
    108          return {
    109              self._date_formatter.YEAR: self._date_modifier.decrement_year,
    110              self._date_formatter.MONTH: self._date_modifier.decrement_month,
    111              self._date_formatter.DAY: self._date_modifier.decrement_day,
    112          }[self._get_region_type()]
    113
    114 -    def _get_region_type(self):
    115          return self._date_formatter.get_region_type(
    116              self.view.GetText(),
    117              self.view.GetCursorPosition()
    118          )
    119