mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Keyboard modifiers (again) for delegates. Make CTRL+F2 clear the field's value for all fields. SHIFT+F2 sets date fields to now().
This commit is contained in:
parent
f8670c524c
commit
b6287452c4
@ -158,6 +158,17 @@ ClearingDoubleSpinBox = make_clearing_spinbox(QDoubleSpinBox)
|
||||
|
||||
# }}}
|
||||
|
||||
# setter for text-like delegates. Return '' if CTRL is pushed {{{
|
||||
|
||||
def get_val_for_textlike_columns(index_):
|
||||
if QApplication.keyboardModifiers() == Qt.ControlModifier:
|
||||
ct = ''
|
||||
else:
|
||||
ct = index_.data(Qt.DisplayRole) or ''
|
||||
return unicode(ct)
|
||||
|
||||
# }}}
|
||||
|
||||
class RatingDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
@ -190,6 +201,13 @@ class RatingDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
|
||||
r = 0
|
||||
return u'\u2605'*r
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
if QApplication.keyboardModifiers() == Qt.ControlModifier:
|
||||
val = 0
|
||||
else:
|
||||
val = index.data(Qt.EditRole)
|
||||
editor.setValue(val)
|
||||
|
||||
def sizeHint(self, option, index):
|
||||
option.font = self.rf
|
||||
option.textElideMode = self.em
|
||||
@ -223,8 +241,10 @@ class DateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
|
||||
return DateTimeEdit(parent, self.format)
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
if QApplication.keyboardModifiers() == Qt.ShiftModifier:
|
||||
if QApplication.keyboardModifiers() == Qt.ControlModifier:
|
||||
val = UNDEFINED_QDATETIME
|
||||
elif QApplication.keyboardModifiers() == Qt.ShiftModifier:
|
||||
val = now()
|
||||
else:
|
||||
val = index.data(Qt.EditRole)
|
||||
editor.setDateTime(val)
|
||||
@ -251,7 +271,7 @@ class PubDateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
val = index.data(Qt.EditRole)
|
||||
if is_date_undefined(val):
|
||||
if is_date_undefined(val) or QApplication.keyboardModifiers() == Qt.ControlModifier:
|
||||
val = QDate(2000, 1, 1)
|
||||
if isinstance(val, QDateTime):
|
||||
val = val.date()
|
||||
@ -285,8 +305,7 @@ class TextDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
|
||||
return editor
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
ct = unicode(index.data(Qt.DisplayRole) or '')
|
||||
editor.setText(ct)
|
||||
editor.setText(get_val_for_textlike_columns(index))
|
||||
editor.selectAll()
|
||||
|
||||
def setModelData(self, editor, model, index):
|
||||
@ -337,8 +356,7 @@ class CompleteDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
|
||||
return editor
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
ct = unicode(index.data(Qt.DisplayRole) or '')
|
||||
editor.setText(ct)
|
||||
editor.setText(get_val_for_textlike_columns(index))
|
||||
editor.selectAll()
|
||||
|
||||
def setModelData(self, editor, model, index):
|
||||
@ -361,8 +379,7 @@ class LanguagesDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
|
||||
return editor
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
ct = unicode(index.data(Qt.DisplayRole) or '')
|
||||
editor.show_initial_value(ct)
|
||||
editor.show_initial_value(get_val_for_textlike_columns(index))
|
||||
|
||||
def setModelData(self, editor, model, index):
|
||||
val = ','.join(editor.lang_codes)
|
||||
@ -397,8 +414,10 @@ class CcDateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
|
||||
return DateTimeEdit(parent, self.format)
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
if QApplication.keyboardModifiers() == Qt.ShiftModifier:
|
||||
if QApplication.keyboardModifiers() == Qt.ControlModifier:
|
||||
val = UNDEFINED_QDATETIME
|
||||
elif QApplication.keyboardModifiers() == Qt.ShiftModifier:
|
||||
val = now()
|
||||
else:
|
||||
m = index.model()
|
||||
# db col is not named for the field, but for the table number. To get it,
|
||||
@ -437,8 +456,7 @@ class CcTextDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
|
||||
return editor
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
ct = unicode(index.data(Qt.DisplayRole) or '')
|
||||
editor.setText(ct)
|
||||
editor.setText(get_val_for_textlike_columns(index))
|
||||
editor.selectAll()
|
||||
|
||||
def setModelData(self, editor, model, index):
|
||||
@ -481,7 +499,7 @@ class CcNumberDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
|
||||
def setEditorData(self, editor, index):
|
||||
m = index.model()
|
||||
val = m.db.data[index.row()][m.custom_columns[m.column_map[index.column()]]['rec_index']]
|
||||
if val is None:
|
||||
if val is None or QApplication.keyboardModifiers() == Qt.ControlModifier:
|
||||
val = 0
|
||||
editor.setValue(val)
|
||||
|
||||
@ -532,7 +550,7 @@ class CcEnumDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
|
||||
def setEditorData(self, editor, index):
|
||||
m = index.model()
|
||||
val = m.db.data[index.row()][m.custom_columns[m.column_map[index.column()]]['rec_index']]
|
||||
if val is None:
|
||||
if val is None or QApplication.keyboardModifiers() == Qt.ControlModifier:
|
||||
val = ''
|
||||
idx = editor.findText(val)
|
||||
if idx < 0:
|
||||
@ -576,7 +594,10 @@ class CcCommentsDelegate(QStyledItemDelegate): # {{{
|
||||
def createEditor(self, parent, option, index):
|
||||
m = index.model()
|
||||
col = m.column_map[index.column()]
|
||||
text = m.db.data[index.row()][m.custom_columns[col]['rec_index']]
|
||||
if QApplication.keyboardModifiers() == Qt.ControlModifier:
|
||||
text = ''
|
||||
else:
|
||||
text = m.db.data[index.row()][m.custom_columns[col]['rec_index']]
|
||||
editor = CommentsDialog(parent, text, column_name=m.custom_columns[col]['name'])
|
||||
d = editor.exec_()
|
||||
if d:
|
||||
@ -634,9 +655,10 @@ class CcBoolDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
|
||||
m = index.model()
|
||||
val = m.db.data[index.row()][m.custom_columns[m.column_map[index.column()]]['rec_index']]
|
||||
if not m.db.prefs.get('bools_are_tristate'):
|
||||
val = 1 if not val else 0
|
||||
val = 1 if not val or QApplication.keyboardModifiers() != Qt.ControlModifier else 0
|
||||
else:
|
||||
val = 2 if val is None else 1 if not val else 0
|
||||
val = 2 if val is None or QApplication.keyboardModifiers() == Qt.ControlModifier \
|
||||
else 1 if not val else 0
|
||||
editor.setCurrentIndex(val)
|
||||
|
||||
# }}}
|
||||
@ -652,7 +674,10 @@ class CcTemplateDelegate(QStyledItemDelegate): # {{{
|
||||
def createEditor(self, parent, option, index):
|
||||
m = index.model()
|
||||
mi = m.db.get_metadata(index.row(), index_is_id=False)
|
||||
text = m.custom_columns[m.column_map[index.column()]]['display']['composite_template']
|
||||
if QApplication.keyboardModifiers() == Qt.ControlModifier:
|
||||
text = u''
|
||||
else:
|
||||
text = m.custom_columns[m.column_map[index.column()]]['display']['composite_template']
|
||||
editor = TemplateDialog(parent, text, mi)
|
||||
editor.setWindowTitle(_("Edit template"))
|
||||
editor.textbox.setTabChangesFocus(False)
|
||||
@ -662,20 +687,4 @@ class CcTemplateDelegate(QStyledItemDelegate): # {{{
|
||||
m.setData(index, (editor.rule[1]), Qt.EditRole)
|
||||
return None
|
||||
|
||||
def setModelData(self, editor, model, index):
|
||||
val = unicode(editor.textbox.toPlainText())
|
||||
try:
|
||||
validation_formatter.validate(val)
|
||||
except Exception as err:
|
||||
error_dialog(self.parent(), _('Invalid template'),
|
||||
'<p>'+_('The template %s is invalid:')%val +
|
||||
'<br>'+str(err), show=True)
|
||||
model.setData(index, (val), Qt.EditRole)
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
m = index.model()
|
||||
val = m.custom_columns[m.column_map[index.column()]]['display']['composite_template']
|
||||
editor.textbox.setPlainText(val)
|
||||
|
||||
|
||||
# }}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user