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

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

      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 wx
     20
     21  from timelinelib.calendar.gregorian.gregorian import GregorianDateTime
     22  from timelinelib.calendar.gregorian.time import GregorianDelta
     23  from timelinelib.calendar.gregorian.timepicker.datecontroller import GregorianDatePickerController
     24  from timelinelib.calendar.gregorian.timetype import GregorianTimeType
     25  from timelinelib.wxgui.components.textctrl import TextCtrl
     26
     27
     28 -class GregorianDatePicker(wx.Panel):
     29
     30 -    def __init__(self, parent, date_formatter, name=None):
     31          wx.Panel.__init__(self, parent)
     32          self._controller = GregorianDatePickerController(self)
     33          self._create_gui(date_formatter)
     34          self._controller.on_init(
     35              date_formatter,
     36              DateModifier()
     37          )
     38
     39 -    def _create_gui(self, date_formatter):
     40          self._create_date_text(date_formatter)
     41          self._create_bc_button()
     42          self._layout()
     43
     44 -    def _create_date_text(self, date_formatter):
     45          self.date_text = TextCtrl(
     46              self,
     47              style=wx.TE_PROCESS_ENTER,
     48              fit_text=self._format_sample_date(date_formatter)
     49          )
     50          self.date_text.Bind(wx.EVT_CHAR, self._controller.on_char)
     51          self.date_text.Bind(wx.EVT_TEXT, self._controller.on_text)
     52
     53 -    def _create_bc_button(self):
     54          label = _("BC")
     55          self.bc_button = wx.ToggleButton(self, label=label)
     56          label_width = self.bc_button.GetTextExtent(label)[0]
     57          self.bc_button.SetMinSize((label_width + 20, -1))
     58
     59 -    def _layout(self):
     60          sizer = wx.BoxSizer(wx.HORIZONTAL)
     61          sizer.Add(self.date_text, flag=wx.EXPAND, proportion=1)
     62          sizer.Add(self.bc_button, flag=wx.EXPAND)
     63          self.SetSizer(sizer)
     64
     65 -    def _format_sample_date(self, date_formatter):
     66          return date_formatter.format(
     67              GregorianDateTime.from_time(
     68                  GregorianTimeType().now()
     69              ).to_date_tuple()
     70          )[0]
     71
     72 -    def GetGregorianDate(self):
     73          return self._controller.get_gregorian_date()
     74
     75 -    def SetGregorianDate(self, date):
     76          self._controller.set_gregorian_date(date)
     77
     78 -    def GetText(self):
     79          return self.date_text.GetValue()
     80
     81 -    def SetText(self, text):
     82          x = self.date_text.GetInsertionPoint()
     83          self.date_text.SetValue(text)
     84          self.date_text.SetInsertionPoint(x)
     85
     86 -    def SetSelection(self, pos_lenght_tuple):
     87          (pos, lenght) = pos_lenght_tuple
     88          self.date_text.SetSelection(pos, pos + lenght)
     89
     90 -    def GetCursorPosition(self):
     91          return self.date_text.GetInsertionPoint()
     92
     93 -    def GetIsBc(self):
     94          return self.bc_button.GetValue()
     95
     96 -    def SetIsBc(self, is_bc):
     97          self.bc_button.SetValue(is_bc)
     98
     99 -    def SetBackgroundColour(self, colour):
    100          self.date_text.SetBackgroundColour(colour)
    101          self.date_text.Refresh()
    102
    103
    104 -class DateModifier(object):
    105
    106 -    def increment_year(self, date):
    107          max_year = GregorianDateTime.from_time(GregorianTimeType().get_max_time()).year
    108          year, month, day = date
    109          if year < max_year - 1:
    110              return self._set_valid_day(year + 1, month, day)
    111          return date
    112
    113 -    def increment_month(self, date):
    114          max_year = GregorianDateTime.from_time(GregorianTimeType().get_max_time()).year
    115          year, month, day = date
    116          if month < 12:
    117              return self._set_valid_day(year, month + 1, day)
    118          elif year < max_year - 1:
    119              return self._set_valid_day(year + 1, 1, day)
    120          return date
    121
    122 -    def increment_day(self, date):
    123          year, month, day = date
    124          time = GregorianDateTime.from_ymd(year, month, day).to_time()
    125          if time < GregorianTimeType().get_max_time() - GregorianDelta.from_days(1):
    126              return GregorianDateTime.from_time(time + GregorianDelta.from_days(1)).to_date_tuple()
    127          return date
    128
    129 -    def decrement_year(self, date):
    130          year, month, day = date
    131          if year > GregorianDateTime.from_time(GregorianTimeType().get_min_time()).year:
    132              return self._set_valid_day(year - 1, month, day)
    133          return date
    134
    135 -    def decrement_month(self, date):
    136          year, month, day = date
    137          if month > 1:
    138              return self._set_valid_day(year, month - 1, day)
    139          elif year > GregorianDateTime.from_time(GregorianTimeType().get_min_time()).year:
    140              return self._set_valid_day(year - 1, 12, day)
    141          return date
    142
    143 -    def decrement_day(self, date):
    144          year, month, day = date
    145          if day > 1:
    146              return self._set_valid_day(year, month, day - 1)
    147          elif month > 1:
    148              return self._set_valid_day(year, month - 1, 31)
    149          elif year > GregorianDateTime.from_time(GregorianTimeType().get_min_time()).year:
    150              return self._set_valid_day(year - 1, 12, 31)
    151          return date
    152
    153 -    def _set_valid_day(self, new_year, new_month, new_day):
    154          done = False
    155          while not done:
    156              try:
    157                  date = GregorianDateTime.from_ymd(new_year, new_month, new_day)
    158                  done = True
    159              except Exception:
    160                  new_day -= 1
    161          return date.to_date_tuple()
    162