Skip to content

Commit

Permalink
Implement numeric sorting of tree items with visible ID numbers
Browse files Browse the repository at this point in the history
This allows showing object IDs as decimal instead of fixed-width hex
while still sorting as expected.
  • Loading branch information
dgelessus committed Mar 2, 2024
1 parent 59e2c99 commit 95e2dd4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/PrpShop/QPlasmaTreeItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int32_t>(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<const QPlasmaTreeItem&>(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);
}
3 changes: 3 additions & 0 deletions src/PrpShop/QPlasmaTreeItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class QPlasmaTreeItem : public QTreeWidgetItem
plPageInfo* fPage;
bool fHasBuiltIn, fHasTextures;
QString fAge;
int32_t fSortId;

QString fFilename;

Expand Down Expand Up @@ -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<short>(0x8000); }
QString age() const { return (type() == kTypeAge) ? fAge : QString(); }
Expand Down
2 changes: 1 addition & 1 deletion src/PrpShop/QPlasmaUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 95e2dd4

Please sign in to comment.