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) :
30 m_showAllHandles(true),
32 m_pixmapIsDirty(true),
33 m_currentPointIndex(-1)
35 setMouseTracking(true);
36 setAutoFillBackground(false);
37 setAttribute(Qt::WA_OpaquePaintEvent);
38 setMinimumSize(150, 150);
39 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
42 BezierSplineEditor::~BezierSplineEditor()
48 CubicBezierSpline BezierSplineEditor::spline()
53 void BezierSplineEditor::setSpline(const CubicBezierSpline& spline)
55 int precision = m_spline.getPrecision();
57 m_spline.setPrecision(precision);
58 m_currentPointIndex = -1;
60 emit currentPoint(BPoint());
65 BPoint BezierSplineEditor::getCurrentPoint()
67 if (m_currentPointIndex >= 0)
68 return m_spline.points()[m_currentPointIndex];
73 void BezierSplineEditor::updateCurrentPoint(const BPoint& p)
75 if (m_currentPointIndex >= 0) {
76 m_spline.setPoint(m_currentPointIndex, p);
77 // during validation the point might have changed
78 emit currentPoint(m_spline.points()[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)
128 int wWidth = width() - 1;
129 int wHeight = height() - 1;
130 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
131 wWidth -= 2 * offset;
132 wHeight -= 2 * offset;
134 p.translate(offset, offset);
139 p.fillRect(rect().translated(-offset, -offset), palette().background());
140 if (!m_pixmap.isNull()) {
141 if (m_pixmapIsDirty || !m_pixmapCache) {
143 delete m_pixmapCache;
144 m_pixmapCache = new QPixmap(wWidth + 1, wHeight + 1);
145 QPainter cachePainter(m_pixmapCache);
147 cachePainter.scale(1.0*(wWidth+1) / m_pixmap.width(), 1.0*(wHeight+1) / m_pixmap.height());
148 cachePainter.drawPixmap(0, 0, m_pixmap);
149 m_pixmapIsDirty = false;
151 p.drawPixmap(0, 0, *m_pixmapCache);
155 p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
160 if (m_zoomLevel != 0) {
161 p.drawRect(0, 0, wWidth, wHeight);
167 if (m_gridLines != 0) {
168 double stepH = wWidth / (double)(m_gridLines + 1);
169 double stepV = wHeight / (double)(m_gridLines + 1);
170 for (int i = 1; i <= m_gridLines; ++i) {
171 p.drawLine(QLineF(i * stepH, 0, i * stepH, wHeight));
172 p.drawLine(QLineF(0, i * stepV, wWidth, i * stepV));
176 p.setRenderHint(QPainter::Antialiasing);
181 p.drawLine(QLineF(0, wHeight, wWidth, 0));
186 double prevY = wHeight - m_spline.value(0.) * wHeight;
189 double normalizedX = -1;
192 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
193 for (x = 0 ; x < wWidth ; ++x) {
194 normalizedX = x / (double)wWidth;
195 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
198 * Keep in mind that QLineF rounds doubles
199 * to ints mathematically, not just rounds down
202 p.drawLine(QLineF(prevX, prevY, x, curY));
206 p.drawLine(QLineF(prevX, prevY ,
207 x, wHeight - m_spline.value(1.0, true) * wHeight));
212 int max = m_spline.points().count() - 1;
213 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
221 #if QT_VERSION < 0x040600
224 for (int i = 0; i <= max; ++i) {
225 point = m_spline.points().at(i);
226 if (i == m_currentPointIndex) {
227 // selected point: fill p and handles
228 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
229 // connect p and handles with lines
231 p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight));
233 p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
236 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
237 wHeight - 3 - point.p.y() * wHeight, 6, 6));
238 if (i != 0 && (i == m_currentPointIndex || m_showAllHandles)) {
239 #if QT_VERSION >= 0x040600
240 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight));
243 tmp.translate(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight);
244 p.drawConvexPolygon(tmp);
247 if (i != max && (i == m_currentPointIndex || m_showAllHandles)) {
248 #if QT_VERSION >= 0x040600
249 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
252 tmp.translate(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight);
253 p.drawConvexPolygon(tmp);
257 if ( i == m_currentPointIndex)
258 p.setBrush(QBrush(Qt::NoBrush));
262 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
264 m_spline.setPrecision(width() > height() ? width() : height());
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 offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
274 wWidth -= 2 * offset;
275 wHeight -= 2 * offset;
277 double x = (event->pos().x() - offset) / (double)(wWidth);
278 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
280 point_types selectedPoint;
281 int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
283 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
284 m_spline.removePoint(closestPointIndex);
285 setCursor(Qt::ArrowCursor);
287 if (closestPointIndex < m_currentPointIndex)
288 --m_currentPointIndex;
290 if (m_currentPointIndex >= 0)
291 emit currentPoint(m_spline.points()[m_currentPointIndex]);
293 emit currentPoint(BPoint());
296 } else if (event->button() != Qt::LeftButton) {
300 if (closestPointIndex < 0) {
301 m_currentPointIndex = m_spline.addPoint(BPoint(QPointF(x-0.05, y-0.05),
303 QPointF(x+0.05, y+0.05)));
304 m_currentPointType = PTypeP;
306 m_currentPointIndex = closestPointIndex;
307 m_currentPointType = selectedPoint;
310 BPoint point = m_spline.points()[m_currentPointIndex];
312 m_grabPOriginal = point;
313 if (m_currentPointIndex > 0)
314 m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1];
315 if (m_currentPointIndex < m_spline.points().count() - 1)
316 m_grabPNext = m_spline.points()[m_currentPointIndex + 1];
317 m_grabOffsetX = point[(int)m_currentPointType].x() - x;
318 m_grabOffsetY = point[(int)m_currentPointType].y() - y;
320 point[(int)m_currentPointType] = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
322 m_spline.setPoint(m_currentPointIndex, point);
326 emit currentPoint(point);
330 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
332 if (event->button() != Qt::LeftButton)
335 setCursor(Qt::ArrowCursor);
341 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
343 int wWidth = width() - 1;
344 int wHeight = height() - 1;
345 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
346 wWidth -= 2 * offset;
347 wHeight -= 2 * offset;
349 double x = (event->pos().x() - offset) / (double)(wWidth);
350 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
352 if (m_mode == ModeNormal) {
353 // If no point is selected set the the cursor shape if on top
355 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
357 if (nearestPointIndex < 0)
358 setCursor(Qt::ArrowCursor);
360 setCursor(Qt::CrossCursor);
362 // Else, drag the selected point
363 setCursor(Qt::CrossCursor);
370 BPoint point = m_spline.points()[m_currentPointIndex];
371 switch (m_currentPointType) {
373 rightX = point.p.x();
374 if (m_currentPointIndex == 0)
377 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
379 x = qBound(leftX, x, rightX);
380 point.setH1(QPointF(x, y));
384 if (m_currentPointIndex == 0)
386 else if (m_currentPointIndex == m_spline.points().count() - 1)
389 x = qBound(leftX, x, rightX);
390 y = qBound(0., y, 1.);
392 // handles might have changed because we neared another point
394 point.h1 = m_grabPOriginal.h1;
395 point.h2 = m_grabPOriginal.h2;
396 // and move by same offset
397 // (using update handle in point.setP won't work because the offset between new and old point is very small)
398 point.h1 += QPointF(x, y) - m_grabPOriginal.p;
399 point.h2 += QPointF(x, y) - m_grabPOriginal.p;
401 point.setP(QPointF(x, y), false);
406 if (m_currentPointIndex == m_spline.points().count() - 1)
409 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
411 x = qBound(leftX, x, rightX);
412 point.setH2(QPointF(x, y));
415 int index = m_currentPointIndex;
416 m_currentPointIndex = m_spline.setPoint(m_currentPointIndex, point);
418 if (m_currentPointType == PTypeP) {
419 // we might have changed the handles of other points
421 if (index == m_currentPointIndex) {
422 if (m_currentPointIndex > 0)
423 m_spline.setPoint(m_currentPointIndex - 1, m_grabPPrevious);
424 if (m_currentPointIndex < m_spline.points().count() -1)
425 m_spline.setPoint(m_currentPointIndex + 1, m_grabPNext);
427 if (m_currentPointIndex < index) {
428 m_spline.setPoint(index, m_grabPPrevious);
429 m_grabPNext = m_grabPPrevious;
430 if (m_currentPointIndex > 0)
431 m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1];
433 m_spline.setPoint(index, m_grabPNext);
434 m_grabPPrevious = m_grabPNext;
435 if (m_currentPointIndex < m_spline.points().count() - 1)
436 m_grabPNext = m_spline.points()[m_currentPointIndex + 1];
441 emit currentPoint(point);
446 void BezierSplineEditor::leaveEvent(QEvent* event)
448 QWidget::leaveEvent(event);
451 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
453 double nearestDistanceSquared = 1000;
454 point_types selectedPoint;
455 int nearestIndex = -1;
458 double distanceSquared;
459 // find out distance using the Pythagorean theorem
460 foreach(const BPoint & point, m_spline.points()) {
461 for (int j = 0; j < 3; ++j) {
462 distanceSquared = pow(point[j].x() - p.x(), 2) + pow(point[j].y() - p.y(), 2);
463 if (distanceSquared < nearestDistanceSquared) {
465 nearestDistanceSquared = distanceSquared;
466 selectedPoint = (point_types)j;
472 if (nearestIndex >= 0 && (nearestIndex == m_currentPointIndex || selectedPoint == PTypeP || m_showAllHandles)) {
473 // a point was found and it is not a hidden handle
474 BPoint point = m_spline.points()[nearestIndex];
475 if (qAbs(p.x() - point[(int)selectedPoint].x()) * wWidth < 5 && qAbs(p.y() - point[(int)selectedPoint].y()) * wHeight < 5) {
476 *sel = selectedPoint;
484 #include "beziersplineeditor.moc"