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