Skip to content

Commit 45ea547

Browse files
committed
Add sort button for the scene menu, remove CategoriesSet
1 parent 92c3147 commit 45ea547

File tree

4 files changed

+85
-56
lines changed

4 files changed

+85
-56
lines changed

include/QtNodes/internal/DataFlowGraphicsScene.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class NODE_EDITOR_PUBLIC DataFlowGraphicsScene : public BasicGraphicsScene
2525
public:
2626
QMenu *createSceneMenu(QPointF const scenePos) override;
2727

28+
void sortSceneMenu(bool sortMenu = true);
29+
2830
public Q_SLOTS:
2931
void save() const;
3032

@@ -35,6 +37,8 @@ public Q_SLOTS:
3537

3638
private:
3739
DataFlowGraphModel &_graphModel;
40+
41+
bool _sortMenu;
3842
};
3943

4044
} // namespace QtNodes

include/QtNodes/internal/NodeDelegateModelRegistry.hpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <functional>
1111
#include <memory>
12-
#include <set>
1312
#include <type_traits>
1413
#include <unordered_map>
1514
#include <utility>
@@ -24,8 +23,7 @@ class NODE_EDITOR_PUBLIC NodeDelegateModelRegistry
2423
using RegistryItemPtr = std::unique_ptr<NodeDelegateModel>;
2524
using RegistryItemCreator = std::function<RegistryItemPtr()>;
2625
using RegisteredModelCreatorsMap = std::unordered_map<QString, RegistryItemCreator>;
27-
using RegisteredModelsCategoryMap = std::unordered_map<QString, QString>;
28-
using CategoriesSet = std::set<QString>;
26+
using RegisteredModelsCategoryMap = std::vector<std::pair<QString, QString>>;
2927

3028
//using RegisteredTypeConvertersMap = std::map<TypeConverterId, TypeConverter>;
3129

@@ -46,8 +44,7 @@ class NODE_EDITOR_PUBLIC NodeDelegateModelRegistry
4644
QString const name = computeName<ModelType>(HasStaticMethodName<ModelType>{}, creator);
4745
if (!_registeredItemCreators.count(name)) {
4846
_registeredItemCreators[name] = std::move(creator);
49-
_categories.insert(category);
50-
_registeredModelsCategory[name] = category;
47+
_registeredModelsCategory.push_back(std::make_pair(name, category));
5148
}
5249
}
5350

@@ -100,8 +97,6 @@ class NODE_EDITOR_PUBLIC NodeDelegateModelRegistry
10097

10198
RegisteredModelsCategoryMap const &registeredModelsCategoryAssociation() const;
10299

