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