]> git.sesse.net Git - kdenlive/blob - src/beziercurve/beziersplinewidget.cpp
Add bézier color curves GUI (WIP)
[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     for (int i = 0; i < m_spline.points().count(); ++i) {
90         point = m_spline.points().at(i);
91         p.drawEllipse(QRectF(point.h1.x() * wWidth - 3,
92                             wHeight - 3 - point.h1.y() * wHeight, 6, 6));
93         p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
94                              wHeight - 3 - point.p.y() * wHeight, 6, 6));
95         p.drawEllipse(QRectF(point.h2.x() * wWidth - 3,
96                              wHeight - 3 - point.h2.y() * wHeight, 6, 6));
97     }
98 }
99
100 void BezierSplineWidget::resizeEvent(QResizeEvent* event)
101 {
102     m_spline.setPrecision(width());
103     QWidget::resizeEvent(event);
104 }
105
106 void BezierSplineWidget::mousePressEvent(QMouseEvent* event)
107 {
108     double x = event->pos().x() / (double)(width() - 1);
109     double y = 1.0 - event->pos().y() / (double)(height() - 1);
110
111     point_types selectedPoint;
112     int closest_point_index = nearestPointInRange(QPointF(x, y), width(), height(), &selectedPoint);
113
114     /*if (event->button() == Qt::RightButton && closest_point_index > 0 && closest_point_index < d->m_curve.points().count() - 1) {
115         d->m_curve.removePoint(closest_point_index);
116         setCursor(Qt::ArrowCursor);
117         d->setState(ST_NORMAL);
118         if (closest_point_index < d->m_grab_point_index)
119             --d->m_grab_point_index;
120         d->setCurveModified();
121         return;
122     } else*/ if (event->button() != Qt::LeftButton) return;
123
124     if (closest_point_index < 0) {
125         BPoint po;
126         po.p = QPointF(x, y);
127         po.h1 = QPointF(x-0.05, y-0.05);
128         po.h2 = QPointF(x+0.05, y+0.05);
129         m_currentPointIndex = m_spline.addPoint(po);
130         m_currentPointType = PTypeP;
131         if (m_currentPointIndex == -1) // x already in use
132             return;
133         /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
134     } else {
135         m_currentPointIndex = closest_point_index;
136         m_currentPointType = selectedPoint;
137     }
138
139     BPoint point = m_spline.points()[m_currentPointIndex];
140     QPointF p;
141     switch (m_currentPointType) {
142     case PTypeH1:
143         p = point.h1;
144         break;
145     case PTypeP:
146         p = point.p;
147         break;
148     case PTypeH2:
149         p = point.h2;
150     }
151
152     m_grabOriginalX = p.x();
153     m_grabOriginalY = p.y();
154     m_grabOffsetX = p.x() - x;
155     m_grabOffsetY = p.y() - y;
156
157     switch (m_currentPointType) {
158         case PTypeH1:
159             point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
160             break;
161         case PTypeP:
162             point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
163             break;
164         case PTypeH2:
165             point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
166     }
167     m_spline.setPoint(m_currentPointIndex, point);
168     
169     //d->m_draggedAwayPointIndex = -1;
170
171     m_mode = ModeDrag;
172
173     update();
174 }
175
176 void BezierSplineWidget::mouseReleaseEvent(QMouseEvent* event)
177 {
178     if (event->button() != Qt::LeftButton)
179         return;
180
181     setCursor(Qt::ArrowCursor);
182     m_mode = ModeNormal;
183     
184     emit modified();
185 }
186
187 void BezierSplineWidget::mouseMoveEvent(QMouseEvent* event)
188 {
189     double x = event->pos().x() / (double)(width() - 1);
190     double y = 1.0 - event->pos().y() / (double)(height() - 1);
191     
192     if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
193         point_types type;
194         int nearestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &type);
195         
196         if (nearestPointIndex < 0)
197             setCursor(Qt::ArrowCursor);
198         else
199             setCursor(Qt::CrossCursor);
200     } else { // Else, drag the selected point
201         /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
202         event->pos().x() < -MOUSE_AWAY_THRES;
203         bool crossedVert =  event->pos().y() - height() > MOUSE_AWAY_THRES ||
204         event->pos().y() < -MOUSE_AWAY_THRES;
205         
206         bool removePoint = (crossedHoriz || crossedVert);
207         
208         if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
209             // point is no longer dragged away so reinsert it
210             QPointF newPoint(d->m_draggedAwayPoint);
211             d->m_grab_point_index = d->m_curve.addPoint(newPoint);
212             d->m_draggedAwayPointIndex = -1;
213         }
214         
215         if (removePoint &&
216             (d->m_draggedAwayPointIndex >= 0))
217             return;
218         */
219         
220         setCursor(Qt::CrossCursor);
221         
222         x += m_grabOffsetX;
223         y += m_grabOffsetY;
224         
225         double leftX, rightX;
226         BPoint point = m_spline.points()[m_currentPointIndex];
227         switch (m_currentPointType) {
228         case PTypeH1:
229             rightX = point.p.x();
230             if (m_currentPointIndex == 0)
231                 leftX = -1000;
232             else
233                 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
234             x = qBound(leftX, x, rightX);
235             point.h1 = QPointF(x, y);
236             break;
237         case PTypeP:
238             if (m_currentPointIndex == 0) {
239                 leftX = 0.0;
240                 rightX = 0.0;
241                 /*if (d->m_curve.points().count() > 1)
242                  *           rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
243                  *       else
244                  *           rightX = 1.0;*/
245             } else if (m_currentPointIndex == m_spline.points().count() - 1) {
246                 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
247                 rightX = 1.0;
248             } else {
249                 //// the 1E-4 addition so we can grab the dot later.
250                 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
251                 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
252             }
253             x = qBound(leftX, x, rightX);
254             y = qBound(0., y, 1.);
255             point.p = QPointF(x, y);
256             break;
257         case PTypeH2:
258             leftX = point.p.x();
259             if (m_currentPointIndex == m_spline.points().count() - 1)
260                 rightX = 1001;
261             else
262                 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
263             x = qBound(leftX, x, rightX);
264             point.h2 = QPointF(x, y);
265         };
266
267         m_spline.setPoint(m_currentPointIndex, point);
268         
269         /*if (removePoint && d->m_curve.points().count() > 2) {
270             d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
271             d->m_draggedAwayPointIndex = d->m_grab_point_index;
272             d->m_curve.removePoint(d->m_grab_point_index);
273             d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
274         }
275         
276         d->setCurveModified();*/
277         update();
278     }
279 }
280
281 void BezierSplineWidget::leaveEvent(QEvent* event)
282 {
283     QWidget::leaveEvent(event);
284 }
285
286 int BezierSplineWidget::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineWidget::point_types* sel)
287 {
288     double nearestDistanceSquared = 1000;
289     point_types selectedPoint;
290     int nearestIndex = -1;
291     int i = 0;
292
293     double distanceSquared;
294     foreach(const BPoint & point, m_spline.points()) {
295         distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
296         if (distanceSquared < nearestDistanceSquared) {
297             nearestIndex = i;
298             nearestDistanceSquared = distanceSquared;
299             selectedPoint = PTypeH1;
300         }
301         distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
302         if (distanceSquared < nearestDistanceSquared) {
303             nearestIndex = i;
304             nearestDistanceSquared = distanceSquared;
305             selectedPoint = PTypeP;
306         }
307         distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
308         if (distanceSquared < nearestDistanceSquared) {
309             nearestIndex = i;
310             nearestDistanceSquared = distanceSquared;
311             selectedPoint = PTypeH2;
312         }
313         ++i;
314     }
315
316     if (nearestIndex >= 0) {
317         BPoint point = m_spline.points()[nearestIndex];
318         QPointF p2;
319         switch (selectedPoint) {
320         case PTypeH1:
321             p2 = point.h1;
322             break;
323         case PTypeP:
324             p2 = point.p;
325             break;
326         case PTypeH2:
327             p2 = point.h2;
328         }
329         if (qAbs(p.x() - p2.x()) * (wWidth - 1) < 5 && qAbs(p.y() - p2.y()) * (wHeight - 1) < 5) {
330             *sel = selectedPoint;
331             return nearestIndex;
332         }
333     }
334
335     return -1;
336 }
337
338 #include "beziersplinewidget.moc"