Another try at preventing random edit dialogs while not losing "editing" when tabbing over read-only fields.

The problem of random dialogs is handled in delegates.py. Losing "editing" when tabbing is handled by jumping over fields that are readonly or are implemented by a doalog. This is a behavior change. We will see whether it will result in screams of complaint.

NB: after hours of trying, this was the only solution I found to both the random dialog and the loss of editing.
This commit is contained in:
Charles Haley 2024-12-30 14:20:41 +00:00
parent 9eaef3ceb0
commit b3b08b0e17
2 changed files with 27 additions and 5 deletions

View File

@ -218,12 +218,15 @@ class StyledItemDelegate(QStyledItemDelegate):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.table_widget = args[0]
# Set this to True here. It is up the the subclasses to set it to False if needed.
self.is_editable_with_tab = True
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()}')
if self.table_widget.currentIndex() != index:
idx = self.table_widget.currentIndex()
print(f'createEditor idx err: delegate={self.__class__.__name__}. '
f'cur idx=({idx.row()}, {idx.column()}), '
f'given idx=({index.row()}, {index.column()})')
return None
return self.create_editor(parent, option, index)
@ -579,6 +582,7 @@ class CcLongTextDelegate(StyledItemDelegate): # {{{
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
self.document = QTextDocument()
self.is_editable_with_tab = False
def create_editor(self, parent, option, index):
m = index.model()
@ -607,6 +611,7 @@ class CcMarkdownDelegate(StyledItemDelegate): # {{{
super().__init__(parent)
self.table_widget = parent
self.document = QTextDocument()
self.is_editable_with_tab = False
def paint(self, painter, option, index):
self.initStyleOption(option, index)
@ -760,6 +765,7 @@ class CcCommentsDelegate(StyledItemDelegate): # {{{
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
self.document = QTextDocument()
self.is_editable_with_tab = False
def paint(self, painter, option, index):
self.initStyleOption(option, index)
@ -886,6 +892,7 @@ class CcTemplateDelegate(StyledItemDelegate): # {{{
StyledItemDelegate.__init__(self, parent)
self.table_widget = parent
self.disallow_edit = gprefs['edit_metadata_templates_only_F2_on_booklist']
self.is_editable_with_tab = False
def create_editor(self, parent, option, index):
if self.disallow_edit:

View File

@ -1652,7 +1652,22 @@ class BooksView(QTableView): # {{{
hint = QAbstractItemDelegate.EndEditHint.NoHint
ans = super().closeEditor(editor, hint)
if move_by is not None and self.currentIndex() == orig and self.state() is not QAbstractItemView.State.EditingState:
# Skip over columns that aren't editable or are implemented by a dialog
while True:
index = self.moveCursor(move_by, Qt.KeyboardModifier.NoModifier)
if not index.isValid():
break
self.setCurrentIndex(index)
m = self._model
col = m.column_map[index.column()]
if m.is_custom_column(col):
# Don't try to open editors implemented by dialogs such as
# markdown, composites and comments
if self.itemDelegateForIndex(index).is_editable_with_tab:
break
elif m.flags(index) & Qt.ItemFlag.ItemIsEditable:
# Standard editable column
break
if index.isValid():
def edit():
if index.isValid():