File indexing completed on 2024-05-12 05:44:25

0001 /***************************************************************************
0002  *   Copyright (C) 2006-2009 by Rajko Albrecht                             *
0003  *   ral@alwins-world.de                                                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 #include "graphtreelabel.h"
0021 #include "graphtree_defines.h"
0022 
0023 #include <QPainter>
0024 #include <QPixmap>
0025 #include <QStyleOptionGraphicsItem>
0026 
0027 GraphTreeLabel::GraphTreeLabel(const QString &text, const QString &_nodename, const QRectF &r, QGraphicsItem *p)
0028     : QGraphicsRectItem(r, p)
0029     , StoredDrawParams()
0030     , m_Nodename(_nodename)
0031     , m_SourceNode()
0032 {
0033     m_Nodename = _nodename;
0034     setText(0, text);
0035     setPosition(0, DrawParams::TopCenter);
0036     drawFrame(true);
0037 }
0038 
0039 GraphTreeLabel::~GraphTreeLabel()
0040 {
0041 }
0042 
0043 const QString &GraphTreeLabel::nodename() const
0044 {
0045     return m_Nodename;
0046 }
0047 
0048 int GraphTreeLabel::type() const
0049 {
0050     return GRAPHTREE_LABEL;
0051 }
0052 
0053 void GraphTreeLabel::setBgColor(const QColor &c)
0054 {
0055     _backColor = c;
0056 }
0057 
0058 void GraphTreeLabel::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
0059 {
0060     Q_UNUSED(option);
0061     QRect r = rect().toRect();
0062 
0063     RectDrawing d(r);
0064     d.drawBack(p, this);
0065     d.drawField(p, 0, this);
0066     d.drawField(p, 1, this);
0067 }
0068 
0069 void GraphTreeLabel::setSelected(bool s)
0070 {
0071     QGraphicsItem::setSelected(s);
0072     StoredDrawParams::setSelected(s);
0073     update();
0074 }
0075 
0076 const QString &GraphTreeLabel::source() const
0077 {
0078     return m_SourceNode;
0079 }
0080 
0081 void GraphTreeLabel::setSource(const QString &_s)
0082 {
0083     m_SourceNode = _s;
0084 }
0085 
0086 GraphEdge::GraphEdge(QGraphicsItem *c)
0087     : QGraphicsPathItem(c)
0088 {
0089 }
0090 
0091 GraphEdge::~GraphEdge()
0092 {
0093 }
0094 
0095 void GraphEdge::paint(QPainter *p, const QStyleOptionGraphicsItem *options, QWidget *)
0096 {
0097     Q_UNUSED(options);
0098     p->save();
0099     p->setRenderHint(QPainter::Antialiasing);
0100 
0101     QPen pen = QPen(Qt::black);
0102     pen.setWidthF(1.0);
0103     p->setPen(pen);
0104     p->drawPath(path());
0105     p->restore();
0106 }
0107 
0108 const QPolygonF &GraphEdge::controlPoints() const
0109 {
0110     return _points;
0111 }
0112 
0113 void GraphEdge::setControlPoints(const QPolygonF &pa)
0114 {
0115     _points = pa;
0116 
0117     QPainterPath path;
0118     path.moveTo(pa[0]);
0119     for (int i = 1; i < pa.size(); i += 3) {
0120         path.cubicTo(pa[i], pa[(i + 1) % pa.size()], pa[(i + 2) % pa.size()]);
0121     }
0122 
0123     setPath(path);
0124 }
0125 
0126 int GraphEdge::type() const
0127 {
0128     return GRAPHTREE_LINE;
0129 }
0130 
0131 GraphEdgeArrow::GraphEdgeArrow(GraphEdge *_parent, QGraphicsItem *p)
0132     : QGraphicsPolygonItem(p)
0133     , _edge(_parent)
0134 {
0135 }
0136 
0137 void GraphEdgeArrow::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
0138 {
0139     p->save();
0140     p->setRenderHint(QPainter::Antialiasing);
0141     p->setBrush(Qt::black);
0142     p->drawPolygon(polygon(), Qt::OddEvenFill);
0143     p->restore();
0144 }
0145 
0146 int GraphEdgeArrow::type() const
0147 {
0148     return GRAPHTREE_ARROW;
0149 }
0150 
0151 GraphEdge *GraphEdgeArrow::edge()
0152 {
0153     return _edge;
0154 }
0155 
0156 /* taken from KCacheGrind project */
0157 QPixmap *GraphMark::_p = nullptr;
0158 
0159 GraphMark::GraphMark(GraphTreeLabel *n, QGraphicsItem *p)
0160     : QGraphicsRectItem(p)
0161 {
0162     if (!_p) {
0163         int d = 5;
0164         float v1 = 130.0f, v2 = 10.0f, v = v1, f = 1.03f;
0165 
0166         // calculate pix size
0167         QRect r(0, 0, 30, 30);
0168         while (v > v2) {
0169             r.setRect(r.x() - d, r.y() - d, r.width() + 2 * d, r.height() + 2 * d);
0170             v /= f;
0171         }
0172 
0173         _p = new QPixmap(r.size());
0174         _p->fill(Qt::white);
0175         QPainter p(_p);
0176         p.setPen(Qt::NoPen);
0177 
0178         r.translate(-r.x(), -r.y());
0179 
0180         while (v < v1) {
0181             v *= f;
0182             p.setBrush(QColor(qRound(265 - v), qRound(265 - v), qRound(265 - v)));
0183 
0184             p.drawRect(QRect(r.x(), r.y(), r.width(), d));
0185             p.drawRect(QRect(r.x(), r.bottom() - d, r.width(), d));
0186             p.drawRect(QRect(r.x(), r.y() + d, d, r.height() - 2 * d));
0187             p.drawRect(QRect(r.right() - d, r.y() + d, d, r.height() - 2 * d));
0188 
0189             r.setRect(r.x() + d, r.y() + d, r.width() - 2 * d, r.height() - 2 * d);
0190         }
0191     }
0192 
0193     setRect(QRectF(n->rect().center().x() - _p->width() / 2, n->rect().center().y() - _p->height() / 2, _p->width(), _p->height()));
0194 }
0195 
0196 GraphMark::~GraphMark()
0197 {
0198 }
0199 
0200 bool GraphMark::hit(const QPoint &) const
0201 {
0202     return false;
0203 }
0204 
0205 int GraphMark::type() const
0206 {
0207     return GRAPHTREE_MARK;
0208 }
0209 
0210 void GraphMark::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
0211 {
0212     if (option->levelOfDetail < .5) {
0213         QRadialGradient g(rect().center(), rect().width() / 3);
0214         g.setColorAt(0.0, Qt::gray);
0215         g.setColorAt(1.0, Qt::white);
0216 
0217         p->setBrush(QBrush(g));
0218         p->setPen(Qt::NoPen);
0219         p->drawRect(rect());
0220         return;
0221     }
0222 
0223     p->drawPixmap(int(rect().x()), int(rect().y()), *_p);
0224 }