From cca64b14f3b1d9909e7d24eea0ab3314e5b1217c Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Tue, 12 Mar 2024 16:12:03 +0100 Subject: [PATCH] ExternalTools: Fix drag and drop in the preferences It mostly broke when Qt changed containers from int to qsizetype: When we use QDataStream to serialize the value of `QList::indexOf`, but deserialize that into an `int` variable, we don't get the same value back. Fix that, and also use begin/endMoveRows, which results in a better selection behavior after dropping. Fixes: QTCREATORBUG-30469 Change-Id: Ic99181ea7f75958766977ce7cf9d17c3d96103e2 Reviewed-by: Christian Stenger Reviewed-by: Qt CI Bot --- .../coreplugin/dialogs/externaltoolconfig.cpp | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/plugins/coreplugin/dialogs/externaltoolconfig.cpp b/src/plugins/coreplugin/dialogs/externaltoolconfig.cpp index 1916a3ba259..fe724a893a7 100644 --- a/src/plugins/coreplugin/dialogs/externaltoolconfig.cpp +++ b/src/plugins/coreplugin/dialogs/externaltoolconfig.cpp @@ -174,19 +174,27 @@ bool ExternalToolModel::dropMimeData(const QMimeData *data, return false; QDataStream stream(&ba, QIODevice::ReadOnly); QString category; - int pos = -1; + qsizetype pos = -1; stream >> category; stream >> pos; QList &items = m_tools[category]; QTC_ASSERT(pos >= 0 && pos < items.count(), return false); - beginRemoveRows(index(m_tools.keys().indexOf(category), 0), pos, pos); + const int sourceCategoryIndex = std::distance(m_tools.constBegin(), m_tools.constFind(category)); + const int targetCategoryIndex + = std::distance(m_tools.constBegin(), m_tools.constFind(toCategory)); + QTC_ASSERT(sourceCategoryIndex >= 0 && targetCategoryIndex >= 0, return false); + if (row < 0) // target row can be -1 when dropping onto the category itself + row = 0; + if (sourceCategoryIndex == targetCategoryIndex) { + if (row == pos || row == pos + 1) // would end at the same place, don't + return false; + } + beginMoveRows(index(sourceCategoryIndex, 0), pos, pos, index(targetCategoryIndex, 0), row); ExternalTool *tool = items.takeAt(pos); - endRemoveRows(); - if (row < 0) - row = m_tools.value(toCategory).count(); - beginInsertRows(index(m_tools.keys().indexOf(toCategory), 0), row, row); + if (category == toCategory && pos < row) // adapt the target row for the removed item + --row; m_tools[toCategory].insert(row, tool); - endInsertRows(); + endMoveRows(); return true; }