From 829e0eb912a5681d5c6fddfd461165ae9f5cb093 Mon Sep 17 00:00:00 2001 From: Jin Liu Date: Fri, 11 Oct 2024 16:08:31 +0800 Subject: [PATCH] KFileItemModel: fix `setData` in the renaming usage The `setData` function is used by inline renaming to update the `text` role (i.e. file name) in the model before actually do the renaming. However, the current implementation only updates `text` and url in KFileItem in this case, leaving the `url` role and the `m_items` cache untouched. This makes the cache incoherent, so `index()` won't find the renamed item. It also makes the `url` role incoherent with the `text` role. This also fixes the problem mentioned in bug #481561 where when inline renaming fails, it still shows the new name. (Because `m_model->index` fails to find the new name and rename it back on job failure.) BUG: 481561 --- src/kitemviews/kfileitemmodel.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index 3e4a8c663..a7796bc93 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -202,13 +202,20 @@ bool KFileItemModel::setData(int index, const QHash &value return false; } - m_itemData[index]->values = currentValues; if (changedRoles.contains("text")) { QUrl url = m_itemData[index]->item.url(); + m_items.remove(url); url = url.adjusted(QUrl::RemoveFilename); url.setPath(url.path() + currentValues["text"].toString()); m_itemData[index]->item.setUrl(url); + m_items.insert(url, index); + + if (!changedRoles.contains("url")) { + changedRoles.insert("url"); + currentValues["url"] = url; + } } + m_itemData[index]->values = currentValues; emitItemsChangedAndTriggerResorting(KItemRangeList() << KItemRange(index, 1), changedRoles); -- 2.47.3