From 861d1ff53ef63f2e75da609575896558f6b602e5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 18 May 2013 10:12:55 +0530 Subject: [PATCH] When editing dates such as published, allow pressing the minus key to clear the date and the = key to set the date to today. Fixes #1181449 ([enhance] Now & Clear for Date Cells) --- src/calibre/gui2/library/delegates.py | 43 ++++++++++++---------- src/calibre/gui2/metadata/basic_widgets.py | 12 +++++- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/calibre/gui2/library/delegates.py b/src/calibre/gui2/library/delegates.py index 62e4eaf0a2..ae40352aee 100644 --- a/src/calibre/gui2/library/delegates.py +++ b/src/calibre/gui2/library/delegates.py @@ -9,7 +9,7 @@ import sys from PyQt4.Qt import (Qt, QApplication, QStyle, QIcon, QDoubleSpinBox, QVariant, QSpinBox, QStyledItemDelegate, QComboBox, QTextDocument, - QAbstractTextDocumentLayout, QFont, QFontInfo, QDate) + QAbstractTextDocumentLayout, QFont, QFontInfo, QDate, QDateTimeEdit, QDateTime) from calibre.gui2 import UNDEFINED_QDATETIME, error_dialog, rating_font from calibre.constants import iswindows @@ -23,6 +23,26 @@ from calibre.gui2.dialogs.comments_dialog import CommentsDialog from calibre.gui2.dialogs.template_dialog import TemplateDialog from calibre.gui2.languages import LanguagesEdit +class DateTimeEdit(QDateTimeEdit): # {{{ + + def __init__(self, parent, format): + QDateTimeEdit.__init__(self, parent) + self.setFrame(False) + self.setMinimumDateTime(UNDEFINED_QDATETIME) + self.setSpecialValueText(_('Undefined')) + self.setCalendarPopup(True) + self.setDisplayFormat(format) + + def keyPressEvent(self, ev): + if ev.key() == Qt.Key_Minus: + ev.accept() + self.setDateTime(self.minimumDateTime()) + elif ev.key() == Qt.Key_Equal: + ev.accept() + self.setDateTime(QDateTime.currentDateTime()) + else: + return QDateTimeEdit.keyPressEvent(self, ev) +# }}} class RatingDelegate(QStyledItemDelegate): # {{{ @@ -77,12 +97,7 @@ class DateDelegate(QStyledItemDelegate): # {{{ return format_date(qt_to_dt(d, as_utc=False), self.format) def createEditor(self, parent, option, index): - qde = QStyledItemDelegate.createEditor(self, parent, option, index) - qde.setDisplayFormat(self.format) - qde.setMinimumDateTime(UNDEFINED_QDATETIME) - qde.setSpecialValueText(_('Undefined')) - qde.setCalendarPopup(True) - return qde + return DateTimeEdit(parent, self.format) # }}} @@ -101,12 +116,7 @@ class PubDateDelegate(QStyledItemDelegate): # {{{ return format_date(qt_to_dt(d, as_utc=False), self.format) def createEditor(self, parent, option, index): - qde = QStyledItemDelegate.createEditor(self, parent, option, index) - qde.setDisplayFormat(self.format) - qde.setMinimumDateTime(UNDEFINED_QDATETIME) - qde.setSpecialValueText(_('Undefined')) - qde.setCalendarPopup(True) - return qde + return DateTimeEdit(parent, self.format) def setEditorData(self, editor, index): val = index.data(Qt.EditRole).toDate() @@ -230,12 +240,7 @@ class CcDateDelegate(QStyledItemDelegate): # {{{ return format_date(qt_to_dt(d, as_utc=False), self.format) def createEditor(self, parent, option, index): - qde = QStyledItemDelegate.createEditor(self, parent, option, index) - qde.setDisplayFormat(self.format) - qde.setMinimumDateTime(UNDEFINED_QDATETIME) - qde.setSpecialValueText(_('Undefined')) - qde.setCalendarPopup(True) - return qde + return DateTimeEdit(parent, self.format) def setEditorData(self, editor, index): m = index.model() diff --git a/src/calibre/gui2/metadata/basic_widgets.py b/src/calibre/gui2/metadata/basic_widgets.py index 5544c52273..56e1317948 100644 --- a/src/calibre/gui2/metadata/basic_widgets.py +++ b/src/calibre/gui2/metadata/basic_widgets.py @@ -13,7 +13,7 @@ from PyQt4.Qt import (Qt, QDateTimeEdit, pyqtSignal, QMessageBox, QIcon, QToolButton, QWidget, QLabel, QGridLayout, QApplication, QDoubleSpinBox, QListWidgetItem, QSize, QPixmap, QDialog, QMenu, QPushButton, QSpinBox, QLineEdit, QSizePolicy, QDialogButtonBox, - QAction, QCalendarWidget, QDate) + QAction, QCalendarWidget, QDate, QDateTime) from calibre.gui2.widgets import EnLineEdit, FormatList as _FormatList, ImageView from calibre.utils.icu import sort_key @@ -1472,6 +1472,16 @@ class DateEdit(QDateTimeEdit): o, c = self.original_val, self.current_val return o != c + def keyPressEvent(self, ev): + if ev.key() == Qt.Key_Minus: + ev.accept() + self.setDateTime(self.minimumDateTime()) + elif ev.key() == Qt.Key_Equal: + ev.accept() + self.setDateTime(QDateTime.currentDateTime()) + else: + return QDateTimeEdit.keyPressEvent(self, ev) + class PubdateEdit(DateEdit): LABEL = _('Publishe&d:') FMT = 'MMM yyyy'