GCC Code Coverage Report


Directory: QGVCore/
File: QGVCore/QGVNode.cpp
Date: 2024-12-03 11:07:38
Exec Total Coverage
Lines: 0 81 0.0%
Branches: 0 24 0.0%

Line Branch Exec Source
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 <QGVCore.h>
19 #include <QGVGraphPrivate.h>
20 #include <QGVNode.h>
21 #include <QGVNodePrivate.h>
22 #include <QGVScene.h>
23
24 #include <QDebug>
25 #include <QPainter>
26
27 ✗ QGVNode::QGVNode(QGVNodePrivate *node, QGVScene *scene)
28 ✗ : _scene(scene), _node(node) {
29 ✗ setFlag(QGraphicsItem::ItemIsSelectable, true);
30 ✗ setFlag(QGraphicsItem::ItemIsMovable, true);
31 ✗ setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
32 }
33
34 ✗ QGVNode::~QGVNode() {
35 ✗ _scene->removeItem(this);
36 ✗ delete _node;
37 }
38
39 ✗ QString QGVNode::label() const { return getAttribute("label"); }
40
41 ✗ void QGVNode::setLabel(const QString &label) { setAttribute("label", label); }
42
43 ✗ QRectF QGVNode::boundingRect() const { return _path.boundingRect(); }
44
45 ✗ void QGVNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
46 QWidget *) {
47 ✗ painter->save();
48
49 ✗ painter->setPen(_pen);
50
51 ✗ if (isSelected()) {
52 ✗ QBrush tbrush(_brush);
53 ✗ tbrush.setColor(tbrush.color().darker(120));
54 ✗ painter->setBrush(tbrush);
55 ✗ } else
56 ✗ painter->setBrush(_brush);
57
58 ✗ painter->drawPath(_path);
59
60 ✗ painter->setPen(QGVCore::toColor(getAttribute("labelfontcolor")));
61
62 ✗ const QRectF rect = boundingRect().adjusted(2, 2, -2, -2); // Margin
63 ✗ if (_icon.isNull()) {
64 ✗ painter->drawText(rect, Qt::AlignCenter, QGVNode::label());
65 } else {
66 ✗ painter->drawText(rect.adjusted(0, 0, 0, -rect.height() * 2 / 3),
67 ✗ Qt::AlignCenter, QGVNode::label());
68
69 ✗ const QRectF img_rect = rect.adjusted(0, rect.height() / 3, 0, 0);
70 ✗ QImage img = _icon.scaled(img_rect.size().toSize(), Qt::KeepAspectRatio,
71 ✗ Qt::SmoothTransformation);
72 ✗ painter->drawImage(
73 ✗ img_rect.topLeft() +
74 ✗ QPointF((img_rect.width() - img.rect().width()) / 2, 0),
75 img);
76 }
77 ✗ painter->restore();
78 }
79
80 ✗ void QGVNode::setAttribute(const QString &name, const QString &value) {
81 ✗ char empty[] = "";
82 ✗ agsafeset(_node->node(), name.toLocal8Bit().data(),
83 ✗ value.toLocal8Bit().data(), empty);
84 }
85
86 ✗ QString QGVNode::getAttribute(const QString &name) const {
87 ✗ char *value = agget(_node->node(), name.toLocal8Bit().data());
88 ✗ if (value) return value;
89 ✗ return QString();
90 }
91
92 ✗ QString QGVNode::posToAttributeString() const {
93 ✗ qreal gheight = QGVCore::graphHeight(_scene->_graph->graph());
94 ✗ qreal width = ND_width(_node->node()) * DotDefaultDPI;
95 ✗ qreal height = ND_height(_node->node()) * DotDefaultDPI;
96 ✗ return QGVCore::qtToGvPos(QGVCore::centerToOrigin(pos(), -width, -height),
97 ✗ gheight);
98 }
99
100 ✗ void QGVNode::setIcon(const QImage &icon) { _icon = icon; }
101
102 ✗ QVariant QGVNode::itemChange(GraphicsItemChange change, const QVariant &value) {
103 ✗ if (change == ItemPositionChange && _scene) {
104 // value is the new position.
105 ✗ QString oldStr = getAttribute(QString::fromLocal8Bit("pos"));
106 ✗ qreal gheight = QGVCore::graphHeight(_scene->_graph->graph());
107 ✗ QPointF newPos = value.toPointF();
108 ✗ qreal width = ND_width(_node->node()) * DotDefaultDPI;
109 ✗ qreal height = ND_height(_node->node()) * DotDefaultDPI;
110 QString newStr = QGVCore::qtToGvPos(
111 ✗ QGVCore::centerToOrigin(newPos, -width, -height), gheight);
112
113 ✗ if (!newStr.isEmpty()) {
114 ✗ if (oldStr != newStr) {
115 ✗ setAttribute("pos", newStr.toLocal8Bit().data());
116 }
117 ✗ return newPos;
118 }
119 ✗ } else if (change == ItemPositionHasChanged && _scene) {
120 ✗ emit _scene->nodeChanged(this);
121 ✗ return value;
122 }
123 ✗ return QGraphicsItem::itemChange(change, value);
124 }
125
126 ✗ void QGVNode::updateLayout() {
127 ✗ prepareGeometryChange();
128 ✗ qreal width = ND_width(_node->node()) * DotDefaultDPI;
129 ✗ qreal height = ND_height(_node->node()) * DotDefaultDPI;
130
131 // Node Position (center)
132 ✗ qreal gheight = QGVCore::graphHeight(_scene->_graph->graph());
133 ✗ setPos(QGVCore::centerToOrigin(
134 ✗ QGVCore::toPoint(ND_coord(_node->node()), gheight), width, height));
135
136 // Node on top
137 ✗ setZValue(1);
138
139 // Node path
140 _path =
141 ✗ QGVCore::toPath(ND_shape(_node->node())->name,
142 ✗ (polygon_t *)ND_shape_info(_node->node()), width, height);
143 ✗ _pen.setWidth(1);
144
145 ✗ _brush.setStyle(QGVCore::toBrushStyle(getAttribute("style")));
146 ✗ _brush.setColor(QGVCore::toColor(getAttribute("fillcolor")));
147 ✗ _pen.setStyle(QGVCore::toPenStyle(getAttribute("style")));
148 ✗ _pen.setColor(QGVCore::toColor(getAttribute("color")));
149
150 ✗ setToolTip(getAttribute("tooltip"));
151 }
152