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