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