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 "beziersplineeditor.h"
22 #include <QMouseEvent>
26 BezierSplineEditor::BezierSplineEditor(QWidget* parent) :
29 m_currentPointIndex(-1)
31 setMouseTracking(true);
32 setAutoFillBackground(false);
33 setAttribute(Qt::WA_OpaquePaintEvent);
34 setMinimumSize(150, 150);
35 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
38 CubicBezierSpline BezierSplineEditor::spline()
43 void BezierSplineEditor::setSpline(const CubicBezierSpline& spline)
46 m_spline.fromString(spline.toString());
49 BPoint BezierSplineEditor::getCurrentPoint()
51 if (m_currentPointIndex >= 0)
52 return m_spline.points()[m_currentPointIndex];
57 void BezierSplineEditor::updateCurrentPoint(const BPoint& p)
59 if (m_currentPointIndex >= 0) {
60 m_spline.setPoint(m_currentPointIndex, p);
61 // during validation the point might have changed
62 emit currentPoint(m_spline.points()[m_currentPointIndex]);
67 void BezierSplineEditor::paintEvent(QPaintEvent* event)
73 p.fillRect(rect(), palette().background());
75 int wWidth = width() - 1;
76 int wHeight = height() - 1;
81 /*p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
82 p.drawLine(QLineF(0, wHeight, wWidth, 0));*/
87 double prevY = wHeight - m_spline.value(0.) * wHeight;
90 double normalizedX = -1;
93 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
94 for (x = 0 ; x < wWidth ; ++x) {
95 normalizedX = x / (double)wWidth;
96 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
99 * Keep in mind that QLineF rounds doubles
100 * to ints mathematically, not just rounds down
103 p.drawLine(QLineF(prevX, prevY,
108 p.drawLine(QLineF(prevX, prevY ,
109 x, wHeight - m_spline.value(1.0, true) * wHeight));
114 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
122 for (int i = 0; i < m_spline.points().count(); ++i) {
123 point = m_spline.points().at(i);
124 if (i == m_currentPointIndex) {
125 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
126 p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight));
127 p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
130 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
131 wHeight - 3 - point.p.y() * wHeight, 6, 6));
132 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight));
133 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
135 if ( i == m_currentPointIndex)
136 p.setBrush(QBrush(Qt::NoBrush));
140 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
142 m_spline.setPrecision(width());
143 QWidget::resizeEvent(event);
146 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
148 double x = event->pos().x() / (double)(width() - 1);
149 double y = 1.0 - event->pos().y() / (double)(height() - 1);
151 point_types selectedPoint;
152 int closestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &selectedPoint);
154 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
155 m_spline.removePoint(closestPointIndex);
156 setCursor(Qt::ArrowCursor);
158 if (closestPointIndex < m_currentPointIndex)
159 --m_currentPointIndex;
161 if (m_currentPointIndex >= 0)
162 emit currentPoint(m_spline.points()[m_currentPointIndex]);
164 emit currentPoint(BPoint());
167 } else if (event->button() != Qt::LeftButton) {
171 if (closestPointIndex < 0) {
173 po.p = QPointF(x, y);
174 po.h1 = QPointF(x-0.05, y-0.05);
175 po.h2 = QPointF(x+0.05, y+0.05);
176 m_currentPointIndex = m_spline.addPoint(po);
177 m_currentPointType = PTypeP;
178 /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
180 m_currentPointIndex = closestPointIndex;
181 m_currentPointType = selectedPoint;
184 BPoint point = m_spline.points()[m_currentPointIndex];
186 switch (m_currentPointType) {
197 m_grabOriginalX = p.x();
198 m_grabOriginalY = p.y();
199 m_grabOffsetX = p.x() - x;
200 m_grabOffsetY = p.y() - y;
202 switch (m_currentPointType) {
204 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
207 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
210 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
212 m_spline.setPoint(m_currentPointIndex, point);
214 //d->m_draggedAwayPointIndex = -1;
218 emit currentPoint(point);
222 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
224 if (event->button() != Qt::LeftButton)
227 setCursor(Qt::ArrowCursor);
233 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
235 double x = event->pos().x() / (double)(width() - 1);
236 double y = 1.0 - event->pos().y() / (double)(height() - 1);
238 if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
240 int nearestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &type);
242 if (nearestPointIndex < 0)
243 setCursor(Qt::ArrowCursor);
245 setCursor(Qt::CrossCursor);
246 } else { // Else, drag the selected point
247 /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
248 event->pos().x() < -MOUSE_AWAY_THRES;
249 bool crossedVert = event->pos().y() - height() > MOUSE_AWAY_THRES ||
250 event->pos().y() < -MOUSE_AWAY_THRES;
252 bool removePoint = (crossedHoriz || crossedVert);
254 if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
255 // point is no longer dragged away so reinsert it
256 QPointF newPoint(d->m_draggedAwayPoint);
257 d->m_grab_point_index = d->m_curve.addPoint(newPoint);
258 d->m_draggedAwayPointIndex = -1;
262 (d->m_draggedAwayPointIndex >= 0))
266 setCursor(Qt::CrossCursor);
271 double leftX, rightX;
272 BPoint point = m_spline.points()[m_currentPointIndex];
273 switch (m_currentPointType) {
275 rightX = point.p.x();
276 if (m_currentPointIndex == 0)
279 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
280 x = qBound(leftX, x, rightX);
281 point.h1 = QPointF(x, y);
284 if (m_currentPointIndex == 0) {
287 /*if (d->m_curve.points().count() > 1)
288 * rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
291 } else if (m_currentPointIndex == m_spline.points().count() - 1) {
292 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
295 //// the 1E-4 addition so we can grab the dot later.
296 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
297 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
299 x = qBound(leftX, x, rightX);
300 y = qBound(0., y, 1.);
302 // move handles by same offset
303 point.h1 += QPointF(x, y) - point.p;
304 point.h2 += QPointF(x, y) - point.p;
306 point.p = QPointF(x, y);
310 if (m_currentPointIndex == m_spline.points().count() - 1)
313 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
314 x = qBound(leftX, x, rightX);
315 point.h2 = QPointF(x, y);
318 m_spline.setPoint(m_currentPointIndex, point);
320 /*if (removePoint && d->m_curve.points().count() > 2) {
321 d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
322 d->m_draggedAwayPointIndex = d->m_grab_point_index;
323 d->m_curve.removePoint(d->m_grab_point_index);
324 d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
327 d->setCurveModified();*/
328 emit currentPoint(point);
333 void BezierSplineEditor::leaveEvent(QEvent* event)
335 QWidget::leaveEvent(event);
338 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
340 double nearestDistanceSquared = 1000;
341 point_types selectedPoint;
342 int nearestIndex = -1;
345 double distanceSquared;
346 foreach(const BPoint & point, m_spline.points()) {
347 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
348 if (distanceSquared < nearestDistanceSquared) {
350 nearestDistanceSquared = distanceSquared;
351 selectedPoint = PTypeH1;
353 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
354 if (distanceSquared < nearestDistanceSquared) {
356 nearestDistanceSquared = distanceSquared;
357 selectedPoint = PTypeP;
359 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
360 if (distanceSquared < nearestDistanceSquared) {
362 nearestDistanceSquared = distanceSquared;
363 selectedPoint = PTypeH2;
368 if (nearestIndex >= 0) {
369 BPoint point = m_spline.points()[nearestIndex];
371 switch (selectedPoint) {
381 if (qAbs(p.x() - p2.x()) * (wWidth - 1) < 5 && qAbs(p.y() - p2.y()) * (wHeight - 1) < 5) {
382 *sel = selectedPoint;
390 #include "beziersplineeditor.moc"