Editing book list: Be robust against the book list changing in the background when a book is auto added or similar while a cell is being added. Fixes #2131622 [Miss edit when rows ordre change during editing of a cell](https://bugs.launchpad.net/calibre/+bug/2131622)

This commit is contained in:
Kovid Goyal 2025-11-20 13:57:07 +05:30
parent 2f78ad94f8
commit 22f3cec440
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 15 additions and 0 deletions

View File

@ -7,6 +7,7 @@ __docformat__ = 'restructuredtext en'
import os
import sys
from contextlib import suppress
from datetime import datetime
from qt.core import (
@ -208,8 +209,20 @@ class StyledItemDelegate(QStyledItemDelegate):
def createEditor(self, parent, option, index):
e = self.create_editor(parent, option, index)
if (book_id := index.data(Qt.ItemDataRole.UserRole)) and isinstance(book_id, int):
setattr(e, 'underlying_book_id', book_id)
return e
def setModelData(self, editor, model, index):
# Refresh the index using the underlying book_id in case the book list
# was changed while the editor was open, for example, by auto add
if book_id := getattr(editor, 'underlying_book_id', 0):
if (db := getattr(model, 'db', None)) and callable(getattr(db, 'row', None)):
with suppress(Exception):
row = db.row(book_id)
index = model.index(row, index.column(), index.parent())
super().setModelData(editor, model, index)
def setEditorData(self, editor, index):
# This method exists because of the ignore_kb_mods_on_edit flag. The
# flag is cleared after the editor data is set, in set_editor_data. It

View File

@ -1179,6 +1179,8 @@ class BooksModel(QAbstractTableModel): # {{{
elif role == Qt.ItemDataRole.FontRole and self.styled_columns:
cname = self.column_map[index.column()]
return self.styled_columns.get(cname)
elif role == Qt.ItemDataRole.UserRole:
return self.id(index)
# elif role == Qt.ItemDataRole.ToolTipRole and index.isValid():
# if self.column_map[index.column()] in self.editable_cols:
# return (_("Double click to <b>edit</b> me<br><br>"))