Skip to content
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
5 changes: 5 additions & 0 deletions examples/calculator/LongProcessingRandomNumber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class RandomNumberModel : public MathOperationDataModel
{
public:
RandomNumberModel() {
setFrozenMenu(true);

this->setNodeProcessingStatus(QtNodes::NodeProcessingStatus::Empty);


Expand Down Expand Up @@ -44,6 +46,9 @@ class RandomNumberModel : public MathOperationDataModel
private:
void compute() override
{
if (frozen())
return;

Q_EMIT computingStarted();
PortIndex const outPortIndex = 0;

Expand Down
5 changes: 3 additions & 2 deletions examples/calculator/MathOperationDataModel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "MathOperationDataModel.hpp"

#include "DecimalData.hpp"

unsigned int MathOperationDataModel::nPorts(PortType portType) const
Expand All @@ -21,7 +20,9 @@ NodeDataType MathOperationDataModel::dataType(PortType, PortIndex) const

std::shared_ptr<NodeData> MathOperationDataModel::outData(PortIndex)
{
return std::static_pointer_cast<NodeData>(_result);
auto output = std::static_pointer_cast<NodeData>(_result);

return output;
}

void MathOperationDataModel::setInData(std::shared_ptr<NodeData> data, PortIndex portIndex)
Expand Down
1 change: 0 additions & 1 deletion examples/calculator/MathOperationDataModel.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <QtNodes/NodeDelegateModel>

#include <QtCore/QJsonObject>
#include <QtCore/QObject>
#include <QtWidgets/QLabel>
Expand Down
8 changes: 5 additions & 3 deletions include/QtNodes/internal/BasicGraphicsScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "ConnectionIdHash.hpp"
#include "Definitions.hpp"
#include "Export.hpp"

#include "QUuidStdHash.hpp"

#include <QtCore/QUuid>
Expand All @@ -17,7 +16,6 @@
#include <tuple>
#include <unordered_map>


class QUndoStack;

namespace QtNodes {
Expand All @@ -29,7 +27,7 @@ class ConnectionGraphicsObject;
class NodeGraphicsObject;
class NodeStyle;

/// An instance of QGraphicsScene, holds connections and nodes.
/// An instance of QGraphicsScene , holds connections and nodes.
class NODE_EDITOR_PUBLIC BasicGraphicsScene : public QGraphicsScene
{
Q_OBJECT
Expand Down Expand Up @@ -112,6 +110,10 @@ class NODE_EDITOR_PUBLIC BasicGraphicsScene : public QGraphicsScene
*/
virtual QMenu *createSceneMenu(QPointF const scenePos);

QMenu *createFreezeMenu();

void freezeModelAndConnections(bool isFreeze);

Q_SIGNALS:
void modified(BasicGraphicsScene *);
void nodeMoved(NodeId const nodeId, QPointF const &newLocation);
Expand Down
6 changes: 6 additions & 0 deletions include/QtNodes/internal/ConnectionState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class NODE_EDITOR_PUBLIC ConnectionState
ConnectionState(ConnectionGraphicsObject &cgo)
: _cgo(cgo)
, _hovered(false)
, _frozen(false)
{}

ConnectionState(ConnectionState const &) = delete;
Expand All @@ -42,6 +43,9 @@ class NODE_EDITOR_PUBLIC ConnectionState
bool hovered() const;
void setHovered(bool hovered);

bool frozen() const { return _frozen; }
void setFrozen(bool frozen) { _frozen = frozen; }

public:
/// Caches NodeId for further interaction.
void setLastHoveredNode(NodeId const nodeId);
Expand All @@ -56,5 +60,7 @@ class NODE_EDITOR_PUBLIC ConnectionState
bool _hovered;

NodeId _lastHoveredNode{InvalidNodeId};

bool _frozen;
};
} // namespace QtNodes
3 changes: 3 additions & 0 deletions include/QtNodes/internal/DataFlowGraphicsScene.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include "BasicGraphicsScene.hpp"
#include "ConnectionGraphicsObject.hpp"
#include "DataFlowGraphModel.hpp"
#include "Export.hpp"
#include "NodeConnectionInteraction.hpp"

namespace QtNodes {

Expand All @@ -22,6 +24,7 @@ class NODE_EDITOR_PUBLIC DataFlowGraphicsScene : public BasicGraphicsScene
public:
std::vector<NodeId> selectedNodes() const;
QMenu *createSceneMenu(QPointF const scenePos) override;
void updateConnectionGraphics(const std::unordered_set<ConnectionId> &connections, bool state);

public Q_SLOTS:
bool save() const;
Expand Down
13 changes: 13 additions & 0 deletions include/QtNodes/internal/NodeDelegateModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "NodeData.hpp"
#include "NodeStyle.hpp"
#include "Serializable.hpp"
#include <QtGui/QColor>

namespace QtNodes {

Expand Down Expand Up @@ -132,6 +133,14 @@ class NODE_EDITOR_PUBLIC NodeDelegateModel

virtual bool resizable() const { return false; }

bool frozen() const { return _frozen; }

void setFrozenState(bool state) { _frozen = state; }

bool frozenMenu() const { return _frozenMenu; }

void setFrozenMenu(bool state) { _frozenMenu = state; }

public Q_SLOTS:
virtual void inputConnectionCreated(ConnectionId const &) {}
virtual void inputConnectionDeleted(ConnectionId const &) {}
Expand Down Expand Up @@ -185,6 +194,10 @@ public Q_SLOTS:
private:
NodeStyle _nodeStyle;

bool _frozen{false};

bool _frozenMenu{false};

NodeValidationState _nodeValidationState;

NodeProcessingStatus _processingStatus{NodeProcessingStatus::NoStatus};
Expand Down
95 changes: 95 additions & 0 deletions src/BasicGraphicsScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
#include "AbstractNodeGeometry.hpp"
#include "ConnectionGraphicsObject.hpp"
#include "ConnectionIdUtils.hpp"
#include "DataFlowGraphModel.hpp"
#include "DefaultConnectionPainter.hpp"
#include "DefaultHorizontalNodeGeometry.hpp"
#include "DefaultNodePainter.hpp"
#include "DefaultVerticalNodeGeometry.hpp"
#include "NodeDelegateModel.hpp"
#include "NodeGraphicsObject.hpp"

#include <QUndoStack>

#include <QHeaderView>
#include <QLineEdit>
#include <QTreeWidget>
#include <QWidgetAction>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QGraphicsSceneMoveEvent>

Expand Down Expand Up @@ -203,6 +209,68 @@ QMenu *BasicGraphicsScene::createSceneMenu(QPointF const scenePos)
return nullptr;
}

QMenu *BasicGraphicsScene::createFreezeMenu()
{
QMenu *menu = new QMenu();

auto *txtBox = new QLineEdit(menu);
txtBox->setPlaceholderText(QStringLiteral("Filter"));
txtBox->setClearButtonEnabled(true);

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

QTreeWidget *treeView = new QTreeWidget(menu);
treeView->header()->close();

treeView->setMaximumHeight(100);
treeView->setMaximumWidth(150);

auto *treeViewAction = new QWidgetAction(menu);
treeViewAction->setDefaultWidget(treeView);
menu->addAction(treeViewAction);

auto freezeItem = new QTreeWidgetItem(treeView);
freezeItem->setText(0, "Freeze");

auto unfreezeItem = new QTreeWidgetItem(treeView);
unfreezeItem->setText(0, "Unfreeze");

treeView->expandAll();

connect(treeView, &QTreeWidget::itemClicked, [this, menu](QTreeWidgetItem *item, int) {
if (item->text(0) == "Freeze") {
freezeModelAndConnections(true);

menu->close();
return;
}
if (item->text(0) == "Unfreeze") {
freezeModelAndConnections(false);

menu->close();
return;
}
});

// Filtro
connect(txtBox, &QLineEdit::textChanged, [treeView](const QString &text) {
QTreeWidgetItemIterator it(treeView);
while (*it) {
auto modelName = (*it)->text(0);
const bool match = (modelName.contains(text, Qt::CaseInsensitive));
(*it)->setHidden(!match);
++it;
}
});

txtBox->setFocus();
menu->setAttribute(Qt::WA_DeleteOnClose);

return menu;
}

void BasicGraphicsScene::traverseGraphAndPopulateGraphicsObjects()
{
auto allNodeIds = _graphModel.allNodeIds();
Expand Down Expand Up @@ -327,4 +395,31 @@ void BasicGraphicsScene::onModelReset()
traverseGraphAndPopulateGraphicsObjects();
}

void BasicGraphicsScene::freezeModelAndConnections(bool isFreeze)
{
for (QGraphicsItem *item : selectedItems()) {
if (auto n = qgraphicsitem_cast<NodeGraphicsObject *>(item)) {
int portCount = graphModel().nodeData(n->nodeId(), NodeRole::OutPortCount).toInt();
for (int i = 0; i < portCount; i++) {
auto graphConnections = graphModel().connections(n->nodeId(),
QtNodes::PortType::Out,
QtNodes::PortIndex(i));

for (auto const &c : graphConnections) {
if (auto *cgo = connectionGraphicsObject(c)) {
cgo->connectionState().setFrozen(isFreeze);
cgo->update();
}
}
}

if (auto *dfModel = dynamic_cast<DataFlowGraphModel *>(&graphModel())) {
if (auto *delegate = dfModel->delegateModel<NodeDelegateModel>(n->nodeId())) {
delegate->setFrozenState(isFreeze);
}
}
}
}
}

} // namespace QtNodes
3 changes: 3 additions & 0 deletions src/DataFlowGraphModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ bool DataFlowGraphModel::setPortData(
switch (role) {
case PortRole::Data:
if (portType == PortType::In) {
if (model->frozen())
return false;

model->setInData(value.value<std::shared_ptr<NodeData>>(), portIndex);

// Triggers repainting on the scene.
Expand Down
12 changes: 11 additions & 1 deletion src/DataFlowGraphicsScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <stdexcept>
#include <utility>


namespace QtNodes {

DataFlowGraphicsScene::DataFlowGraphicsScene(DataFlowGraphModel &graphModel, QObject *parent)
Expand Down Expand Up @@ -191,4 +190,15 @@ bool DataFlowGraphicsScene::load()
return true;
}

void DataFlowGraphicsScene::updateConnectionGraphics(
const std::unordered_set<ConnectionId> &connections, bool state)
{
for (auto const &c : connections) {
if (auto *cgo = connectionGraphicsObject(c)) {
cgo->connectionState().setFrozen(state);
cgo->update();
}
}
}

} // namespace QtNodes
18 changes: 11 additions & 7 deletions src/DefaultConnectionPainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include <QtGui/QIcon>


namespace QtNodes {

QPainterPath DefaultConnectionPainter::cubicPath(ConnectionGraphicsObject const &connection) const
Expand All @@ -27,11 +26,12 @@ QPainterPath DefaultConnectionPainter::cubicPath(ConnectionGraphicsObject const
return cubic;
}

void DefaultConnectionPainter::drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo) const
void DefaultConnectionPainter::drawSketchLine(QPainter *painter,
ConnectionGraphicsObject const &cgo) const
{
ConnectionState const &state = cgo.connectionState();

if (state.requiresPort()) {
if (state.requiresPort() || state.frozen()) {
auto const &connectionStyle = QtNodes::StyleCollection::connectionStyle();

QPen pen;
Expand All @@ -49,7 +49,8 @@ void DefaultConnectionPainter::drawSketchLine(QPainter *painter, ConnectionGraph
}
}

void DefaultConnectionPainter::drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo) const
void DefaultConnectionPainter::drawHoveredOrSelected(QPainter *painter,
ConnectionGraphicsObject const &cgo) const
{
bool const hovered = cgo.connectionState().hovered();
bool const selected = cgo.isSelected();
Expand All @@ -74,11 +75,12 @@ void DefaultConnectionPainter::drawHoveredOrSelected(QPainter *painter, Connecti
}
}

void DefaultConnectionPainter::drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cgo) const
void DefaultConnectionPainter::drawNormalLine(QPainter *painter,
ConnectionGraphicsObject const &cgo) const
{
ConnectionState const &state = cgo.connectionState();

if (state.requiresPort())
if (state.requiresPort() || state.frozen())
return;

// colors
Expand Down Expand Up @@ -132,6 +134,7 @@ void DefaultConnectionPainter::drawNormalLine(QPainter *painter, ConnectionGraph
painter->setBrush(Qt::NoBrush);

QColor cOut = normalColorOut;

if (selected)
cOut = cOut.darker(200);
p.setColor(cOut);
Expand Down Expand Up @@ -200,7 +203,8 @@ void DefaultConnectionPainter::paint(QPainter *painter, ConnectionGraphicsObject
painter->drawEllipse(cgo.in(), pointRadius, pointRadius);
}

QPainterPath DefaultConnectionPainter::getPainterStroke(ConnectionGraphicsObject const &connection) const
QPainterPath DefaultConnectionPainter::getPainterStroke(
ConnectionGraphicsObject const &connection) const
{
auto cubic = cubicPath(connection);

Expand Down
Loading
Loading