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