]> git.sesse.net Git - kdenlive/blob - src/onmonitoritems/rotoscoping/splineitem.cpp
rotoscoping: make it easier to select points on the spline
[kdenlive] / src / onmonitoritems / rotoscoping / splineitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 by Till Theato (root@ttill.de)                     *
3  *   This file is part of Kdenlive (www.kdenlive.org).                     *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
18
19 #include "splineitem.h"
20 #include "bpointitem.h"
21 #include "nearestpoint.h"
22 #include "kdenlivesettings.h"
23
24 #include <QGraphicsScene>
25 #include <QCursor>
26 #include <QGraphicsSceneMouseEvent>
27 #include <QGraphicsView>
28
29
30 inline QPointF closestPointInRect(QPointF point, QRectF rect)
31 {
32     QPointF closest;
33     rect = rect.normalized();
34     closest.setX(qBound<qreal>(rect.left(), point.x(), rect.right()));
35     closest.setY(qBound<qreal>(rect.top(), point.y(), rect.bottom()));
36     return closest;
37 }
38
39 void deCasteljau(BPoint *p1, BPoint *p2, BPoint *res, double t)
40 {
41     QPointF ab, bc, cd;
42
43     ab = QLineF(p1->p, p1->h2).pointAt(t);
44     bc = QLineF(p1->h2, p2->h1).pointAt(t);
45     cd = QLineF(p2->h1, p2->p).pointAt(t);
46
47     res->h1 = QLineF(ab, bc).pointAt(t);
48     res->h2 = QLineF(bc, cd).pointAt(t);
49     res->p = QLineF(res->h1, res->h2).pointAt(t);
50
51     p1->h2 = ab;
52     p2->h1 = cd;
53 }
54
55
56 SplineItem::SplineItem(const QList< BPoint >& points, QGraphicsItem* parent, QGraphicsScene *scene) :
57     QGraphicsPathItem(parent, scene),
58     m_closed(false),
59     m_editing(false)
60 {
61     QPen framepen(Qt::SolidLine);
62     framepen.setColor(Qt::yellow);
63     setPen(framepen);
64     setBrush(Qt::NoBrush);
65     setAcceptHoverEvents(true);
66
67     m_view = scene->views()[0];
68
69     setPoints(points);
70 }
71
72 int SplineItem::type() const
73 {
74     return Type;
75 }
76
77 bool SplineItem::editing()
78 {
79     return m_editing;
80 }
81
82 void SplineItem::updateSpline(bool editing)
83 {
84     QPainterPath path(qgraphicsitem_cast<BPointItem *>(childItems().at(0))->getPoint().p);
85
86     BPoint p1, p2;
87     int j;
88     for (int i = 0; i < childItems().count() - !m_closed; ++i) {
89         j = (i + 1) % childItems().count();
90         p1 = qgraphicsitem_cast<BPointItem *>(childItems().at(i))->getPoint();
91         p2 = qgraphicsitem_cast<BPointItem *>(childItems().at(j))->getPoint();
92         path.cubicTo(p1.h2, p2.h1, p2.p);
93     }
94     setPath(path);
95
96     m_editing = editing;
97
98     if (m_closed && (!editing || KdenliveSettings::monitorscene_directupdate()))
99         emit changed(editing);
100 }
101
102 QList <BPoint> SplineItem::getPoints()
103 {
104     QList <BPoint> points;
105     foreach (QGraphicsItem *child, childItems())
106         points << qgraphicsitem_cast<BPointItem *>(child)->getPoint();
107     return points;
108 }
109
110 void SplineItem::setPoints(const QList< BPoint >& points)
111 {
112     if (points.count() < 2) {
113         m_closed = false;
114         grabMouse();
115         return;
116     } else {
117         ungrabMouse();
118         m_closed = true;
119     }
120
121     qDeleteAll(childItems());
122     childItems().clear();
123
124     QPainterPath path(points.at(0).p);
125     int j;
126     for (int i = 0; i < points.count(); ++i) {
127         new BPointItem(points.at(i), this);
128         j = (i + 1) % points.count();
129         path.cubicTo(points.at(i).h2, points.at(j).h1, points.at(j).p);
130     }
131     setPath(path);
132 }
133
134 void SplineItem::removeChild(QGraphicsItem* child)
135 {
136     if (childItems().count() > 2) {
137         scene()->removeItem(child);
138         delete child;
139         updateSpline();
140     }
141 }
142
143 void SplineItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
144 {
145     event->setAccepted(m_closed);
146     QGraphicsItem::mousePressEvent(event);
147
148     if (event->isAccepted())
149         return;
150
151     if (m_closed) {
152         qreal size = 12 / m_view->matrix().m11();
153         QRectF r(event->scenePos() - QPointF(size / 2, size / 2), QSizeF(size, size));
154         if (event->button() == Qt::LeftButton && path().intersects(r) && !path().contains(r)) {
155             double t = 0;
156             BPointItem *i1, *i2;
157             BPoint p, p1, p2;
158             int ix = getClosestPointOnCurve(event->scenePos(), &t);
159             i1 = qgraphicsitem_cast<BPointItem *>(childItems().at(ix));
160             i2 = qgraphicsitem_cast<BPointItem *>(childItems().at((ix + 1) % childItems().count()));
161             p1 = i1->getPoint();
162             p2 = i2->getPoint();
163
164             deCasteljau(&p1, &p2, &p, t);
165
166             i1->setPoint(p1);
167             i2->setPoint(p2);
168
169 #if QT_VERSION >= 0x040600
170             BPointItem *i = new BPointItem(p, this);
171             i->stackBefore(i2);
172 #else
173             QList <BPoint> points;
174             BPointItem *item;
175             while (childItems().count()) {
176                 item = qgraphicsitem_cast<BPointItem *>(childItems().takeFirst());
177                 points.append(item->getPoint());
178                 delete item;
179             }
180             int j = 0;
181             for ( ; j < points.count(); ++j) {
182                 if (j == ix + 1)
183                     new BPointItem(p, this);
184                 new BPointItem(points.at(j), this);
185             }
186             if (j == ix + 1)
187                 new BPointItem(p, this);
188 #endif
189             updateSpline();
190         }
191     } else {
192         if (event->button() == Qt::RightButton) {
193             if (childItems().count() > 1) {
194                 // close the spline
195                 BPointItem *i1 = qgraphicsitem_cast<BPointItem *>(childItems().first());
196                 BPointItem *i2 = qgraphicsitem_cast<BPointItem *>(childItems().last());
197                 BPoint p1 = i1->getPoint();
198                 BPoint p2 = i2->getPoint();
199                 p1.h1 = QLineF(p1.p, p2.p).pointAt(.2);
200                 p2.h2 = QLineF(p1.p, p2.p).pointAt(.8);
201                 i1->setPoint(p1);
202                 i2->setPoint(p2);
203                 m_closed = true;
204                 ungrabMouse();
205                 updateSpline();
206             }
207         } else if (event->modifiers() == Qt::NoModifier) {
208             BPoint p;
209             p.p = p.h1 = p.h2 = event->scenePos();
210             if (childItems().count()) {
211                 BPointItem *i = qgraphicsitem_cast<BPointItem *>(childItems().last());
212                 BPoint prev = i->getPoint();
213                 prev.h2 = QLineF(prev.p, p.p).pointAt(.2);
214                 p.h1 = QLineF(prev.p, p.p).pointAt(.8);
215                 i->setPoint(prev);
216             }
217             new BPointItem(p, this);
218             updateSpline();
219         }
220     }
221 }
222
223 void SplineItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
224 {
225     QGraphicsItem::mouseMoveEvent(event);
226 }
227
228 void SplineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
229 {
230     QGraphicsItem::mouseReleaseEvent(event);
231 }
232
233 void SplineItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
234 {
235     QGraphicsItem::hoverMoveEvent(event);
236
237     qreal size = 12 / m_view->matrix().m11();
238     QRectF r(event->scenePos() - QPointF(size / 2, size / 2), QSizeF(size, size));
239     if (path().intersects(r) && !path().contains(r))
240         setCursor(QCursor(Qt::PointingHandCursor));
241     else
242         unsetCursor();
243 }
244
245 int SplineItem::getClosestPointOnCurve(QPointF point, double *tFinal)
246 {
247     // TODO: proper minDiff
248     qreal diff = 10000, param = 0;
249     BPoint p1, p2;
250     int curveSegment = 0, j;
251     for (int i = 0; i < childItems().count(); ++i) {
252         j = (i + 1) % childItems().count();
253         p1 = qgraphicsitem_cast<BPointItem *>(childItems().at(i))->getPoint();
254         p2 = qgraphicsitem_cast<BPointItem *>(childItems().at(j))->getPoint();
255         QPolygonF bounding = QPolygonF() << p1.p << p1.h2 << p2.h1 << p2.p;
256         QPointF cl = closestPointInRect(point, bounding.boundingRect());
257 #if QT_VERSION >= 0x040600
258         qreal d = (point - cl).manhattanLength();
259 #else
260         qreal d = qAbs((point - cl).x()) + qAbs((point - cl).y());
261 #endif
262
263         if (d > diff)
264             continue;
265
266         Point2 b[4], p;
267         double t;
268
269         b[0].x = p1.p.x();
270         b[0].y = p1.p.y();
271         b[1].x = p1.h2.x();
272         b[1].y = p1.h2.y();
273         b[2].x = p2.h1.x();
274         b[2].y = p2.h1.y();
275         b[3].x = p2.p.x();
276         b[3].y = p2.p.y();
277
278         p.x = point.x();
279         p.y = point.y();
280
281         Point2 n = NearestPointOnCurve(p, b, &t);
282         cl.setX(n.x);
283         cl.setY(n.y);
284
285 #if QT_VERSION >= 0x040600
286         d = (point - cl).manhattanLength();
287 #else
288         d = qAbs((point - cl).x()) + qAbs((point - cl).y());
289 #endif
290         if (d < diff) {
291             diff = d;
292             param = t;
293             curveSegment = i;
294         }
295     }
296
297     *tFinal = param;
298     return curveSegment;
299 }
300
301 #include "splineitem.moc"