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