Skip to content
Merged
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 docs/guide/styling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ Or in a ``NodeDelegateModel``:
}
};

Set the style before the node is shown. To change it later, call
``setNodeStyle()`` and then emit ``requestNodeUpdate()``.

The ``styles`` example uses this route. It has a second node type with a green background.

Loading Styles from File
------------------------

Expand Down
15 changes: 15 additions & 0 deletions examples/styles/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QtNodes/ConnectionStyle>
#include <QtNodes/DataFlowGraphModel>
#include <QtNodes/DataFlowGraphicsScene>
#include <QtNodes/Definitions>
#include <QtNodes/GraphicsView>
#include <QtNodes/GraphicsViewStyle>
#include <QtNodes/NodeData>
Expand All @@ -17,13 +18,17 @@ using QtNodes::DataFlowGraphModel;
using QtNodes::GraphicsView;
using QtNodes::GraphicsViewStyle;
using QtNodes::NodeDelegateModelRegistry;
using QtNodes::NodeId;
using QtNodes::NodeRole;
using QtNodes::NodeStyle;

static std::shared_ptr<NodeDelegateModelRegistry> registerDataModels()
{
auto ret = std::make_shared<NodeDelegateModelRegistry>();

ret->registerModel<MyDataModel>();
// Same node with its own per-node style. Add both from the context menu to compare.
ret->registerModel<MyGreenDataModel>();

return ret;
}
Expand Down Expand Up @@ -93,6 +98,16 @@ int main(int argc, char *argv[])
std::shared_ptr<NodeDelegateModelRegistry> registry = registerDataModels();
DataFlowGraphModel dataFlowGraphModel(registry);

// Start with both node types side by side to compare the global style
// (left column) with the per-node style (right column).
for (int i = 0; i < 2; ++i) {
NodeId plain = dataFlowGraphModel.addNode("MyDataModel");
dataFlowGraphModel.setNodeData(plain, NodeRole::Position, QPointF(0, i * 150));

NodeId green = dataFlowGraphModel.addNode("MyGreenDataModel");
dataFlowGraphModel.setNodeData(green, NodeRole::Position, QPointF(250, i * 150));
}

DataFlowGraphicsScene scene(dataFlowGraphModel);

GraphicsView view(&scene);
Expand Down
25 changes: 25 additions & 0 deletions examples/styles/models.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#include <QtNodes/NodeData>
#include <QtNodes/NodeDelegateModel>
#include <QtNodes/NodeStyle>

#include <QtGui/QColor>

#include <memory>

Expand Down Expand Up @@ -62,3 +65,25 @@ class MyDataModel : public NodeDelegateModel

QWidget *embeddedWidget() override { return nullptr; }
};

//------------------------------------------------------------------------------

/// Shows a per-node style. This node has the same ports as MyDataModel,
/// but its delegate model overrides the background and border colors of
/// the global style. It does this in the constructor, before the node is shown.
class MyGreenDataModel : public MyDataModel
{
Q_OBJECT
public:
MyGreenDataModel()
{
QtNodes::NodeStyle style = nodeStyle();
style.setBackgroundColor(QColor(200, 235, 200));
style.NormalBoundaryColor = QColor(60, 140, 60);
setNodeStyle(style);
}

QString caption() const override { return QString("My Green Data Model"); }

QString name() const override { return QString("MyGreenDataModel"); }
};
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ add_executable(test_nodes
include/TestGraphModel.hpp
include/UITestHelper.hpp
include/TestDataFlowNodes.hpp
include/TestDelegateModels.hpp
include/LockableModel.hpp
)

