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 <christian.stenger@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Eike Ziller
2024-03-12 16:12:03 +01:00
parent 11752615c5
commit cca64b14f3

View File

@@ -174,19 +174,27 @@ bool ExternalToolModel::dropMimeData(const QMimeData *data,
return false; return false;
QDataStream stream(&ba, QIODevice::ReadOnly); QDataStream stream(&ba, QIODevice::ReadOnly);
QString category; QString category;
int pos = -1; qsizetype pos = -1;
stream >> category; stream >> category;
stream >> pos; stream >> pos;
QList<ExternalTool *> &items = m_tools[category]; QList<ExternalTool *> &items = m_tools[category];
QTC_ASSERT(pos >= 0 && pos < items.count(), return false); 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); ExternalTool *tool = items.takeAt(pos);
endRemoveRows(); if (category == toCategory && pos < row) // adapt the target row for the removed item
if (row < 0) --row;
row = m_tools.value(toCategory).count();
beginInsertRows(index(m_tools.keys().indexOf(toCategory), 0), row, row);
m_tools[toCategory].insert(row, tool); m_tools[toCategory].insert(row, tool);
endInsertRows(); endMoveRows();
return true; return true;
} }