1 /***************************************************************************
2 * Copyright (C) 2010 by Till Theato (root@ttill.de) *
3 * This file is part of Kdenlive (www.kdenlive.org). *
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. *
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. *
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 ***************************************************************************/
19 #include "beziersplinewidget.h"
22 #include <QMouseEvent>
26 BezierSplineWidget::BezierSplineWidget(QWidget* parent) :
29 m_currentPointIndex(-1)
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);
39 CubicBezierSpline BezierSplineWidget::spline()
44 void BezierSplineWidget::setSpline(const CubicBezierSpline& spline)
47 m_spline.fromString(spline.toString());
50 void BezierSplineWidget::paintEvent(QPaintEvent* event)
56 p.fillRect(rect(), palette().background());
58 int wWidth = width() - 1;
59 int wHeight = height() - 1;
62 double prevY = wHeight - m_spline.value(0.) * wHeight;
65 double normalizedX = -1;
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;
74 * Keep in mind that QLineF rounds doubles
75 * to ints mathematically, not just rounds down
78 p.drawLine(QLineF(prevX, prevY,
83 p.drawLine(QLineF(prevX, prevY ,
84 x, wHeight - m_spline.value(1.0, true) * wHeight));
86 // Drawing curve handles.
87 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
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));
100 void BezierSplineWidget::resizeEvent(QResizeEvent* event)
102 m_spline.setPrecision(width());
103 QWidget::resizeEvent(event);
106 void BezierSplineWidget::mousePressEvent(QMouseEvent* event)
108 double x = event->pos().x() / (double)(width() - 1);
109 double y = 1.0 - event->pos().y() / (double)(height() - 1);
111 point_types selectedPoint;
112 int closest_point_index = nearestPointInRange(QPointF(x, y), width(), height(), &selectedPoint);
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();
122 } else*/ if (event->button() != Qt::LeftButton) return;
124 if (closest_point_index < 0) {
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
133 /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
135 m_currentPointIndex = closest_point_index;
136 m_currentPointType = selectedPoint;
139 BPoint point = m_spline.points()[m_currentPointIndex];
141 switch (m_currentPointType) {
152 m_grabOriginalX = p.x();
153 m_grabOriginalY = p.y();
154 m_grabOffsetX = p.x() - x;
155 m_grabOffsetY = p.y() - y;
157 switch (m_currentPointType) {
159 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
162 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
165 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
167 m_spline.setPoint(m_currentPointIndex, point);
169 //d->m_draggedAwayPointIndex = -1;
176 void BezierSplineWidget::mouseReleaseEvent(QMouseEvent* event)
178 if (event->button() != Qt::LeftButton)
181 setCursor(Qt::ArrowCursor);
187 void BezierSplineWidget::mouseMoveEvent(QMouseEvent* event)
189 double x = event->pos().x() / (double)(width() - 1);
190 double y = 1.0 - event->pos().y() / (double)(height() - 1);
192 if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
194 int nearestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &type);
196 if (nearestPointIndex < 0)
197 setCursor(Qt::ArrowCursor);
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;
206 bool removePoint = (crossedHoriz || crossedVert);
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;
216 (d->m_draggedAwayPointIndex >= 0))
220 setCursor(Qt::CrossCursor);
225 double leftX, rightX;
226 BPoint point = m_spline.points()[m_currentPointIndex];
227 switch (m_currentPointType) {
229 rightX = point.p.x();
230 if (m_currentPointIndex == 0)
233 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
234 x = qBound(leftX, x, rightX);
235 point.h1 = QPointF(x, y);
238 if (m_currentPointIndex == 0) {
241 /*if (d->m_curve.points().count() > 1)
242 * rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
245 } else if (m_currentPointIndex == m_spline.points().count() - 1) {
246 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
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;
253 x = qBound(leftX, x, rightX);
254 y = qBound(0., y, 1.);
255 point.p = QPointF(x, y);
259 if (m_currentPointIndex == m_spline.points().count() - 1)
262 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
263 x = qBound(leftX, x, rightX);
264 point.h2 = QPointF(x, y);
267 m_spline.setPoint(m_currentPointIndex, point);
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);
276 d->setCurveModified();*/
281 void BezierSplineWidget::leaveEvent(QEvent* event)
283 QWidget::leaveEvent(event);
286 int BezierSplineWidget::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineWidget::point_types* sel)
288 double nearestDistanceSquared = 1000;
289 point_types selectedPoint;
290 int nearestIndex = -1;
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) {
298 nearestDistanceSquared = distanceSquared;
299 selectedPoint = PTypeH1;
301 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
302 if (distanceSquared < nearestDistanceSquared) {
304 nearestDistanceSquared = distanceSquared;
305 selectedPoint = PTypeP;
307 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
308 if (distanceSquared < nearestDistanceSquared) {
310 nearestDistanceSquared = distanceSquared;
311 selectedPoint = PTypeH2;
316 if (nearestIndex >= 0) {
317 BPoint point = m_spline.points()[nearestIndex];
319 switch (selectedPoint) {
329 if (qAbs(p.x() - p2.x()) * (wWidth - 1) < 5 && qAbs(p.y() - p2.y()) * (wHeight - 1) < 5) {
330 *sel = selectedPoint;
338 #include "beziersplinewidget.moc"