Skip to content

Add sort button for the scene menu, remove CategoriesSet #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/QtNodes/internal/DataFlowGraphicsScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class NODE_EDITOR_PUBLIC DataFlowGraphicsScene : public BasicGraphicsScene
public:
QMenu *createSceneMenu(QPointF const scenePos) override;

void sortSceneMenu(bool sortMenu = true);

public Q_SLOTS:
void save() const;

Expand All @@ -35,6 +37,8 @@ public Q_SLOTS:

private:
DataFlowGraphModel &_graphModel;

bool _sortMenu;
};

} // namespace QtNodes
11 changes: 2 additions & 9 deletions include/QtNodes/internal/NodeDelegateModelRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include <functional>
#include <memory>
#include <set>
#include <type_traits>
#include <unordered_map>
#include <utility>
Expand All @@ -24,8 +23,7 @@ class NODE_EDITOR_PUBLIC NodeDelegateModelRegistry
using RegistryItemPtr = std::unique_ptr<NodeDelegateModel>;
using RegistryItemCreator = std::function<RegistryItemPtr()>;
using RegisteredModelCreatorsMap = std::unordered_map<QString, RegistryItemCreator>;
using RegisteredModelsCategoryMap = std::unordered_map<QString, QString>;
using CategoriesSet = std::set<QString>;
using RegisteredModelsCategoryMap = std::vector<std::pair<QString, QString>>;

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

Expand All @@ -46,8 +44,7 @@ class NODE_EDITOR_PUBLIC NodeDelegateModelRegistry
QString const name = computeName<ModelType>(HasStaticMethodName<ModelType>{}, creator);
if (!_registeredItemCreators.count(name)) {
_registeredItemCreators[name] = std::move(creator);
_categories.insert(category);
_registeredModelsCategory[name] = category;
_registeredModelsCategory.push_back(std::make_pair(name, category));
}
}

Expand Down Expand Up @@ -100,8 +97,6 @@ class NODE_EDITOR_PUBLIC NodeDelegateModelRegistry

RegisteredModelsCategoryMap const &registeredModelsCategoryAssociation() const;

CategoriesSet const &categories() const;

