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