103-
CategoriesSet const &categories() const;
104-
105100
#if 0
106101
TypeConverter
107102
getTypeConverter(NodeDataType const& d1,
@@ -111,8 +106,6 @@ class NODE_EDITOR_PUBLIC NodeDelegateModelRegistry
111106
private:
112107
RegisteredModelsCategoryMap _registeredModelsCategory;
113108

114-
CategoriesSet _categories;
115-
116109
RegisteredModelCreatorsMap _registeredItemCreators;
117110

118111
#if 0

src/DataFlowGraphicsScene.cpp

Lines changed: 79 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
#include "NodeGraphicsObject.hpp"
77
#include "UndoCommands.hpp"
88

9+
#include <QGridLayout>
10+
#include <QPushButton>
11+
#include <QStandardItem>
912
#include <QtWidgets/QFileDialog>
1013
#include <QtWidgets/QGraphicsSceneMoveEvent>
1114
#include <QtWidgets/QHeaderView>
1215
#include <QtWidgets/QLineEdit>
16+
#include <QtWidgets/QTreeView>
1317
#include <QtWidgets/QTreeWidget>
1418
#include <QtWidgets/QWidgetAction>
1519

@@ -31,6 +35,7 @@ namespace QtNodes {
3135
DataFlowGraphicsScene::DataFlowGraphicsScene(DataFlowGraphModel &graphModel, QObject *parent)
3236
: BasicGraphicsScene(graphModel, parent)
3337
, _graphModel(graphModel)
38+
, _sortMenu(true)
3439
{
3540
connect(&_graphModel,
3641
&DataFlowGraphModel::inPortDataWasSet,
@@ -61,77 +66,104 @@ QMenu *DataFlowGraphicsScene::createSceneMenu(QPointF const scenePos)
6166
{
6267
QMenu *modelMenu = new QMenu();
6368

69+
QWidget *menuWidget = new QWidget();
70+
QGridLayout *layout = new QGridLayout(menuWidget);
71+
layout->setContentsMargins(0, 0, 0, 0);
72+
layout->setSpacing(3);
73+
6474
// Add filterbox to the context menu
65-
auto *txtBox = new QLineEdit(modelMenu);
75+
QLineEdit *txtBox = new QLineEdit();
6676
txtBox->setPlaceholderText(QStringLiteral("Filter"));
6777
txtBox->setClearButtonEnabled(true);
78+
txtBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
79+
layout->addWidget(txtBox, 0, 0);
6880

69-
auto *txtBoxAction = new QWidgetAction(modelMenu);
70-
txtBoxAction->setDefaultWidget(txtBox);
71-
72-
// 1.
73-
modelMenu->addAction(txtBoxAction);
81+
// Add sort button
82+
QPushButton *sortButton = new QPushButton("Sort");
83+
sortButton->setCheckable(true);
84+
layout->addWidget(sortButton, 0, 1);
7485

7586
// Add result treeview to the context menu
76-
QTreeWidget *treeView = new QTreeWidget(modelMenu);
87+
QTreeView *treeView = new QTreeView();
7788
treeView->header()->close();
89+
layout->addWidget(treeView, 1, 0, 1, 2);
90+
QStandardItemModel *treeModel = new QStandardItemModel(treeView);
7891

79-
auto *treeViewAction = new QWidgetAction(modelMenu);
80-
treeViewAction->setDefaultWidget(treeView);
92+
auto *menuWidgetAction = new QWidgetAction(menuWidget);
93+
menuWidgetAction->setDefaultWidget(menuWidget);
8194

82-
// 2.
83-
modelMenu->addAction(treeViewAction);
95+
modelMenu->addAction(menuWidgetAction);
8496

8597
auto registry = _graphModel.dataModelRegistry();
8698

87-
for (auto const &cat : registry->categories()) {
88-
auto item = new QTreeWidgetItem(treeView);
89-
item->setText(0, cat);
90-
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
91-
}
92-
99+
int sortCount = 0;
93100
for (auto const &assoc : registry->registeredModelsCategoryAssociation()) {
94-
QList<QTreeWidgetItem *> parent = treeView->findItems(assoc.second, Qt::MatchExactly);
101+
QList<QStandardItem *> parentList;
102+
parentList.clear();
103+
parentList = treeModel->findItems(assoc.second, Qt::MatchExactly);
104+
105+
if (parentList.count() <= 0) {
106+
// Create a parent if it does not exist
107+
auto parentItem = new QStandardItem(assoc.second);
108+
parentItem->setData(sortCount, Qt::UserRole);
109+
parentItem->setFlags(parentItem->flags() & ~Qt::ItemIsSelectable);
110+
parentList.push_back(parentItem);
111+
treeModel->appendRow(parentItem);
112+
}
95113

96-
if (parent.count() <= 0)
97-
continue;
114+
auto childItem = new QStandardItem(assoc.first);
115+
childItem->setData(sortCount, Qt::UserRole);
116+
parentList.first()->appendRow(childItem);
98117

99-
auto item = new QTreeWidgetItem(parent.first());
100-
item->setText(0, assoc.first);
118+
sortCount++;
101119
}
102120

121+
treeView->setModel(treeModel);
103122
treeView->expandAll();
104123

124+
if (_sortMenu) {
125+
sortButton->setChecked(_sortMenu);
126+
treeView->sortByColumn(0, Qt::AscendingOrder);
127+
}
128+
129+
connect(sortButton, &QPushButton::clicked, this, [this, treeModel, treeView](bool checked) {
130+
if (checked)
131+
treeModel->setSortRole(Qt::DisplayRole);
132+
else
133+
treeModel->setSortRole(Qt::UserRole);
134+
135+
_sortMenu = checked;
136+
treeView->sortByColumn(0, Qt::AscendingOrder);
137+
});
138+
105139
connect(treeView,
106-
&QTreeWidget::itemClicked,
107-
[this, modelMenu, scenePos](QTreeWidgetItem *item, int) {
108-
if (!(item->flags() & (Qt::ItemIsSelectable))) {
140+
&QTreeView::clicked,
141+
[this, modelMenu, treeModel, scenePos](const QModelIndex &index) {
142+
auto item = treeModel->itemFromIndex(index);
143+
144+
if (item->hasChildren())
109145
return;
110-
}
111146

112-
this->undoStack().push(new CreateCommand(this, item->text(0), scenePos));
147+
this->undoStack().push(new CreateCommand(this, item->text(), scenePos));
113148

114149
modelMenu->close();
115150
});
116151

117152
//Setup filtering
118-
connect(txtBox, &QLineEdit::textChanged, [treeView](const QString &text) {
119-
QTreeWidgetItemIterator categoryIt(treeView, QTreeWidgetItemIterator::HasChildren);
120-
while (*categoryIt)
121-
(*categoryIt++)->setHidden(true);
122-
QTreeWidgetItemIterator it(treeView, QTreeWidgetItemIterator::NoChildren);
123-
while (*it) {
124-
auto modelName = (*it)->text(0);
125-
const bool match = (modelName.contains(text, Qt::CaseInsensitive));
126-
(*it)->setHidden(!match);
127-
if (match) {
128-
QTreeWidgetItem *parent = (*it)->parent();
129-
while (parent) {
130-
parent->setHidden(false);
131-
parent = parent->parent();
153+
connect(txtBox, &QLineEdit::textChanged, [treeView, treeModel](const QString &text) {
154+
QModelIndex treeViewIndex = treeView->rootIndex();
155+
156+
for (int i = 0; i < treeModel->rowCount(); ++i) {
157+
QStandardItem *parent = treeModel->item(i);
158+
treeView->setRowHidden(i, treeViewIndex, true);
159+
160+
for (int j = 0; j < parent->rowCount(); ++j) {
161+
const bool match = parent->child(j)->text().contains(text, Qt::CaseInsensitive);
162+
treeView->setRowHidden(j, parent->index(), !match);
163+
if (match) {
164+
treeView->setRowHidden(i, treeViewIndex, false);
132165
}
133166
}
134-
++it;
135167
}
136168
});
137169

@@ -144,6 +176,11 @@ QMenu *DataFlowGraphicsScene::createSceneMenu(QPointF const scenePos)
144176
return modelMenu;
145177
}
146178

179+
void DataFlowGraphicsScene::sortSceneMenu(bool sortMenu)
180+
{
181+
_sortMenu = sortMenu;
182+
}
183+
147184
void DataFlowGraphicsScene::save() const
148185
{
149186
QString fileName = QFileDialog::getSaveFileName(nullptr,

src/NodeDelegateModelRegistry.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,3 @@ NodeDelegateModelRegistry::registeredModelsCategoryAssociation() const
2929
{
3030
return _registeredModelsCategory;
3131
}
32-
33-
NodeDelegateModelRegistry::CategoriesSet const &NodeDelegateModelRegistry::categories() const
34-
{
35-
return _categories;
36-
}

0 commit comments

Comments
 (0)