From 3b9bde533a6a86f6ae5b3902e55a6f649d092608 Mon Sep 17 00:00:00 2001 From: Sergiusz Bazanski Date: Mon, 30 Jul 2018 09:46:01 +0100 Subject: gui: sort tree elements somewhat smarter --- gui/treemodel.cc | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/gui/treemodel.cc b/gui/treemodel.cc index 59391f02..0a503003 100644 --- a/gui/treemodel.cc +++ b/gui/treemodel.cc @@ -21,11 +21,6 @@ NEXTPNR_NAMESPACE_BEGIN -static bool contextTreeItemLessThan(const ContextTreeItem *v1, const ContextTreeItem *v2) - { - return v1->name() < v2->name(); - } - ContextTreeItem::ContextTreeItem() { parentNode = nullptr; } ContextTreeItem::ContextTreeItem(QString name) @@ -54,7 +49,40 @@ void ContextTreeItem::sort() { for (auto item : children) if (item->count()>1) item->sort(); - qSort(children.begin(), children.end(), contextTreeItemLessThan); + qSort(children.begin(), children.end(), [&](const ContextTreeItem *a, const ContextTreeItem *b){ + QString name_a = a->name(); + QString name_b = b->name(); + // Try to extract a common prefix from both strings. + QString common; + for (int i = 0; i < std::min(name_a.size(), name_b.size()); i++) { + const QChar c_a = name_a[i]; + const QChar c_b = name_b[i]; + if (c_a == c_b) { + common.push_back(c_a); + } else { + break; + } + } + // No common part? lexical sort. + if (common.size() == 0) { + return a->name() < b->name(); + } + + // Get the non-common parts. + name_a.remove(0, common.size()); + name_b.remove(0, common.size()); + // And see if they're strings. + bool ok = true; + int num_a = name_a.toInt(&ok); + if (!ok) { + return a->name() < b->name(); + } + int num_b = name_b.toInt(&ok); + if (!ok) { + return a->name() < b->name(); + } + return num_a < num_b; + }); } ContextTreeModel::ContextTreeModel(QObject *parent) : QAbstractItemModel(parent) { root = new ContextTreeItem(); } @@ -326,4 +354,4 @@ Qt::ItemFlags ContextTreeModel::flags(const QModelIndex &index) const ContextTreeItem *node = nodeFromIndex(index); return Qt::ItemIsEnabled | (node->type() != ElementType::NONE ? Qt::ItemIsSelectable : Qt::NoItemFlags); } -NEXTPNR_NAMESPACE_END \ No newline at end of file +NEXTPNR_NAMESPACE_END -- cgit v1.2.3