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

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

      1

      2  import datetime
      3

      4  from PyQt4 import QtGui, QtCore
      5  from PyQt4.QtCore import Qt
      6

      7  from customeditor import CustomEditor, editingFinished
      8  from camelot.core import constants
      9  from camelot.view.art import Icon
     10  from camelot.view.utils import local_date_format, date_from_string, ParsingError
     11  from camelot.view.controls.decorated_line_edit import DecoratedLineEdit
     12

     13 -class DateEditor(CustomEditor):
     14      """Widget for editing date values"""
     15

     16      calendar_action_trigger = QtCore.SIGNAL('trigger()')
     17

     18 -    def __init__(self,

     19                   parent=None,

     20                   editable=True,

     21                   nullable=True,

     22                   format=constants.camelot_date_format,

     23                   **kwargs):
     24          CustomEditor.__init__(self, parent)
     25

     26          self.date_format = local_date_format()
     27          self.line_edit = DecoratedLineEdit()
     28          self.line_edit.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
     29          self.line_edit.set_background_text( QtCore.QDate(2000,1,1).toString(self.date_format) )
     30

     31          # The order of creation of this widgets and their parenting

     32          # seems very sensitive under windows and creates system crashes

     33          # so don't change this without extensive testing on windows

     34          special_date_menu = QtGui.QMenu(self)
     35          calendar_widget_action = QtGui.QWidgetAction(special_date_menu)
     36          self.calendar_widget = QtGui.QCalendarWidget(special_date_menu)
     37          self.connect( self.calendar_widget, QtCore.SIGNAL('activated(const QDate&)'), self.calendar_widget_activated)
     38          self.connect( self.calendar_widget, QtCore.SIGNAL('clicked(const QDate&)'), self.calendar_widget_activated)
     39          calendar_widget_action.setDefaultWidget(self.calendar_widget)
     40

     41          self.connect( self, self.calendar_action_trigger, special_date_menu.hide )
     42          special_date_menu.addAction(calendar_widget_action)
     43          special_date_menu.addAction('Today')
     44          special_date_menu.addAction('Far future')
     45          self.special_date = QtGui.QToolButton(self)
     46          self.special_date.setIcon(

     47              Icon('tango/16x16/apps/office-calendar.png').getQIcon())
     48          self.special_date.setAutoRaise(True)
     49          self.special_date.setToolTip('Calendar and special dates')
     50          self.special_date.setMenu(special_date_menu)
     51          self.special_date.setPopupMode(QtGui.QToolButton.InstantPopup)
     52          self.special_date.setFixedHeight(self.get_height())
     53          # end of sensitive part

     54

     55          self.set_enabled(editable)
     56

     57          if nullable:
     58              special_date_menu.addAction('Clear')
     59

     60          self.hlayout = QtGui.QHBoxLayout()
     61          self.hlayout.addWidget(self.line_edit)
     62          self.hlayout.addWidget(self.special_date)
     63

     64          self.hlayout.setContentsMargins(0, 0, 0, 0)
     65          self.hlayout.setMargin(0)
     66          self.hlayout.setSpacing(0)
     67

     68          self.setContentsMargins(0, 0, 0, 0)
     69          self.setLayout(self.hlayout)
     70

     71          self.minimum = datetime.date.min
     72          self.maximum = datetime.date.max
     73          self.setFocusProxy(self.line_edit)
     74

     75          self.connect(self.line_edit,

     76                       QtCore.SIGNAL('editingFinished()'),

     77                       self.editingFinished)
     78          self.connect(self.line_edit,

     79                       QtCore.SIGNAL('textEdited(const QString&)'),

     80                       self.text_edited)
     81          self.connect(special_date_menu,

     82                       QtCore.SIGNAL('triggered(QAction*)'),

     83                       self.setSpecialDate)
     84

     85 -    def calendar_widget_activated(self, date):
     86          self.emit(self.calendar_action_trigger)
     87          self.set_value(date)
     88          self.emit(editingFinished)
     89

     90 -    def editingFinished(self):
     91          self.emit(QtCore.SIGNAL('editingFinished()'))
     92

     93 -    def focusOutEvent(self, event):
     94          self.emit(QtCore.SIGNAL('editingFinished()'))
     95

     96 -    def set_value(self, value):
     97          value = CustomEditor.set_value(self, value)
     98          if value:
     99              qdate = QtCore.QDate(value)
    100              formatted_date = qdate.toString(self.date_format)
    101              self.line_edit.set_user_input(formatted_date)
    102              self.calendar_widget.setSelectedDate(qdate)
    103          else:
    104              self.line_edit.set_user_input('')
    105

    106 -    def text_edited(self, text ):
    107          try:
    108              date_from_string( self.line_edit.user_input() )
    109              self.line_edit.set_valid(True)
    110          except ParsingError:
    111              self.line_edit.set_valid(False)
    112

    113 -    def get_value(self):
    114          try:
    115              value = date_from_string( self.line_edit.user_input() )
    116          except ParsingError:
    117              value = None
    118          return CustomEditor.get_value(self) or value
    119

    120 -    def set_enabled(self, editable=True):
    121          self.line_edit.setEnabled(editable)
    122          if editable:
    123              self.special_date.show()
    124          else:
    125              self.special_date.hide()
    126

    127 -    def setSpecialDate(self, action):
    128          if action.text().compare('Today') == 0:
    129              self.set_value(datetime.date.today())
    130          elif action.text().compare('Far future') == 0:
    131              self.set_value(datetime.date( year = 2400, month = 12, day = 31 ))
    132          elif action.text().compare('Clear') == 0:
    133              self.set_value(None)
    134          self.emit(QtCore.SIGNAL('editingFinished()'))
    135