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