add more spacing
[personal-kdebase.git] / runtime / phonon / tests / guitest / pathitem.cpp
blob0c262717cb735a7cd8be44b138dab727fd095a91
1 /* This file is part of the KDE project
2 Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
20 #include "pathitem.h"
21 #include <QtGui/QLinearGradient>
22 #include <QtGui/QPainterPath>
23 #include <QtGui/QPen>
25 PathItem::PathItem(WidgetRectItem *start, WidgetRectItem *end, const Path &_path)
26 : m_path(_path), m_startItem(start), m_endItem(end)
28 m_startPos.setX(start->sceneBoundingRect().right());
29 m_startPos.setY(start->sceneBoundingRect().center().y());
30 m_endPos.setX(end->sceneBoundingRect().left());
31 m_endPos.setY(end->sceneBoundingRect().center().y());
32 updatePainterPath();
34 setZValue(-1.0);
35 start->addPath(this);
36 end->addPath(this);
39 void PathItem::endPointMoved(const WidgetRectItem *item)
41 if (item == m_startItem) {
42 m_startPos.setX(item->sceneBoundingRect().right());
43 m_startPos.setY(item->sceneBoundingRect().center().y());
44 } else if (item == m_endItem) {
45 m_endPos.setX(item->sceneBoundingRect().left());
46 m_endPos.setY(item->sceneBoundingRect().center().y());
48 updatePainterPath();
51 void PathItem::updatePainterPath()
53 QPainterPath path;
54 path.moveTo(m_startPos);
55 path.cubicTo(m_startPos + QPointF(150.0, 0.0),
56 m_endPos - QPointF(150.0, 0.0),
57 m_endPos);
58 setPath(path);
60 QLinearGradient gradient(m_startPos, m_endPos);
61 gradient.setColorAt(0, QColor(64, 0, 0, 200));
62 gradient.setColorAt(1, QColor(0, 64, 0, 200));
63 setPen(QPen(gradient, 3.0));
65 updateChildrenPositions();
68 void PathItem::updateChildrenPositions()
70 const qreal divisor = QGraphicsItem::children().count() + 1;
71 int positionOnLine = 0;
72 foreach (QGraphicsItem *item, QGraphicsItem::children()) {
73 item->setPos(QGraphicsPathItem::path().pointAtPercent(++positionOnLine / divisor) - item->boundingRect().center());