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]);
81 void BezierSplineEditor::setPixmap(const QPixmap& pixmap)
84 m_pixmapIsDirty = true;
88 void BezierSplineEditor::slotZoomIn()
90 m_zoomLevel = qMax(m_zoomLevel-1, 0);
91 m_pixmapIsDirty = true;
95 void BezierSplineEditor::slotZoomOut()
97 m_zoomLevel = qMin(m_zoomLevel+1, 3);
98 m_pixmapIsDirty = true;
102 int BezierSplineEditor::gridLines()
107 void BezierSplineEditor::setGridLines(int lines)
109 m_gridLines = qBound(0, lines, 8);
113 void BezierSplineEditor::paintEvent(QPaintEvent* event)
119 int wWidth = width() - 1;
120 int wHeight = height() - 1;
121 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
122 wWidth -= 2 * offset;
123 wHeight -= 2 * offset;
125 p.translate(offset, offset);
130 p.fillRect(rect().translated(-offset, -offset), palette().background());
131 if (!m_pixmap.isNull()) {
132 if (m_pixmapIsDirty || !m_pixmapCache) {
134 delete m_pixmapCache;
135 m_pixmapCache = new QPixmap(wWidth + 1, wHeight + 1);
136 QPainter cachePainter(m_pixmapCache);
138 cachePainter.scale(1.0*(wWidth+1) / m_pixmap.width(), 1.0*(wHeight+1) / m_pixmap.height());
139 cachePainter.drawPixmap(0, 0, m_pixmap);
140 m_pixmapIsDirty = false;
142 p.drawPixmap(0, 0, *m_pixmapCache);
146 p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
151 if (m_zoomLevel != 0) {
152 p.drawRect(0, 0, wWidth, wHeight);
158 if (m_gridLines != 0) {
159 double stepH = wWidth / (double)(m_gridLines + 1);
160 double stepV = wHeight / (double)(m_gridLines + 1);
161 for (int i = 1; i <= m_gridLines; ++i) {
162 p.drawLine(QLineF(i * stepH, 0, i * stepH, wHeight));
163 p.drawLine(QLineF(0, i * stepV, wWidth, i * stepV));
167 p.setRenderHint(QPainter::Antialiasing);
172 p.drawLine(QLineF(0, wHeight, wWidth, 0));
177 double prevY = wHeight - m_spline.value(0.) * wHeight;
180 double normalizedX = -1;
183 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
184 for (x = 0 ; x < wWidth ; ++x) {
185 normalizedX = x / (double)wWidth;
186 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
189 * Keep in mind that QLineF rounds doubles
190 * to ints mathematically, not just rounds down
193 p.drawLine(QLineF(prevX, prevY, x, curY));
197 p.drawLine(QLineF(prevX, prevY ,
198 x, wHeight - m_spline.value(1.0, true) * wHeight));
203 int max = m_spline.points().count() - 1;
204 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
212 #if QT_VERSION < 0x040600
215 for (int i = 0; i <= max; ++i) {
216 point = m_spline.points().at(i);
217 if (i == m_currentPointIndex) {
218 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
220 p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight));
222 p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
225 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
226 wHeight - 3 - point.p.y() * wHeight, 6, 6));
228 #if QT_VERSION >= 0x040600
229 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight));
232 tmp.translate(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight);
233 p.drawConvexPolygon(tmp);
237 #if QT_VERSION >= 0x040600
238 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
241 tmp.translate(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight);
242 p.drawConvexPolygon(tmp);
246 if ( i == m_currentPointIndex)
247 p.setBrush(QBrush(Qt::NoBrush));
251 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
253 m_spline.setPrecision(width() > height() ? width() : height());
254 m_pixmapIsDirty = true;
255 QWidget::resizeEvent(event);
258 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
260 int wWidth = width() - 1;
261 int wHeight = height() - 1;
262 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
263 wWidth -= 2 * offset;
264 wHeight -= 2 * offset;
266 double x = (event->pos().x() - offset) / (double)(wWidth);
267 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
269 point_types selectedPoint;
270 int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
272 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
273 m_spline.removePoint(closestPointIndex);
274 setCursor(Qt::ArrowCursor);
276 if (closestPointIndex < m_currentPointIndex)
277 --m_currentPointIndex;
279 if (m_currentPointIndex >= 0)
280 emit currentPoint(m_spline.points()[m_currentPointIndex]);
282 emit currentPoint(BPoint());
285 } else if (event->button() != Qt::LeftButton) {
289 if (closestPointIndex < 0) {
291 po.p = QPointF(x, y);
292 po.h1 = QPointF(x-0.05, y-0.05);
293 po.h2 = QPointF(x+0.05, y+0.05);
294 m_currentPointIndex = m_spline.addPoint(po);
295 m_currentPointType = PTypeP;
297 m_currentPointIndex = closestPointIndex;
298 m_currentPointType = selectedPoint;
301 BPoint point = m_spline.points()[m_currentPointIndex];
303 switch (m_currentPointType) {
314 m_grabPOriginal = point;
315 if (m_currentPointIndex > 0)
316 m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1];
317 if (m_currentPointIndex < m_spline.points().count() - 1)
318 m_grabPNext = m_spline.points()[m_currentPointIndex + 1];
319 m_grabOffsetX = p.x() - x;
320 m_grabOffsetY = p.y() - y;
322 switch (m_currentPointType) {
324 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
327 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
330 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
332 m_spline.setPoint(m_currentPointIndex, point);
336 emit currentPoint(point);
340 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
342 if (event->button() != Qt::LeftButton)
345 setCursor(Qt::ArrowCursor);
351 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
353 int wWidth = width() - 1;
354 int wHeight = height() - 1;
355 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
356 wWidth -= 2 * offset;
357 wHeight -= 2 * offset;
359 double x = (event->pos().x() - offset) / (double)(wWidth);
360 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
362 if (m_mode == ModeNormal) {
363 // If no point is selected set the the cursor shape if on top
365 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
367 if (nearestPointIndex < 0)
368 setCursor(Qt::ArrowCursor);
370 setCursor(Qt::CrossCursor);
372 // Else, drag the selected point
373 setCursor(Qt::CrossCursor);
380 BPoint point = m_spline.points()[m_currentPointIndex];
381 switch (m_currentPointType) {
383 rightX = point.p.x();
384 if (m_currentPointIndex == 0)
387 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
389 x = qBound(leftX, x, rightX);
390 point.h1 = QPointF(x, y);
394 if (m_currentPointIndex == 0)
396 else if (m_currentPointIndex == m_spline.points().count() - 1)
399 x = qBound(leftX, x, rightX);
400 y = qBound(0., y, 1.);
402 // handles might have changed because we neared another point
404 point.h1 = m_grabPOriginal.h1;
405 point.h2 = m_grabPOriginal.h2;
406 // and then move them by same offset
407 point.h1 += QPointF(x, y) - m_grabPOriginal.p;
408 point.h2 += QPointF(x, y) - m_grabPOriginal.p;
410 point.p = QPointF(x, y);
415 if (m_currentPointIndex == m_spline.points().count() - 1)
418 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
420 x = qBound(leftX, x, rightX);
421 point.h2 = QPointF(x, y);
424 int index = m_currentPointIndex;
425 m_currentPointIndex = m_spline.setPoint(m_currentPointIndex, point);
427 if (m_currentPointType == PTypeP) {
428 // we might have changed the handles of other points
430 if (index == m_currentPointIndex) {
431 if (m_currentPointIndex > 0)
432 m_spline.setPoint(m_currentPointIndex - 1, m_grabPPrevious);
433 if (m_currentPointIndex < m_spline.points().count() -1)
434 m_spline.setPoint(m_currentPointIndex + 1, m_grabPNext);
436 if (m_currentPointIndex < index) {
437 m_spline.setPoint(index, m_grabPPrevious);
438 m_grabPNext = m_grabPPrevious;
439 if (m_currentPointIndex > 0)
440 m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1];
442 m_spline.setPoint(index, m_grabPNext);
443 m_grabPPrevious = m_grabPNext;
444 if (m_currentPointIndex < m_spline.points().count() - 1)
445 m_grabPNext = m_spline.points()[m_currentPointIndex + 1];
450 emit currentPoint(point);
455 void BezierSplineEditor::leaveEvent(QEvent* event)
457 QWidget::leaveEvent(event);
460 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
462 double nearestDistanceSquared = 1000;
463 point_types selectedPoint;
464 int nearestIndex = -1;
467 double distanceSquared;
468 // find out distance using the Pythagorean theorem
469 foreach(const BPoint & point, m_spline.points()) {
470 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
471 if (distanceSquared < nearestDistanceSquared) {
473 nearestDistanceSquared = distanceSquared;
474 selectedPoint = PTypeH1;
476 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
477 if (distanceSquared < nearestDistanceSquared) {
479 nearestDistanceSquared = distanceSquared;
480 selectedPoint = PTypeP;
482 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
483 if (distanceSquared < nearestDistanceSquared) {
485 nearestDistanceSquared = distanceSquared;
486 selectedPoint = PTypeH2;
491 if (nearestIndex >= 0) {
492 BPoint point = m_spline.points()[nearestIndex];
494 switch (selectedPoint) {
504 if (qAbs(p.x() - p2.x()) * wWidth < 5 && qAbs(p.y() - p2.y()) * wHeight < 5) {
505 *sel = selectedPoint;
513 #include "beziersplineeditor.moc"