This is a more elegant implementation of the changes to delegates.py.

This commit is contained in:
Charles Haley 2024-12-29 15:10:31 +00:00
parent 0e1e7d1dd7
commit 9eaef3ceb0

View File

@ -202,23 +202,40 @@ def get_val_for_textlike_columns(index_):
# }}}
# When closing an editor and opening another, Qt sometimes picks what appears to
# be a random line and column for the second editor. This function checks that
# the row of a new editor is the same as the current row in the view. If it
# isn't then the caller shouldn't open the editor.
def check_index_of_editor(delegate, index):
ans = delegate.table_widget.currentIndex().row() == index.row()
if not ans:
print(f'check_index_of_editor False. delegate: {delegate.__class__.__name__}.'
f'current row: {delegate.table_widget.currentIndex().row()}, '
f'specified row: {index.row()}, specified column:{index.column()}')
return ans
class RatingDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
class StyledItemDelegate(QStyledItemDelegate):
'''
When closing an editor and opening another, Qt sometimes picks what appears
to be a random line and column for the second editor. This function checks
that the row of a new editor is the same as the current row in the view. If
it isn't then the caller shouldn't open the editor.
'''
def __init__(self, *args, **kwargs):
QStyledItemDelegate.__init__(self, *args)
super().__init__(*args, **kwargs)
self.table_widget = args[0]
def createEditor(self, parent, option, index):
if self.table_widget.currentIndex().row() != index.row():
print(f'createEditor row error: delegate: {self.__class__.__name__}. '
f'row: {self.table_widget.currentIndex().row()}, '
f'index row: {index.row()}, index column:{index.column()}')
return None
return self.create_editor(parent, option, index)
def create_editor(self, parent, option, index):
# Must be overridden by the "real" createEditor
raise NotImplementedError
class RatingDelegate(StyledItemDelegate, UpdateEditorGeometry): # {{{
def __init__(self, *args, **kwargs):
StyledItemDelegate.__init__(self, *args)
self.is_half_star = kwargs.get('is_half_star', False)
self.table_widget = args[0]
self.rf = QFont(rating_font())
@ -234,9 +251,7 @@ class RatingDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
def displayText(self, value, locale):
return rating_to_stars(value, self.is_half_star)
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
return RatingEditor(parent, is_half_star=self.is_half_star)
def setEditorData(self, editor, index):
@ -253,21 +268,21 @@ class RatingDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
def sizeHint(self, option, index):
option.font = self.rf
option.textElideMode = self.em
return QStyledItemDelegate.sizeHint(self, option, index)
return StyledItemDelegate.sizeHint(self, option, index)
def paint(self, painter, option, index):
option.font = self.rf
option.textElideMode = self.em
return QStyledItemDelegate.paint(self, painter, option, index)
return StyledItemDelegate.paint(self, painter, option, index)
# }}}
class DateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
class DateDelegate(StyledItemDelegate, UpdateEditorGeometry): # {{{
def __init__(self, parent, tweak_name='gui_timestamp_display_format',
default_format='dd MMM yyyy'):
QStyledItemDelegate.__init__(self, parent)
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
self.tweak_name = tweak_name
self.format = tweaks[self.tweak_name]
@ -280,9 +295,7 @@ class DateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
return ''
return format_date(d, self.format)
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
return DateTimeEdit(parent, self.format)
def setEditorData(self, editor, index):
@ -301,10 +314,10 @@ class DateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
# }}}
class PubDateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
class PubDateDelegate(StyledItemDelegate, UpdateEditorGeometry): # {{{
def __init__(self, *args, **kwargs):
QStyledItemDelegate.__init__(self, *args, **kwargs)
StyledItemDelegate.__init__(self, *args, **kwargs)
self.format = tweaks['gui_pubdate_display_format']
self.table_widget = args[0]
if self.format is None:
@ -316,9 +329,7 @@ class PubDateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
return ''
return format_date(d, self.format)
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
return DateTimeEdit(parent, self.format)
def setEditorData(self, editor, index):
@ -336,7 +347,7 @@ class PubDateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
# }}}
class TextDelegate(QStyledItemDelegate, UpdateEditorGeometry, EditableTextDelegate): # {{{
class TextDelegate(StyledItemDelegate, UpdateEditorGeometry, EditableTextDelegate): # {{{
use_title_sort = False
@ -346,16 +357,14 @@ class TextDelegate(QStyledItemDelegate, UpdateEditorGeometry, EditableTextDelega
of text items to auto-complete with. If the function is None no
auto-complete will be used.
'''
QStyledItemDelegate.__init__(self, parent)
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
self.auto_complete_function = None
def set_auto_complete_function(self, f):
self.auto_complete_function = f
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
if self.auto_complete_function:
if self.use_title_sort:
editor = EditWithComplete(parent, sort_func=title_sort)
@ -374,7 +383,7 @@ class TextDelegate(QStyledItemDelegate, UpdateEditorGeometry, EditableTextDelega
val = editor.lineEdit().text()
model.setData(index, (val), Qt.ItemDataRole.EditRole)
else:
QStyledItemDelegate.setModelData(self, editor, model, index)
StyledItemDelegate.setModelData(self, editor, model, index)
# }}}
@ -389,10 +398,10 @@ class SeriesDelegate(TextDelegate): # {{{
# }}}
class CompleteDelegate(QStyledItemDelegate, UpdateEditorGeometry, EditableTextDelegate): # {{{
class CompleteDelegate(StyledItemDelegate, UpdateEditorGeometry, EditableTextDelegate): # {{{
def __init__(self, parent, sep, items_func_name, space_before_sep=False):
QStyledItemDelegate.__init__(self, parent)
StyledItemDelegate.__init__(self, parent)
self.sep = sep
self.items_func_name = items_func_name
self.space_before_sep = space_before_sep
@ -401,9 +410,7 @@ class CompleteDelegate(QStyledItemDelegate, UpdateEditorGeometry, EditableTextDe
def set_database(self, db):
self.db = db
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
if self.db and hasattr(self.db, self.items_func_name):
m = index.model()
col = m.column_map[index.column()]
@ -437,19 +444,17 @@ class CompleteDelegate(QStyledItemDelegate, UpdateEditorGeometry, EditableTextDe
val = editor.lineEdit().text()
model.setData(index, (val), Qt.ItemDataRole.EditRole)
else:
QStyledItemDelegate.setModelData(self, editor, model, index)
StyledItemDelegate.setModelData(self, editor, model, index)
# }}}
class LanguagesDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
class LanguagesDelegate(StyledItemDelegate, UpdateEditorGeometry): # {{{
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
editor = LanguagesEdit(parent=parent)
editor.init_langs(index.model().db)
return editor
@ -464,7 +469,7 @@ class LanguagesDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
# }}}
class CcDateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
class CcDateDelegate(StyledItemDelegate, UpdateEditorGeometry): # {{{
'''
Delegate for custom columns dates. Because this delegate stores the
@ -473,7 +478,7 @@ class CcDateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
'''
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
def set_format(self, _format):
@ -490,9 +495,7 @@ class CcDateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
return ''
return format_date(d, self.format)
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
return DateTimeEdit(parent, self.format)
def setEditorData(self, editor, index):
@ -517,7 +520,7 @@ class CcDateDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
# }}}
class CcTextDelegate(QStyledItemDelegate, UpdateEditorGeometry, EditableTextDelegate): # {{{
class CcTextDelegate(StyledItemDelegate, UpdateEditorGeometry, EditableTextDelegate): # {{{
'''
Delegate for text data.
@ -525,12 +528,10 @@ class CcTextDelegate(QStyledItemDelegate, UpdateEditorGeometry, EditableTextDele
use_title_sort = False
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
m = index.model()
col = m.column_map[index.column()]
key = m.db.field_metadata.key_to_label(col)
@ -568,20 +569,18 @@ class CcSeriesDelegate(CcTextDelegate): # {{{
# }}}
class CcLongTextDelegate(QStyledItemDelegate): # {{{
class CcLongTextDelegate(StyledItemDelegate): # {{{
'''
Delegate for comments data.
'''
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
self.document = QTextDocument()
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
m = index.model()
col = m.column_map[index.column()]
if check_key_modifier(Qt.KeyboardModifier.ControlModifier):
@ -598,7 +597,7 @@ class CcLongTextDelegate(QStyledItemDelegate): # {{{
# }}}
class CcMarkdownDelegate(QStyledItemDelegate): # {{{
class CcMarkdownDelegate(StyledItemDelegate): # {{{
'''
Delegate for markdown data.
@ -631,9 +630,7 @@ class CcMarkdownDelegate(QStyledItemDelegate): # {{{
self.document.documentLayout().draw(painter, ctx)
painter.restore()
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
m = index.model()
col = m.column_map[index.column()]
if check_key_modifier(Qt.KeyboardModifier.ControlModifier):
@ -654,19 +651,17 @@ class CcMarkdownDelegate(QStyledItemDelegate): # {{{
# }}}
class CcNumberDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
class CcNumberDelegate(StyledItemDelegate, UpdateEditorGeometry): # {{{
'''
Delegate for text/int/float data.
'''
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
m = index.model()
col = m.column_map[index.column()]
if m.custom_columns[col]['datatype'] == 'int':
@ -707,20 +702,18 @@ class CcNumberDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
# }}}
class CcEnumDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
class CcEnumDelegate(StyledItemDelegate, UpdateEditorGeometry): # {{{
'''
Delegate for text/int/float data.
'''
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
self.longest_text = ''
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
m = index.model()
col = m.column_map[index.column()]
editor = DelegateCB(parent)
@ -757,14 +750,14 @@ class CcEnumDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
# }}}
class CcCommentsDelegate(QStyledItemDelegate): # {{{
class CcCommentsDelegate(StyledItemDelegate): # {{{
'''
Delegate for comments data.
'''
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
self.document = QTextDocument()
@ -790,9 +783,7 @@ class CcCommentsDelegate(QStyledItemDelegate): # {{{
self.document.documentLayout().draw(painter, ctx)
painter.restore()
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
m = index.model()
col = m.column_map[index.column()]
if check_key_modifier(Qt.KeyboardModifier.ControlModifier):
@ -822,19 +813,17 @@ class DelegateCB(QComboBox): # {{{
# }}}
class CcBoolDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
class CcBoolDelegate(StyledItemDelegate, UpdateEditorGeometry): # {{{
def __init__(self, parent):
'''
Delegate for custom_column bool data.
'''
self.nuke_option_data = False
QStyledItemDelegate.__init__(self, parent)
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
editor = DelegateCB(parent)
items = [_('Yes'), _('No'), _('Undefined')]
icons = ['ok.png', 'list_remove.png', 'blank.png']
@ -888,19 +877,17 @@ class CcBoolDelegate(QStyledItemDelegate, UpdateEditorGeometry): # {{{
# }}}
class CcTemplateDelegate(QStyledItemDelegate): # {{{
class CcTemplateDelegate(StyledItemDelegate): # {{{
def __init__(self, parent):
'''
Delegate for composite custom_columns.
'''
QStyledItemDelegate.__init__(self, parent)
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
self.disallow_edit = gprefs['edit_metadata_templates_only_F2_on_booklist']
def createEditor(self, parent, option, index):
if not check_index_of_editor(self, index):
return None
def create_editor(self, parent, option, index):
if self.disallow_edit:
editor = QLineEdit(parent)
editor.setText(_('Template editing disabled'))