-
Notifications
You must be signed in to change notification settings - Fork 948
Expand file tree
/
Copy pathTestCustomPainters.cpp
More file actions
180 lines (142 loc) · 5.46 KB
/
TestCustomPainters.cpp
File metadata and controls
180 lines (142 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "ApplicationSetup.hpp"
#include "TestGraphModel.hpp"
#include "UITestHelper.hpp"
#include <catch2/catch.hpp>
#include <QtNodes/internal/AbstractConnectionPainter.hpp>
#include <QtNodes/internal/AbstractNodePainter.hpp>
#include <QtNodes/internal/BasicGraphicsScene.hpp>
#include <QtNodes/internal/ConnectionGraphicsObject.hpp>
#include <QtNodes/internal/GraphicsView.hpp>
#include <QtNodes/internal/NodeGraphicsObject.hpp>
#include <QImage>
#include <QPainter>
#include <QPixmap>
#include <QTest>
using QtNodes::AbstractConnectionPainter;
using QtNodes::AbstractNodePainter;
using QtNodes::BasicGraphicsScene;
using QtNodes::ConnectionGraphicsObject;
using QtNodes::ConnectionId;
using QtNodes::GraphicsView;
using QtNodes::NodeGraphicsObject;
using QtNodes::NodeId;
using QtNodes::NodeRole;
/// Custom node painter for testing
class TestNodePainter : public AbstractNodePainter
{
public:
mutable int paintCallCount = 0;
mutable NodeId lastPaintedNodeId = 0;
void paint(QPainter *painter, NodeGraphicsObject &ngo) const override
{
paintCallCount++;
lastPaintedNodeId = ngo.nodeId();
// Simple paint implementation
painter->setBrush(Qt::blue);
painter->drawRect(0, 0, 100, 50);
}
};
/// Custom connection painter for testing
class TestConnectionPainter : public AbstractConnectionPainter
{
public:
mutable int paintCallCount = 0;
void paint(QPainter *painter, ConnectionGraphicsObject const &cgo) const override
{
paintCallCount++;
QPen pen(Qt::red, 2);
painter->setPen(pen);
painter->drawLine(cgo.endPoint(QtNodes::PortType::Out), cgo.endPoint(QtNodes::PortType::In));
}
QPainterPath getPainterStroke(ConnectionGraphicsObject const &cgo) const override
{
QPainterPath path;
path.moveTo(cgo.endPoint(QtNodes::PortType::Out));
path.lineTo(cgo.endPoint(QtNodes::PortType::In));
QPainterPathStroker stroker;
stroker.setWidth(10.0);
return stroker.createStroke(path);
}
};
TEST_CASE("Custom painters registration", "[painters]")
{
auto app = applicationSetup();
TestGraphModel model;
BasicGraphicsScene scene(model);
SECTION("Scene has default painters initially")
{
// Scene should have valid painters
AbstractNodePainter &nodePainter = scene.nodePainter();
AbstractConnectionPainter &connPainter = scene.connectionPainter();
// Basic check that painters exist (won't crash)
CHECK(&nodePainter != nullptr);
CHECK(&connPainter != nullptr);
}
SECTION("Custom node painter can be registered")
{
auto customPainter = std::make_unique<TestNodePainter>();
TestNodePainter *painterPtr = customPainter.get();
scene.setNodePainter(std::move(customPainter));
// Verify the painter was set
AbstractNodePainter ¤tPainter = scene.nodePainter();
CHECK(¤tPainter == painterPtr);
}
SECTION("Custom connection painter can be registered")
{
auto customPainter = std::make_unique<TestConnectionPainter>();
TestConnectionPainter *painterPtr = customPainter.get();
scene.setConnectionPainter(std::move(customPainter));
// Verify the painter was set
AbstractConnectionPainter ¤tPainter = scene.connectionPainter();
CHECK(¤tPainter == painterPtr);
}
SECTION("Painter replacement")
{
auto painter1 = std::make_unique<TestNodePainter>();
auto painter2 = std::make_unique<TestNodePainter>();
TestNodePainter *ptr2 = painter2.get();
scene.setNodePainter(std::move(painter1));
scene.setNodePainter(std::move(painter2));
// Second painter should be active
AbstractNodePainter ¤tPainter = scene.nodePainter();
CHECK(¤tPainter == ptr2);
}
}
TEST_CASE("Custom painter with scene operations", "[painters]")
{
auto app = applicationSetup();
auto model = std::make_shared<TestGraphModel>();
BasicGraphicsScene scene(*model);
auto customNodePainter = std::make_unique<TestNodePainter>();
TestNodePainter *nodePainterPtr = customNodePainter.get();
scene.setNodePainter(std::move(customNodePainter));
SECTION("Custom painter persists after node creation and view operations")
{
GraphicsView view(&scene);
view.resize(800, 600);
view.show();
REQUIRE(QTest::qWaitForWindowExposed(&view));
NodeId nodeId = model->addNode("TestNode");
model->setNodeData(nodeId, NodeRole::Position, QPointF(100, 100));
QCoreApplication::processEvents();
// Verify the node graphics object exists
auto *ngo = scene.nodeGraphicsObject(nodeId);
REQUIRE(ngo != nullptr);
// Verify the custom painter is still set on the scene after all operations
CHECK(&scene.nodePainter() == nodePainterPtr);
}
SECTION("Custom painter persists through multiple node lifecycle events")
{
// Create nodes
NodeId node1 = model->addNode("TestNode1");
NodeId node2 = model->addNode("TestNode2");
model->setNodeData(node1, NodeRole::Position, QPointF(0, 0));
model->setNodeData(node2, NodeRole::Position, QPointF(200, 0));
QCoreApplication::processEvents();
// Delete one node
model->deleteNode(node1);
QCoreApplication::processEvents();
// Custom painter should still be set
CHECK(&scene.nodePainter() == nodePainterPtr);
}
}