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