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