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)
36 setMouseTracking(true);
37 setAutoFillBackground(false);
38 setAttribute(Qt::WA_OpaquePaintEvent);
39 setMinimumSize(150, 150);
40 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
43 BezierSplineEditor::~BezierSplineEditor()
49 CubicBezierSpline BezierSplineEditor::spline()
54 void BezierSplineEditor::setSpline(const CubicBezierSpline& spline)
57 m_currentPointIndex = -1;
59 emit currentPoint(BPoint());
64 BPoint BezierSplineEditor::getCurrentPoint()
66 if (m_currentPointIndex >= 0)
67 return m_spline.getPoint(m_currentPointIndex);
72 void BezierSplineEditor::updateCurrentPoint(const BPoint& p, bool final)
74 if (m_currentPointIndex >= 0) {
75 m_spline.setPoint(m_currentPointIndex, p);
76 // during validation the point might have changed
77 emit currentPoint(m_spline.getPoint(m_currentPointIndex));
84 void BezierSplineEditor::setPixmap(const QPixmap& pixmap)
87 m_pixmapIsDirty = true;
91 void BezierSplineEditor::slotZoomIn()
93 m_zoomLevel = qMax(m_zoomLevel-1, 0);
94 m_pixmapIsDirty = true;
98 void BezierSplineEditor::slotZoomOut()
100 m_zoomLevel = qMin(m_zoomLevel+1, 3);
101 m_pixmapIsDirty = true;
105 void BezierSplineEditor::setShowAllHandles(bool show)
107 m_showAllHandles = show;
111 int BezierSplineEditor::gridLines()
116 void BezierSplineEditor::setGridLines(int lines)
118 m_gridLines = qBound(0, lines, 8);
122 void BezierSplineEditor::paintEvent(QPaintEvent* event)
131 int wWidth = width() - 1;
132 int wHeight = height() - 1;
133 int offsetX = 1/8. * m_zoomLevel * wWidth;
134 int offsetY = 1/8. * m_zoomLevel * wHeight;
135 wWidth -= 2 * offsetX;
136 wHeight -= 2 * offsetY;
138 p.translate(offsetX, offsetY);
143 p.fillRect(rect().translated(-offsetX, -offsetY), palette().background());
144 if (!m_pixmap.isNull()) {
145 if (m_pixmapIsDirty || !m_pixmapCache) {
147 delete m_pixmapCache;
148 m_pixmapCache = new QPixmap(wWidth + 1, wHeight + 1);
149 QPainter cachePainter(m_pixmapCache);
151 cachePainter.scale(1.0*(wWidth+1) / m_pixmap.width(), 1.0*(wHeight+1) / m_pixmap.height());
152 cachePainter.drawPixmap(0, 0, m_pixmap);
153 m_pixmapIsDirty = false;
155 p.drawPixmap(0, 0, *m_pixmapCache);
159 p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
164 if (m_zoomLevel != 0) {
165 p.drawRect(0, 0, wWidth, wHeight);
171 if (m_gridLines != 0) {
172 double stepH = wWidth / (double)(m_gridLines + 1);
173 double stepV = wHeight / (double)(m_gridLines + 1);
174 for (int i = 1; i <= m_gridLines; ++i) {
175 p.drawLine(QLineF(i * stepH, 0, i * stepH, wHeight));
176 p.drawLine(QLineF(0, i * stepV, wWidth, i * stepV));
180 p.setRenderHint(QPainter::Antialiasing);
185 p.drawLine(QLineF(0, wHeight, wWidth, 0));
189 * Prepare Spline, Points
191 int max = m_spline.points().count() - 1;
194 BPoint point(m_spline.getPoint(0, wWidth, wHeight, true));
201 QPainterPath splinePath(QPointF(point.p.x(), point.p.y()));
202 for (int i = 0; i < max; ++i) {
203 point = m_spline.getPoint(i, wWidth, wHeight, true);
204 next = m_spline.getPoint(i + 1, wWidth, wHeight, true);
205 splinePath.cubicTo(point.h2, next.h1, next.p);
207 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
208 p.drawPath(splinePath);
214 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
216 QPolygonF handle = QPolygonF() << QPointF(0, -3) << QPointF(3, 0) << QPointF(0, 3) << QPointF(-3, 0);
217 #if QT_VERSION < 0x040600
221 for (int i = 0; i <= max; ++i) {
222 point = m_spline.getPoint(i, wWidth, wHeight, true);
224 if (i == m_currentPointIndex) {
225 // selected point: fill p and handles
226 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
227 // connect p and handles with lines
229 p.drawLine(QLineF(point.h1.x(), point.h1.y(), point.p.x(),point.p.y()));
231 p.drawLine(QLineF(point.p.x(), point.p.y(), point.h2.x(), point.h2.y()));
234 p.drawEllipse(QRectF(point.p.x() - 3,
235 point.p.y() - 3, 6, 6));
236 if (i != 0 && (i == m_currentPointIndex || m_showAllHandles)) {
237 #if QT_VERSION >= 0x040600
238 p.drawConvexPolygon(handle.translated(point.h1.x(), point.h1.y()));
241 tmp.translate(point.h1.x(), point.h1.y());
242 p.drawConvexPolygon(tmp);
245 if (i != max && (i == m_currentPointIndex || m_showAllHandles)) {
246 #if QT_VERSION >= 0x040600
247 p.drawConvexPolygon(handle.translated(point.h2.x(), point.h2.y()));
250 tmp.translate(point.h2.x(), point.h2.y());
251 p.drawConvexPolygon(tmp);
255 if ( i == m_currentPointIndex)
256 p.setBrush(QBrush(Qt::NoBrush));
260 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
262 m_pixmapIsDirty = true;
263 QWidget::resizeEvent(event);
266 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
268 int wWidth = width() - 1;
269 int wHeight = height() - 1;
270 int offsetX = 1/8. * m_zoomLevel * wWidth;
271 int offsetY = 1/8. * m_zoomLevel * wHeight;
272 wWidth -= 2 * offsetX;
273 wHeight -= 2 * offsetY;
275 double x = (event->pos().x() - offsetX) / (double)(wWidth);
276 double y = 1.0 - (event->pos().y() - offsetY) / (double)(wHeight);
278 point_types selectedPoint;
279 int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
281 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
282 m_spline.removePoint(closestPointIndex);
283 setCursor(Qt::ArrowCursor);
285 if (closestPointIndex < m_currentPointIndex)
286 --m_currentPointIndex;
288 if (m_currentPointIndex >= 0)
289 emit currentPoint(m_spline.getPoint(m_currentPointIndex));
291 emit currentPoint(BPoint());
294 } else if (event->button() != Qt::LeftButton) {
298 if (closestPointIndex < 0) {
299 m_currentPointIndex = m_spline.addPoint(BPoint(QPointF(x-0.05, y-0.05),
301 QPointF(x+0.05, y+0.05)));
302 m_currentPointType = PTypeP;
304 m_currentPointIndex = closestPointIndex;
305 m_currentPointType = selectedPoint;
308 BPoint point = m_spline.getPoint(m_currentPointIndex);
310 m_grabPOriginal = point;
311 if (m_currentPointIndex > 0)
312 m_grabPPrevious = m_spline.getPoint(m_currentPointIndex - 1);
313 if (m_currentPointIndex < m_spline.points().count() - 1)
314 m_grabPNext = m_spline.getPoint(m_currentPointIndex + 1);
315 m_grabOffsetX = point[(int)m_currentPointType].x() - x;
316 m_grabOffsetY = point[(int)m_currentPointType].y() - y;
318 point[(int)m_currentPointType] = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
320 m_spline.setPoint(m_currentPointIndex, point);
324 emit currentPoint(point);
328 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
330 if (event->button() != Qt::LeftButton)
333 setCursor(Qt::ArrowCursor);
339 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
341 int wWidth = width() - 1;
342 int wHeight = height() - 1;
343 int offsetX = 1/8. * m_zoomLevel * wWidth;
344 int offsetY = 1/8. * m_zoomLevel * wHeight;
345 wWidth -= 2 * offsetX;
346 wHeight -= 2 * offsetY;
348 double x = (event->pos().x() - offsetX) / (double)(wWidth);
349 double y = 1.0 - (event->pos().y() - offsetY) / (double)(wHeight);
351 if (m_mode == ModeNormal) {
352 // If no point is selected set the the cursor shape if on top
354 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
356 if (nearestPointIndex < 0)
357 setCursor(Qt::ArrowCursor);
359 setCursor(Qt::CrossCursor);
361 // Else, drag the selected point
362 setCursor(Qt::CrossCursor);
369 BPoint point = m_spline.getPoint(m_currentPointIndex);
370 switch (m_currentPointType) {
372 rightX = point.p.x();
373 if (m_currentPointIndex == 0)
376 leftX = m_spline.getPoint(m_currentPointIndex - 1).p.x();
378 x = qBound(leftX, x, rightX);
379 point.setH1(QPointF(x, y));
383 if (m_currentPointIndex == 0)
385 else if (m_currentPointIndex == m_spline.points().count() - 1)
388 x = qBound(leftX, x, rightX);
389 y = qBound(0., y, 1.);
391 // handles might have changed because we neared another point
393 point.h1 = m_grabPOriginal.h1;
394 point.h2 = m_grabPOriginal.h2;
395 // and move by same offset
396 // (using update handle in point.setP won't work because the offset between new and old point is very small)
397 point.h1 += QPointF(x, y) - m_grabPOriginal.p;
398 point.h2 += QPointF(x, y) - m_grabPOriginal.p;
400 point.setP(QPointF(x, y), false);
405 if (m_currentPointIndex == m_spline.points().count() - 1)
408 rightX = m_spline.getPoint(m_currentPointIndex + 1).p.x();
410 x = qBound(leftX, x, rightX);
411 point.setH2(QPointF(x, y));
414 int index = m_currentPointIndex;
415 m_currentPointIndex = m_spline.setPoint(m_currentPointIndex, point);
417 if (m_currentPointType == PTypeP) {
418 // we might have changed the handles of other points
420 if (index == m_currentPointIndex) {
421 if (m_currentPointIndex > 0)
422 m_spline.setPoint(m_currentPointIndex - 1, m_grabPPrevious);
423 if (m_currentPointIndex < m_spline.points().count() -1)
424 m_spline.setPoint(m_currentPointIndex + 1, m_grabPNext);
426 if (m_currentPointIndex < index) {
427 m_spline.setPoint(index, m_grabPPrevious);
428 m_grabPNext = m_grabPPrevious;
429 if (m_currentPointIndex > 0)
430 m_grabPPrevious = m_spline.getPoint(m_currentPointIndex - 1);
432 m_spline.setPoint(index, m_grabPNext);
433 m_grabPPrevious = m_grabPNext;
434 if (m_currentPointIndex < m_spline.points().count() - 1)
435 m_grabPNext = m_spline.getPoint(m_currentPointIndex + 1);
440 emit currentPoint(point);
441 if (KdenliveSettings::dragvalue_directupdate())
447 void BezierSplineEditor::mouseDoubleClickEvent(QMouseEvent* /*event*/)
449 if (m_currentPointIndex >= 0) {
450 BPoint p = m_spline.getPoint(m_currentPointIndex);
451 p.handlesLinked = !p.handlesLinked;
452 m_spline.setPoint(m_currentPointIndex, p);
453 emit currentPoint(p);
457 void BezierSplineEditor::leaveEvent(QEvent* event)
459 QWidget::leaveEvent(event);
462 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
464 double nearestDistanceSquared = 1000;
465 point_types selectedPoint = PTypeP;
466 int nearestIndex = -1;
469 double distanceSquared;
470 // find out distance using the Pythagorean theorem
471 foreach(const BPoint & point, m_spline.points()) {
472 for (int j = 0; j < 3; ++j) {
473 distanceSquared = pow(point[j].x() - p.x(), 2) + pow(point[j].y() - p.y(), 2);
474 if (distanceSquared < nearestDistanceSquared) {
476 nearestDistanceSquared = distanceSquared;
477 selectedPoint = (point_types)j;
483 if (nearestIndex >= 0 && (nearestIndex == m_currentPointIndex || selectedPoint == PTypeP || m_showAllHandles)) {
484 // a point was found and it is not a hidden handle
485 BPoint point = m_spline.getPoint(nearestIndex);
486 if (qAbs(p.x() - point[(int)selectedPoint].x()) * wWidth < 5 && qAbs(p.y() - point[(int)selectedPoint].y()) * wHeight < 5) {
487 *sel = selectedPoint;
495 #include "beziersplineeditor.moc"