]> git.sesse.net Git - kdenlive/blob - src/beziercurve/beziersplineeditor.cpp
Bézier spline: Connect Handle1 + Point and Handle2+Point of the selected point with...
[kdenlive] / src / beziercurve / beziersplineeditor.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 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 "beziersplineeditor.h"
20
21 #include <QPainter>
22 #include <QMouseEvent>
23
24 #include <KDebug>
25
26 BezierSplineEditor::BezierSplineEditor(QWidget* parent) :
27         QWidget(parent),
28         m_mode(ModeNormal),
29         m_currentPointIndex(-1)
30 {
31     setMouseTracking(true);
32     setAutoFillBackground(false);
33     setAttribute(Qt::WA_OpaquePaintEvent);
34     setMinimumSize(150, 150);
35     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
36 }
37
38 CubicBezierSpline BezierSplineEditor::spline()
39 {
40     return m_spline;
41 }
42
43 void BezierSplineEditor::setSpline(const CubicBezierSpline& spline)
44 {
45     // TODO: cleanup
46     m_spline.fromString(spline.toString());
47 }
48
49 BPoint BezierSplineEditor::getCurrentPoint()
50 {
51     if (m_currentPointIndex >= 0)
52         return m_spline.points()[m_currentPointIndex];
53     else
54         return BPoint();
55 }
56
57 void BezierSplineEditor::updateCurrentPoint(const BPoint& p)
58 {
59     if (m_currentPointIndex >= 0) {
60         m_spline.setPoint(m_currentPointIndex, p);
61         // during validation the point might have changed
62         emit currentPoint(m_spline.points()[m_currentPointIndex]);
63         update();
64     }
65 }
66
67 void BezierSplineEditor::paintEvent(QPaintEvent* event)
68 {
69     Q_UNUSED(event);
70
71     QPainter p(this);
72
73     p.fillRect(rect(), palette().background());
74
75     int    wWidth = width() - 1;
76     int    wHeight = height() - 1;
77
78     /*
79      * Standard line
80      */
81     /*p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
82     p.drawLine(QLineF(0, wHeight, wWidth, 0));*/
83
84     /*
85      * Spline
86      */
87     double prevY = wHeight - m_spline.value(0.) * wHeight;
88     double prevX = 0.;
89     double curY;
90     double normalizedX = -1;
91     int x;
92     
93     p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
94     for (x = 0 ; x < wWidth ; ++x) {
95         normalizedX = x / (double)wWidth;
96         curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
97         
98         /*
99          * Keep in mind that QLineF rounds doubles
100          * to ints mathematically, not just rounds down
101          * like in C
102          */
103         p.drawLine(QLineF(prevX, prevY,
104                           x, curY));
105         prevX = x;
106         prevY = curY;
107     }
108     p.drawLine(QLineF(prevX, prevY ,
109                       x, wHeight - m_spline.value(1.0, true) * wHeight));
110
111     /*
112      * Points + Handles
113      */
114     p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
115     BPoint point;
116     QPolygon handle(4);
117     handle.setPoints(4,
118                      1,  -2,
119                      4,  1,
120                      1,  4,
121                      -2, 1);
122     for (int i = 0; i < m_spline.points().count(); ++i) {
123         point = m_spline.points().at(i);
124         if (i == m_currentPointIndex) {
125             p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
126             p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight));
127             p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
128         }
129
130         p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
131                      wHeight - 3 - point.p.y() * wHeight, 6, 6));
132         p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight));
133         p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
134
135         if ( i == m_currentPointIndex)
136             p.setBrush(QBrush(Qt::NoBrush));
137     }
138 }
139
140 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
141 {
142     m_spline.setPrecision(width());
143     QWidget::resizeEvent(event);
144 }
145
146 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
147 {
148     double x = event->pos().x() / (double)(width() - 1);
149     double y = 1.0 - event->pos().y() / (double)(height() - 1);
150
151     point_types selectedPoint;
152     int closestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &selectedPoint);
153
154     if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
155         m_spline.removePoint(closestPointIndex);
156         setCursor(Qt::ArrowCursor);
157         m_mode = ModeNormal;
158         if (closestPointIndex < m_currentPointIndex)
159             --m_currentPointIndex;
160         update();
161         if (m_currentPointIndex >= 0)
162             emit currentPoint(m_spline.points()[m_currentPointIndex]);
163         else
164             emit currentPoint(BPoint());
165         emit modified();
166         return;
167     } else if (event->button() != Qt::LeftButton) {
168         return;
169     }
170
171     if (closestPointIndex < 0) {
172         BPoint po;
173         po.p = QPointF(x, y);
174         po.h1 = QPointF(x-0.05, y-0.05);
175         po.h2 = QPointF(x+0.05, y+0.05);
176         m_currentPointIndex = m_spline.addPoint(po);
177         m_currentPointType = PTypeP;
178         /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
179     } else {
180         m_currentPointIndex = closestPointIndex;
181         m_currentPointType = selectedPoint;
182     }
183
184     BPoint point = m_spline.points()[m_currentPointIndex];
185     QPointF p;
186     switch (m_currentPointType) {
187     case PTypeH1:
188         p = point.h1;
189         break;
190     case PTypeP:
191         p = point.p;
192         break;
193     case PTypeH2:
194         p = point.h2;
195     }
196
197     m_grabOriginalX = p.x();
198     m_grabOriginalY = p.y();
199     m_grabOffsetX = p.x() - x;
200     m_grabOffsetY = p.y() - y;
201
202     switch (m_currentPointType) {
203         case PTypeH1:
204             point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
205             break;
206         case PTypeP:
207             point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
208             break;
209         case PTypeH2:
210             point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
211     }
212     m_spline.setPoint(m_currentPointIndex, point);
213
214     //d->m_draggedAwayPointIndex = -1;
215
216     m_mode = ModeDrag;
217
218     emit currentPoint(point);
219     update();
220 }
221
222 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
223 {
224     if (event->button() != Qt::LeftButton)
225         return;
226
227     setCursor(Qt::ArrowCursor);
228     m_mode = ModeNormal;
229
230     emit modified();
231 }
232
233 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
234 {
235     double x = event->pos().x() / (double)(width() - 1);
236     double y = 1.0 - event->pos().y() / (double)(height() - 1);
237     
238     if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
239         point_types type;
240         int nearestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &type);
241         
242         if (nearestPointIndex < 0)
243             setCursor(Qt::ArrowCursor);
244         else
245             setCursor(Qt::CrossCursor);
246     } else { // Else, drag the selected point
247         /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
248         event->pos().x() < -MOUSE_AWAY_THRES;
249         bool crossedVert =  event->pos().y() - height() > MOUSE_AWAY_THRES ||
250         event->pos().y() < -MOUSE_AWAY_THRES;
251         
252         bool removePoint = (crossedHoriz || crossedVert);
253         
254         if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
255             // point is no longer dragged away so reinsert it
256             QPointF newPoint(d->m_draggedAwayPoint);
257             d->m_grab_point_index = d->m_curve.addPoint(newPoint);
258             d->m_draggedAwayPointIndex = -1;
259         }
260         
261         if (removePoint &&
262             (d->m_draggedAwayPointIndex >= 0))
263             return;
264         */
265         
266         setCursor(Qt::CrossCursor);
267         
268         x += m_grabOffsetX;
269         y += m_grabOffsetY;
270         
271         double leftX, rightX;
272         BPoint point = m_spline.points()[m_currentPointIndex];
273         switch (m_currentPointType) {
274         case PTypeH1:
275             rightX = point.p.x();
276             if (m_currentPointIndex == 0)
277                 leftX = -1000;
278             else
279                 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
280             x = qBound(leftX, x, rightX);
281             point.h1 = QPointF(x, y);
282             break;
283         case PTypeP:
284             if (m_currentPointIndex == 0) {
285                 leftX = 0.0;
286                 rightX = 0.0;
287                 /*if (d->m_curve.points().count() > 1)
288                  *           rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
289                  *       else
290                  *           rightX = 1.0;*/
291             } else if (m_currentPointIndex == m_spline.points().count() - 1) {
292                 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
293                 rightX = 1.0;
294             } else {
295                 //// the 1E-4 addition so we can grab the dot later.
296                 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
297                 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
298             }
299             x = qBound(leftX, x, rightX);
300             y = qBound(0., y, 1.);
301
302             // move handles by same offset
303             point.h1 += QPointF(x, y) - point.p;
304             point.h2 += QPointF(x, y) - point.p;
305
306             point.p = QPointF(x, y);
307             break;
308         case PTypeH2:
309             leftX = point.p.x();
310             if (m_currentPointIndex == m_spline.points().count() - 1)
311                 rightX = 1001;
312             else
313                 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
314             x = qBound(leftX, x, rightX);
315             point.h2 = QPointF(x, y);
316         };
317
318         m_spline.setPoint(m_currentPointIndex, point);
319         
320         /*if (removePoint && d->m_curve.points().count() > 2) {
321             d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
322             d->m_draggedAwayPointIndex = d->m_grab_point_index;
323             d->m_curve.removePoint(d->m_grab_point_index);
324             d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
325         }
326         
327         d->setCurveModified();*/
328         emit currentPoint(point);
329         update();
330     }
331 }
332
333 void BezierSplineEditor::leaveEvent(QEvent* event)
334 {
335     QWidget::leaveEvent(event);
336 }
337
338 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
339 {
340     double nearestDistanceSquared = 1000;
341     point_types selectedPoint;
342     int nearestIndex = -1;
343     int i = 0;
344
345     double distanceSquared;
346     foreach(const BPoint & point, m_spline.points()) {
347         distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
348         if (distanceSquared < nearestDistanceSquared) {
349             nearestIndex = i;
350             nearestDistanceSquared = distanceSquared;
351             selectedPoint = PTypeH1;
352         }
353         distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
354         if (distanceSquared < nearestDistanceSquared) {
355             nearestIndex = i;
356             nearestDistanceSquared = distanceSquared;
357             selectedPoint = PTypeP;
358         }
359         distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
360         if (distanceSquared < nearestDistanceSquared) {
361             nearestIndex = i;
362             nearestDistanceSquared = distanceSquared;
363             selectedPoint = PTypeH2;
364         }
365         ++i;
366     }
367
368     if (nearestIndex >= 0) {
369         BPoint point = m_spline.points()[nearestIndex];
370         QPointF p2;
371         switch (selectedPoint) {
372         case PTypeH1:
373             p2 = point.h1;
374             break;
375         case PTypeP:
376             p2 = point.p;
377             break;
378         case PTypeH2:
379             p2 = point.h2;
380         }
381         if (qAbs(p.x() - p2.x()) * (wWidth - 1) < 5 && qAbs(p.y() - p2.y()) * (wHeight - 1) < 5) {
382             *sel = selectedPoint;
383             return nearestIndex;
384         }
385     }
386
387     return -1;
388 }
389
390 #include "beziersplineeditor.moc"