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) :
31 m_pixmapIsDirty(true),
32 m_currentPointIndex(-1)
34 setMouseTracking(true);
35 setAutoFillBackground(false);
36 setAttribute(Qt::WA_OpaquePaintEvent);
37 setMinimumSize(150, 150);
38 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
41 BezierSplineEditor::~BezierSplineEditor()
47 CubicBezierSpline BezierSplineEditor::spline()
52 void BezierSplineEditor::setSpline(const CubicBezierSpline& spline)
54 int precision = m_spline.getPrecision();
56 m_spline.setPrecision(precision);
57 m_currentPointIndex = -1;
63 BPoint BezierSplineEditor::getCurrentPoint()
65 if (m_currentPointIndex >= 0)
66 return m_spline.points()[m_currentPointIndex];
71 void BezierSplineEditor::updateCurrentPoint(const BPoint& p)
73 if (m_currentPointIndex >= 0) {
74 m_spline.setPoint(m_currentPointIndex, p);
75 // during validation the point might have changed
76 emit currentPoint(m_spline.points()[m_currentPointIndex]);
82 void BezierSplineEditor::setPixmap(const QPixmap& pixmap)
85 m_pixmapIsDirty = true;
89 void BezierSplineEditor::slotZoomIn()
91 m_zoomLevel = qMax(m_zoomLevel-1, 0);
92 m_pixmapIsDirty = true;
96 void BezierSplineEditor::slotZoomOut()
98 m_zoomLevel = qMin(m_zoomLevel+1, 3);
99 m_pixmapIsDirty = true;
103 int BezierSplineEditor::gridLines()
108 void BezierSplineEditor::setGridLines(int lines)
110 m_gridLines = qBound(0, lines, 8);
114 void BezierSplineEditor::paintEvent(QPaintEvent* event)
120 int wWidth = width() - 1;
121 int wHeight = height() - 1;
122 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
123 wWidth -= 2 * offset;
124 wHeight -= 2 * offset;
126 p.translate(offset, offset);
131 p.fillRect(rect().translated(-offset, -offset), palette().background());
132 if (!m_pixmap.isNull()) {
133 if (m_pixmapIsDirty || !m_pixmapCache) {
135 delete m_pixmapCache;
136 m_pixmapCache = new QPixmap(wWidth + 1, wHeight + 1);
137 QPainter cachePainter(m_pixmapCache);
139 cachePainter.scale(1.0*(wWidth+1) / m_pixmap.width(), 1.0*(wHeight+1) / m_pixmap.height());
140 cachePainter.drawPixmap(0, 0, m_pixmap);
141 m_pixmapIsDirty = false;
143 p.drawPixmap(0, 0, *m_pixmapCache);
147 p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
152 if (m_zoomLevel != 0) {
153 p.drawRect(0, 0, wWidth, wHeight);
159 if (m_gridLines != 0) {
160 double stepH = wWidth / (double)(m_gridLines + 1);
161 double stepV = wHeight / (double)(m_gridLines + 1);
162 for (int i = 1; i <= m_gridLines; ++i) {
163 p.drawLine(QLineF(i * stepH, 0, i * stepH, wHeight));
164 p.drawLine(QLineF(0, i * stepV, wWidth, i * stepV));
168 p.setRenderHint(QPainter::Antialiasing);
173 p.drawLine(QLineF(0, wHeight, wWidth, 0));
178 double prevY = wHeight - m_spline.value(0.) * wHeight;
181 double normalizedX = -1;
184 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
185 for (x = 0 ; x < wWidth ; ++x) {
186 normalizedX = x / (double)wWidth;
187 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
190 * Keep in mind that QLineF rounds doubles
191 * to ints mathematically, not just rounds down
194 p.drawLine(QLineF(prevX, prevY, x, curY));
198 p.drawLine(QLineF(prevX, prevY ,
199 x, wHeight - m_spline.value(1.0, true) * wHeight));
204 int max = m_spline.points().count() - 1;
205 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
213 #if QT_VERSION < 0x040600
216 for (int i = 0; i <= max; ++i) {
217 point = m_spline.points().at(i);
218 if (i == m_currentPointIndex) {
219 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
221 p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight));
223 p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
226 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
227 wHeight - 3 - point.p.y() * wHeight, 6, 6));
229 #if QT_VERSION >= 0x040600
230 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight));
233 tmp.translate(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight);
234 p.drawConvexPolygon(tmp);
238 #if QT_VERSION >= 0x040600
239 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
242 tmp.translate(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight);
243 p.drawConvexPolygon(tmp);
247 if ( i == m_currentPointIndex)
248 p.setBrush(QBrush(Qt::NoBrush));
252 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
254 m_spline.setPrecision(width() > height() ? width() : height());
255 m_pixmapIsDirty = true;
256 QWidget::resizeEvent(event);
259 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
261 int wWidth = width() - 1;
262 int wHeight = height() - 1;
263 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
264 wWidth -= 2 * offset;
265 wHeight -= 2 * offset;
267 double x = (event->pos().x() - offset) / (double)(wWidth);
268 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
270 point_types selectedPoint;
271 int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
273 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
274 m_spline.removePoint(closestPointIndex);
275 setCursor(Qt::ArrowCursor);
277 if (closestPointIndex < m_currentPointIndex)
278 --m_currentPointIndex;
280 if (m_currentPointIndex >= 0)
281 emit currentPoint(m_spline.points()[m_currentPointIndex]);
283 emit currentPoint(BPoint());
286 } else if (event->button() != Qt::LeftButton) {
290 if (closestPointIndex < 0) {
292 po.p = QPointF(x, y);
293 po.h1 = QPointF(x-0.05, y-0.05);
294 po.h2 = QPointF(x+0.05, y+0.05);
295 m_currentPointIndex = m_spline.addPoint(po);
296 m_currentPointType = PTypeP;
298 m_currentPointIndex = closestPointIndex;
299 m_currentPointType = selectedPoint;
302 BPoint point = m_spline.points()[m_currentPointIndex];
304 switch (m_currentPointType) {
315 m_grabPOriginal = point;
316 if (m_currentPointIndex > 0)
317 m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1];
318 if (m_currentPointIndex < m_spline.points().count() - 1)
319 m_grabPNext = m_spline.points()[m_currentPointIndex + 1];
320 m_grabOffsetX = p.x() - x;
321 m_grabOffsetY = p.y() - y;
323 switch (m_currentPointType) {
325 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
328 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
331 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
333 m_spline.setPoint(m_currentPointIndex, point);
337 emit currentPoint(point);
341 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
343 if (event->button() != Qt::LeftButton)
346 setCursor(Qt::ArrowCursor);
352 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
354 int wWidth = width() - 1;
355 int wHeight = height() - 1;
356 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
357 wWidth -= 2 * offset;
358 wHeight -= 2 * offset;
360 double x = (event->pos().x() - offset) / (double)(wWidth);
361 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
363 if (m_mode == ModeNormal) {
364 // If no point is selected set the the cursor shape if on top
366 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
368 if (nearestPointIndex < 0)
369 setCursor(Qt::ArrowCursor);
371 setCursor(Qt::CrossCursor);
373 // Else, drag the selected point
374 setCursor(Qt::CrossCursor);
381 BPoint point = m_spline.points()[m_currentPointIndex];
382 switch (m_currentPointType) {
384 rightX = point.p.x();
385 if (m_currentPointIndex == 0)
388 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
390 x = qBound(leftX, x, rightX);
391 point.setH1(QPointF(x, y));
395 if (m_currentPointIndex == 0)
397 else if (m_currentPointIndex == m_spline.points().count() - 1)
400 x = qBound(leftX, x, rightX);
401 y = qBound(0., y, 1.);
403 // handles might have changed because we neared another point
405 point.h1 = m_grabPOriginal.h1;
406 point.h2 = m_grabPOriginal.h2;
407 // and move by same offset
408 // (using update handle in point.setP won't work because the offset between new and old point is very small)
409 point.h1 += QPointF(x, y) - m_grabPOriginal.p;
410 point.h2 += QPointF(x, y) - m_grabPOriginal.p;
412 point.setP(QPointF(x, y), false);
417 if (m_currentPointIndex == m_spline.points().count() - 1)
420 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
422 x = qBound(leftX, x, rightX);
423 point.setH2(QPointF(x, y));
426 int index = m_currentPointIndex;
427 m_currentPointIndex = m_spline.setPoint(m_currentPointIndex, point);
429 if (m_currentPointType == PTypeP) {
430 // we might have changed the handles of other points
432 if (index == m_currentPointIndex) {
433 if (m_currentPointIndex > 0)
434 m_spline.setPoint(m_currentPointIndex - 1, m_grabPPrevious);
435 if (m_currentPointIndex < m_spline.points().count() -1)
436 m_spline.setPoint(m_currentPointIndex + 1, m_grabPNext);
438 if (m_currentPointIndex < index) {
439 m_spline.setPoint(index, m_grabPPrevious);
440 m_grabPNext = m_grabPPrevious;
441 if (m_currentPointIndex > 0)
442 m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1];
444 m_spline.setPoint(index, m_grabPNext);
445 m_grabPPrevious = m_grabPNext;
446 if (m_currentPointIndex < m_spline.points().count() - 1)
447 m_grabPNext = m_spline.points()[m_currentPointIndex + 1];
452 emit currentPoint(point);
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;
466 int nearestIndex = -1;
469 double distanceSquared;
470 // find out distance using the Pythagorean theorem
471 foreach(const BPoint & point, m_spline.points()) {
472 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
473 if (distanceSquared < nearestDistanceSquared) {
475 nearestDistanceSquared = distanceSquared;
476 selectedPoint = PTypeH1;
478 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
479 if (distanceSquared < nearestDistanceSquared) {
481 nearestDistanceSquared = distanceSquared;
482 selectedPoint = PTypeP;
484 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
485 if (distanceSquared < nearestDistanceSquared) {
487 nearestDistanceSquared = distanceSquared;
488 selectedPoint = PTypeH2;
493 if (nearestIndex >= 0) {
494 BPoint point = m_spline.points()[nearestIndex];
496 switch (selectedPoint) {
506 if (qAbs(p.x() - p2.x()) * wWidth < 5 && qAbs(p.y() - p2.y()) * wHeight < 5) {
507 *sel = selectedPoint;
515 #include "beziersplineeditor.moc"