]> git.sesse.net Git - kdenlive/blob - src/beziercurve/beziersplineeditor.cpp
Bezier Spline Widget: Make it possible to zoom out (because handles can have values...
[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 #include <KDebug>
25
26 BezierSplineEditor::BezierSplineEditor(QWidget* parent) :
27         QWidget(parent),
28         m_zoomLevel(0),
29         m_mode(ModeNormal),
30         m_currentPointIndex(-1)
31 {
32     setMouseTracking(true);
33     setAutoFillBackground(false);
34     setAttribute(Qt::WA_OpaquePaintEvent);
35     setMinimumSize(150, 150);
36     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
37 }
38
39 CubicBezierSpline BezierSplineEditor::spline()
40 {
41     return m_spline;
42 }
43
44 void BezierSplineEditor::setSpline(const CubicBezierSpline& spline)
45 {
46     // TODO: cleanup
47     m_spline.fromString(spline.toString());
48 }
49
50 BPoint BezierSplineEditor::getCurrentPoint()
51 {
52     if (m_currentPointIndex >= 0)
53         return m_spline.points()[m_currentPointIndex];
54     else
55         return BPoint();
56 }
57
58 void BezierSplineEditor::updateCurrentPoint(const BPoint& p)
59 {
60     if (m_currentPointIndex >= 0) {
61         m_spline.setPoint(m_currentPointIndex, p);
62         // during validation the point might have changed
63         emit currentPoint(m_spline.points()[m_currentPointIndex]);
64         update();
65     }
66 }
67
68 void BezierSplineEditor::slotZoomIn()
69 {
70     m_zoomLevel = qMax(m_zoomLevel-1, 0);
71     update();
72 }
73
74 void BezierSplineEditor::slotZoomOut()
75 {
76     m_zoomLevel = qMin(m_zoomLevel+1, 3);
77     update();
78 }
79
80 void BezierSplineEditor::paintEvent(QPaintEvent* event)
81 {
82     Q_UNUSED(event);
83
84     QPainter p(this);
85
86     p.fillRect(rect(), palette().background());
87
88     int wWidth = width() - 1;
89     int wHeight = height() - 1;
90     int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
91     wWidth -= 2 * offset;
92     wHeight -= 2 * offset;
93
94     /*
95      * Standard line
96      */
97     /*p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
98     p.drawLine(QLineF(0, wHeight, wWidth, 0));*/
99
100     /*
101      * Spline
102      */
103     double prevY = wHeight - m_spline.value(0.) * wHeight;
104     double prevX = 0.;
105     double curY;
106     double normalizedX = -1;
107     int x;
108     
109     p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
110     for (x = 0 ; x < wWidth ; ++x) {
111         normalizedX = x / (double)wWidth;
112         curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
113
114         /*
115          * Keep in mind that QLineF rounds doubles
116          * to ints mathematically, not just rounds down
117          * like in C
118          */
119         p.drawLine(QLineF(prevX, prevY,
120                           x, curY).translated(offset, offset));
121         prevX = x;
122         prevY = curY;
123     }
124     p.drawLine(QLineF(prevX, prevY ,
125                       x, wHeight - m_spline.value(1.0, true) * wHeight).translated(offset, offset));
126
127     /*
128      * Points + Handles
129      */
130     p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
131     BPoint point;
132     QPolygon handle(4);
133     handle.setPoints(4,
134                      1,  -2,
135                      4,  1,
136                      1,  4,
137                      -2, 1);
138     for (int i = 0; i < m_spline.points().count(); ++i) {
139         point = m_spline.points().at(i);
140         if (i == m_currentPointIndex) {
141             p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
142             p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight).translated(offset, offset));
143             p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
144         }
145
146         p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
147                              wHeight - 3 - point.p.y() * wHeight, 6, 6).translated(offset, offset));
148         p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight).translated(offset, offset));
149         p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
150
151         if ( i == m_currentPointIndex)
152             p.setBrush(QBrush(Qt::NoBrush));
153     }
154 }
155
156 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
157 {
158     m_spline.setPrecision(width());
159     QWidget::resizeEvent(event);
160 }
161
162 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
163 {
164     int wWidth = width() - 1;
165     int wHeight = height() - 1;
166     int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
167     wWidth -= 2 * offset;
168     wHeight -= 2 * offset;
169
170     double x = (event->pos().x() - offset) / (double)(wWidth);
171     double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
172
173     point_types selectedPoint;
174     int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
175
176     if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
177         m_spline.removePoint(closestPointIndex);
178         setCursor(Qt::ArrowCursor);
179         m_mode = ModeNormal;
180         if (closestPointIndex < m_currentPointIndex)
181             --m_currentPointIndex;
182         update();
183         if (m_currentPointIndex >= 0)
184             emit currentPoint(m_spline.points()[m_currentPointIndex]);
185         else
186             emit currentPoint(BPoint());
187         emit modified();
188         return;
189     } else if (event->button() != Qt::LeftButton) {
190         return;
191     }
192
193     if (closestPointIndex < 0) {
194         BPoint po;
195         po.p = QPointF(x, y);
196         po.h1 = QPointF(x-0.05, y-0.05);
197         po.h2 = QPointF(x+0.05, y+0.05);
198         m_currentPointIndex = m_spline.addPoint(po);
199         m_currentPointType = PTypeP;
200         /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
201     } else {
202         m_currentPointIndex = closestPointIndex;
203         m_currentPointType = selectedPoint;
204     }
205
206     BPoint point = m_spline.points()[m_currentPointIndex];
207     QPointF p;
208     switch (m_currentPointType) {
209     case PTypeH1:
210         p = point.h1;
211         break;
212     case PTypeP:
213         p = point.p;
214         break;
215     case PTypeH2:
216         p = point.h2;
217     }
218
219     m_grabOriginalX = p.x();
220     m_grabOriginalY = p.y();
221     m_grabOffsetX = p.x() - x;
222     m_grabOffsetY = p.y() - y;
223
224     switch (m_currentPointType) {
225         case PTypeH1:
226             point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
227             break;
228         case PTypeP:
229             point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
230             break;
231         case PTypeH2:
232             point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
233     }
234     m_spline.setPoint(m_currentPointIndex, point);
235
236     //d->m_draggedAwayPointIndex = -1;
237
238     m_mode = ModeDrag;
239
240     emit currentPoint(point);
241     update();
242 }
243
244 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
245 {
246     if (event->button() != Qt::LeftButton)
247         return;
248
249     setCursor(Qt::ArrowCursor);
250     m_mode = ModeNormal;
251
252     emit modified();
253 }
254
255 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
256 {
257     int wWidth = width() - 1;
258     int wHeight = height() - 1;
259     int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
260     wWidth -= 2 * offset;
261     wHeight -= 2 * offset;
262
263     double x = (event->pos().x() - offset) / (double)(wWidth);
264     double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
265     
266     if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
267         point_types type;
268         int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
269         
270         if (nearestPointIndex < 0)
271             setCursor(Qt::ArrowCursor);
272         else
273             setCursor(Qt::CrossCursor);
274     } else { // Else, drag the selected point
275         /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
276         event->pos().x() < -MOUSE_AWAY_THRES;
277         bool crossedVert =  event->pos().y() - height() > MOUSE_AWAY_THRES ||
278         event->pos().y() < -MOUSE_AWAY_THRES;
279         
280         bool removePoint = (crossedHoriz || crossedVert);
281         
282         if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
283             // point is no longer dragged away so reinsert it
284             QPointF newPoint(d->m_draggedAwayPoint);
285             d->m_grab_point_index = d->m_curve.addPoint(newPoint);
286             d->m_draggedAwayPointIndex = -1;
287         }
288         
289         if (removePoint &&
290             (d->m_draggedAwayPointIndex >= 0))
291             return;
292         */
293         
294         setCursor(Qt::CrossCursor);
295         
296         x += m_grabOffsetX;
297         y += m_grabOffsetY;
298         
299         double leftX, rightX;
300         BPoint point = m_spline.points()[m_currentPointIndex];
301         switch (m_currentPointType) {
302         case PTypeH1:
303             rightX = point.p.x();
304             if (m_currentPointIndex == 0)
305                 leftX = -1000;
306             else
307                 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
308             x = qBound(leftX, x, rightX);
309             point.h1 = QPointF(x, y);
310             break;
311         case PTypeP:
312             if (m_currentPointIndex == 0) {
313                 leftX = 0.0;
314                 rightX = 0.0;
315                 /*if (d->m_curve.points().count() > 1)
316                  *           rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
317                  *       else
318                  *           rightX = 1.0;*/
319             } else if (m_currentPointIndex == m_spline.points().count() - 1) {
320                 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
321                 rightX = 1.0;
322             } else {
323                 //// the 1E-4 addition so we can grab the dot later.
324                 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
325                 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
326             }
327             x = qBound(leftX, x, rightX);
328             y = qBound(0., y, 1.);
329
330             // move handles by same offset
331             point.h1 += QPointF(x, y) - point.p;
332             point.h2 += QPointF(x, y) - point.p;
333
334             point.p = QPointF(x, y);
335             break;
336         case PTypeH2:
337             leftX = point.p.x();
338             if (m_currentPointIndex == m_spline.points().count() - 1)
339                 rightX = 1001;
340             else
341                 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
342             x = qBound(leftX, x, rightX);
343             point.h2 = QPointF(x, y);
344         };
345
346         m_spline.setPoint(m_currentPointIndex, point);
347         
348         /*if (removePoint && d->m_curve.points().count() > 2) {
349             d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
350             d->m_draggedAwayPointIndex = d->m_grab_point_index;
351             d->m_curve.removePoint(d->m_grab_point_index);
352             d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
353         }
354         
355         d->setCurveModified();*/
356         emit currentPoint(point);
357         update();
358     }
359 }
360
361 void BezierSplineEditor::leaveEvent(QEvent* event)
362 {
363     QWidget::leaveEvent(event);
364 }
365
366 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
367 {
368     double nearestDistanceSquared = 1000;
369     point_types selectedPoint;
370     int nearestIndex = -1;
371     int i = 0;
372
373     double distanceSquared;
374     foreach(const BPoint & point, m_spline.points()) {
375         distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
376         if (distanceSquared < nearestDistanceSquared) {
377             nearestIndex = i;
378             nearestDistanceSquared = distanceSquared;
379             selectedPoint = PTypeH1;
380         }
381         distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
382         if (distanceSquared < nearestDistanceSquared) {
383             nearestIndex = i;
384             nearestDistanceSquared = distanceSquared;
385             selectedPoint = PTypeP;
386         }
387         distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
388         if (distanceSquared < nearestDistanceSquared) {
389             nearestIndex = i;
390             nearestDistanceSquared = distanceSquared;
391             selectedPoint = PTypeH2;
392         }
393         ++i;
394     }
395
396     if (nearestIndex >= 0) {
397         BPoint point = m_spline.points()[nearestIndex];
398         QPointF p2;
399         switch (selectedPoint) {
400         case PTypeH1:
401             p2 = point.h1;
402             break;
403         case PTypeP:
404             p2 = point.p;
405             break;
406         case PTypeH2:
407             p2 = point.h2;
408         }
409         if (qAbs(p.x() - p2.x()) * wWidth < 5 && qAbs(p.y() - p2.y()) * wHeight < 5) {
410             *sel = selectedPoint;
411             return nearestIndex;
412         }
413     }
414
415     return -1;
416 }
417
418 #include "beziersplineeditor.moc"