def __init__(self, parent, editable=True, nullable=True, **kwargs): CustomEditor.__init__(self, parent) import itertools self.nullable = nullable layout = QtGui.QHBoxLayout() self.dateedit = DateEditor(self, editable=editable, nullable=nullable, **kwargs) self.dateedit.editingFinished.connect(self.editing_finished) layout.addWidget(self.dateedit, 1) self.timeedit = QtGui.QComboBox(self) self.timeedit.setEditable(True) if not editable: self.timeedit.setEnabled(False) time_entries = [ entry for entry in itertools.chain(*(('%02i:00' % i, '%02i:30' % i) for i in range(0, 24))) ] self.timeedit.addItems(time_entries) self.timeedit.setValidator(TimeValidator(self, nullable)) self.timeedit.currentIndexChanged.connect(self.editing_finished) self.timeedit.editTextChanged.connect(self.editing_finished) self.timeedit.setFocusPolicy(Qt.StrongFocus) layout.addWidget(self.timeedit, 1) # focus proxy is needed to activate the editor with a single click self.setFocusProxy(self.dateedit) self.setLayout(layout) layout.setMargin(0) layout.setSpacing(0)
def __init__(self, parent, editable=True, nullable=True, **kwargs): CustomEditor.__init__(self, parent) import itertools self.nullable = nullable layout = QtGui.QHBoxLayout() self.dateedit = DateEditor(self, editable=editable, nullable=nullable, **kwargs) self.dateedit.editingFinished.connect( self.editing_finished ) layout.addWidget(self.dateedit, 1) self.timeedit = QtGui.QComboBox(self) self.timeedit.setEditable(True) if not editable: self.timeedit.setEnabled(False) time_entries = [entry for entry in itertools.chain(*(('%02i:00'%i, '%02i:30'%i) for i in range(0,24)))] self.timeedit.addItems(time_entries) self.timeedit.setValidator(TimeValidator(self, nullable)) self.timeedit.currentIndexChanged.connect( self.editing_finished ) self.timeedit.editTextChanged.connect( self.editing_finished ) self.timeedit.setFocusPolicy( Qt.StrongFocus ) layout.addWidget(self.timeedit, 1) # focus proxy is needed to activate the editor with a single click self.setFocusProxy(self.dateedit) self.setLayout(layout)
class DateTimeEditor(CustomEditor): """Widget for editing date and time separated and with popups""" def __init__(self, parent, editable=True, nullable=True, **kwargs): CustomEditor.__init__(self, parent) import itertools self.nullable = nullable layout = QtGui.QHBoxLayout() self.dateedit = DateEditor(self, editable=editable, nullable=nullable, **kwargs) self.dateedit.editingFinished.connect( self.editing_finished ) layout.addWidget(self.dateedit, 1) self.timeedit = QtGui.QComboBox(self) self.timeedit.setEditable(True) if not editable: self.timeedit.setEnabled(False) time_entries = [entry for entry in itertools.chain(*(('%02i:00'%i, '%02i:30'%i) for i in range(0,24)))] self.timeedit.addItems(time_entries) self.timeedit.setValidator(TimeValidator(self, nullable)) self.timeedit.currentIndexChanged.connect( self.editing_finished ) self.timeedit.editTextChanged.connect( self.editing_finished ) self.timeedit.setFocusPolicy( Qt.StrongFocus ) layout.addWidget(self.timeedit, 1) # focus proxy is needed to activate the editor with a single click self.setFocusProxy(self.dateedit) self.setLayout(layout) layout.setMargin(0) layout.setSpacing(0) @QtCore.pyqtSlot(QtCore.QString) @QtCore.pyqtSlot(int) @QtCore.pyqtSlot() def editing_finished(self, _arg=None): if self.time() and self.date(): self.editingFinished.emit() def get_value(self): time_value = self.time() date_value = self.date() if time_value not in (None, ValueLoading) and date_value not in (None, ValueLoading): value = datetime.datetime(hour=time_value.hour(), minute=time_value.minute(), second=time_value.second(), year=date_value.year, month=date_value.month, day=date_value.day) else: value = None return CustomEditor.get_value(self) or value def set_value(self, value): value = CustomEditor.set_value(self, value) if value: self.dateedit.set_value(value.date()) self.timeedit.lineEdit().setText('%02i:%02i'%(value.hour, value.minute)) else: self.dateedit.set_value(None) self.timeedit.lineEdit().setText('--:--') def date(self): return self.dateedit.get_value() def time(self): text = str(self.timeedit.currentText()) if text=='--:--': return None parts = text.split(':') return QtCore.QTime(int(parts[0]), int(parts[1])) def set_enabled(self, editable=True):
class DateTimeEditor(CustomEditor): """Widget for editing date and time separated and with popups""" def __init__(self, parent, editable=True, nullable=True, **kwargs): CustomEditor.__init__(self, parent) import itertools self.nullable = nullable layout = QtGui.QHBoxLayout() self.dateedit = DateEditor(self, editable=editable, nullable=nullable, **kwargs) self.dateedit.editingFinished.connect(self.editing_finished) layout.addWidget(self.dateedit, 1) self.timeedit = QtGui.QComboBox(self) self.timeedit.setEditable(True) if not editable: self.timeedit.setEnabled(False) time_entries = [ entry for entry in itertools.chain(*(('%02i:00' % i, '%02i:30' % i) for i in range(0, 24))) ] self.timeedit.addItems(time_entries) self.timeedit.setValidator(TimeValidator(self, nullable)) self.timeedit.currentIndexChanged.connect(self.editing_finished) self.timeedit.editTextChanged.connect(self.editing_finished) self.timeedit.setFocusPolicy(Qt.StrongFocus) layout.addWidget(self.timeedit, 1) # focus proxy is needed to activate the editor with a single click self.setFocusProxy(self.dateedit) self.setLayout(layout) layout.setMargin(0) layout.setSpacing(0) @QtCore.pyqtSlot(QtCore.QString) @QtCore.pyqtSlot(int) @QtCore.pyqtSlot() def editing_finished(self, _arg=None): if self.time() and self.date(): self.editingFinished.emit() def get_value(self): time_value = self.time() date_value = self.date() if time_value not in (None, ValueLoading) and date_value not in ( None, ValueLoading): value = datetime.datetime(hour=time_value.hour(), minute=time_value.minute(), second=time_value.second(), year=date_value.year, month=date_value.month, day=date_value.day) else: value = None return CustomEditor.get_value(self) or value def set_value(self, value): value = CustomEditor.set_value(self, value) if value: self.dateedit.set_value(value.date()) self.timeedit.lineEdit().setText('%02i:%02i' % (value.hour, value.minute)) else: self.dateedit.set_value(None) self.timeedit.lineEdit().setText('--:--') def date(self): return self.dateedit.get_value() def time(self): text = str(self.timeedit.currentText()) if text == '--:--': return None parts = text.split(':') return QtCore.QTime(int(parts[0]), int(parts[1])) def set_enabled(self, editable=True): self.timeedit.setEnabled(editable) self.dateedit.setEnabled(editable)