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