-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQGVNode.cpp
127 lines (103 loc) · 3.58 KB
/
QGVNode.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
/***************************************************************
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 <QGVNode.h>
#include <QGVCore.h>
#include <QGVScene.h>
#include <QGVGraphPrivate.h>
#include <QGVNodePrivate.h>
#include <QDebug>
#include <QPainter>
QGVNode::QGVNode(QGVNodePrivate *node, QGVScene *scene): _node(node), _scene(scene)
{
setFlag(QGraphicsItem::ItemIsSelectable, true);
}
QGVNode::~QGVNode()
{
_scene->removeItem(this);
delete _node;
}
QString QGVNode::label() const
{
return getAttribute("label");
}
void QGVNode::setLabel(const QString &label)
{
setAttribute("label", label);
}
QRectF QGVNode::boundingRect() const
{
return _path.boundingRect();
}
void QGVNode::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
painter->save();
painter->setPen(_pen);
if(isSelected())
{
QBrush tbrush(_brush);
tbrush.setColor(tbrush.color().darker(120));
painter->setBrush(tbrush);
}
else
painter->setBrush(_brush);
painter->drawPath(_path);
painter->setPen(QGVCore::toColor(getAttribute("labelfontcolor")));
const QRectF rect = boundingRect().adjusted(2,2,-2,-2); //Margin
if(_icon.isNull())
{
painter->drawText(rect, Qt::AlignCenter , QGVNode::label());
}
else
{
painter->drawText(rect.adjusted(0,0,0, -rect.height()*2/3), Qt::AlignCenter , QGVNode::label());
const QRectF img_rect = rect.adjusted(0, rect.height()/3,0, 0);
QImage img = _icon.scaled(img_rect.size().toSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
painter->drawImage(img_rect.topLeft() + QPointF((img_rect.width() - img.rect().width())/2, 0), img);
}
painter->restore();
}
void QGVNode::setAttribute(const QString &name, const QString &value)
{
agsafeset(_node->node(), name.toLocal8Bit().data(), value.toLocal8Bit().data(), "");
}
QString QGVNode::getAttribute(const QString &name) const
{
char* value = agget(_node->node(), name.toLocal8Bit().data());
if(value)
return value;
return QString();
}
void QGVNode::setIcon(const QImage &icon)
{
_icon = icon;
}
void QGVNode::updateLayout()
{
prepareGeometryChange();
qreal width = ND_width(_node->node())*DotDefaultDPI;
qreal height = ND_height(_node->node())*DotDefaultDPI;
//Node Position (center)
qreal gheight = QGVCore::graphHeight(_scene->_graph->graph());
setPos(QGVCore::centerToOrigin(QGVCore::toPoint(ND_coord(_node->node()), gheight), width, height));
//Node on top
setZValue(1);
//Node path
_path = QGVCore::toPath(ND_shape(_node->node())->name, (polygon_t*)ND_shape_info(_node->node()), width, height);
_pen.setWidth(1);
_brush.setStyle(QGVCore::toBrushStyle(getAttribute("style")));
_brush.setColor(QGVCore::toColor(getAttribute("fillcolor")));
_pen.setColor(QGVCore::toColor(getAttribute("color")));
setToolTip(getAttribute("tooltip"));
}