From 052993a96adda6cf2bbc26eccf314ac9c693e306 Mon Sep 17 00:00:00 2001 From: Marco Langer Date: Sun, 10 Dec 2023 18:51:21 +0100 Subject: [PATCH] Fix sorting of advanced attribute list Sort advanced attribute list using locale aware sort. Fixes #6175 --- src/gui/entry/EntryAttributesModel.cpp | 17 +++++++++++++++-- tests/TestEntryModel.cpp | 13 +++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/gui/entry/EntryAttributesModel.cpp b/src/gui/entry/EntryAttributesModel.cpp index 9ce9ed251..6e1f692aa 100644 --- a/src/gui/entry/EntryAttributesModel.cpp +++ b/src/gui/entry/EntryAttributesModel.cpp @@ -19,6 +19,18 @@ #include "core/EntryAttributes.h" +#include + +namespace +{ + void sortLocaleAware(QList& strings) + { + QCollator collator; + collator.setNumericMode(true); + std::sort(strings.begin(), strings.end(), collator); + } +} // namespace + EntryAttributesModel::EntryAttributesModel(QObject* parent) : QAbstractListModel(parent) , m_entryAttributes(nullptr) @@ -150,7 +162,7 @@ void EntryAttributesModel::attributeAboutToAdd(const QString& key) { QList rows = m_attributes; rows.append(key); - std::sort(rows.begin(), rows.end()); + sortLocaleAware(rows); int row = rows.indexOf(key); beginInsertRows(QModelIndex(), row, row); } @@ -180,7 +192,7 @@ void EntryAttributesModel::attributeAboutToRename(const QString& oldKey, const Q QList rows = m_attributes; rows.removeOne(oldKey); rows.append(newKey); - std::sort(rows.begin(), rows.end()); + sortLocaleAware(rows); int newRow = rows.indexOf(newKey); if (newRow > oldRow) { newRow++; @@ -232,4 +244,5 @@ void EntryAttributesModel::updateAttributes() m_attributes.append(key); } } + sortLocaleAware(m_attributes); } diff --git a/tests/TestEntryModel.cpp b/tests/TestEntryModel.cpp index 350642618..e3cdb4a46 100644 --- a/tests/TestEntryModel.cpp +++ b/tests/TestEntryModel.cpp @@ -201,6 +201,9 @@ void TestEntryModel::testAttributesModel() // make sure these don't generate messages entryAttributes->set("Title", "test"); + entryAttributes->set("UserName", "test"); + entryAttributes->set("Password", "test"); + entryAttributes->set("URL", "test"); entryAttributes->set("Notes", "test"); QCOMPARE(spyDataChanged.count(), 1); @@ -214,6 +217,16 @@ void TestEntryModel::testAttributesModel() entryAttributes->set("2nd", value, true); QVERIFY(entryAttributes->isProtected("2nd")); QCOMPARE(entryAttributes->value("2nd"), value); + entryAttributes->clear(); + + // test attribute sorting + entryAttributes->set("Test1", "1"); + entryAttributes->set("Test11", "11"); + entryAttributes->set("Test2", "2"); + QCOMPARE(model->rowCount(), 3); + QCOMPARE(model->data(model->index(0, 0)).toString(), QString("Test1")); + QCOMPARE(model->data(model->index(1, 0)).toString(), QString("Test2")); + QCOMPARE(model->data(model->index(2, 0)).toString(), QString("Test11")); QSignalSpy spyReset(model, SIGNAL(modelReset())); entryAttributes->clear();