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