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