1 /***************************************************************************
2 * Copyright (C) 2011 by Till Theato (root@ttill.de) *
3 * This file is part of Kdenlive (www.kdenlive.org). *
5 * Kdenlive is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
10 * Kdenlive 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 *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with Kdenlive. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
19 #include "splineitem.h"
20 #include "bpointitem.h"
21 #include "nearestpoint.h"
23 #include <QGraphicsScene>
25 #include <QGraphicsSceneMouseEvent>
28 inline QPointF closestPointInRect(QPointF point, QRectF rect)
31 rect = rect.normalized();
32 closest.setX(qBound<qreal>(rect.left(), point.x(), rect.right()));
33 closest.setY(qBound<qreal>(rect.top(), point.y(), rect.bottom()));
37 void deCasteljau(BPoint *p1, BPoint *p2, BPoint *res, double t)
41 ab = QLineF(p1->p, p1->h2).pointAt(t);
42 bc = QLineF(p1->h2, p2->h1).pointAt(t);
43 cd = QLineF(p2->h1, p2->p).pointAt(t);
45 res->h1 = QLineF(ab, bc).pointAt(t);
46 res->h2 = QLineF(bc, cd).pointAt(t);
47 res->p = QLineF(res->h1, res->h2).pointAt(t);
54 SplineItem::SplineItem(const QList< BPoint >& points, QGraphicsItem* parent, QGraphicsScene *scene) :
55 QGraphicsPathItem(parent, scene)
57 QPen framepen(Qt::SolidLine);
58 framepen.setColor(Qt::yellow);
60 setBrush(Qt::NoBrush);
61 setAcceptHoverEvents(true);
66 QPainterPath path(points.at(0).p);
68 for (int i = 0; i < points.count(); ++i) {
69 new BPointItem(points.at(i), this);
70 j = (i + 1) % points.count();
71 path.cubicTo(points.at(i).h2, points.at(j).h1, points.at(j).p);
76 int SplineItem::type() const
81 void SplineItem::updateSpline()
83 QPainterPath path(qgraphicsitem_cast<BPointItem *>(childItems().at(0))->getPoint().p);
87 for (int i = 0; i < childItems().count(); ++i) {
88 j = (i + 1) % childItems().count();
89 p1 = qgraphicsitem_cast<BPointItem *>(childItems().at(i))->getPoint();
90 p2 = qgraphicsitem_cast<BPointItem *>(childItems().at(j))->getPoint();
91 path.cubicTo(p1.h2, p2.h1, p2.p);
98 QList <BPoint> SplineItem::getPoints()
100 QList <BPoint> points;
101 foreach (QGraphicsItem *child, childItems())
102 points << qgraphicsitem_cast<BPointItem *>(child)->getPoint();
106 void SplineItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
108 QGraphicsItem::mousePressEvent(event);
110 if (!event->isAccepted()) {
111 QRectF r(event->scenePos() - QPointF(6, 6), QSizeF(12, 12));
112 if (path().intersects(r) && !path().contains(r)) {
114 BPointItem *i, *i1, *i2;
116 int ix = getClosestPointOnCurve(event->scenePos(), &t);
117 i1 = qgraphicsitem_cast<BPointItem *>(childItems().at(ix));
118 i2 = qgraphicsitem_cast<BPointItem *>(childItems().at((ix + 1) % childItems().count()));
122 deCasteljau(&p1, &p2, &p, t);
127 i = new BPointItem(p, this);
128 // TODO: make it work with Qt < 4.6
129 #if QT_VERSION >= 0x040600
136 void SplineItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
138 QGraphicsItem::mouseMoveEvent(event);
141 void SplineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
143 QGraphicsItem::mouseReleaseEvent(event);
146 void SplineItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
148 QGraphicsItem::hoverMoveEvent(event);
150 QRectF r(event->scenePos() - QPointF(6, 6), QSizeF(12, 12));
151 if (path().intersects(r) && !path().contains(r))
152 setCursor(QCursor(Qt::PointingHandCursor));
157 int SplineItem::getClosestPointOnCurve(QPointF point, double *tFinal)
159 // TODO: proper minDiff
160 qreal diff = 10000, param = 0;
162 int curveSegment = 0, j;
163 for (int i = 0; i < childItems().count(); ++i) {
164 j = (i + 1) % childItems().count();
165 p1 = qgraphicsitem_cast<BPointItem *>(childItems().at(i))->getPoint();
166 p2 = qgraphicsitem_cast<BPointItem *>(childItems().at(j))->getPoint();
167 QPolygonF bounding = QPolygonF() << p1.p << p1.h2 << p2.h1 << p2.p;
168 QPointF cl = closestPointInRect(point, bounding.boundingRect());
169 qreal d = (point - cl).manhattanLength();
189 Point2 n = NearestPointOnCurve(p, b, &t);
193 d = (point - cl).manhattanLength();
205 #include "splineitem.moc"