-
Notifications
You must be signed in to change notification settings - Fork 948
Expand file tree
/
Copy pathModelSelectionWidget.cpp
More file actions
102 lines (79 loc) · 2.64 KB
/
ModelSelectionWidget.cpp
File metadata and controls
102 lines (79 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "ModelSelectionWidget.hpp"
#include <utility>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QTreeWidget>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidgetAction>
#include <nodes/DataModelRegistry>
using QtNodes::DataModelRegistry;
using QtNodes::ModelSelectionWidget;
static auto const SkipText = QStringLiteral("skip me");
ModelSelectionWidget::
ModelSelectionWidget(DataModelRegistry& registry, QWidget* parent)
: QWidget(parent)
{
auto* layout = new QVBoxLayout(this);
setLayout(layout);
// Add filterbox to the context menu
auto* filter = new QLineEdit(this);
filter->setPlaceholderText(QStringLiteral("Filter"));
filter->setClearButtonEnabled(true);
auto* filterAction = new QWidgetAction(this);
filterAction->setDefaultWidget(filter);
addAction(filterAction);
layout->addWidget(filter);
// Add result treeview to the context menu
auto* treeView = new QTreeWidget(this);
treeView->header()->close();
auto* treeViewAction = new QWidgetAction(this);
treeViewAction->setDefaultWidget(treeView);
addAction(treeViewAction);
layout->addWidget(treeView);
QMap<QString, QTreeWidgetItem*> topLevelItems;
for (auto const& cat : registry.categoriesOrder())
{
auto item = new QTreeWidgetItem(treeView);
item->setText(0, cat);
item->setData(0, Qt::UserRole, SkipText);
topLevelItems[cat] = item;
}
auto const& assocCategory = registry.registeredModelsCategoryAssociation();
for (auto const& name : registry.registeredModelsOrder())
{
auto topLevelParent = topLevelItems[assocCategory.at(name)];
auto item = new QTreeWidgetItem(topLevelParent);
item->setText(0, name);
item->setData(0, Qt::UserRole, name);
}
treeView->expandAll();
connect(treeView, &QTreeWidget::itemClicked, this, [this](QTreeWidgetItem* item) {
QString modelName = item->data(0, Qt::UserRole).toString();
if (modelName == SkipText)
{
return;
}
emit modelSelected(modelName);
});
// Setup filtering
connect(filter, &QLineEdit::textChanged, [topLevelItems = std::move(topLevelItems)](const QString& text) {
for (auto& topLvlItem : topLevelItems)
{
for (int i = 0; i < topLvlItem->childCount(); ++i)
{
auto child = topLvlItem->child(i);
auto modelName = child->data(0, Qt::UserRole).toString();
if (modelName.contains(text, Qt::CaseInsensitive))
{
child->setHidden(false);
}
else
{
child->setHidden(true);
}
}
}
});
// make sure the text box gets focus so the user doesn't have to click on it
filter->setFocus();
}