#if 0
TypeConverter
getTypeConverter(NodeDataType const& d1,
Expand All @@ -111,8 +106,6 @@ class NODE_EDITOR_PUBLIC NodeDelegateModelRegistry
private:
RegisteredModelsCategoryMap _registeredModelsCategory;

CategoriesSet _categories;

RegisteredModelCreatorsMap _registeredItemCreators;

#if 0
Expand Down
121 changes: 79 additions & 42 deletions src/DataFlowGraphicsScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
#include "NodeGraphicsObject.hpp"
#include "UndoCommands.hpp"

#include <QGridLayout>
#include <QPushButton>
#include <QStandardItem>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QGraphicsSceneMoveEvent>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QTreeView>
#include <QtWidgets/QTreeWidget>
#include <QtWidgets/QWidgetAction>

Expand All @@ -31,6 +35,7 @@ namespace QtNodes {
DataFlowGraphicsScene::DataFlowGraphicsScene(DataFlowGraphModel &graphModel, QObject *parent)
: BasicGraphicsScene(graphModel, parent)
, _graphModel(graphModel)
, _sortMenu(true)
{
connect(&_graphModel,
&DataFlowGraphModel::inPortDataWasSet,
Expand Down Expand Up @@ -61,77 +66,104 @@ QMenu *DataFlowGraphicsScene::createSceneMenu(QPointF const scenePos)
{
QMenu *modelMenu = new QMenu();

QWidget *menuWidget = new QWidget();
QGridLayout *layout = new QGridLayout(menuWidget);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(3);

// Add filterbox to the context menu
auto *txtBox = new QLineEdit(modelMenu);
QLineEdit *txtBox = new QLineEdit();
txtBox->setPlaceholderText(QStringLiteral("Filter"));
txtBox->setClearButtonEnabled(true);
txtBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
layout->addWidget(txtBox, 0, 0);

auto *txtBoxAction = new QWidgetAction(modelMenu);
txtBoxAction->setDefaultWidget(txtBox);

// 1.
modelMenu->addAction(txtBoxAction);
// Add sort button
QPushButton *sortButton = new QPushButton("Sort");
sortButton->setCheckable(true);
layout->addWidget(sortButton, 0, 1);

// Add result treeview to the context menu
QTreeWidget *treeView = new QTreeWidget(modelMenu);
QTreeView *treeView = new QTreeView();
treeView->header()->close();
layout->addWidget(treeView, 1, 0, 1, 2);
QStandardItemModel *treeModel = new QStandardItemModel(treeView);

auto *treeViewAction = new QWidgetAction(modelMenu);
treeViewAction->setDefaultWidget(treeView);
auto *menuWidgetAction = new QWidgetAction(menuWidget);
menuWidgetAction->setDefaultWidget(menuWidget);

// 2.
modelMenu->addAction(treeViewAction);
modelMenu->addAction(menuWidgetAction);

auto registry = _graphModel.dataModelRegistry();

for (auto const &cat : registry->categories()) {
auto item = new QTreeWidgetItem(treeView);
item->setText(0, cat);
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
}

int sortCount = 0;
for (auto const &assoc : registry->registeredModelsCategoryAssociation()) {
QList<QTreeWidgetItem *> parent = treeView->findItems(assoc.second, Qt::MatchExactly);
QList<QStandardItem *> parentList;
parentList.clear();
parentList = treeModel->findItems(assoc.second, Qt::MatchExactly);

if (parentList.count() <= 0) {
// Create a parent if it does not exist
auto parentItem = new QStandardItem(assoc.second);
parentItem->setData(sortCount, Qt::UserRole);
parentItem->setFlags(parentItem->flags() & ~Qt::ItemIsSelectable);
parentList.push_back(parentItem);
treeModel->appendRow(parentItem);
}

if (parent.count() <= 0)
continue;
auto childItem = new QStandardItem(assoc.first);
childItem->setData(sortCount, Qt::UserRole);
parentList.first()->appendRow(childItem);

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

treeView->setModel(treeModel);
treeView->expandAll();

if (_sortMenu) {
sortButton->setChecked(_sortMenu);
treeView->sortByColumn(0, Qt::AscendingOrder);
}

connect(sortButton, &QPushButton::clicked, this, [this, treeModel, treeView](bool checked) {
if (checked)
treeModel->setSortRole(Qt::DisplayRole);
else
treeModel->setSortRole(Qt::UserRole);

_sortMenu = checked;
treeView->sortByColumn(0, Qt::AscendingOrder);
});

connect(treeView,
&QTreeWidget::itemClicked,
[this, modelMenu, scenePos](QTreeWidgetItem *item, int) {
if (!(item->flags() & (Qt::ItemIsSelectable))) {
&QTreeView::clicked,
[this, modelMenu, treeModel, scenePos](const QModelIndex &index) {
auto item = treeModel->itemFromIndex(index);

if (item->hasChildren())
return;
}

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

modelMenu->close();
});

//Setup filtering
connect(txtBox, &QLineEdit::textChanged, [treeView](const QString &text) {
QTreeWidgetItemIterator categoryIt(treeView, QTreeWidgetItemIterator::HasChildren);
while (*categoryIt)
(*categoryIt++)->setHidden(true);
QTreeWidgetItemIterator it(treeView, QTreeWidgetItemIterator::NoChildren);
while (*it) {
auto modelName = (*it)->text(0);
const bool match = (modelName.contains(text, Qt::CaseInsensitive));
(*it)->setHidden(!match);
if (match) {
QTreeWidgetItem *parent = (*it)->parent();
while (parent) {
parent->setHidden(false);
parent = parent->parent();
connect(txtBox, &QLineEdit::textChanged, [treeView, treeModel](const QString &text) {
QModelIndex treeViewIndex = treeView->rootIndex();

for (int i = 0; i < treeModel->rowCount(); ++i) {
QStandardItem *parent = treeModel->item(i);
treeView->setRowHidden(i, treeViewIndex, true);

for (int j = 0; j < parent->rowCount(); ++j) {
const bool match = parent->child(j)->text().contains(text, Qt::CaseInsensitive);
treeView->setRowHidden(j, parent->index(), !match);
if (match) {
treeView->setRowHidden(i, treeViewIndex, false);
}
}
++it;
}
});

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

void DataFlowGraphicsScene::sortSceneMenu(bool sortMenu)
{
_sortMenu = sortMenu;
}

void DataFlowGraphicsScene::save() const
{
QString fileName = QFileDialog::getSaveFileName(nullptr,
Expand Down
5 changes: 0 additions & 5 deletions src/NodeDelegateModelRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,3 @@ NodeDelegateModelRegistry::registeredModelsCategoryAssociation() const
{
return _registeredModelsCategory;
}

NodeDelegateModelRegistry::CategoriesSet const &NodeDelegateModelRegistry::categories() const
{
return _categories;
}