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"
20 #include "kdenlivesettings.h"
23 #include <QMouseEvent>
26 BezierSplineEditor::BezierSplineEditor(QWidget* parent) :
31 , m_showAllHandles(true)
33 , m_pixmapIsDirty(true)
34 , m_currentPointIndex(-1)
35 , m_currentPointType(PTypeP)
39 setMouseTracking(true);
40 setAutoFillBackground(false);
41 setAttribute(Qt::WA_OpaquePaintEvent);
42 setMinimumSize(150, 150);
43 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
46 BezierSplineEditor::~BezierSplineEditor()
52 CubicBezierSpline BezierSplineEditor::spline()
57 void BezierSplineEditor::setSpline(const CubicBezierSpline& spline)
60 m_currentPointIndex = -1;
62 emit currentPoint(BPoint());
67 BPoint BezierSplineEditor::getCurrentPoint()
69 if (m_currentPointIndex >= 0)
70 return m_spline.getPoint(m_currentPointIndex);
75 void BezierSplineEditor::updateCurrentPoint(const BPoint& p, bool final)
77 if (m_currentPointIndex >= 0) {
78 m_spline.setPoint(m_currentPointIndex, p);
79 // during validation the point might have changed
80 emit currentPoint(m_spline.getPoint(m_currentPointIndex));
87 void BezierSplineEditor::setPixmap(const QPixmap& pixmap)
90 m_pixmapIsDirty = true;
94 void BezierSplineEditor::slotZoomIn()
96 m_zoomLevel = qMax(m_zoomLevel-1, 0);
97 m_pixmapIsDirty = true;
101 void BezierSplineEditor::slotZoomOut()
103 m_zoomLevel = qMin(m_zoomLevel+1, 3);
104 m_pixmapIsDirty = true;
108 void BezierSplineEditor::setShowAllHandles(bool show)
110 m_showAllHandles = show;
114 int BezierSplineEditor::gridLines()
119 void BezierSplineEditor::setGridLines(int lines)
121 m_gridLines = qBound(0, lines, 8);
125 void BezierSplineEditor::paintEvent(QPaintEvent* event)
134 int wWidth = width() - 1;
135 int wHeight = height() - 1;
136 int offsetX = 1/8. * m_zoomLevel * wWidth;
137 int offsetY = 1/8. * m_zoomLevel * wHeight;
138 wWidth -= 2 * offsetX;
139 wHeight -= 2 * offsetY;
141 p.translate(offsetX, offsetY);
146 p.fillRect(rect().translated(-offsetX, -offsetY), palette().background());
147 if (!m_pixmap.isNull()) {
148 if (m_pixmapIsDirty || !m_pixmapCache) {
150 delete m_pixmapCache;
151 m_pixmapCache = new QPixmap(wWidth + 1, wHeight + 1);
152 QPainter cachePainter(m_pixmapCache);
154 cachePainter.scale(1.0*(wWidth+1) / m_pixmap.width(), 1.0*(wHeight+1) / m_pixmap.height());
155 cachePainter.drawPixmap(0, 0, m_pixmap);
156 m_pixmapIsDirty = false;
158 p.drawPixmap(0, 0, *m_pixmapCache);
162 p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
167 if (m_zoomLevel != 0) {
168 p.drawRect(0, 0, wWidth, wHeight);
174 if (m_gridLines != 0) {
175 double stepH = wWidth / (double)(m_gridLines + 1);
176 double stepV = wHeight / (double)(m_gridLines + 1);
177 for (int i = 1; i <= m_gridLines; ++i) {
178 p.drawLine(QLineF(i * stepH, 0, i * stepH, wHeight));
179 p.drawLine(QLineF(0, i * stepV, wWidth, i * stepV));
183 p.setRenderHint(QPainter::Antialiasing);
188 p.drawLine(QLineF(0, wHeight, wWidth, 0));
192 * Prepare Spline, Points
194 int max = m_spline.points().count() - 1;
197 BPoint point(m_spline.getPoint(0, wWidth, wHeight, true));
204 QPainterPath splinePath(QPointF(point.p.x(), point.p.y()));
205 for (int i = 0; i < max; ++i) {
206 point = m_spline.getPoint(i, wWidth, wHeight, true);
207 next = m_spline.getPoint(i + 1, wWidth, wHeight, true);
208 splinePath.cubicTo(point.h2, next.h1, next.p);
210 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
211 p.drawPath(splinePath);
217 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
219 QPolygonF handle = QPolygonF() << QPointF(0, -3) << QPointF(3, 0) << QPointF(0, 3) << QPointF(-3, 0);
220 #if QT_VERSION < 0x040600
224 for (int i = 0; i <= max; ++i) {
225 point = m_spline.getPoint(i, wWidth, wHeight, true);
227 if (i == m_currentPointIndex) {
228 // selected point: fill p and handles
229 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
230 // connect p and handles with lines
232 p.drawLine(QLineF(point.h1.x(), point.h1.y(), point.p.x(),point.p.y()));
234 p.drawLine(QLineF(point.p.x(), point.p.y(), point.h2.x(), point.h2.y()));
237 p.drawEllipse(QRectF(point.p.x() - 3,
238 point.p.y() - 3, 6, 6));
239 if (i != 0 && (i == m_currentPointIndex || m_showAllHandles)) {
240 #if QT_VERSION >= 0x040600
241 p.drawConvexPolygon(handle.translated(point.h1.x(), point.h1.y()));
244 tmp.translate(point.h1.x(), point.h1.y());
245 p.drawConvexPolygon(tmp);
248 if (i != max && (i == m_currentPointIndex || m_showAllHandles)) {
249 #if QT_VERSION >= 0x040600
250 p.drawConvexPolygon(handle.translated(point.h2.x(), point.h2.y()));
253 tmp.translate(point.h2.x(), point.h2.y());
254 p.drawConvexPolygon(tmp);
258 if ( i == m_currentPointIndex)
259 p.setBrush(QBrush(Qt::NoBrush));
263 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
265 m_pixmapIsDirty = true;
266 QWidget::resizeEvent(event);
269 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
271 int wWidth = width() - 1;
272 int wHeight = height() - 1;
273 int offsetX = 1/8. * m_zoomLevel * wWidth;
274 int offsetY = 1/8. * m_zoomLevel * wHeight;
275 wWidth -= 2 * offsetX;
276 wHeight -= 2 * offsetY;
278 double x = (event->pos().x() - offsetX) / (double)(wWidth);
279 double y = 1.0 - (event->pos().y() - offsetY) / (double)(wHeight);
281 point_types selectedPoint;
282 int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
284 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
285 m_spline.removePoint(closestPointIndex);
286 setCursor(Qt::ArrowCursor);
288 if (closestPointIndex < m_currentPointIndex)
289 --m_currentPointIndex;
291 if (m_currentPointIndex >= 0)
292 emit currentPoint(m_spline.getPoint(m_currentPointIndex));
294 emit currentPoint(BPoint());
297 } else if (event->button() != Qt::LeftButton) {
301 if (closestPointIndex < 0) {
302 m_currentPointIndex = m_spline.addPoint(BPoint(QPointF(x-0.05, y-0.05),
304 QPointF(x+0.05, y+0.05)));
305 m_currentPointType = PTypeP;
307 m_currentPointIndex = closestPointIndex;
308 m_currentPointType = selectedPoint;
311 BPoint point = m_spline.getPoint(m_currentPointIndex);
313 m_grabPOriginal = point;
314 if (m_currentPointIndex > 0)
315 m_grabPPrevious = m_spline.getPoint(m_currentPointIndex - 1);
316 if (m_currentPointIndex < m_spline.points().count() - 1)
317 m_grabPNext = m_spline.getPoint(m_currentPointIndex + 1);
318 m_grabOffsetX = point[(int)m_currentPointType].x() - x;
319 m_grabOffsetY = point[(int)m_currentPointType].y() - y;
321 point[(int)m_currentPointType] = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
323 m_spline.setPoint(m_currentPointIndex, point);
327 emit currentPoint(point);
331 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
333 if (event->button() != Qt::LeftButton)
336 setCursor(Qt::ArrowCursor);
342 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
344 int wWidth = width() - 1;
345 int wHeight = height() - 1;
346 int offsetX = 1/8. * m_zoomLevel * wWidth;
347 int offsetY = 1/8. * m_zoomLevel * wHeight;
348 wWidth -= 2 * offsetX;
349 wHeight -= 2 * offsetY;
351 double x = (event->pos().x() - offsetX) / (double)(wWidth);
352 double y = 1.0 - (event->pos().y() - offsetY) / (double)(wHeight);
354 if (m_mode == ModeNormal) {
355 // If no point is selected set the the cursor shape if on top
357 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
359 if (nearestPointIndex < 0)
360 setCursor(Qt::ArrowCursor);
362 setCursor(Qt::CrossCursor);
364 // Else, drag the selected point
365 setCursor(Qt::CrossCursor);
372 BPoint point = m_spline.getPoint(m_currentPointIndex);
373 switch (m_currentPointType) {
375 rightX = point.p.x();
376 if (m_currentPointIndex == 0)
379 leftX = m_spline.getPoint(m_currentPointIndex - 1).p.x();
381 x = qBound(leftX, x, rightX);
382 point.setH1(QPointF(x, y));
386 if (m_currentPointIndex == 0)
388 else if (m_currentPointIndex == m_spline.points().count() - 1)
391 x = qBound(leftX, x, rightX);
392 y = qBound(0., y, 1.);
394 // handles might have changed because we neared another point
396 point.h1 = m_grabPOriginal.h1;
397 point.h2 = m_grabPOriginal.h2;
398 // and move by same offset
399 // (using update handle in point.setP won't work because the offset between new and old point is very small)
400 point.h1 += QPointF(x, y) - m_grabPOriginal.p;
401 point.h2 += QPointF(x, y) - m_grabPOriginal.p;
403 point.setP(QPointF(x, y), false);
408 if (m_currentPointIndex == m_spline.points().count() - 1)
411 rightX = m_spline.getPoint(m_currentPointIndex + 1).p.x();
413 x = qBound(leftX, x, rightX);
414 point.setH2(QPointF(x, y));
417 int index = m_currentPointIndex;
418 m_currentPointIndex = m_spline.setPoint(m_currentPointIndex, point);
420 if (m_currentPointType == PTypeP) {
421 // we might have changed the handles of other points
423 if (index == m_currentPointIndex) {
424 if (m_currentPointIndex > 0)
425 m_spline.setPoint(m_currentPointIndex - 1, m_grabPPrevious);
426 if (m_currentPointIndex < m_spline.points().count() -1)
427 m_spline.setPoint(m_currentPointIndex + 1, m_grabPNext);
429 if (m_currentPointIndex < index) {
430 m_spline.setPoint(index, m_grabPPrevious);
431 m_grabPNext = m_grabPPrevious;
432 if (m_currentPointIndex > 0)
433 m_grabPPrevious = m_spline.getPoint(m_currentPointIndex - 1);
435 m_spline.setPoint(index, m_grabPNext);
436 m_grabPPrevious = m_grabPNext;
437 if (m_currentPointIndex < m_spline.points().count() - 1)
438 m_grabPNext = m_spline.getPoint(m_currentPointIndex + 1);
443 emit currentPoint(point);
444 if (KdenliveSettings::dragvalue_directupdate())
450 void BezierSplineEditor::mouseDoubleClickEvent(QMouseEvent* /*event*/)
452 if (m_currentPointIndex >= 0) {
453 BPoint p = m_spline.getPoint(m_currentPointIndex);
454 p.handlesLinked = !p.handlesLinked;
455 m_spline.setPoint(m_currentPointIndex, p);
456 emit currentPoint(p);
460 void BezierSplineEditor::leaveEvent(QEvent* event)
462 QWidget::leaveEvent(event);
465 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
467 double nearestDistanceSquared = 1000;
468 point_types selectedPoint = PTypeP;
469 int nearestIndex = -1;
472 double distanceSquared;
473 // find out distance using the Pythagorean theorem
474 foreach(const BPoint & point, m_spline.points()) {
475 for (int j = 0; j < 3; ++j) {
476 distanceSquared = pow(point[j].x() - p.x(), 2) + pow(point[j].y() - p.y(), 2);
477 if (distanceSquared < nearestDistanceSquared) {
479 nearestDistanceSquared = distanceSquared;
480 selectedPoint = (point_types)j;
486 if (nearestIndex >= 0 && (nearestIndex == m_currentPointIndex || selectedPoint == PTypeP || m_showAllHandles)) {
487 // a point was found and it is not a hidden handle
488 BPoint point = m_spline.getPoint(nearestIndex);
489 if (qAbs(p.x() - point[(int)selectedPoint].x()) * wWidth < 5 && qAbs(p.y() - point[(int)selectedPoint].y()) * wHeight < 5) {
490 *sel = selectedPoint;
498 #include "beziersplineeditor.moc"