]> git.sesse.net Git - kdenlive/blob - src/beziercurve/beziersplineeditor.cpp
Bezier Spline: Fix point entries remaining active for a point not existing anymore...
[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
25 BezierSplineEditor::BezierSplineEditor(QWidget* parent) :
26         QWidget(parent),
27         m_mode(ModeNormal),
28         m_zoomLevel(0),
29         m_gridLines(3),
30         m_pixmapCache(NULL),
31         m_pixmapIsDirty(true),
32         m_currentPointIndex(-1)
33 {
34     setMouseTracking(true);
35     setAutoFillBackground(false);
36     setAttribute(Qt::WA_OpaquePaintEvent);
37     setMinimumSize(150, 150);
38     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
39 }
40
41 BezierSplineEditor::~BezierSplineEditor()
42 {
43     if (m_pixmapCache)
44         delete m_pixmapCache;
45 }
46
47 CubicBezierSpline BezierSplineEditor::spline()
48 {
49     return m_spline;
50 }
51
52 void BezierSplineEditor::setSpline(const CubicBezierSpline& spline)
53 {
54     int precision = m_spline.getPrecision();
55     m_spline = spline;
56     m_spline.setPrecision(precision);
57     m_currentPointIndex = -1;
58     m_mode = ModeNormal;
59     emit currentPoint(BPoint());
60     emit modified();
61     update();
62 }
63
64 BPoint BezierSplineEditor::getCurrentPoint()
65 {
66     if (m_currentPointIndex >= 0)
67         return m_spline.points()[m_currentPointIndex];
68     else
69         return BPoint();
70 }
71
72 void BezierSplineEditor::updateCurrentPoint(const BPoint& p)
73 {
74     if (m_currentPointIndex >= 0) {
75         m_spline.setPoint(m_currentPointIndex, p);
76         // during validation the point might have changed
77         emit currentPoint(m_spline.points()[m_currentPointIndex]);
78         emit modified();
79         update();
80     }
81 }
82
83 void BezierSplineEditor::setPixmap(const QPixmap& pixmap)
84 {
85     m_pixmap = pixmap;
86     m_pixmapIsDirty = true;
87     update();
88 }
89
90 void BezierSplineEditor::slotZoomIn()
91 {
92     m_zoomLevel = qMax(m_zoomLevel-1, 0);
93     m_pixmapIsDirty = true;
94     update();
95 }
96
97 void BezierSplineEditor::slotZoomOut()
98 {
99     m_zoomLevel = qMin(m_zoomLevel+1, 3);
100     m_pixmapIsDirty = true;
101     update();
102 }
103
104 int BezierSplineEditor::gridLines()
105 {
106     return m_gridLines;
107 }
108
109 void BezierSplineEditor::setGridLines(int lines)
110 {
111     m_gridLines = qBound(0, lines, 8);
112     update();
113 }
114
115 void BezierSplineEditor::paintEvent(QPaintEvent* event)
116 {
117     Q_UNUSED(event);
118
119     QPainter p(this);
120
121     int wWidth = width() - 1;
122     int wHeight = height() - 1;
123     int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
124     wWidth -= 2 * offset;
125     wHeight -= 2 * offset;
126
127     p.translate(offset, offset);
128
129     /*
130      * Background
131      */
132     p.fillRect(rect().translated(-offset, -offset), palette().background());
133     if (!m_pixmap.isNull()) {
134         if (m_pixmapIsDirty || !m_pixmapCache) {
135             if (m_pixmapCache)
136                 delete m_pixmapCache;
137             m_pixmapCache = new QPixmap(wWidth + 1, wHeight + 1);
138             QPainter cachePainter(m_pixmapCache);
139
140             cachePainter.scale(1.0*(wWidth+1) / m_pixmap.width(), 1.0*(wHeight+1) / m_pixmap.height());
141             cachePainter.drawPixmap(0, 0, m_pixmap);
142             m_pixmapIsDirty = false;
143         }
144         p.drawPixmap(0, 0, *m_pixmapCache);
145     }
146
147
148     p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
149
150     /*
151      * Borders
152      */
153     if (m_zoomLevel != 0) {
154         p.drawRect(0, 0, wWidth, wHeight);
155     }
156
157     /*
158      * Grid
159      */
160     if (m_gridLines != 0) {
161         double stepH = wWidth / (double)(m_gridLines + 1);
162         double stepV = wHeight / (double)(m_gridLines + 1);
163         for (int i = 1; i <= m_gridLines; ++i) {
164             p.drawLine(QLineF(i * stepH, 0, i * stepH, wHeight));
165             p.drawLine(QLineF(0, i * stepV, wWidth, i * stepV));
166         }
167     }
168
169     p.setRenderHint(QPainter::Antialiasing);
170
171     /*
172      * Standard line
173      */
174     p.drawLine(QLineF(0, wHeight, wWidth, 0));
175
176     /*
177      * Spline
178      */
179     double prevY = wHeight - m_spline.value(0.) * wHeight;
180     double prevX = 0.;
181     double curY;
182     double normalizedX = -1;
183     int x;
184     
185     p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
186     for (x = 0 ; x < wWidth ; ++x) {
187         normalizedX = x / (double)wWidth;
188         curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
189
190         /*
191          * Keep in mind that QLineF rounds doubles
192          * to ints mathematically, not just rounds down
193          * like in C
194          */
195         p.drawLine(QLineF(prevX, prevY, x, curY));
196         prevX = x;
197         prevY = curY;
198     }
199     p.drawLine(QLineF(prevX, prevY ,
200                       x, wHeight - m_spline.value(1.0, true) * wHeight));
201
202     /*
203      * Points + Handles
204      */
205     int max = m_spline.points().count() - 1;
206     p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
207     BPoint point;
208     QPolygon handle(4);
209     handle.setPoints(4,
210                      1,  -2,
211                      4,  1,
212                      1,  4,
213                      -2, 1);
214 #if QT_VERSION < 0x040600
215     QPolygon tmp;
216 #endif
217     for (int i = 0; i <= max; ++i) {
218         point = m_spline.points().at(i);
219         if (i == m_currentPointIndex) {
220             // selected point: fill p and handles
221             p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
222             // connect p and handles with lines
223             if (i != 0)
224                 p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight));
225             if (i != max)
226                 p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
227         }
228
229         p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
230                              wHeight - 3 - point.p.y() * wHeight, 6, 6));
231         if (i != 0) {
232 #if QT_VERSION >= 0x040600
233             p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight));
234 #else
235             tmp = handle;
236             tmp.translate(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight);
237             p.drawConvexPolygon(tmp);
238 #endif
239         }
240         if (i != max) {
241 #if QT_VERSION >= 0x040600
242             p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
243 #else
244             tmp = handle;
245             tmp.translate(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight);
246             p.drawConvexPolygon(tmp);
247 #endif
248         }
249
250         if ( i == m_currentPointIndex)
251             p.setBrush(QBrush(Qt::NoBrush));
252     }
253 }
254
255 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
256 {
257     m_spline.setPrecision(width() > height() ? width() : height());
258     m_pixmapIsDirty = true;
259     QWidget::resizeEvent(event);
260 }
261
262 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
263 {
264     int wWidth = width() - 1;
265     int wHeight = height() - 1;
266     int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
267     wWidth -= 2 * offset;
268     wHeight -= 2 * offset;
269
270     double x = (event->pos().x() - offset) / (double)(wWidth);
271     double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
272
273     point_types selectedPoint;
274     int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
275
276     if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
277         m_spline.removePoint(closestPointIndex);
278         setCursor(Qt::ArrowCursor);
279         m_mode = ModeNormal;
280         if (closestPointIndex < m_currentPointIndex)
281             --m_currentPointIndex;
282         update();
283         if (m_currentPointIndex >= 0)
284             emit currentPoint(m_spline.points()[m_currentPointIndex]);
285         else
286             emit currentPoint(BPoint());
287         emit modified();
288         return;
289     } else if (event->button() != Qt::LeftButton) {
290         return;
291     }
292
293     if (closestPointIndex < 0) {
294         m_currentPointIndex = m_spline.addPoint(BPoint(QPointF(x-0.05, y-0.05),
295                                                        QPointF(x, y),
296                                                        QPointF(x+0.05, y+0.05)));
297         m_currentPointType = PTypeP;
298     } else {
299         m_currentPointIndex = closestPointIndex;
300         m_currentPointType = selectedPoint;
301     }
302
303     BPoint point = m_spline.points()[m_currentPointIndex];
304
305     m_grabPOriginal = point;
306     if (m_currentPointIndex > 0)
307         m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1];
308     if (m_currentPointIndex < m_spline.points().count() - 1)
309         m_grabPNext = m_spline.points()[m_currentPointIndex + 1];
310     m_grabOffsetX = point[(int)m_currentPointType].x() - x;
311     m_grabOffsetY = point[(int)m_currentPointType].y() - y;
312
313     point[(int)m_currentPointType] = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
314
315     m_spline.setPoint(m_currentPointIndex, point);
316
317     m_mode = ModeDrag;
318
319     emit currentPoint(point);
320     update();
321 }
322
323 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
324 {
325     if (event->button() != Qt::LeftButton)
326         return;
327
328     setCursor(Qt::ArrowCursor);
329     m_mode = ModeNormal;
330
331     emit modified();
332 }
333
334 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
335 {
336     int wWidth = width() - 1;
337     int wHeight = height() - 1;
338     int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
339     wWidth -= 2 * offset;
340     wHeight -= 2 * offset;
341
342     double x = (event->pos().x() - offset) / (double)(wWidth);
343     double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
344     
345     if (m_mode == ModeNormal) {
346         // If no point is selected set the the cursor shape if on top
347         point_types type;
348         int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
349
350         if (nearestPointIndex < 0)
351             setCursor(Qt::ArrowCursor);
352         else
353             setCursor(Qt::CrossCursor);
354     } else {
355         // Else, drag the selected point
356         setCursor(Qt::CrossCursor);
357
358         x += m_grabOffsetX;
359         y += m_grabOffsetY;
360
361         double leftX = 0.;
362         double rightX = 1.;
363         BPoint point = m_spline.points()[m_currentPointIndex];
364         switch (m_currentPointType) {
365         case PTypeH1:
366             rightX = point.p.x();
367             if (m_currentPointIndex == 0)
368                 leftX = -4;
369             else
370                 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
371
372             x = qBound(leftX, x, rightX);
373             point.setH1(QPointF(x, y));
374             break;
375
376         case PTypeP:
377             if (m_currentPointIndex == 0)
378                 rightX = 0.0;
379             else if (m_currentPointIndex == m_spline.points().count() - 1)
380                 leftX = 1.0;
381
382             x = qBound(leftX, x, rightX);
383             y = qBound(0., y, 1.);
384
385             // handles might have changed because we neared another point
386             // try to restore
387             point.h1 = m_grabPOriginal.h1;
388             point.h2 = m_grabPOriginal.h2;
389             // and move by same offset
390             // (using update handle in point.setP won't work because the offset between new and old point is very small)
391             point.h1 += QPointF(x, y) - m_grabPOriginal.p;
392             point.h2 += QPointF(x, y) - m_grabPOriginal.p;
393
394             point.setP(QPointF(x, y), false);
395             break;
396
397         case PTypeH2:
398             leftX = point.p.x();
399             if (m_currentPointIndex == m_spline.points().count() - 1)
400                 rightX = 5;
401             else
402                 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
403
404             x = qBound(leftX, x, rightX);
405             point.setH2(QPointF(x, y));
406         };
407
408         int index = m_currentPointIndex;
409         m_currentPointIndex = m_spline.setPoint(m_currentPointIndex, point);
410
411         if (m_currentPointType == PTypeP) {
412             // we might have changed the handles of other points
413             // try to restore
414             if (index == m_currentPointIndex) {
415                 if (m_currentPointIndex > 0)
416                     m_spline.setPoint(m_currentPointIndex - 1, m_grabPPrevious);
417                 if (m_currentPointIndex < m_spline.points().count() -1)
418                     m_spline.setPoint(m_currentPointIndex + 1, m_grabPNext);
419             } else {
420                 if (m_currentPointIndex < index) {
421                     m_spline.setPoint(index, m_grabPPrevious);
422                     m_grabPNext = m_grabPPrevious;
423                     if (m_currentPointIndex > 0)
424                         m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1];
425                 } else {
426                     m_spline.setPoint(index, m_grabPNext);
427                     m_grabPPrevious = m_grabPNext;
428                     if (m_currentPointIndex < m_spline.points().count() - 1)
429                         m_grabPNext = m_spline.points()[m_currentPointIndex + 1];
430                 }
431             }
432         }
433
434         emit currentPoint(point);
435         update();
436     }
437 }
438
439 void BezierSplineEditor::leaveEvent(QEvent* event)
440 {
441     QWidget::leaveEvent(event);
442 }
443
444 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
445 {
446     double nearestDistanceSquared = 1000;
447     point_types selectedPoint;
448     int nearestIndex = -1;
449     int i = 0;
450
451     double distanceSquared;
452     // find out distance using the Pythagorean theorem
453     foreach(const BPoint & point, m_spline.points()) {
454         for (int j = 0; j < 3; ++j) {
455             distanceSquared = pow(point[j].x() - p.x(), 2) + pow(point[j].y() - p.y(), 2);
456             if (distanceSquared < nearestDistanceSquared) {
457                 nearestIndex = i;
458                 nearestDistanceSquared = distanceSquared;
459                 selectedPoint = (point_types)j;
460             }
461         }
462         ++i;
463     }
464
465     if (nearestIndex >= 0) {
466         BPoint point = m_spline.points()[nearestIndex];
467         if (qAbs(p.x() - point[(int)selectedPoint].x()) * wWidth < 5 && qAbs(p.y() - point[(int)selectedPoint].y()) * wHeight < 5) {
468             *sel = selectedPoint;
469             return nearestIndex;
470         }
471     }
472
473     return -1;
474 }
475
476 #include "beziersplineeditor.moc"