target_include_directories(test_nodes
Expand Down
35 changes: 35 additions & 0 deletions test/include/LockableModel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <QtNodes/Definitions>

#include <set>

/// Adds NodeFlag::Locked to a graph model, per node.
/// Base is a graph model with a nodeFlags() method, such as TestGraphModel
/// or DataFlowGraphModel. Neither model can lock a node by itself.
template<class Base>
class LockableModel : public Base
{
public:
using Base::Base;

QtNodes::NodeFlags nodeFlags(QtNodes::NodeId nodeId) const override
{
auto flags = Base::nodeFlags(nodeId);
if (_locked.count(nodeId) != 0)
flags |= QtNodes::NodeFlag::Locked;
return flags;
}

void setLocked(QtNodes::NodeId nodeId, bool locked)
{
if (locked)
_locked.insert(nodeId);
else
_locked.erase(nodeId);
Q_EMIT this->nodeFlagsUpdated(nodeId);
}

private:
std::set<QtNodes::NodeId> _locked;
};
57 changes: 57 additions & 0 deletions test/include/TestDelegateModels.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#include <QtNodes/Definitions>
#include <QtNodes/NodeDelegateModel>

#include <memory>
#include <utility>

/// A node delegate model with no data and no widget.
/// The tests set the name, the caption and the port counts.
/// Subclass it to add small extras. It has no static Name(), so register it
/// with a creator function, or through a subclass with a default constructor.
class StubDelegateModel : public QtNodes::NodeDelegateModel
{
public:
StubDelegateModel(QString name,
QString caption,
unsigned int nInPorts = 0,
unsigned int nOutPorts = 0)
: _name(std::move(name))
, _caption(std::move(caption))
, _nInPorts(nInPorts)
, _nOutPorts(nOutPorts)
{}

QString name() const override { return _name; }
QString caption() const override { return _caption; }

unsigned int nPorts(QtNodes::PortType portType) const override
{
switch (portType) {
case QtNodes::PortType::In:
return _nInPorts;
case QtNodes::PortType::Out:
return _nOutPorts;
default:
return 0;
}
}

QtNodes::NodeDataType dataType(QtNodes::PortType, QtNodes::PortIndex) const override
{
return {};
}

std::shared_ptr<QtNodes::NodeData> outData(QtNodes::PortIndex) override { return nullptr; }

void setInData(std::shared_ptr<QtNodes::NodeData>, QtNodes::PortIndex) override {}

QWidget *embeddedWidget() override { return nullptr; }

private:
QString _name;
QString _caption;
unsigned int _nInPorts;
unsigned int _nOutPorts;
};
27 changes: 11 additions & 16 deletions test/src/TestDataFlowGraphModel.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ApplicationSetup.hpp"
#include "TestDelegateModels.hpp"

#include <QtNodes/DataFlowGraphModel>
#include <QtNodes/NodeDelegateModel>
Expand All @@ -16,26 +17,20 @@ using QtNodes::NodeId;
using QtNodes::NodeRole;
using QtNodes::PortType;

class TestNodeDelegate : public NodeDelegateModel
namespace {

std::unique_ptr<NodeDelegateModel> createTestNode()
{
public:
QString name() const override { return "TestNode"; }
QString caption() const override { return "Test Node"; }
unsigned int nPorts(QtNodes::PortType portType) const override
{
return (portType == PortType::In) ? 2 : 1;
}
QtNodes::NodeDataType dataType(QtNodes::PortType, QtNodes::PortIndex) const override { return {}; }
void setInData(std::shared_ptr<QtNodes::NodeData>, QtNodes::PortIndex const) override {}
std::shared_ptr<QtNodes::NodeData> outData(QtNodes::PortIndex const) override { return nullptr; }
QWidget* embeddedWidget() override { return nullptr; }
};
return std::make_unique<StubDelegateModel>("TestNode", "Test Node", 2, 1);
}

} // namespace

TEST_CASE("DataFlowGraphModel basic functionality", "[dataflow]")
{
auto app = applicationSetup();
auto registry = std::make_shared<NodeDelegateModelRegistry>();
registry->registerModel<TestNodeDelegate>("TestNode");
registry->registerModel(createTestNode, "TestNode");

DataFlowGraphModel model(registry);

Expand Down Expand Up @@ -76,7 +71,7 @@ TEST_CASE("DataFlowGraphModel connections", "[dataflow]")
{
auto app = applicationSetup();
auto registry = std::make_shared<NodeDelegateModelRegistry>();
registry->registerModel<TestNodeDelegate>("TestNode");
registry->registerModel(createTestNode, "TestNode");

DataFlowGraphModel model(registry);

Expand Down Expand Up @@ -142,7 +137,7 @@ TEST_CASE("DataFlowGraphModel serialization support", "[dataflow]")
{
auto app = applicationSetup();
auto registry = std::make_shared<NodeDelegateModelRegistry>();
registry->registerModel<TestNodeDelegate>("TestNode");
registry->registerModel(createTestNode, "TestNode");

DataFlowGraphModel model(registry);

Expand Down
27 changes: 2 additions & 25 deletions test/src/TestLockedNodePress.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ApplicationSetup.hpp"
#include "LockableModel.hpp"
#include "TestGraphModel.hpp"
#include "UITestHelper.hpp"

Expand All @@ -13,40 +14,16 @@

using QtNodes::BasicGraphicsScene;
using QtNodes::GraphicsView;
using QtNodes::NodeFlag;
using QtNodes::NodeFlags;
using QtNodes::NodeId;
using QtNodes::NodeRole;
using QtNodes::PortType;

namespace {

class LockableGraphModel : public TestGraphModel
{
public:
NodeFlags nodeFlags(NodeId) const override
{
return _locked ? NodeFlags(NodeFlag::Locked) : NodeFlags(NodeFlag::NoFlags);
}

void setLocked(NodeId nodeId, bool locked)
{
_locked = locked;
Q_EMIT nodeFlagsUpdated(nodeId);
}

private:
bool _locked = false;
};

} // namespace

TEST_CASE("Clicking a port of a locked node does not crash or start a connection",
"[ui][lock]")
{
auto app = applicationSetup();

LockableGraphModel model;
LockableModel<TestGraphModel> model;
BasicGraphicsScene scene(model);
GraphicsView view(&scene);
view.resize(800, 600);
Expand Down
Loading
Loading