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