From 22f3cec44010b978f9271419240c404f4ace68b2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 20 Nov 2025 13:57:07 +0530 Subject: [PATCH] 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) --- src/calibre/gui2/library/delegates.py | 13 +++++++++++++ src/calibre/gui2/library/models.py | 2 ++ 2 files changed, 15 insertions(+) diff --git a/src/calibre/gui2/library/delegates.py b/src/calibre/gui2/library/delegates.py index b9e6ba626f..d8ea501c9b 100644 --- a/src/calibre/gui2/library/delegates.py +++ b/src/calibre/gui2/library/delegates.py @@ -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 diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 28c382e8ef..973211ed33 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -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 edit me

"))