1 |
|
|
/*************************************************************** |
2 |
|
|
QGVCore |
3 |
|
|
Copyright (c) 2014, Bergont Nicolas, All rights reserved. |
4 |
|
|
|
5 |
|
|
This library is free software; you can redistribute it and/or |
6 |
|
|
modify it under the terms of the GNU Lesser General Public |
7 |
|
|
License as published by the Free Software Foundation; either |
8 |
|
|
version 3.0 of the License, or (at your option) any later version. |
9 |
|
|
|
10 |
|
|
This library is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
|
|
Lesser General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU Lesser General Public |
16 |
|
|
License along with this library. |
17 |
|
|
***************************************************************/ |
18 |
|
|
#include "QGVSubGraph.h" |
19 |
|
|
|
20 |
|
|
#include <QGVCore.h> |
21 |
|
|
#include <QGVGraphPrivate.h> |
22 |
|
|
#include <QGVNode.h> |
23 |
|
|
#include <QGVNodePrivate.h> |
24 |
|
|
#include <QGVScene.h> |
25 |
|
|
|
26 |
|
|
#include <QDebug> |
27 |
|
|
#include <QPainter> |
28 |
|
|
|
29 |
|
|
QGVSubGraph::QGVSubGraph(QGVGraphPrivate *subGraph, QGVScene *scene) |
30 |
|
|
: _scene(scene), _sgraph(subGraph) { |
31 |
|
|
// setFlag(QGraphicsItem::ItemIsSelectable, true); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
QGVSubGraph::~QGVSubGraph() { |
35 |
|
|
_scene->removeItem(this); |
36 |
|
|
delete _sgraph; |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
QString QGVSubGraph::name() const { |
40 |
|
|
return QString::fromLocal8Bit(GD_label(_sgraph->graph())->text); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
QGVNode *QGVSubGraph::addNode(const QString &label) { |
44 |
|
|
Agnode_t *node = agnode(_sgraph->graph(), NULL, true); |
45 |
|
|
if (node == NULL) { |
46 |
|
|
qWarning() << "Invalid sub node :" << label; |
47 |
|
|
return 0; |
48 |
|
|
} |
49 |
|
|
agsubnode(_sgraph->graph(), node, true); |
50 |
|
|
|
51 |
|
|
QGVNode *item = new QGVNode(new QGVNodePrivate(node), _scene); |
52 |
|
|
item->setLabel(label); |
53 |
|
|
_scene->addItem(item); |
54 |
|
|
_scene->_nodes.append(item); |
55 |
|
|
_nodes.append(item); |
56 |
|
|
return item; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
QGVSubGraph *QGVSubGraph::addSubGraph(const QString &name, bool cluster) { |
60 |
|
|
Agraph_t *sgraph; |
61 |
|
|
if (cluster) |
62 |
|
|
sgraph = agsubg(_sgraph->graph(), ("cluster_" + name).toLocal8Bit().data(), |
63 |
|
|
true); |
64 |
|
|
else |
65 |
|
|
sgraph = agsubg(_sgraph->graph(), name.toLocal8Bit().data(), true); |
66 |
|
|
|
67 |
|
|
if (sgraph == NULL) { |
68 |
|
|
qWarning() << "Invalid subGraph :" << name; |
69 |
|
|
return 0; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
QGVSubGraph *item = new QGVSubGraph(new QGVGraphPrivate(sgraph), _scene); |
73 |
|
|
_scene->_subGraphs.append(item); |
74 |
|
|
_scene->addItem(item); |
75 |
|
|
return item; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
QRectF QGVSubGraph::boundingRect() const { |
79 |
|
|
return QRectF(0, 0, _width, _height); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
void QGVSubGraph::paint(QPainter *painter, const QStyleOptionGraphicsItem *, |
83 |
|
|
QWidget *) { |
84 |
|
|
painter->save(); |
85 |
|
|
|
86 |
|
|
painter->setPen(_pen); |
87 |
|
|
painter->setBrush(_brush); |
88 |
|
|
|
89 |
|
|
painter->drawRect(boundingRect()); |
90 |
|
|
painter->drawText(_label_rect, Qt::AlignCenter, _label); |
91 |
|
|
painter->restore(); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
void QGVSubGraph::setAttribute(const QString &name, const QString &value) { |
95 |
|
|
char empty[] = ""; |
96 |
|
|
agsafeset(_sgraph->graph(), name.toLocal8Bit().data(), |
97 |
|
|
value.toLocal8Bit().data(), empty); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
QString QGVSubGraph::getAttribute(const QString &name) const { |
101 |
|
|
char *value = agget(_sgraph->graph(), name.toLocal8Bit().data()); |
102 |
|
|
if (value) return value; |
103 |
|
|
return QString(); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
void QGVSubGraph::updateLayout() { |
107 |
|
|
prepareGeometryChange(); |
108 |
|
|
|
109 |
|
|
// SubGraph box |
110 |
|
|
boxf box = GD_bb(_sgraph->graph()); |
111 |
|
|
pointf p1 = box.UR; |
112 |
|
|
pointf p2 = box.LL; |
113 |
|
|
_width = p1.x - p2.x; |
114 |
|
|
_height = p1.y - p2.y; |
115 |
|
|
|
116 |
|
|
qreal gheight = QGVCore::graphHeight(_scene->_graph->graph()); |
117 |
|
|
setPos(p2.x, gheight - p1.y); |
118 |
|
|
|
119 |
|
|
_pen.setWidth(1); |
120 |
|
|
_brush.setStyle(QGVCore::toBrushStyle(getAttribute("style"))); |
121 |
|
|
_brush.setColor(QGVCore::toColor(getAttribute("fillcolor"))); |
122 |
|
|
_pen.setColor(QGVCore::toColor(getAttribute("color"))); |
123 |
|
|
|
124 |
|
|
// SubGraph label |
125 |
|
|
textlabel_t *xlabel = GD_label(_sgraph->graph()); |
126 |
|
|
if (xlabel) { |
127 |
|
|
_label = xlabel->text; |
128 |
|
|
_label_rect.setSize(QSize(xlabel->dimen.x, xlabel->dimen.y)); |
129 |
|
|
_label_rect.moveCenter( |
130 |
|
|
QGVCore::toPoint(xlabel->pos, |
131 |
|
|
QGVCore::graphHeight(_scene->_graph->graph())) - |
132 |
|
|
pos()); |
133 |
|
|
} |
134 |
|
|
} |