def _create_widget(cls, c, parent, host=None): dtt = QDateTimeEdit(parent) dtt.setObjectName(u'{0}_{1}'.format(cls._TYPE_PREFIX, c.name)) dtt.setCalendarPopup(True) # Set ranges if c.min_use_current_datetime: dtt.setMinimumDateTime(datetime.today()) else: dtt.setMinimumDateTime(c.minimum) if c.max_use_current_datetime: dtt.setMaximumDateTime(datetime.today()) else: dtt.setMaximumDateTime(c.maximum) # Set maximum datetime as current datetime dtt.setDateTime(dtt.maximumDateTime()) return dtt
class QtDatetimeSelector(QtBoundedDatetime, ProxyDatetimeSelector): """ A Qt implementation of an Enaml ProxyDatetimeSelector. """ #: A reference to the widget created by the proxy. widget = Typed(QDateTimeEdit) #-------------------------------------------------------------------------- # Initialization API #-------------------------------------------------------------------------- def create_widget(self): """ Create the QDateTimeEdit widget. """ self.widget = QDateTimeEdit(self.parent_widget()) def init_widget(self): """ Initialize the widget. """ super(QtDatetimeSelector, self).init_widget() d = self.declaration self.set_datetime_format(d.datetime_format) self.set_calendar_popup(d.calendar_popup) self.widget.dateTimeChanged.connect(self.on_datetime_changed) #-------------------------------------------------------------------------- # Abstract API Implementation #-------------------------------------------------------------------------- def get_datetime(self): """ Return the current datetime in the control. Returns ------- result : datetime The current control datetime as a datetime object. """ return self.widget.dateTime().toPyDateTime() def set_minimum(self, datetime): """ Set the widget's minimum datetime. Parameters ---------- datetime : datetime The datetime object to use for setting the minimum datetime. """ self.widget.setMinimumDateTime(datetime) def set_maximum(self, datetime): """ Set the widget's maximum datetime. Parameters ---------- datetime : datetime The datetime object to use for setting the maximum datetime. """ self.widget.setMaximumDateTime(datetime) def set_datetime(self, datetime): """ Set the widget's current datetime. Parameters ---------- datetime : datetime The datetime object to use for setting the datetime. """ self._guard |= CHANGED_GUARD try: self.widget.setDateTime(datetime) finally: self._guard &= ~CHANGED_GUARD def set_datetime_format(self, format): """ Set the widget's datetime format. Parameters ---------- format : str A Python time formatting string. """ # XXX make sure Python's and Qt's format strings are the # same, or convert between the two. self.widget.setDisplayFormat(format) def set_calendar_popup(self, popup): """ Set whether a calendar popup is available on the widget. Parameters ---------- popup : bool Whether the calendar popup is enabled. """ self.widget.setCalendarPopup(popup)