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