-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQGVScene.cpp
254 lines (216 loc) · 7.82 KB
/
QGVScene.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
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
/***************************************************************
QGVCore
Copyright (c) 2014, Bergont Nicolas, All rights reserved.
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 3.0 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.
***************************************************************/
#include "QGVScene.h"
// The following include allows the automoc to detect, that it must moc this class
#include "moc_QGVScene.cpp"
#include <QDebug>
#include <QGVNode.h>
#include <QGVEdge.h>
#include <QGVSubGraph.h>
#include <QGVCore.h>
#include <QGVGraphPrivate.h>
#include <QGVGvcPrivate.h>
#include <QGVEdgePrivate.h>
#include <QGVNodePrivate.h>
QGVScene::QGVScene(const QString &name, QObject *parent) : QGraphicsScene(parent)
{
_context = new QGVGvcPrivate(gvContext());
_graph = new QGVGraphPrivate(agopen(name.toLocal8Bit().data(), Agdirected, NULL));
//setGraphAttribute("fontname", QFont().family());
}
QGVScene::~QGVScene()
{
gvFreeLayout(_context->context(), _graph->graph());
agclose(_graph->graph());
gvFreeContext(_context->context());
delete _graph;
delete _context;
}
void QGVScene::setGraphAttribute(const QString &name, const QString &value)
{
agattr(_graph->graph(), AGRAPH, name.toLocal8Bit().data(), value.toLocal8Bit().data());
}
void QGVScene::setNodeAttribute(const QString &name, const QString &value)
{
agattr(_graph->graph(), AGNODE, name.toLocal8Bit().data(), value.toLocal8Bit().data());
}
void QGVScene::setEdgeAttribute(const QString &name, const QString &value)
{
agattr(_graph->graph(), AGEDGE, name.toLocal8Bit().data(), value.toLocal8Bit().data());
}
QGVNode *QGVScene::addNode(const QString &label)
{
Agnode_t *node = agnode(_graph->graph(), NULL, TRUE);
if(node == NULL)
{
qWarning()<<"Invalid node :"<<label;
return 0;
}
QGVNode *item = new QGVNode(new QGVNodePrivate(node), this);
item->setLabel(label);
addItem(item);
_nodes.append(item);
return item;
}
QGVEdge *QGVScene::addEdge(QGVNode *source, QGVNode *target, const QString &label)
{
Agedge_t* edge = agedge(_graph->graph(), source->_node->node(), target->_node->node(), NULL, TRUE);
if(edge == NULL)
{
qWarning()<<"Invalid egde :"<<label;
return 0;
}
QGVEdge *item = new QGVEdge(new QGVEdgePrivate(edge), this);
item->setLabel(label);
addItem(item);
_edges.append(item);
return item;
}
QGVSubGraph *QGVScene::addSubGraph(const QString &name, bool cluster)
{
Agraph_t* sgraph;
if(cluster)
sgraph = agsubg(_graph->graph(), ("cluster_" + name).toLocal8Bit().data(), TRUE);
else
sgraph = agsubg(_graph->graph(), name.toLocal8Bit().data(), TRUE);
if(sgraph == NULL)
{
qWarning()<<"Invalid subGraph :"<<name;
return 0;
}
QGVSubGraph *item = new QGVSubGraph(new QGVGraphPrivate(sgraph), this);
addItem(item);
_subGraphs.append(item);
return item;
}
void QGVScene::setRootNode(QGVNode *node)
{
Q_ASSERT(_nodes.contains(node));
agset(_graph->graph(), "root", node->label().toLocal8Bit().data());
}
void QGVScene::loadLayout(const QString &text)
{
_graph->setGraph(QGVCore::agmemread2(text.toLocal8Bit().constData()));
if(gvLayout(_context->context(), _graph->graph(), "dot") != 0)
{
qCritical()<<"Layout render error"<<agerrors()<<QString::fromLocal8Bit(aglasterr());
return;
}
//Debug output
//gvRenderFilename(_context->context(), _graph->graph(), "png", "debug.png");
//Read nodes and edges
for (Agnode_t* node = agfstnode(_graph->graph()); node != NULL; node = agnxtnode(_graph->graph(), node))
{
QGVNode *inode = new QGVNode(new QGVNodePrivate(node), this);
inode->updateLayout();
addItem(inode);
for (Agedge_t* edge = agfstout(_graph->graph(), node); edge != NULL; edge = agnxtout(_graph->graph(), edge))
{
QGVEdge *iedge = new QGVEdge(new QGVEdgePrivate(edge), this);
iedge->updateLayout();
addItem(iedge);
}
}
update();
}
void QGVScene::applyLayout()
{
if(gvLayout(_context->context(), _graph->graph(), "dot") != 0)
{
/*
* Si plantage ici :
* - Verifier que les dll sont dans le repertoire d'execution
* - Verifie que le fichier "configN" est dans le repertoire d'execution !
*/
qCritical()<<"Layout render error"<<agerrors()<<QString::fromLocal8Bit(aglasterr());
return;
}
//Debug output
//gvRenderFilename(_context->context(), _graph->graph(), "canon", "debug.dot");
//gvRenderFilename(_context->context(), _graph->graph(), "png", "debug.png");
//Update items layout
foreach(QGVNode* node, _nodes)
node->updateLayout();
foreach(QGVEdge* edge, _edges)
edge->updateLayout();
foreach(QGVSubGraph* sgraph, _subGraphs)
sgraph->updateLayout();
//Graph label
textlabel_t *xlabel = GD_label(_graph->graph());
if(xlabel)
{
QGraphicsTextItem *item = addText(xlabel->text);
item->setPos(QGVCore::centerToOrigin(QGVCore::toPoint(xlabel->pos, QGVCore::graphHeight(_graph->graph())), xlabel->dimen.x, -4));
}
update();
}
void QGVScene::clear()
{
gvFreeLayout(_context->context(), _graph->graph());
_nodes.clear();
_edges.clear();
_subGraphs.clear();
QGraphicsScene::clear();
}
#include <QGraphicsSceneContextMenuEvent>
void QGVScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent)
{
QGraphicsItem *item = itemAt(contextMenuEvent->scenePos(), QTransform());
if(item)
{
item->setSelected(true);
if(item->type() == QGVNode::Type)
emit nodeContextMenu(qgraphicsitem_cast<QGVNode*>(item));
else if(item->type() == QGVEdge::Type)
emit edgeContextMenu(qgraphicsitem_cast<QGVEdge*>(item));
else if(item->type() == QGVSubGraph::Type)
emit subGraphContextMenu(qgraphicsitem_cast<QGVSubGraph*>(item));
else
emit graphContextMenuEvent();
}
QGraphicsScene::contextMenuEvent(contextMenuEvent);
}
void QGVScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
QGraphicsItem *item = itemAt(mouseEvent->scenePos(), QTransform());
if(item)
{
if(item->type() == QGVNode::Type)
emit nodeDoubleClick(qgraphicsitem_cast<QGVNode*>(item));
else if(item->type() == QGVEdge::Type)
emit edgeDoubleClick(qgraphicsitem_cast<QGVEdge*>(item));
else if(item->type() == QGVSubGraph::Type)
emit subGraphDoubleClick(qgraphicsitem_cast<QGVSubGraph*>(item));
}
QGraphicsScene::mouseDoubleClickEvent(mouseEvent);
}
#include <QVarLengthArray>
#include <QPainter>
void QGVScene::drawBackground(QPainter * painter, const QRectF & rect)
{
const int gridSize = 25;
const qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
const qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
QVarLengthArray<QLineF, 100> lines;
for (qreal x = left; x < rect.right(); x += gridSize)
lines.append(QLineF(x, rect.top(), x, rect.bottom()));
for (qreal y = top; y < rect.bottom(); y += gridSize)
lines.append(QLineF(rect.left(), y, rect.right(), y));
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setPen(QColor(Qt::lightGray).lighter(110));
painter->drawLines(lines.data(), lines.size());
painter->setPen(Qt::black);
//painter->drawRect(sceneRect());
}