Package Camelot :: Package camelot :: Package view :: Package controls :: Package editors :: Module datetimeeditor |
[frames] | no frames]

# Source Code for Module Camelot.camelot.view.controls.editors.datetimeeditor

      1

      2  import datetime
      3

      4  from PyQt4 import QtGui, QtCore
      5

      6  from customeditor import CustomEditor
      7  from camelot.core import constants
      8

      9 -class DateTimeEditor(CustomEditor):
     10      """Widget for editing date and time separated and with popups"""
     11

     12 -    def __init__(self,

     13                   parent,

     14                   editable=True,

     15                   format=constants.camelot_datetime_format,

     16                   nullable=True,

     17                   **kwargs):
     18          CustomEditor.__init__(self, parent)
     19          import itertools
     20          self.nullable = nullable
     21          dateformat, _timeformat = format.split(' ')
     22          layout = QtGui.QHBoxLayout()
     23          self.dateedit = QtGui.QDateEdit(self)
     24          self.dateedit.setEnabled(editable)
     25          self.dateedit.setDisplayFormat(dateformat)
     26          self.dateedit.setCalendarPopup(True)
     27          layout.addWidget(self.dateedit)
     28

     29          class TimeValidator(QtGui.QValidator):
     30              def __init__(self, parent):
     31                  QtGui.QValidator.__init__(self, parent)
     32              def validate(self, input, pos):
     33                  parts = str(input).split(':')
     34                  if len(parts)!=2:
     35                      return (QtGui.QValidator.Invalid, pos)
     36                  if str(input)=='--:--' and nullable:
     37                      return (QtGui.QValidator.Acceptable, pos)
     38                  for part in parts:
     39                      if not part.isdigit():
     40                          return (QtGui.QValidator.Invalid, pos)
     41                      if len(part) not in (1,2):
     42                          return (QtGui.QValidator.Intermediate, pos)
     43                  if not int(parts[0]) in range(0,24):
     44                      return (QtGui.QValidator.Invalid, pos)
     45                  if not int(parts[1]) in range(0,60):
     46                      return (QtGui.QValidator.Invalid, pos)
     47                  return (QtGui.QValidator.Acceptable, pos)
     48

     49          self.timeedit = QtGui.QComboBox(self)
     50          self.timeedit.setEditable(True)
     51          if not editable:
     52              self.timeedit.setEnabled(False)
     53

     54          time_entries = [entry

     55                          for entry in itertools.chain(*(('%02i:00'%i, '%02i:30'%i)

     56                          for i in range(0,24)))]
     57          self.timeedit.addItems(time_entries)
     58          self.timeedit.setValidator(TimeValidator(self))
     59

     60  #    self.timeedit = QtGui.QTimeEdit(self)

     61  #    self.timeedit.setDisplayFormat(timeformat)

     62  #

     63  #    setting the tab order does not seem to work inside a table

     64  #

     65  #    self.dateedit.setTabOrder(self.dateedit, self.timeedit)

     66

     67  #    Completion doesn't seems to work with a QTimeEdit widget

     68  #

     69  #    time_lineedit = self.timeedit.lineEdit()

     70  #    time_completions_model = QtGui.QStringListModel(['00:00', '00:30'], parent)

     71  #    time_completer = QtGui.QCompleter()

     72  #    time_completer.setModel(time_completions_model)

     73  #    time_completer.setCaseSensitivity(Qt.CaseInsensitive)

     74  #    time_completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)

     75  #    time_lineedit.setCompleter(time_completer)

     76

     77          layout.addWidget(self.timeedit)
     78          self.setFocusProxy(self.dateedit)
     79          self.setLayout(layout)
     80          layout.setMargin(0)
     81          layout.setSpacing(0)
     82          layout.addStretch(1)
     83

     84 -    def get_value(self):
     85          time_value = self.time()
     86          date_value = self.date()
     87          if time_value!=None and date_value!=None:
     88              value = datetime.datetime(hour=time_value.hour(),

     89                                        minute=time_value.minute(),

     90                                        second=time_value.second(),

     91                                        year=date_value.year(),

     92                                        month=date_value.month(),

     93                                        day=date_value.day())
     94          else:
     95              value = None
     96          return CustomEditor.get_value(self) or value
     97

     98 -    def set_value(self, value):
     99          value = CustomEditor.set_value(self, value)
    100          if value:
    101              self.dateedit.setDate(QtCore.QDate(value.year, value.month, value.day))
    102              self.timeedit.lineEdit().setText('%02i:%02i'%(value.hour, value.minute))
    103          else:
    104              self.dateedit.setDate(self.dateedit.minimumDate())
    105              self.timeedit.lineEdit().setText('--:--')
    106

    107 -    def date(self):
    108          return self.dateedit.date()
    109

    110 -    def time(self):
    111          text = str(self.timeedit.currentText())
    112          if text=='--:--':
    113              return None
    114          parts = text.split(':')
    115          return QtCore.QTime(int(parts[0]), int(parts[1]))
    116

    117 -    def set_enabled(self, editable=True):
    118          self.timeedit.setEnabled(editable)
    119          self.dateedit.setEnabled(editable)
    120