diff --git a/docs/guide/styling.rst b/docs/guide/styling.rst index f641e826b..cc6d4d3ee 100644 --- a/docs/guide/styling.rst +++ b/docs/guide/styling.rst @@ -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 ------------------------ diff --git a/examples/styles/main.cpp b/examples/styles/main.cpp index 7afbaabda..f5912d4c8 100644 --- a/examples/styles/main.cpp +++ b/examples/styles/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -17,6 +18,8 @@ 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 registerDataModels() @@ -24,6 +27,8 @@ static std::shared_ptr registerDataModels() auto ret = std::make_shared(); ret->registerModel(); + // Same node with its own per-node style. Add both from the context menu to compare. + ret->registerModel(); return ret; } @@ -93,6 +98,16 @@ int main(int argc, char *argv[]) std::shared_ptr 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); diff --git a/examples/styles/models.hpp b/examples/styles/models.hpp index 199f80bee..29bbd6814 100644 --- a/examples/styles/models.hpp +++ b/examples/styles/models.hpp @@ -4,6 +4,9 @@ #include #include +#include + +#include #include @@ -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"); } +}; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 56d4f56ef..a233155e3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 diff --git a/test/include/LockableModel.hpp b/test/include/LockableModel.hpp new file mode 100644 index 000000000..a4e1f4f34 --- /dev/null +++ b/test/include/LockableModel.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include + +#include + +/// 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 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 _locked; +}; diff --git a/test/include/TestDelegateModels.hpp b/test/include/TestDelegateModels.hpp new file mode 100644 index 000000000..6f0ef8a14 --- /dev/null +++ b/test/include/TestDelegateModels.hpp @@ -0,0 +1,57 @@ +#pragma once + +#include +#include + +#include +#include + +/// 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 outData(QtNodes::PortIndex) override { return nullptr; } + + void setInData(std::shared_ptr, QtNodes::PortIndex) override {} + + QWidget *embeddedWidget() override { return nullptr; } + +private: + QString _name; + QString _caption; + unsigned int _nInPorts; + unsigned int _nOutPorts; +}; diff --git a/test/src/TestDataFlowGraphModel.cpp b/test/src/TestDataFlowGraphModel.cpp index db0f78913..1040623c5 100644 --- a/test/src/TestDataFlowGraphModel.cpp +++ b/test/src/TestDataFlowGraphModel.cpp @@ -1,4 +1,5 @@ #include "ApplicationSetup.hpp" +#include "TestDelegateModels.hpp" #include #include @@ -16,26 +17,20 @@ using QtNodes::NodeId; using QtNodes::NodeRole; using QtNodes::PortType; -class TestNodeDelegate : public NodeDelegateModel +namespace { + +std::unique_ptr 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::PortIndex const) override {} - std::shared_ptr outData(QtNodes::PortIndex const) override { return nullptr; } - QWidget* embeddedWidget() override { return nullptr; } -}; + return std::make_unique("TestNode", "Test Node", 2, 1); +} + +} // namespace TEST_CASE("DataFlowGraphModel basic functionality", "[dataflow]") { auto app = applicationSetup(); auto registry = std::make_shared(); - registry->registerModel("TestNode"); + registry->registerModel(createTestNode, "TestNode"); DataFlowGraphModel model(registry); @@ -76,7 +71,7 @@ TEST_CASE("DataFlowGraphModel connections", "[dataflow]") { auto app = applicationSetup(); auto registry = std::make_shared(); - registry->registerModel("TestNode"); + registry->registerModel(createTestNode, "TestNode"); DataFlowGraphModel model(registry); @@ -142,7 +137,7 @@ TEST_CASE("DataFlowGraphModel serialization support", "[dataflow]") { auto app = applicationSetup(); auto registry = std::make_shared(); - registry->registerModel("TestNode"); + registry->registerModel(createTestNode, "TestNode"); DataFlowGraphModel model(registry); diff --git a/test/src/TestLockedNodePress.cpp b/test/src/TestLockedNodePress.cpp index df39bf93e..005ebd5df 100644 --- a/test/src/TestLockedNodePress.cpp +++ b/test/src/TestLockedNodePress.cpp @@ -1,4 +1,5 @@ #include "ApplicationSetup.hpp" +#include "LockableModel.hpp" #include "TestGraphModel.hpp" #include "UITestHelper.hpp" @@ -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 model; BasicGraphicsScene scene(model); GraphicsView view(&scene); view.resize(800, 600); diff --git a/test/src/TestNodeGroup.cpp b/test/src/TestNodeGroup.cpp index 09ae6bc7d..cdaf29386 100644 --- a/test/src/TestNodeGroup.cpp +++ b/test/src/TestNodeGroup.cpp @@ -1,4 +1,6 @@ #include "ApplicationSetup.hpp" +#include "LockableModel.hpp" +#include "TestDelegateModels.hpp" #include #include @@ -42,31 +44,16 @@ using QtNodes::PortIndex; using QtNodes::PortType; namespace { -class DummyNodeModel : public NodeDelegateModel +std::unique_ptr createDummyNode() { -public: - QString caption() const override { return QStringLiteral("Dummy Node"); } - QString name() const override { return QStringLiteral("DummyNode"); } - - unsigned int nPorts(PortType portType) const override - { - Q_UNUSED(portType); - return 1U; - } - - QtNodes::NodeDataType dataType(PortType, PortIndex) const override { return {}; } - - std::shared_ptr outData(PortIndex const) override { return nullptr; } - - void setInData(std::shared_ptr, PortIndex const) override {} - - QWidget *embeddedWidget() override { return nullptr; } -}; + return std::make_unique(QStringLiteral("DummyNode"), + QStringLiteral("Dummy Node"), 1, 1); +} std::shared_ptr createDummyRegistry() { auto registry = std::make_shared(); - registry->registerModel(QStringLiteral("Test")); + registry->registerModel(createDummyNode, QStringLiteral("Test")); return registry; } @@ -462,33 +449,6 @@ TEST_CASE("A mouse move on a node of a locked group moves nothing", "[node-group } namespace { -// DataFlowGraphModel has no way to set NodeFlag::Locked, so a test model adds it. -class LockableModel : public DataFlowGraphModel -{ -public: - using DataFlowGraphModel::DataFlowGraphModel; - - QtNodes::NodeFlags nodeFlags(NodeId nodeId) const override - { - auto flags = DataFlowGraphModel::nodeFlags(nodeId); - if (_locked.count(nodeId) != 0) - flags |= QtNodes::NodeFlag::Locked; - return flags; - } - - void setLocked(NodeId nodeId, bool locked) - { - if (locked) - _locked.insert(nodeId); - else - _locked.erase(nodeId); - Q_EMIT nodeFlagsUpdated(nodeId); - } - -private: - std::set _locked; -}; - void pressPort(BasicGraphicsScene &scene, NodeId nodeId, PortType type) { auto *item = scene.nodeGraphicsObject(nodeId); @@ -517,7 +477,7 @@ TEST_CASE("A model-locked node does not start a draft connection", "[node-lock]" auto app = applicationSetup(); auto registry = createDummyRegistry(); - LockableModel model(registry); + LockableModel model(registry); BasicGraphicsScene scene(model); NodeId const a = createNode(model, scene); @@ -533,7 +493,7 @@ TEST_CASE("A model-locked node does not detach a connection", "[node-lock]") auto app = applicationSetup(); auto registry = createDummyRegistry(); - LockableModel model(registry); + LockableModel model(registry); BasicGraphicsScene scene(model); NodeId const a = createNode(model, scene); @@ -554,7 +514,7 @@ TEST_CASE("A node stays unselectable while its model lock or its group lock is s auto app = applicationSetup(); auto registry = createDummyRegistry(); - LockableModel model(registry); + LockableModel model(registry); BasicGraphicsScene scene(model); scene.setGroupingEnabled(true); diff --git a/test/src/TestNodeNickname.cpp b/test/src/TestNodeNickname.cpp index 4fd316f6c..6b43f91d1 100644 --- a/test/src/TestNodeNickname.cpp +++ b/test/src/TestNodeNickname.cpp @@ -1,4 +1,5 @@ #include "ApplicationSetup.hpp" +#include "TestDelegateModels.hpp" #include #include @@ -23,20 +24,16 @@ namespace { // Creates a test nickname node, implementing the NodeDelegateModel interface // As a minimal node, it has no inputs or outputs, no port data type, and no embedded widget -class NicknameNode : public NodeDelegateModel +class NicknameNode : public StubDelegateModel { - public: - QString name() const override { return QStringLiteral("NicknameNode"); } - QString caption() const override { return QStringLiteral("Test Node"); } - QString label() const override { return QStringLiteral("Default nickname"); } - bool labelVisible() const override { return false; } - bool labelEditable() const override { return true; } - - unsigned int nPorts(PortType) const override { return 0; } - NodeDataType dataType(PortType, PortIndex) const override { return {}; } - void setInData(std::shared_ptr, PortIndex) override {} - std::shared_ptr outData(PortIndex) override { return nullptr; } - QWidget *embeddedWidget() override { return nullptr; } +public: + NicknameNode() + : StubDelegateModel(QStringLiteral("NicknameNode"), QStringLiteral("Test Node")) + {} + + QString label() const override { return QStringLiteral("Default nickname"); } + bool labelVisible() const override { return false; } + bool labelEditable() const override { return true; } }; std::shared_ptr makeRegistry() diff --git a/test/src/TestNodeStyleRole.cpp b/test/src/TestNodeStyleRole.cpp index bd26e43f0..3894fb4b4 100644 --- a/test/src/TestNodeStyleRole.cpp +++ b/test/src/TestNodeStyleRole.cpp @@ -1,4 +1,5 @@ #include "ApplicationSetup.hpp" +#include "TestDelegateModels.hpp" #include @@ -25,11 +26,11 @@ using QtNodes::StyleCollection; namespace { /// Delegate that sets its own style in the constructor. -class StyledModel : public NodeDelegateModel +class StyledModel : public StubDelegateModel { - Q_OBJECT public: StyledModel() + : StubDelegateModel("Styled", "Styled") { NodeStyle style = nodeStyle(); style.PenWidth = 7.0f; @@ -37,14 +38,6 @@ class StyledModel : public NodeDelegateModel setNodeStyle(style); } - QString caption() const override { return "Styled"; } - QString name() const override { return "Styled"; } - unsigned int nPorts(PortType) const override { return 0; } - QtNodes::NodeDataType dataType(PortType, PortIndex) const override { return {}; } - std::shared_ptr outData(PortIndex) override { return nullptr; } - void setInData(std::shared_ptr, PortIndex) override {} - QWidget *embeddedWidget() override { return nullptr; } - void changeStyle(QColor const &color) { NodeStyle style = nodeStyle(); @@ -56,18 +49,10 @@ class StyledModel : public NodeDelegateModel }; /// Delegate that never sets its style. It keeps the global style copied at construction. -class PlainModel : public NodeDelegateModel +std::unique_ptr createPlainModel() { - Q_OBJECT -public: - QString caption() const override { return "Plain"; } - QString name() const override { return "Plain"; } - unsigned int nPorts(PortType) const override { return 0; } - QtNodes::NodeDataType dataType(PortType, PortIndex) const override { return {}; } - std::shared_ptr outData(PortIndex) override { return nullptr; } - void setInData(std::shared_ptr, PortIndex) override {} - QWidget *embeddedWidget() override { return nullptr; } -}; + return std::make_unique("Plain", "Plain"); +} NodeStyle styleOf(DataFlowGraphModel &model, NodeId id) { @@ -133,7 +118,7 @@ TEST_CASE("A node that never sets a style keeps the global style from the time i { GlobalStyleGuard guard; auto registry = std::make_shared(); - registry->registerModel("Test"); + registry->registerModel(createPlainModel, "Test"); DataFlowGraphModel model(registry); NodeStyle changed = StyleCollection::nodeStyle(); @@ -170,5 +155,3 @@ TEST_CASE("A node update request after a style change makes the model emit nodeU CHECK(spy.first().first().value() == id); CHECK(styleOf(model, id).GradientColor0 == QColor(1, 2, 3)); } - -#include "TestNodeStyleRole.moc" diff --git a/test/src/TestNodeValidation.cpp b/test/src/TestNodeValidation.cpp index b7b3b96e7..27621d278 100644 --- a/test/src/TestNodeValidation.cpp +++ b/test/src/TestNodeValidation.cpp @@ -1,3 +1,5 @@ +#include "TestDelegateModels.hpp" + #include #include @@ -16,30 +18,21 @@ using QtNodes::NodeValidationState; using QtNodes::PortIndex; using QtNodes::PortType; +namespace { + /// Test delegate model that exposes validation and status setters -class TestValidatedModel : public NodeDelegateModel +class TestValidatedModel : public StubDelegateModel { - Q_OBJECT public: - QString caption() const override { return "Test Validated"; } - QString name() const override { return "TestValidated"; } - - unsigned int nPorts(PortType portType) const override - { - return portType == PortType::In ? 1 : 1; - } + TestValidatedModel() + : StubDelegateModel("TestValidated", "Test Validated", 1, 1) + {} QtNodes::NodeDataType dataType(PortType, PortIndex) const override { return QtNodes::NodeDataType{"test", "Test"}; } - std::shared_ptr outData(PortIndex) override { return nullptr; } - - void setInData(std::shared_ptr, PortIndex) override {} - - QWidget *embeddedWidget() override { return nullptr; } - // Expose validation methods for testing void setTestValidationState(NodeValidationState::State state, QString const &message) { @@ -52,6 +45,8 @@ class TestValidatedModel : public NodeDelegateModel void setTestProcessingStatus(NodeProcessingStatus status) { setNodeProcessingStatus(status); } }; +} // namespace + TEST_CASE("NodeValidationState basic functionality", "[validation]") { SECTION("Default validation state is Valid") @@ -147,5 +142,3 @@ TEST_CASE("NodeProcessingStatus enum values", "[validation]") CHECK(static_cast(NodeProcessingStatus::Failed) == 5); CHECK(static_cast(NodeProcessingStatus::Partial) == 6); } - -#include "TestNodeValidation.moc" diff --git a/test/src/TestSerialization.cpp b/test/src/TestSerialization.cpp index 5b856154e..854076f4e 100644 --- a/test/src/TestSerialization.cpp +++ b/test/src/TestSerialization.cpp @@ -1,4 +1,5 @@ #include "ApplicationSetup.hpp" +#include "TestDelegateModels.hpp" #include #include @@ -20,27 +21,21 @@ using QtNodes::NodeDelegateModelRegistry; using QtNodes::NodeId; using QtNodes::NodeRole; -class SerializableTestModel : public NodeDelegateModel +namespace { + +std::unique_ptr createSerializableModel() { -public: - QString name() const override { return "SerializableTestModel"; } - QString caption() const override { return "Test Model for Serialization"; } - - unsigned int nPorts(QtNodes::PortType portType) const override - { - return (portType == QtNodes::PortType::In) ? 1 : 1; - } - QtNodes::NodeDataType dataType(QtNodes::PortType, QtNodes::PortIndex) const override { return {}; } - void setInData(std::shared_ptr, QtNodes::PortIndex const) override {} - std::shared_ptr outData(QtNodes::PortIndex const) override { return nullptr; } - QWidget* embeddedWidget() override { return nullptr; } -}; + return std::make_unique("SerializableTestModel", + "Test Model for Serialization", 1, 1); +} + +} // namespace TEST_CASE("DataFlowGraphModel serialization", "[serialization]") { auto app = applicationSetup(); auto registry = std::make_shared(); - registry->registerModel("SerializableTestModel"); + registry->registerModel(createSerializableModel, "SerializableTestModel"); DataFlowGraphModel model(registry); @@ -139,7 +134,7 @@ TEST_CASE("Individual node serialization", "[serialization]") { auto app = applicationSetup(); auto registry = std::make_shared(); - registry->registerModel("SerializableTestModel"); + registry->registerModel(createSerializableModel, "SerializableTestModel"); DataFlowGraphModel model(registry);