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 double prevY = wHeight - m_spline.value(0.) * wHeight;
84 double normalizedX = -1;
87 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
88 for (x = 0 ; x < wWidth ; ++x) {
89 normalizedX = x / (double)wWidth;
90 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
93 * Keep in mind that QLineF rounds doubles
94 * to ints mathematically, not just rounds down
97 p.drawLine(QLineF(prevX, prevY,
102 p.drawLine(QLineF(prevX, prevY ,
103 x, wHeight - m_spline.value(1.0, true) * wHeight));
108 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
116 for (int i = 0; i < m_spline.points().count(); ++i) {
117 point = m_spline.points().at(i);
118 if (i == m_currentPointIndex)
119 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
121 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight));
122 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
123 wHeight - 3 - point.p.y() * wHeight, 6, 6));
124 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
126 if ( i == m_currentPointIndex)
127 p.setBrush(QBrush(Qt::NoBrush));
131 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
133 m_spline.setPrecision(width());
134 QWidget::resizeEvent(event);
137 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
139 double x = event->pos().x() / (double)(width() - 1);
140 double y = 1.0 - event->pos().y() / (double)(height() - 1);
142 point_types selectedPoint;
143 int closestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &selectedPoint);
145 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
146 m_spline.removePoint(closestPointIndex);
147 setCursor(Qt::ArrowCursor);
149 if (closestPointIndex < m_currentPointIndex)
150 --m_currentPointIndex;
152 if (m_currentPointIndex >= 0)
153 emit currentPoint(m_spline.points()[m_currentPointIndex]);
155 emit currentPoint(BPoint());
158 } else if (event->button() != Qt::LeftButton) {
162 if (closestPointIndex < 0) {
164 po.p = QPointF(x, y);
165 po.h1 = QPointF(x-0.05, y-0.05);
166 po.h2 = QPointF(x+0.05, y+0.05);
167 m_currentPointIndex = m_spline.addPoint(po);
168 m_currentPointType = PTypeP;
169 /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
171 m_currentPointIndex = closestPointIndex;
172 m_currentPointType = selectedPoint;
175 BPoint point = m_spline.points()[m_currentPointIndex];
177 switch (m_currentPointType) {
188 m_grabOriginalX = p.x();
189 m_grabOriginalY = p.y();
190 m_grabOffsetX = p.x() - x;
191 m_grabOffsetY = p.y() - y;
193 switch (m_currentPointType) {
195 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
198 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
201 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
203 m_spline.setPoint(m_currentPointIndex, point);
205 //d->m_draggedAwayPointIndex = -1;
209 emit currentPoint(point);
213 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
215 if (event->button() != Qt::LeftButton)
218 setCursor(Qt::ArrowCursor);
224 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
226 double x = event->pos().x() / (double)(width() - 1);
227 double y = 1.0 - event->pos().y() / (double)(height() - 1);
229 if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
231 int nearestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &type);
233 if (nearestPointIndex < 0)
234 setCursor(Qt::ArrowCursor);
236 setCursor(Qt::CrossCursor);
237 } else { // Else, drag the selected point
238 /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
239 event->pos().x() < -MOUSE_AWAY_THRES;
240 bool crossedVert = event->pos().y() - height() > MOUSE_AWAY_THRES ||
241 event->pos().y() < -MOUSE_AWAY_THRES;
243 bool removePoint = (crossedHoriz || crossedVert);
245 if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
246 // point is no longer dragged away so reinsert it
247 QPointF newPoint(d->m_draggedAwayPoint);
248 d->m_grab_point_index = d->m_curve.addPoint(newPoint);
249 d->m_draggedAwayPointIndex = -1;
253 (d->m_draggedAwayPointIndex >= 0))
257 setCursor(Qt::CrossCursor);
262 double leftX, rightX;
263 BPoint point = m_spline.points()[m_currentPointIndex];
264 switch (m_currentPointType) {
266 rightX = point.p.x();
267 if (m_currentPointIndex == 0)
270 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
271 x = qBound(leftX, x, rightX);
272 point.h1 = QPointF(x, y);
275 if (m_currentPointIndex == 0) {
278 /*if (d->m_curve.points().count() > 1)
279 * rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
282 } else if (m_currentPointIndex == m_spline.points().count() - 1) {
283 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
286 //// the 1E-4 addition so we can grab the dot later.
287 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
288 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
290 x = qBound(leftX, x, rightX);
291 y = qBound(0., y, 1.);
293 // move handles by same offset
294 point.h1 += QPointF(x, y) - point.p;
295 point.h2 += QPointF(x, y) - point.p;
297 point.p = QPointF(x, y);
301 if (m_currentPointIndex == m_spline.points().count() - 1)
304 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
305 x = qBound(leftX, x, rightX);
306 point.h2 = QPointF(x, y);
309 m_spline.setPoint(m_currentPointIndex, point);
311 /*if (removePoint && d->m_curve.points().count() > 2) {
312 d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
313 d->m_draggedAwayPointIndex = d->m_grab_point_index;
314 d->m_curve.removePoint(d->m_grab_point_index);
315 d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
318 d->setCurveModified();*/
319 emit currentPoint(point);
324 void BezierSplineEditor::leaveEvent(QEvent* event)
326 QWidget::leaveEvent(event);
329 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
331 double nearestDistanceSquared = 1000;
332 point_types selectedPoint;
333 int nearestIndex = -1;
336 double distanceSquared;
337 foreach(const BPoint & point, m_spline.points()) {
338 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
339 if (distanceSquared < nearestDistanceSquared) {
341 nearestDistanceSquared = distanceSquared;
342 selectedPoint = PTypeH1;
344 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
345 if (distanceSquared < nearestDistanceSquared) {
347 nearestDistanceSquared = distanceSquared;
348 selectedPoint = PTypeP;
350 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
351 if (distanceSquared < nearestDistanceSquared) {
353 nearestDistanceSquared = distanceSquared;
354 selectedPoint = PTypeH2;
359 if (nearestIndex >= 0) {
360 BPoint point = m_spline.points()[nearestIndex];
362 switch (selectedPoint) {
372 if (qAbs(p.x() - p2.x()) * (wWidth - 1) < 5 && qAbs(p.y() - p2.y()) * (wHeight - 1) < 5) {
373 *sel = selectedPoint;
381 #include "beziersplineeditor.moc"