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>
25 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::slotZoomIn()
69 m_zoomLevel = qMax(m_zoomLevel-1, 0);
73 void BezierSplineEditor::slotZoomOut()
75 m_zoomLevel = qMin(m_zoomLevel+1, 3);
79 void BezierSplineEditor::paintEvent(QPaintEvent* event)
85 p.fillRect(rect(), palette().background());
87 int wWidth = width() - 1;
88 int wHeight = height() - 1;
89 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
91 wHeight -= 2 * offset;
96 /*p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
97 p.drawLine(QLineF(0, wHeight, wWidth, 0));*/
99 if (m_zoomLevel != 0) {
103 p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
104 p.drawRect(offset, offset, wWidth, wHeight);
110 double prevY = wHeight - m_spline.value(0.) * wHeight;
113 double normalizedX = -1;
116 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
117 for (x = 0 ; x < wWidth ; ++x) {
118 normalizedX = x / (double)wWidth;
119 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
122 * Keep in mind that QLineF rounds doubles
123 * to ints mathematically, not just rounds down
126 p.drawLine(QLineF(prevX, prevY,
127 x, curY).translated(offset, offset));
131 p.drawLine(QLineF(prevX, prevY ,
132 x, wHeight - m_spline.value(1.0, true) * wHeight).translated(offset, offset));
137 int max = m_spline.points().count() - 1;
138 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
146 for (int i = 0; i <= max; ++i) {
147 point = m_spline.points().at(i);
148 if (i == m_currentPointIndex) {
149 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
151 p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight).translated(offset, offset));
153 p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
156 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
157 wHeight - 3 - point.p.y() * wHeight, 6, 6).translated(offset, offset));
159 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight).translated(offset, offset));
161 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
163 if ( i == m_currentPointIndex)
164 p.setBrush(QBrush(Qt::NoBrush));
168 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
170 m_spline.setPrecision(width());
171 QWidget::resizeEvent(event);
174 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
176 int wWidth = width() - 1;
177 int wHeight = height() - 1;
178 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
179 wWidth -= 2 * offset;
180 wHeight -= 2 * offset;
182 double x = (event->pos().x() - offset) / (double)(wWidth);
183 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
185 point_types selectedPoint;
186 int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
188 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
189 m_spline.removePoint(closestPointIndex);
190 setCursor(Qt::ArrowCursor);
192 if (closestPointIndex < m_currentPointIndex)
193 --m_currentPointIndex;
195 if (m_currentPointIndex >= 0)
196 emit currentPoint(m_spline.points()[m_currentPointIndex]);
198 emit currentPoint(BPoint());
201 } else if (event->button() != Qt::LeftButton) {
205 if (closestPointIndex < 0) {
207 po.p = QPointF(x, y);
208 po.h1 = QPointF(x-0.05, y-0.05);
209 po.h2 = QPointF(x+0.05, y+0.05);
210 m_currentPointIndex = m_spline.addPoint(po);
211 m_currentPointType = PTypeP;
212 /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
214 m_currentPointIndex = closestPointIndex;
215 m_currentPointType = selectedPoint;
218 BPoint point = m_spline.points()[m_currentPointIndex];
220 switch (m_currentPointType) {
231 m_grabOriginalX = p.x();
232 m_grabOriginalY = p.y();
233 m_grabOffsetX = p.x() - x;
234 m_grabOffsetY = p.y() - y;
236 switch (m_currentPointType) {
238 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
241 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
244 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
246 m_spline.setPoint(m_currentPointIndex, point);
248 //d->m_draggedAwayPointIndex = -1;
252 emit currentPoint(point);
256 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
258 if (event->button() != Qt::LeftButton)
261 setCursor(Qt::ArrowCursor);
267 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
269 int wWidth = width() - 1;
270 int wHeight = height() - 1;
271 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
272 wWidth -= 2 * offset;
273 wHeight -= 2 * offset;
275 double x = (event->pos().x() - offset) / (double)(wWidth);
276 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
278 if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
280 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
282 if (nearestPointIndex < 0)
283 setCursor(Qt::ArrowCursor);
285 setCursor(Qt::CrossCursor);
286 } else { // Else, drag the selected point
287 /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
288 event->pos().x() < -MOUSE_AWAY_THRES;
289 bool crossedVert = event->pos().y() - height() > MOUSE_AWAY_THRES ||
290 event->pos().y() < -MOUSE_AWAY_THRES;
292 bool removePoint = (crossedHoriz || crossedVert);
294 if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
295 // point is no longer dragged away so reinsert it
296 QPointF newPoint(d->m_draggedAwayPoint);
297 d->m_grab_point_index = d->m_curve.addPoint(newPoint);
298 d->m_draggedAwayPointIndex = -1;
302 (d->m_draggedAwayPointIndex >= 0))
306 setCursor(Qt::CrossCursor);
311 double leftX, rightX;
312 BPoint point = m_spline.points()[m_currentPointIndex];
313 switch (m_currentPointType) {
315 rightX = point.p.x();
316 if (m_currentPointIndex == 0)
319 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
320 x = qBound(leftX, x, rightX);
321 point.h1 = QPointF(x, y);
324 if (m_currentPointIndex == 0) {
327 /*if (d->m_curve.points().count() > 1)
328 * rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
331 } else if (m_currentPointIndex == m_spline.points().count() - 1) {
332 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
335 //// the 1E-4 addition so we can grab the dot later.
336 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
337 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
339 x = qBound(leftX, x, rightX);
340 y = qBound(0., y, 1.);
342 // move handles by same offset
343 point.h1 += QPointF(x, y) - point.p;
344 point.h2 += QPointF(x, y) - point.p;
346 point.p = QPointF(x, y);
350 if (m_currentPointIndex == m_spline.points().count() - 1)
353 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
354 x = qBound(leftX, x, rightX);
355 point.h2 = QPointF(x, y);
358 m_spline.setPoint(m_currentPointIndex, point);
360 /*if (removePoint && d->m_curve.points().count() > 2) {
361 d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
362 d->m_draggedAwayPointIndex = d->m_grab_point_index;
363 d->m_curve.removePoint(d->m_grab_point_index);
364 d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
367 d->setCurveModified();*/
368 emit currentPoint(point);
373 void BezierSplineEditor::leaveEvent(QEvent* event)
375 QWidget::leaveEvent(event);
378 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
380 double nearestDistanceSquared = 1000;
381 point_types selectedPoint;
382 int nearestIndex = -1;
385 double distanceSquared;
386 foreach(const BPoint & point, m_spline.points()) {
387 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
388 if (distanceSquared < nearestDistanceSquared) {
390 nearestDistanceSquared = distanceSquared;
391 selectedPoint = PTypeH1;
393 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
394 if (distanceSquared < nearestDistanceSquared) {
396 nearestDistanceSquared = distanceSquared;
397 selectedPoint = PTypeP;
399 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
400 if (distanceSquared < nearestDistanceSquared) {
402 nearestDistanceSquared = distanceSquared;
403 selectedPoint = PTypeH2;
408 if (nearestIndex >= 0) {
409 BPoint point = m_spline.points()[nearestIndex];
411 switch (selectedPoint) {
421 if (qAbs(p.x() - p2.x()) * wWidth < 5 && qAbs(p.y() - p2.y()) * wHeight < 5) {
422 *sel = selectedPoint;
430 #include "beziersplineeditor.moc"