]> git.sesse.net Git - kdenlive/blob - src/beziercurve/beziersplineeditor.cpp
a5a5484c18d5695b583701d8a55ec5337fc61483
[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         /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
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_grabOriginalX = p.x();
293     m_grabOriginalY = p.y();
294     m_grabOffsetX = p.x() - x;
295     m_grabOffsetY = p.y() - y;
296
297     switch (m_currentPointType) {
298         case PTypeH1:
299             point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
300             break;
301         case PTypeP:
302             point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
303             break;
304         case PTypeH2:
305             point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
306     }
307     m_spline.setPoint(m_currentPointIndex, point);
308
309     //d->m_draggedAwayPointIndex = -1;
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) { // If no point is selected set the the cursor shape if on top
340         point_types type;
341         int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
342         
343         if (nearestPointIndex < 0)
344             setCursor(Qt::ArrowCursor);
345         else
346             setCursor(Qt::CrossCursor);
347     } else { // Else, drag the selected point
348         /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
349         event->pos().x() < -MOUSE_AWAY_THRES;
350         bool crossedVert =  event->pos().y() - height() > MOUSE_AWAY_THRES ||
351         event->pos().y() < -MOUSE_AWAY_THRES;
352         
353         bool removePoint = (crossedHoriz || crossedVert);
354         
355         if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
356             // point is no longer dragged away so reinsert it
357             QPointF newPoint(d->m_draggedAwayPoint);
358             d->m_grab_point_index = d->m_curve.addPoint(newPoint);
359             d->m_draggedAwayPointIndex = -1;
360         }
361         
362         if (removePoint &&
363             (d->m_draggedAwayPointIndex >= 0))
364             return;
365         */
366         
367         setCursor(Qt::CrossCursor);
368         
369         x += m_grabOffsetX;
370         y += m_grabOffsetY;
371         
372         double leftX, rightX;
373         BPoint point = m_spline.points()[m_currentPointIndex];
374         switch (m_currentPointType) {
375         case PTypeH1:
376             rightX = point.p.x();
377             if (m_currentPointIndex == 0)
378                 leftX = -1000;
379             else
380                 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
381             x = qBound(leftX, x, rightX);
382             point.h1 = QPointF(x, y);
383             break;
384         case PTypeP:
385             if (m_currentPointIndex == 0) {
386                 leftX = 0.0;
387                 rightX = 0.0;
388                 /*if (d->m_curve.points().count() > 1)
389                  *           rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
390                  *       else
391                  *           rightX = 1.0;*/
392             } else if (m_currentPointIndex == m_spline.points().count() - 1) {
393                 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
394                 rightX = 1.0;
395             } else {
396                 //// the 1E-4 addition so we can grab the dot later.
397                 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
398                 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
399             }
400             x = qBound(leftX, x, rightX);
401             y = qBound(0., y, 1.);
402
403             // move handles by same offset
404             point.h1 += QPointF(x, y) - point.p;
405             point.h2 += QPointF(x, y) - point.p;
406
407             point.p = QPointF(x, y);
408             break;
409         case PTypeH2:
410             leftX = point.p.x();
411             if (m_currentPointIndex == m_spline.points().count() - 1)
412                 rightX = 1001;
413             else
414                 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
415             x = qBound(leftX, x, rightX);
416             point.h2 = QPointF(x, y);
417         };
418
419         m_spline.setPoint(m_currentPointIndex, point);
420         
421         /*if (removePoint && d->m_curve.points().count() > 2) {
422             d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
423             d->m_draggedAwayPointIndex = d->m_grab_point_index;
424             d->m_curve.removePoint(d->m_grab_point_index);
425             d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
426         }
427         
428         d->setCurveModified();*/
429         emit currentPoint(point);
430         update();
431     }
432 }
433
434 void BezierSplineEditor::leaveEvent(QEvent* event)
435 {
436     QWidget::leaveEvent(event);
437 }
438
439 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
440 {
441     double nearestDistanceSquared = 1000;
442     point_types selectedPoint;
443     int nearestIndex = -1;
444     int i = 0;
445
446     double distanceSquared;
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"