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)
55 m_spline.fromString(spline.toString());
58 BPoint BezierSplineEditor::getCurrentPoint()
60 if (m_currentPointIndex >= 0)
61 return m_spline.points()[m_currentPointIndex];
66 void BezierSplineEditor::updateCurrentPoint(const BPoint& p)
68 if (m_currentPointIndex >= 0) {
69 m_spline.setPoint(m_currentPointIndex, p);
70 // during validation the point might have changed
71 emit currentPoint(m_spline.points()[m_currentPointIndex]);
76 void BezierSplineEditor::setPixmap(const QPixmap& pixmap)
79 m_pixmapIsDirty = true;
83 void BezierSplineEditor::slotZoomIn()
85 m_zoomLevel = qMax(m_zoomLevel-1, 0);
86 m_pixmapIsDirty = true;
90 void BezierSplineEditor::slotZoomOut()
92 m_zoomLevel = qMin(m_zoomLevel+1, 3);
93 m_pixmapIsDirty = true;
97 int BezierSplineEditor::gridLines()
102 void BezierSplineEditor::setGridLines(int lines)
104 m_gridLines = qBound(0, lines, 8);
108 void BezierSplineEditor::paintEvent(QPaintEvent* event)
114 int wWidth = width() - 1;
115 int wHeight = height() - 1;
116 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
117 wWidth -= 2 * offset;
118 wHeight -= 2 * offset;
123 p.fillRect(rect(), palette().background());
124 if (!m_pixmap.isNull()) {
125 if (m_pixmapIsDirty || !m_pixmapCache) {
127 delete m_pixmapCache;
128 m_pixmapCache = new QPixmap(wWidth + 1, wHeight + 1);
129 QPainter cachePainter(m_pixmapCache);
131 cachePainter.scale(1.0*(wWidth+1) / m_pixmap.width(), 1.0*(wHeight+1) / m_pixmap.height());
132 cachePainter.drawPixmap(0, 0, m_pixmap);
133 m_pixmapIsDirty = false;
135 p.drawPixmap(offset, offset, *m_pixmapCache);
139 p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
144 if (m_zoomLevel != 0) {
145 p.drawRect(offset, offset, wWidth, wHeight);
151 if (m_gridLines != 0) {
152 double stepH = wWidth / (double)(m_gridLines + 1);
153 double stepV = wHeight / (double)(m_gridLines + 1);
154 for (int i = 1; i <= m_gridLines; ++i) {
155 p.drawLine(QLineF(i * stepH, 0, i * stepH, wHeight).translated(offset, offset));
156 p.drawLine(QLineF(0, i * stepV, wWidth, i * stepV).translated(offset, offset));
160 p.setRenderHint(QPainter::Antialiasing);
165 p.drawLine(QLineF(0, wHeight, wWidth, 0).translated(offset, offset));
170 double prevY = wHeight - m_spline.value(0.) * wHeight;
173 double normalizedX = -1;
176 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
177 for (x = 0 ; x < wWidth ; ++x) {
178 normalizedX = x / (double)wWidth;
179 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
182 * Keep in mind that QLineF rounds doubles
183 * to ints mathematically, not just rounds down
186 p.drawLine(QLineF(prevX, prevY,
187 x, curY).translated(offset, offset));
191 p.drawLine(QLineF(prevX, prevY ,
192 x, wHeight - m_spline.value(1.0, true) * wHeight).translated(offset, offset));
197 int max = m_spline.points().count() - 1;
198 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
206 for (int i = 0; i <= max; ++i) {
207 point = m_spline.points().at(i);
208 if (i == m_currentPointIndex) {
209 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
211 p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight).translated(offset, offset));
213 p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
216 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
217 wHeight - 3 - point.p.y() * wHeight, 6, 6).translated(offset, offset));
219 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight).translated(offset, offset));
221 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
223 if ( i == m_currentPointIndex)
224 p.setBrush(QBrush(Qt::NoBrush));
228 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
230 m_spline.setPrecision(width());
231 m_pixmapIsDirty = true;
232 QWidget::resizeEvent(event);
235 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
237 int wWidth = width() - 1;
238 int wHeight = height() - 1;
239 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
240 wWidth -= 2 * offset;
241 wHeight -= 2 * offset;
243 double x = (event->pos().x() - offset) / (double)(wWidth);
244 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
246 point_types selectedPoint;
247 int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
249 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
250 m_spline.removePoint(closestPointIndex);
251 setCursor(Qt::ArrowCursor);
253 if (closestPointIndex < m_currentPointIndex)
254 --m_currentPointIndex;
256 if (m_currentPointIndex >= 0)
257 emit currentPoint(m_spline.points()[m_currentPointIndex]);
259 emit currentPoint(BPoint());
262 } else if (event->button() != Qt::LeftButton) {
266 if (closestPointIndex < 0) {
268 po.p = QPointF(x, y);
269 po.h1 = QPointF(x-0.05, y-0.05);
270 po.h2 = QPointF(x+0.05, y+0.05);
271 m_currentPointIndex = m_spline.addPoint(po);
272 m_currentPointType = PTypeP;
274 m_currentPointIndex = closestPointIndex;
275 m_currentPointType = selectedPoint;
278 BPoint point = m_spline.points()[m_currentPointIndex];
280 switch (m_currentPointType) {
291 m_grabPOriginal = point;
292 if (m_currentPointIndex > 0)
293 m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1];
294 if (m_currentPointIndex < m_spline.points().count() - 1)
295 m_grabPNext = m_spline.points()[m_currentPointIndex + 1];
296 m_grabOffsetX = p.x() - x;
297 m_grabOffsetY = p.y() - y;
299 switch (m_currentPointType) {
301 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
304 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
307 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
309 m_spline.setPoint(m_currentPointIndex, point);
313 emit currentPoint(point);
317 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
319 if (event->button() != Qt::LeftButton)
322 setCursor(Qt::ArrowCursor);
328 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
330 int wWidth = width() - 1;
331 int wHeight = height() - 1;
332 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
333 wWidth -= 2 * offset;
334 wHeight -= 2 * offset;
336 double x = (event->pos().x() - offset) / (double)(wWidth);
337 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
339 if (m_mode == ModeNormal) {
340 // If no point is selected set the the cursor shape if on top
342 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
344 if (nearestPointIndex < 0)
345 setCursor(Qt::ArrowCursor);
347 setCursor(Qt::CrossCursor);
349 // Else, drag the selected point
350 setCursor(Qt::CrossCursor);
357 BPoint point = m_spline.points()[m_currentPointIndex];
358 switch (m_currentPointType) {
360 rightX = point.p.x();
361 if (m_currentPointIndex == 0)
364 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
366 x = qBound(leftX, x, rightX);
367 point.h1 = QPointF(x, y);
371 if (m_currentPointIndex == 0)
373 else if (m_currentPointIndex == m_spline.points().count() - 1)
376 x = qBound(leftX, x, rightX);
377 y = qBound(0., y, 1.);
379 // handles might have changed because we neared another point
381 point.h1 = m_grabPOriginal.h1;
382 point.h2 = m_grabPOriginal.h2;
383 // and then move them by same offset
384 point.h1 += QPointF(x, y) - m_grabPOriginal.p;
385 point.h2 += QPointF(x, y) - m_grabPOriginal.p;
387 point.p = QPointF(x, y);
392 if (m_currentPointIndex == m_spline.points().count() - 1)
395 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
397 x = qBound(leftX, x, rightX);
398 point.h2 = QPointF(x, y);
401 int index = m_currentPointIndex;
402 m_currentPointIndex = m_spline.setPoint(m_currentPointIndex, point);
404 if (m_currentPointType == PTypeP) {
405 // we might have changed the handles of other points
407 if (index == m_currentPointIndex) {
408 if (m_currentPointIndex > 0)
409 m_spline.setPoint(m_currentPointIndex - 1, m_grabPPrevious);
410 if (m_currentPointIndex < m_spline.points().count() -1)
411 m_spline.setPoint(m_currentPointIndex + 1, m_grabPNext);
413 if (m_currentPointIndex < index) {
414 m_spline.setPoint(index, m_grabPPrevious);
415 m_grabPNext = m_grabPPrevious;
416 if (m_currentPointIndex > 0)
417 m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1];
419 m_spline.setPoint(index, m_grabPNext);
420 m_grabPPrevious = m_grabPNext;
421 if (m_currentPointIndex < m_spline.points().count() - 1)
422 m_grabPNext = m_spline.points()[m_currentPointIndex + 1];
427 emit currentPoint(point);
432 void BezierSplineEditor::leaveEvent(QEvent* event)
434 QWidget::leaveEvent(event);
437 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
439 double nearestDistanceSquared = 1000;
440 point_types selectedPoint;
441 int nearestIndex = -1;
444 double distanceSquared;
445 // find out distance using the Pythagorean theorem
446 foreach(const BPoint & point, m_spline.points()) {
447 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
448 if (distanceSquared < nearestDistanceSquared) {
450 nearestDistanceSquared = distanceSquared;
451 selectedPoint = PTypeH1;
453 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
454 if (distanceSquared < nearestDistanceSquared) {
456 nearestDistanceSquared = distanceSquared;
457 selectedPoint = PTypeP;
459 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
460 if (distanceSquared < nearestDistanceSquared) {
462 nearestDistanceSquared = distanceSquared;
463 selectedPoint = PTypeH2;
468 if (nearestIndex >= 0) {
469 BPoint point = m_spline.points()[nearestIndex];
471 switch (selectedPoint) {
481 if (qAbs(p.x() - p2.x()) * wWidth < 5 && qAbs(p.y() - p2.y()) * wHeight < 5) {
482 *sel = selectedPoint;
490 #include "beziersplineeditor.moc"