aboutsummaryrefslogtreecommitdiffstats
path: root/gui
diff options
context:
space:
mode:
authorSergiusz Bazanski <q3k@q3k.org>2018-07-30 09:46:01 +0100
committerSergiusz Bazanski <q3k@q3k.org>2018-07-30 09:46:01 +0100
commit3b9bde533a6a86f6ae5b3902e55a6f649d092608 (patch)
tree580b987cfa0cf7318dd0a7ef9342d81fa0c4ae9e /gui
parent7c8865d2fc9c2d6b19185627d650448df858fe75 (diff)
downloadnextpnr-3b9bde533a6a86f6ae5b3902e55a6f649d092608.tar.gz
nextpnr-3b9bde533a6a86f6ae5b3902e55a6f649d092608.tar.bz2
nextpnr-3b9bde533a6a86f6ae5b3902e55a6f649d092608.zip
gui: sort tree elements somewhat smarter
Diffstat (limited to 'gui')
-rw-r--r--gui/treemodel.cc42
1 files 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