[DeckList] Extract deck-node sort helpers (#7320)

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2026-09-19 10:35:03 +02:00 committed by GitHub
parent 1a6d9d7749
commit c45cb8ac32
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 18 deletions

View file

@ -192,31 +192,35 @@ void InnerDecklistNode::writeElement(QXmlStreamWriter *xml)
xml->writeEndElement(); // zone
}
QVector<QPair<int, int>> InnerDecklistNode::sort(Qt::SortOrder order)
QVector<QPair<int, AbstractDecklistNode *>> InnerDecklistNode::indexedSnapshot() const
{
QVector<QPair<int, AbstractDecklistNode *>> snapshot(size());
for (int i = size() - 1; i >= 0; --i) {
snapshot[i].first = i;
snapshot[i].second = at(i);
}
return snapshot;
}
QVector<QPair<int, int>> InnerDecklistNode::applySortedOrder(const QVector<QPair<int, AbstractDecklistNode *>> &sorted)
{
QVector<QPair<int, int>> result(size());
// Initialize temporary list with contents of current list
QVector<QPair<int, AbstractDecklistNode *>> tempList(size());
for (int i = size() - 1; i >= 0; --i) {
tempList[i].first = i;
tempList[i].second = at(i);
result[i].first = sorted[i].first;
result[i].second = i;
replace(i, sorted[i].second);
}
return result;
}
QVector<QPair<int, int>> InnerDecklistNode::sort(Qt::SortOrder order)
{
auto snapshot = indexedSnapshot();
// Sort temporary list
auto cmp = [order](const auto &a, const auto &b) {
return (order == Qt::AscendingOrder) ? (b.second->compare(a.second)) : (a.second->compare(b.second));
};
std::sort(snapshot.begin(), snapshot.end(), cmp);
std::sort(tempList.begin(), tempList.end(), cmp);
// Map old indexes to new indexes and
// copy temporary list to the current one
for (int i = size() - 1; i >= 0; --i) {
result[i].first = tempList[i].first;
result[i].second = i;
replace(i, tempList[i].second);
}
return result;
return applySortedOrder(snapshot);
}

View file

@ -223,6 +223,20 @@ public:
*/
QVector<QPair<int, int>> sort(Qt::SortOrder order = Qt::AscendingOrder);
private:
/**
* @brief Snapshots the current children as (old index, node) pairs.
*/
QVector<QPair<int, AbstractDecklistNode *>> indexedSnapshot() const;
/**
* @brief Replaces this node's children with @p sorted and maps old indexes to new ones.
*
* @return A list of (old index, new index) pairs for each reordered child.
*/
QVector<QPair<int, int>> applySortedOrder(const QVector<QPair<int, AbstractDecklistNode *>> &sorted);
public:
/**
* @brief Deserialize this node and its children from XML.
* @param xml Reader positioned at this element.