class DateEdit(QLineEdit): KEY2METHOD = { Qt.Key_Left: 'left', Qt.Key_Right: 'right', Qt.Key_Up: 'increase', Qt.Key_Down: 'decrease', Qt.Key_Backspace: 'backspace', Qt.Key_Delete: 'backspace', } ACCEPTED_KEYS = set([Qt.Key_Escape, Qt.Key_Tab, Qt.Key_Backtab, Qt.Key_Return, Qt.Key_Enter]) DATE_FORMAT = 'dd/MM/yyyy' def __init__(self, parent): QLineEdit.__init__(self, parent) self.widget = DateWidget(self.DATE_FORMAT) def _refresh(self): self.setText(self.widget.text) selStart, selEnd = self.widget.selection self.setSelection(selStart, selEnd-selStart+1) #--- QLineEdit overrides def keyPressEvent(self, event): key = event.key() if key in self.KEY2METHOD: getattr(self.widget, self.KEY2METHOD[key])() self._refresh() elif key in self.ACCEPTED_KEYS: # We want keypresses like Escape to go through. QLineEdit.keyPressEvent(self, event) else: text = str(event.text()) if text in "0123456789/-.": self.widget.type(text) self._refresh() def focusInEvent(self, event): QLineEdit.focusInEvent(self, event) self.widget.text = str(self.text()) # A timer is used here because a mouse event following the focusInEvent messes up the # selection (so the refresh *has* to happen after the mouse event). QTimer.singleShot(0, self._refresh) def focusOutEvent(self, event): self.prepareDataForCommit() QLineEdit.focusOutEvent(self, event) #--- Public def prepareDataForCommit(self): self.widget.exit() self._refresh()
def __init__(self): self.date_format = clean_format(proxy.systemShortDateFormat()) self.w = DateWidget(self.date_format)
class PyDateWidget: def __init__(self): self.date_format = clean_format(proxy.systemShortDateFormat()) self.w = DateWidget(self.date_format) def increase(self): self.w.increase() def decrease(self): self.w.decrease() def left(self): self.w.left() def right(self): self.w.right() def backspace(self): self.w.backspace() def exit(self): self.w.exit() def type_(self, something: str): self.w.type(something) def setDate_(self, str_date: str): self.w.text = str_date def text(self) -> str: return self.w.text def selection(self) -> list: # list of numbers return self.w.selection
def __init__(self, parent, is_clearable=False): self.widget = DateWidget(self.DATE_FORMAT) self.widget.text = '' ClearableEdit.__init__(self, parent, is_clearable=is_clearable)
class DateEdit(ClearableEdit): KEY2METHOD = { Qt.Key_Left: 'left', Qt.Key_Right: 'right', Qt.Key_Up: 'increase', Qt.Key_Down: 'decrease', Qt.Key_Backspace: 'backspace', Qt.Key_Delete: 'backspace', } ACCEPTED_KEYS = {Qt.Key_Escape, Qt.Key_Tab, Qt.Key_Backtab, Qt.Key_Return, Qt.Key_Enter} DATE_FORMAT = 'dd/MM/yyyy' def __init__(self, parent, is_clearable=False): self.widget = DateWidget(self.DATE_FORMAT) self.widget.text = '' ClearableEdit.__init__(self, parent, is_clearable=is_clearable) def _refresh(self): self.setText(self.widget.text) selStart, selEnd = self.widget.selection self.setSelection(selStart, selEnd-selStart+1) # --- Overrides def _clearSearch(self): self.widget.date = None self._refresh() def _hasClearableContent(self): return self.widget.date is not None def keyPressEvent(self, event): key = event.key() if key in self.KEY2METHOD: getattr(self.widget, self.KEY2METHOD[key])() self._refresh() elif key in self.ACCEPTED_KEYS: # We want keypresses like Escape to go through. ClearableEdit.keyPressEvent(self, event) else: text = str(event.text()) if text in "0123456789/-.": self.widget.type(text) self._refresh() def focusInEvent(self, event): ClearableEdit.focusInEvent(self, event) self.widget.text = str(self.text()) # A timer is used here because a mouse event following the focusInEvent messes up the # selection (so the refresh *has* to happen after the mouse event). QTimer.singleShot(0, self._refresh) def focusOutEvent(self, event): self.prepareDataForCommit() ClearableEdit.focusOutEvent(self, event) def setText(self, value): self.widget.text = value super().setText(value) # --- Public def prepareDataForCommit(self): self.widget.exit() self._refresh()
def __init__(self): self.date_format = clean_format(proxy.systemShortDateFormat()) self.w = DateWidget(self.date_format)
class PyDateWidget: def __init__(self): self.date_format = clean_format(proxy.systemShortDateFormat()) self.w = DateWidget(self.date_format) def increase(self): self.w.increase() def decrease(self): self.w.decrease() def left(self): self.w.left() def right(self): self.w.right() def backspace(self): self.w.backspace() def exit(self): self.w.exit() def type_(self, something: str): self.w.type(something) def setDate_(self, str_date: str): self.w.text = str_date def text(self) -> str: return self.w.text def selection(self) -> list: # list of numbers return self.w.selection
def __init__(self, parent): QLineEdit.__init__(self, parent) self.widget = DateWidget(self.DATE_FORMAT)