-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQGraphCanvas.cpp
More file actions
331 lines (295 loc) · 11.9 KB
/
QGraphCanvas.cpp
File metadata and controls
331 lines (295 loc) · 11.9 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*!
\file QGraphCanvas.cpp
\author Gregory Schultz <gregory.schultz@embarqmail.com>
\section LICENSE
This file is part of the Open|SpeedShop Graphical User Interface
Copyright (C) 2010-2017 Argo Navis Technologies, LLC
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "QGraphCanvas.h"
#include "QGraphCanvasPrivate.h"
#include "QGraphNode.h"
#include "QGraphEdge.h"
#include <QPainter>
#include <QDebug>
/**
* @brief QGraphCanvas::QGraphCanvas
* @param name - the name of the graph
* @param graphAttributeSettings - a list of name/value pairs specifying graph attribute settings
* @param nodeAttributeSettings - a list of name/value pairs specifying node attribute settings
* @param edgeAttributeSettings - a list of name/value pairs specifying edge attribute settings
* @param parent - the parent widget
*
* Constructs an QGraphCanvas instance of the given parent with no graph nodes or edges defined.
*/
QGraphCanvas::QGraphCanvas(const QString &name,
const NameValueList& graphAttributeSettings,
const NameValueList& nodeAttributeSettings,
const NameValueList& edgeAttributeSettings,
QObject *parent)
: QGraphicsScene( parent )
, d_ptr( new QGraphCanvasPrivate( name, graphAttributeSettings, nodeAttributeSettings, edgeAttributeSettings, this ) )
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
connect( this, &QGraphCanvas::layoutUpdated, this, &QGraphCanvas::handleLayoutUdated );
#else
connect( this, SIGNAL(layoutUpdated()), this, SLOT(handleLayoutUdated()) );
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
connect( this, &QGraphCanvas::selectionChanged, this, &QGraphCanvas::handleSelectionChanged );
#else
connect( this, SIGNAL(selectionChanged()), this, SLOT(handleSelectionChanged()) );
#endif
}
/**
* @brief QGraphCanvas::QGraphCanvas
* @param name - pointer to a DOT formatted character array in memory
* @param graphAttributeSettings - a list of name/value pairs specifying graph attribute settings
* @param nodeAttributeSettings - a list of name/value pairs specifying node attribute settings
* @param edgeAttributeSettings - a list of name/value pairs specifying edge attribute settings
* @param parent - the parent widget
*
* Constructs an QGraphCanvas instance of the given parent from a pointer to a DOT formatted
* character array in memory. The graph nodes and edges will be created automatically.
*/
QGraphCanvas::QGraphCanvas(const char *data,
const NameValueList& graphAttributeSettings,
const NameValueList& nodeAttributeSettings,
const NameValueList& edgeAttributeSettings,
QObject *parent)
: QGraphicsScene( parent )
, d_ptr( new QGraphCanvasPrivate( data, graphAttributeSettings, nodeAttributeSettings, edgeAttributeSettings, this ) )
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
connect( this, &QGraphCanvas::layoutUpdated, this, &QGraphCanvas::handleLayoutUdated );
#else
connect( this, SIGNAL(layoutUpdated()), this, SLOT(handleLayoutUdated()) );
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
connect( this, &QGraphCanvas::selectionChanged, this, &QGraphCanvas::handleSelectionChanged );
#else
connect( this, SIGNAL(selectionChanged()), this, SLOT(handleSelectionChanged()) );
#endif
}
/**
* @brief QGraphCanvas::QGraphCanvas
* @param name - the name of the graph
* @param sceneRect - the defined bounding rectangle for the scene
* @param graphAttributeSettings - a list of name/value pairs specifying graph attribute settings
* @param nodeAttributeSettings - a list of name/value pairs specifying node attribute settings
* @param edgeAttributeSettings - a list of name/value pairs specifying edge attribute settings
* @param parent - the parent widget
*
* Constructs an QGraphCanvas instance of the given parent with no graph nodes or edges defined.
*/
QGraphCanvas::QGraphCanvas(const QString &name, const QRectF &sceneRect,
const NameValueList& graphAttributeSettings,
const NameValueList& nodeAttributeSettings,
const NameValueList& edgeAttributeSettings,
QObject *parent)
: QGraphicsScene( sceneRect, parent )
, d_ptr( new QGraphCanvasPrivate( name, graphAttributeSettings, nodeAttributeSettings, edgeAttributeSettings, this ) )
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
connect( this, &QGraphCanvas::layoutUpdated, this, &QGraphCanvas::handleLayoutUdated );
#else
connect( this, SIGNAL(layoutUpdated()), this, SLOT(handleLayoutUdated()) );
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
connect( this, &QGraphCanvas::selectionChanged, this, &QGraphCanvas::handleSelectionChanged );
#else
connect( this, SIGNAL(selectionChanged()), this, SLOT(handleSelectionChanged()) );
#endif
}
/**
* @brief QGraphCanvas::QGraphCanvas
* @param x - the top-left corner x-coordinate of the bounding rectangle for the scene
* @param y - the top-left corner y-coordinate of the bounding rectangle for the scene
* @param width - the width of the bounding rectangle for the scene
* @param height - the height of the bounding rectangle for the scene
* @param sceneRect - the defined bounding rectangle for the scene
* @param graphAttributeSettings - a list of name/value pairs specifying graph attribute settings
* @param nodeAttributeSettings - a list of name/value pairs specifying node attribute settings
* @param edgeAttributeSettings - a list of name/value pairs specifying edge attribute settings
* @param parent - the parent widget
*
* Constructs an QGraphCanvas instance of the given parent with no graph nodes or edges defined.
*/
QGraphCanvas::QGraphCanvas(const QString &name, qreal x, qreal y, qreal width, qreal height,
const NameValueList& graphAttributeSettings,
const NameValueList& nodeAttributeSettings,
const NameValueList& edgeAttributeSettings,
QObject *parent)
: QGraphicsScene( x, y, width, height, parent )
, d_ptr( new QGraphCanvasPrivate( name, graphAttributeSettings, nodeAttributeSettings, edgeAttributeSettings, this ) )
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
connect( this, &QGraphCanvas::layoutUpdated, this, &QGraphCanvas::handleLayoutUdated );
#else
connect( this, SIGNAL(layoutUpdated()), this, SLOT(handleLayoutUdated()) );
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
connect( this, &QGraphCanvas::selectionChanged, this, &QGraphCanvas::handleSelectionChanged );
#else
connect( this, SIGNAL(selectionChanged()), this, SLOT(handleSelectionChanged()) );
#endif
}
/**
* @brief QGraphCanvas::~QGraphCanvas
*
* Destroys the QGraphCanvas instance.
*/
QGraphCanvas::~QGraphCanvas()
{
Q_D(QGraphCanvas);
delete d;
}
/**
* @brief QGraphCanvas::updateLayout
*
* Updates the layout of the graph canvas.
*/
void QGraphCanvas::updateLayout()
{
Q_D(QGraphCanvas);
d->updateLayout();
}
/**
* @brief QGraphCanvas::showGrid
* @param shown - indicates whether the graph grid background will be shown
*
* Sets the state flag indicating whether the grid will be drawn on the canvas background.
*/
void QGraphCanvas::showGrid(bool shown)
{
Q_D(QGraphCanvas);
d->showGrid( shown );
}
/**
* @brief QGraphCanvas::addGraphNode
* @param node - the QGraphNode instance to add to the graph canvas
*
* Adds a new node instance to the graph. Connects the QGraphCanvas::layoutUpdated signal
* to a graph node handler for updating the node state.
*/
void QGraphCanvas::addGraphNode(QGraphNode *node)
{
addItem( node );
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
connect( this, &QGraphCanvas::layoutUpdated, node, &QGraphNode::updateState );
#else
connect( this, SIGNAL(layoutUpdated()), node, SLOT(updateState()) );
#endif
}
/**
* @brief QGraphCanvas::addGraphNode
* @param edge - the QGraphEdge instance to add to the graph canvas
*
* Adds a new edge instance to the graph. Connects the QGraphCanvas::layoutUpdated signal
* to a graph edge handler for updating the edge state.
*/
void QGraphCanvas::addGraphEdge(QGraphEdge *edge)
{
addItem( edge );
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
connect( this, &QGraphCanvas::layoutUpdated, edge, &QGraphEdge::updateState );
#else
connect( this, SIGNAL(layoutUpdated()), edge, SLOT(updateState()) );
#endif
}
/**
* @brief QGraphCanvas::addItem
* @param item - the QGraphicsItem to add to the scene
*
* Overrides the public QGraphicsScene::addItem method and makes it protected. It is invoked
* by the public QGraphCanvas::addGraphNode and QGraphCanvas::addGraphEdge methods.
*/
void QGraphCanvas::addItem(QGraphicsItem *item)
{
QGraphicsScene::addItem( item );
}
/**
* @brief QGraphCanvas::drawBackground
* @param painter - the scene painter instance to be used for drawing the background
* @param rect - the exposed rectangle within the scene
*
* Reimplements the QGraphicsScene::drawBackground method to draw a grid background.
* All painting is done in scene coordinates.
*/
void QGraphCanvas::drawBackground(QPainter *painter, const QRectF &rect)
{
Q_D(QGraphCanvas);
if ( d->drawGrid() ) {
painter->save();
const QPen gridPen(qRgb(240, 240, 240));
painter->setPen( gridPen );
const QRectF srect = sceneRect();
for (qreal x = srect.x(); x <= srect.width(); x+=10.0 ) {
painter->drawLine( QLineF( QPointF(x,srect.y()), QPointF(x, srect.height()) ) );
}
for (qreal y = srect.y(); y <= srect.height(); y+=10.0) {
painter->drawLine( QLineF( QPointF(srect.x(),y), QPointF(srect.width(),y) ) );
}
const QPen borderPen( QColor(Qt::gray), 1.5 );
painter->setPen( borderPen );
painter->drawRect( srect );
painter->restore();
}
QGraphicsScene::drawBackground( painter, rect );
}
/**
* @brief QGraphCanvas::addGraphNode
* @param node - a libcgraph node to construct a QGraphNode instance
*
* Constructs a QGraphNode instance from a libcgraph node and adds to the graph.
*/
void QGraphCanvas::addGraphNode(void *node)
{
addGraphNode( new QGraphNode( node ) );
}
/**
* @brief QGraphCanvas::addGraphEdge
* @param node - a libcgraph edge to construct a QGraphEdge instance
*
* Constructs a QGraphEdge instance from a libcgraph edge and adds to the graph.
*/
void QGraphCanvas::addGraphEdge(void *edge)
{
addGraphEdge( new QGraphEdge( edge ) );
}
/**
* @brief QGraphCanvas::handleLayoutUdated
*
* Handler for layoutUpdated() signal to set a new scene bounding box.
*/
void QGraphCanvas::handleLayoutUdated()
{
Q_D(QGraphCanvas);
setSceneRect( d->boundingBox() );
}
/**
* @brief QGraphCanvas::handleSelectionChanged
*
* Handlers for QGraphicsScene::selectionChanged() signal emitted when the
* selection of graph items in the scene changes.
*/
void QGraphCanvas::handleSelectionChanged()
{
const QList<QGraphicsItem *> items = selectedItems();
foreach( QGraphicsItem* item, items ) {
if ( item->type() == QGraphNode::Type )
emit graphNodeSelected( qgraphicsitem_cast<QGraphNode*>(item) );
else if ( item->type() == QGraphEdge::Type )
emit graphEdgeSelected( qgraphicsitem_cast<QGraphEdge*>(item) );
}
}