From 95e2dd462ad9f2bcc4113989041385689f65d236 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Sat, 2 Mar 2024 16:05:28 +0100 Subject: [PATCH] Implement numeric sorting of tree items with visible ID numbers This allows showing object IDs as decimal instead of fixed-width hex while still sorting as expected. --- src/PrpShop/QPlasmaTreeItem.cpp | 20 ++++++++++++++++++++ src/PrpShop/QPlasmaTreeItem.h | 3 +++ src/PrpShop/QPlasmaUtils.cpp | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/PrpShop/QPlasmaTreeItem.cpp b/src/PrpShop/QPlasmaTreeItem.cpp index 6fe16bd..1076f5a 100644 --- a/src/PrpShop/QPlasmaTreeItem.cpp +++ b/src/PrpShop/QPlasmaTreeItem.cpp @@ -114,27 +114,47 @@ void QPlasmaTreeItem::reinit() { switch (type()) { case kTypeNone: + fSortId = 0; setIcon(0, QIcon(":/img/folder.png")); break; case kTypeKO: + fSortId = s_showObjectIDs ? static_cast(fObjKey->getID()) : 0; setText(0, pqFormatKeyName(fObjKey)); setIcon(0, pqGetTypeIcon(fObjKey->getType())); break; case kTypeClassType: + fSortId = s_showTypeIDs ? fClassType : 0; setText(0, pqGetFriendlyClassName(fClassType)); setIcon(0, QIcon(":/img/folder.png")); break; case kTypeAge: + fSortId = 0; setText(0, fAge); setIcon(0, QIcon(":/img/age.png")); break; case kTypePage: + fSortId = 0; setText(0, st2qstr(fPage->getPage())); setIcon(0, QIcon(":/img/page.png")); break; } } + +bool QPlasmaTreeItem::operator<(const QTreeWidgetItem& other) const +{ + if (type() >= kTypeNone && type() < kMaxPlasmaTypes) { + // The other item is also one of ours, + // so try sorting numerically by ID. + auto otherPlasma = static_cast(other); + if (fSortId != otherPlasma.fSortId) { + return fSortId < otherPlasma.fSortId; + } + } + // If the other item doesn't belong to us or if the two IDs are equal, + // fall back to default sorting by text. + return QTreeWidgetItem::operator<(other); +} diff --git a/src/PrpShop/QPlasmaTreeItem.h b/src/PrpShop/QPlasmaTreeItem.h index e9fd768..5748d02 100644 --- a/src/PrpShop/QPlasmaTreeItem.h +++ b/src/PrpShop/QPlasmaTreeItem.h @@ -29,6 +29,7 @@ class QPlasmaTreeItem : public QTreeWidgetItem plPageInfo* fPage; bool fHasBuiltIn, fHasTextures; QString fAge; + int32_t fSortId; QString fFilename; @@ -57,6 +58,8 @@ class QPlasmaTreeItem : public QTreeWidgetItem void reinit(); + bool operator<(const QTreeWidgetItem& other) const override; + hsKeyedObject* obj() const { return (type() == kTypeKO) ? fObjKey->getObj() : NULL; } short classType() const { return (type() == kTypeClassType) ? fClassType : static_cast(0x8000); } QString age() const { return (type() == kTypeAge) ? fAge : QString(); } diff --git a/src/PrpShop/QPlasmaUtils.cpp b/src/PrpShop/QPlasmaUtils.cpp index 0f01f95..8f9b5d9 100644 --- a/src/PrpShop/QPlasmaUtils.cpp +++ b/src/PrpShop/QPlasmaUtils.cpp @@ -588,7 +588,7 @@ QString pqFormatKeyName(const plKey& obj) { if (s_showObjectIDs) { return QString("[%1] %2") - .arg(obj->getID(), 8, 16, QChar('0')) + .arg(obj->getID()) .arg(st2qstr(obj->getName())); } else { return st2qstr(obj->getName());