-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp_sample.cpp
181 lines (152 loc) · 7.03 KB
/
cpp_sample.cpp
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
/*
Copyright (c) 2008-2018, Benoit AUTHEMAN All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the author or Destrat.io nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//-----------------------------------------------------------------------------
// This file is a part of the QuickQanava software library.
//
// \file cpp_sample.cpp
// \author benoit@destrat.io
// \date 2018 05 24
//-----------------------------------------------------------------------------
// Qt headers
#include <QGuiApplication>
#include <QtQml>
#include <QQuickStyle>
// QuickQanava headers
#include <QuickQanava.h>
#include "./cpp_sample.h"
using namespace qan;
QQmlComponent *CustomGroup::delegate(QQmlEngine &engine) noexcept {
static std::unique_ptr<QQmlComponent> delegate;
if (!delegate)
delegate = std::make_unique<QQmlComponent>(&engine, "qrc:/CustomGroup.qml");
return delegate.get();
}
qan::Style *CustomGroup::style() noexcept {
static std::unique_ptr<qan::Style> style;
if (!style) {
style = std::make_unique<qan::Style>();
}
return style.get();
}
QQmlComponent *CustomNode::delegate(QQmlEngine &engine) noexcept
{
static std::unique_ptr<QQmlComponent> CustomRectNode_delegate;
if (!CustomRectNode_delegate)
CustomRectNode_delegate =
std::make_unique<QQmlComponent>(&engine, "qrc:/CustomNode.qml");
return CustomRectNode_delegate.get();
}
qan::NodeStyle *CustomNode::style() noexcept
{
static std::unique_ptr<qan::NodeStyle> CustomRectNode_style;
if (!CustomRectNode_style) {
CustomRectNode_style = std::make_unique<qan::NodeStyle>();
CustomRectNode_style->setBackColor(QColor("#ff29fc"));
}
return CustomRectNode_style.get();
}
QQmlComponent *CustomEdge::delegate(QQmlEngine &engine) noexcept {
static std::unique_ptr<QQmlComponent> CustomEdge_delegate;
if (!CustomEdge_delegate)
CustomEdge_delegate =
std::make_unique<QQmlComponent>(&engine, "qrc:/CustomEdge.qml");
return CustomEdge_delegate.get();
}
qan::EdgeStyle *CustomEdge::style() noexcept {
static std::unique_ptr<qan::EdgeStyle> CustomEdge_style;
if (!CustomEdge_style)
CustomEdge_style = std::make_unique<qan::EdgeStyle>();
return CustomEdge_style.get();
}
qan::Group *CustomGraph::insertCustomGroup() {
return qan::Graph::insertGroup<CustomGroup>();
}
qan::Node *CustomGraph::insertCustomNode() {
return qan::Graph::insertNode<CustomNode>(nullptr);
}
qan::Edge *CustomGraph::insertCustomEdge(qan::Node *source,
qan::Node *destination) {
const auto engine = qmlEngine(this);
if (source != nullptr &&
destination != nullptr &&
engine != nullptr)
return qan::Graph::insertEdge<CustomEdge>(*source, destination, CustomEdge::delegate(*engine));
return nullptr;
}
//-----------------------------------------------------------------------------
int main( int argc, char** argv )
{
QGuiApplication app(argc, argv);
QQuickStyle::setStyle("Material");
QQmlApplicationEngine engine;
engine.addPluginPath(QStringLiteral("./QuickQanava/src")); // Necessary only for development when plugin is not installed to QTDIR/qml
QuickQanava::initialize(&engine);
qmlRegisterType<CustomGroup>("MyModule", 1, 0, "CustomGroup");
qmlRegisterType<CustomNode>("MyModule", 1, 0, "CustomNode");
qmlRegisterType<CustomGraph>("MyModule", 1, 0, "CustomGraph");
qmlRegisterType<CustomEdge>("MyModule", 1, 0, "AbstractCustomEdge");
engine.load(QUrl("qrc:/cpp_sample.qml"));
{ // We can here customize QuickQanava graph topology _synchronously_ before the
// Qt/QML event loop starts
QPointer<CustomGraph> graph = nullptr;
for (const auto rootObject : engine.rootObjects()) {
graph = qobject_cast<CustomGraph*>(rootObject->findChild<QQuickItem *>("graph"));
if (graph)
break;
}
if (graph) {
unsigned int n = 0;
static constexpr qreal defaultWidth{40.}, defaultHeight{30.};
static constexpr qreal xSpacing{50.}, ySpacing{30.};
static constexpr int array_size = 3;
qan::Node* nodes[array_size][array_size];
for (int r = 0; r < array_size; r++) {
qreal nodeX = r * (defaultWidth + 2 * xSpacing );
for (int c = 0; c < array_size; c++) {
qreal nodeY = c * (defaultHeight + 2 * ySpacing);
auto node = graph->insertCustomNode();
node->getItem()->setMinimumSize({defaultWidth / 2., defaultHeight / 2.});
node->getItem()->setRect({nodeX, nodeY, defaultWidth, defaultHeight});
// Equivalent to:
//node->getItem()->setX(nodeX);
//node->getItem()->setY(nodeY);
//node->getItem()->setWidth(defaultWidth);
//node->getItem()->setHeight(defaultHeight);
//node->setLabel(QString::number(n++));
nodes[r][c] = node;
} // for columns
} // for rows
auto group = graph->insertCustomGroup();
group->getItem()->setRect({300, 200, 400, 250});
// Grouping nodes from c++
graph->groupNode(group, nodes[0][0], false);
// NOTE: If the node is already on top of the group were we want to insert node, use transformPosition=true
// to convert node position from graphview coordinate system to group coordinate system.
// If the transformPosition=false is used, node will be grouped at (O, O) position of group, and
// it's position should be set after the call to groupNode().
}
}
return app.exec();
}
//-----------------------------------------------------------------------------