-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQGVSubGraph.cpp
138 lines (116 loc) · 3.84 KB
/
QGVSubGraph.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
/***************************************************************
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 "QGVSubGraph.h"
#include <QGVCore.h>
#include <QGVScene.h>
#include <QGVGraphPrivate.h>
#include <QGVNodePrivate.h>
#include <QGVNode.h>
#include <QDebug>
#include <QPainter>
QGVSubGraph::QGVSubGraph(QGVGraphPrivate *subGraph, QGVScene *scene): _sgraph(subGraph), _scene(scene)
{
//setFlag(QGraphicsItem::ItemIsSelectable, true);
}
QGVSubGraph::~QGVSubGraph()
{
_scene->removeItem(this);
delete _sgraph;
}
QString QGVSubGraph::name() const
{
return QString::fromLocal8Bit(GD_label(_sgraph->graph())->text);
}
QGVNode *QGVSubGraph::addNode(const QString &label)
{
Agnode_t *node = agnode(_sgraph->graph(), NULL, TRUE);
if(node == NULL)
{
qWarning()<<"Invalid sub node :"<<label;
return 0;
}
agsubnode(_sgraph->graph(), node, TRUE);
QGVNode *item = new QGVNode(new QGVNodePrivate(node), _scene);
item->setLabel(label);
_scene->addItem(item);
_scene->_nodes.append(item);
_nodes.append(item);
return item;
}
QGVSubGraph *QGVSubGraph::addSubGraph(const QString &name, bool cluster)
{
Agraph_t* sgraph;
if(cluster)
sgraph = agsubg(_sgraph->graph(), ("cluster_" + name).toLocal8Bit().data(), TRUE);
else
sgraph = agsubg(_sgraph->graph(), name.toLocal8Bit().data(), TRUE);
if(sgraph == NULL)
{
qWarning()<<"Invalid subGraph :"<<name;
return 0;
}
QGVSubGraph *item = new QGVSubGraph(new QGVGraphPrivate(sgraph), _scene);
_scene->_subGraphs.append(item);
_scene->addItem(item);
return item;
}
QRectF QGVSubGraph::boundingRect() const
{
return QRectF(0,0, _width, _height);
}
void QGVSubGraph::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
painter->save();
painter->setPen(_pen);
painter->setBrush(_brush);
painter->drawRect(boundingRect());
painter->drawText(_label_rect, Qt::AlignCenter, _label);
painter->restore();
}
void QGVSubGraph::setAttribute(const QString &name, const QString &value)
{
agsafeset(_sgraph->graph(), name.toLocal8Bit().data(), value.toLocal8Bit().data(), "");
}
QString QGVSubGraph::getAttribute(const QString &name) const
{
char* value = agget(_sgraph->graph(), name.toLocal8Bit().data());
if(value)
return value;
return QString();
}
void QGVSubGraph::updateLayout()
{
prepareGeometryChange();
//SubGraph box
boxf box = GD_bb(_sgraph->graph());
pointf p1 = box.UR;
pointf p2 = box.LL;
_width = p1.x - p2.x;
_height = p1.y - p2.y;
qreal gheight = QGVCore::graphHeight(_scene->_graph->graph());
setPos(p2.x, gheight - p1.y);
_pen.setWidth(1);
_brush.setStyle(QGVCore::toBrushStyle(getAttribute("style")));
_brush.setColor(QGVCore::toColor(getAttribute("fillcolor")));
_pen.setColor(QGVCore::toColor(getAttribute("color")));
//SubGraph label
textlabel_t *xlabel = GD_label(_sgraph->graph());
if(xlabel)
{
_label = xlabel->text;
_label_rect.setSize(QSize(xlabel->dimen.x, xlabel->dimen.y));
_label_rect.moveCenter(QGVCore::toPoint(xlabel->pos, QGVCore::graphHeight(_scene->_graph->graph())) - pos());
}
}