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;
59 emit currentPoint(BPoint());
64 BPoint BezierSplineEditor::getCurrentPoint()
66 if (m_currentPointIndex >= 0)
67 return m_spline.points()[m_currentPointIndex];
72 void BezierSplineEditor::updateCurrentPoint(const BPoint& p)
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.points()[m_currentPointIndex]);
83 void BezierSplineEditor::setPixmap(const QPixmap& pixmap)
86 m_pixmapIsDirty = true;
90 void BezierSplineEditor::slotZoomIn()
92 m_zoomLevel = qMax(m_zoomLevel-1, 0);
93 m_pixmapIsDirty = true;
97 void BezierSplineEditor::slotZoomOut()
99 m_zoomLevel = qMin(m_zoomLevel+1, 3);
100 m_pixmapIsDirty = true;
104 int BezierSplineEditor::gridLines()
109 void BezierSplineEditor::setGridLines(int lines)
111 m_gridLines = qBound(0, lines, 8);
115 void BezierSplineEditor::paintEvent(QPaintEvent* event)
121 int wWidth = width() - 1;
122 int wHeight = height() - 1;
123 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
124 wWidth -= 2 * offset;
125 wHeight -= 2 * offset;
127 p.translate(offset, offset);
132 p.fillRect(rect().translated(-offset, -offset), palette().background());
133 if (!m_pixmap.isNull()) {
134 if (m_pixmapIsDirty || !m_pixmapCache) {
136 delete m_pixmapCache;
137 m_pixmapCache = new QPixmap(wWidth + 1, wHeight + 1);
138 QPainter cachePainter(m_pixmapCache);
140 cachePainter.scale(1.0*(wWidth+1) / m_pixmap.width(), 1.0*(wHeight+1) / m_pixmap.height());
141 cachePainter.drawPixmap(0, 0, m_pixmap);
142 m_pixmapIsDirty = false;
144 p.drawPixmap(0, 0, *m_pixmapCache);
148 p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
153 if (m_zoomLevel != 0) {
154 p.drawRect(0, 0, wWidth, wHeight);
160 if (m_gridLines != 0) {
161 double stepH = wWidth / (double)(m_gridLines + 1);
162 double stepV = wHeight / (double)(m_gridLines + 1);
163 for (int i = 1; i <= m_gridLines; ++i) {
164 p.drawLine(QLineF(i * stepH, 0, i * stepH, wHeight));
165 p.drawLine(QLineF(0, i * stepV, wWidth, i * stepV));
169 p.setRenderHint(QPainter::Antialiasing);
174 p.drawLine(QLineF(0, wHeight, wWidth, 0));
179 double prevY = wHeight - m_spline.value(0.) * wHeight;
182 double normalizedX = -1;
185 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
186 for (x = 0 ; x < wWidth ; ++x) {
187 normalizedX = x / (double)wWidth;
188 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
191 * Keep in mind that QLineF rounds doubles
192 * to ints mathematically, not just rounds down
195 p.drawLine(QLineF(prevX, prevY, x, curY));
199 p.drawLine(QLineF(prevX, prevY ,
200 x, wHeight - m_spline.value(1.0, true) * wHeight));
205 int max = m_spline.points().count() - 1;
206 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
214 #if QT_VERSION < 0x040600
217 for (int i = 0; i <= max; ++i) {
218 point = m_spline.points().at(i);
219 if (i == m_currentPointIndex) {
220 // selected point: fill p and handles
221 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
222 // connect p and handles with lines
224 p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight));
226 p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
229 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
230 wHeight - 3 - point.p.y() * wHeight, 6, 6));
232 #if QT_VERSION >= 0x040600
233 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight));
236 tmp.translate(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight);
237 p.drawConvexPolygon(tmp);
241 #if QT_VERSION >= 0x040600
242 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
245 tmp.translate(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight);
246 p.drawConvexPolygon(tmp);
250 if ( i == m_currentPointIndex)
251 p.setBrush(QBrush(Qt::NoBrush));
255 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
257 m_spline.setPrecision(width() > height() ? width() : height());
258 m_pixmapIsDirty = true;
259 QWidget::resizeEvent(event);
262 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
264 int wWidth = width() - 1;
265 int wHeight = height() - 1;
266 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
267 wWidth -= 2 * offset;
268 wHeight -= 2 * offset;
270 double x = (event->pos().x() - offset) / (double)(wWidth);
271 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
273 point_types selectedPoint;
274 int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
276 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
277 m_spline.removePoint(closestPointIndex);
278 setCursor(Qt::ArrowCursor);
280 if (closestPointIndex < m_currentPointIndex)
281 --m_currentPointIndex;
283 if (m_currentPointIndex >= 0)
284 emit currentPoint(m_spline.points()[m_currentPointIndex]);
286 emit currentPoint(BPoint());
289 } else if (event->button() != Qt::LeftButton) {
293 if (closestPointIndex < 0) {
294 m_currentPointIndex = m_spline.addPoint(BPoint(QPointF(x-0.05, y-0.05),
296 QPointF(x+0.05, y+0.05)));
297 m_currentPointType = PTypeP;
299 m_currentPointIndex = closestPointIndex;
300 m_currentPointType = selectedPoint;
303 BPoint point = m_spline.points()[m_currentPointIndex];
305 m_grabPOriginal = point;
306 if (m_currentPointIndex > 0)
307 m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1];
308 if (m_currentPointIndex < m_spline.points().count() - 1)
309 m_grabPNext = m_spline.points()[m_currentPointIndex + 1];
310 m_grabOffsetX = point[(int)m_currentPointType].x() - x;
311 m_grabOffsetY = point[(int)m_currentPointType].y() - y;
313 point[(int)m_currentPointType] = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
315 m_spline.setPoint(m_currentPointIndex, point);
319 emit currentPoint(point);
323 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
325 if (event->button() != Qt::LeftButton)
328 setCursor(Qt::ArrowCursor);
334 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
336 int wWidth = width() - 1;
337 int wHeight = height() - 1;
338 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
339 wWidth -= 2 * offset;
340 wHeight -= 2 * offset;
342 double x = (event->pos().x() - offset) / (double)(wWidth);
343 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
345 if (m_mode == ModeNormal) {
346 // If no point is selected set the the cursor shape if on top
348 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
350 if (nearestPointIndex < 0)
351 setCursor(Qt::ArrowCursor);
353 setCursor(Qt::CrossCursor);
355 // Else, drag the selected point
356 setCursor(Qt::CrossCursor);
363 BPoint point = m_spline.points()[m_currentPointIndex];
364 switch (m_currentPointType) {
366 rightX = point.p.x();
367 if (m_currentPointIndex == 0)
370 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
372 x = qBound(leftX, x, rightX);
373 point.setH1(QPointF(x, y));
377 if (m_currentPointIndex == 0)
379 else if (m_currentPointIndex == m_spline.points().count() - 1)
382 x = qBound(leftX, x, rightX);
383 y = qBound(0., y, 1.);
385 // handles might have changed because we neared another point
387 point.h1 = m_grabPOriginal.h1;
388 point.h2 = m_grabPOriginal.h2;
389 // and move by same offset
390 // (using update handle in point.setP won't work because the offset between new and old point is very small)
391 point.h1 += QPointF(x, y) - m_grabPOriginal.p;
392 point.h2 += QPointF(x, y) - m_grabPOriginal.p;
394 point.setP(QPointF(x, y), false);
399 if (m_currentPointIndex == m_spline.points().count() - 1)
402 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
404 x = qBound(leftX, x, rightX);
405 point.setH2(QPointF(x, y));
408 int index = m_currentPointIndex;
409 m_currentPointIndex = m_spline.setPoint(m_currentPointIndex, point);
411 if (m_currentPointType == PTypeP) {
412 // we might have changed the handles of other points
414 if (index == m_currentPointIndex) {
415 if (m_currentPointIndex > 0)
416 m_spline.setPoint(m_currentPointIndex - 1, m_grabPPrevious);
417 if (m_currentPointIndex < m_spline.points().count() -1)
418 m_spline.setPoint(m_currentPointIndex + 1, m_grabPNext);
420 if (m_currentPointIndex < index) {
421 m_spline.setPoint(index, m_grabPPrevious);
422 m_grabPNext = m_grabPPrevious;
423 if (m_currentPointIndex > 0)
424 m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1];
426 m_spline.setPoint(index, m_grabPNext);
427 m_grabPPrevious = m_grabPNext;
428 if (m_currentPointIndex < m_spline.points().count() - 1)
429 m_grabPNext = m_spline.points()[m_currentPointIndex + 1];
434 emit currentPoint(point);
439 void BezierSplineEditor::leaveEvent(QEvent* event)
441 QWidget::leaveEvent(event);
444 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
446 double nearestDistanceSquared = 1000;
447 point_types selectedPoint;
448 int nearestIndex = -1;
451 double distanceSquared;
452 // find out distance using the Pythagorean theorem
453 foreach(const BPoint & point, m_spline.points()) {
454 for (int j = 0; j < 3; ++j) {
455 distanceSquared = pow(point[j].x() - p.x(), 2) + pow(point[j].y() - p.y(), 2);
456 if (distanceSquared < nearestDistanceSquared) {
458 nearestDistanceSquared = distanceSquared;
459 selectedPoint = (point_types)j;
465 if (nearestIndex >= 0) {
466 BPoint point = m_spline.points()[nearestIndex];
467 if (qAbs(p.x() - point[(int)selectedPoint].x()) * wWidth < 5 && qAbs(p.y() - point[(int)selectedPoint].y()) * wHeight < 5) {
468 *sel = selectedPoint;
476 #include "beziersplineeditor.moc"