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