forked from cneben/QuickQanava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqanGraph.hpp
219 lines (204 loc) · 9.31 KB
/
qanGraph.hpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
Copyright (c) 2008-2023, 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 qanGraph.hpp
// \author [email protected]
// \date 2017 03 19
//-----------------------------------------------------------------------------
namespace qan { // ::qan
/* Graph Factories *///--------------------------------------------------------
template <class Node_t>
Node_t* Graph::insertNode(QQmlComponent* nodeComponent, qan::NodeStyle* nodeStyle)
{
if (nodeComponent == nullptr) {
const auto engine = qmlEngine(this);
nodeComponent = _nodeDelegate.get(); // If no delegate component is specified, try the node type delegate() factory
if (nodeComponent == nullptr &&
engine != nullptr) // Otherwise, use default node delegate component
nodeComponent = Node_t::delegate(*engine);
}
if (nodeComponent != nullptr &&
nodeComponent->isError()) { // If component exists, it should be instanciable
qWarning() << "Component error: " << nodeComponent->errors();
return nullptr;
}
const auto node = new Node_t{}; // Might leak, but using unique_ptr is unsafe given current mxed bool/except error handling
try {
QQmlEngine::setObjectOwnership(node, QQmlEngine::CppOwnership);
if (nodeStyle == nullptr)
nodeStyle = Node_t::style(nullptr);
_styleManager.setStyleComponent(nodeStyle, nodeComponent); // nullptr nodeComponent is ok
qan::NodeItem* nodeItem = nodeComponent != nullptr ? static_cast<qan::NodeItem*>(createFromComponent(nodeComponent,
*nodeStyle,
node)) :
nullptr;
if (nodeItem != nullptr) {
nodeItem->setNode(node);
nodeItem->setGraph(this);
node->setItem(nodeItem);
auto notifyNodeClicked = [this] (qan::NodeItem* nodeItem, QPointF p) {
if (nodeItem != nullptr && nodeItem->getNode() != nullptr)
emit this->nodeClicked(nodeItem->getNode(), p);
};
connect(nodeItem, &qan::NodeItem::nodeClicked,
this, notifyNodeClicked);
auto notifyNodeRightClicked = [this] (qan::NodeItem* nodeItem, QPointF p) {
if (nodeItem != nullptr && nodeItem->getNode() != nullptr)
emit this->nodeRightClicked(nodeItem->getNode(), p);
};
connect(nodeItem, &qan::NodeItem::nodeRightClicked,
this, notifyNodeRightClicked);
auto notifyNodeDoubleClicked = [this] (qan::NodeItem* nodeItem, QPointF p) {
if (nodeItem != nullptr && nodeItem->getNode() != nullptr)
emit this->nodeDoubleClicked(nodeItem->getNode(), p);
};
connect(nodeItem, &qan::NodeItem::nodeDoubleClicked,
this, notifyNodeDoubleClicked);
{ // Send item to front
_maxZ += 1;
nodeItem->setZ(_maxZ);
}
}
insert_node(node); // Insert visual or non visual node
} catch (const qan::Error& e) {
qWarning() << "qan::Graph::insertNode(): Error: " << e.getMsg();
return nullptr; // node eventually destroyed by shared_ptr
}
catch (...) {
qWarning() << "qan::Graph::insertNode(): Error: Topology error.";
return nullptr; // node eventually destroyed by shared_ptr
}
if (node != nullptr) { // Notify user.
onNodeInserted(*node);
emit nodeInserted(node);
}
return node;
}
template <class Node_t>
Node_t* Graph::insertNonVisualNode()
{
const auto node = new Node_t();
try {
QQmlEngine::setObjectOwnership(node, QQmlEngine::CppOwnership);
if (!insert_node(node))
qWarning() << "qan::Graph::insertNonVisualNode(): Error: Insertion error.";
} catch ( ... ) {
qWarning() << "qan::Graph::insertNonVisualNode(): Error: Topology error.";
return nullptr;
}
if (node != nullptr) { // Notify user.
onNodeInserted(*node);
emit nodeInserted(node);
}
return node;
}
//-----------------------------------------------------------------------------
/* Graph Edge Management *///--------------------------------------------------
template < class Edge_t >
qan::Edge* Graph::insertEdge(qan::Node& src, qan::Node* dstNode, QQmlComponent* edgeComponent)
{
if (dstNode == nullptr)
return nullptr;
if (edgeComponent == nullptr) {
const auto engine = qmlEngine(this);
if (engine != nullptr)
edgeComponent = Edge_t::delegate(*engine, nullptr); // If no delegate component is specified, try the edge type delegate() factory
if (edgeComponent == nullptr)
edgeComponent = _edgeDelegate.get(); // Otherwise, use default edge delegate component
}
const auto style = qobject_cast<qan::EdgeStyle*>(Edge_t::style(nullptr));
if (style == nullptr) {
qWarning() << "qan::Graph::insertEdge(): Error: style() factory has returned a nullptr style.";
return nullptr;
}
qan::Edge* configuredEdge = nullptr;
try {
auto edge = new Edge_t{nullptr};
QQmlEngine::setObjectOwnership(edge, QQmlEngine::CppOwnership);
if (edgeComponent != nullptr &&
style != nullptr)
configureEdge(*edge, *edgeComponent, *style,
src, dstNode);
insert_edge(edge);
configuredEdge = edge;
} catch (...) {
qWarning() << "qan::Graph::insertEdge<>(): Error: Topology error.";
// Note: edge is cleaned automatically if it has still not been inserted to graph
}
if (configuredEdge != nullptr)
emit edgeInserted(configuredEdge);
return configuredEdge;
}
template < class Edge_t >
qan::Edge* Graph::insertNonVisualEdge(qan::Node& src, qan::Node* dstNode)
{
if (dstNode == nullptr)
return nullptr;
auto edge = new Edge_t();
try {
QQmlEngine::setObjectOwnership(edge, QQmlEngine::CppOwnership);
edge->set_src(&src);
if (dstNode != nullptr)
edge->set_dst(dstNode);
insert_edge(edge);
} catch (...) {
qWarning() << "qan::Graph::insertNonVisualEdge<>(): Error: Topology error.";
}
return edge;
}
//-----------------------------------------------------------------------------
/* Graph Group Management *///-------------------------------------------------
template <class Group_t>
qan::Group* Graph::insertGroup(QQmlComponent* groupComponent)
{
if (groupComponent == nullptr) {
const auto engine = qmlEngine(this);
if (engine != nullptr)
groupComponent = Group_t::delegate(*engine, nullptr);
}
if (groupComponent == nullptr)
groupComponent = _groupDelegate.get();
auto group = new Group_t();
if (!insertGroup(group, groupComponent, nullptr))
qWarning() << "qan::Graph::insertGroup<>(): Warning: Error at group insertion.";
return group;
}
template <class TableGroup_t>
qan::Group* Graph::insertTable(int cols, int rows)
{
const auto engine = qmlEngine(this);
QQmlComponent* groupComponent = nullptr;
if (engine != nullptr)
groupComponent = TableGroup_t::delegate(*engine, nullptr);
if (groupComponent == nullptr)
groupComponent = _groupDelegate.get();
auto group = new TableGroup_t(cols, rows);
if (!insertGroup(group, groupComponent, nullptr))
qWarning() << "qan::Graph::insertGroup<>(): Warning: Error at group insertion.";
return group;
}
//-----------------------------------------------------------------------------
} // ::qan