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;
273 /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
275 m_currentPointIndex = closestPointIndex;
276 m_currentPointType = selectedPoint;
279 BPoint point = m_spline.points()[m_currentPointIndex];
281 switch (m_currentPointType) {
292 m_grabOriginalX = p.x();
293 m_grabOriginalY = p.y();
294 m_grabOffsetX = p.x() - x;
295 m_grabOffsetY = p.y() - y;
297 switch (m_currentPointType) {
299 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
302 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
305 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
307 m_spline.setPoint(m_currentPointIndex, point);
309 //d->m_draggedAwayPointIndex = -1;
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) { // If no point is selected set the the cursor shape if on top
341 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
343 if (nearestPointIndex < 0)
344 setCursor(Qt::ArrowCursor);
346 setCursor(Qt::CrossCursor);
347 } else { // Else, drag the selected point
348 /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
349 event->pos().x() < -MOUSE_AWAY_THRES;
350 bool crossedVert = event->pos().y() - height() > MOUSE_AWAY_THRES ||
351 event->pos().y() < -MOUSE_AWAY_THRES;
353 bool removePoint = (crossedHoriz || crossedVert);
355 if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
356 // point is no longer dragged away so reinsert it
357 QPointF newPoint(d->m_draggedAwayPoint);
358 d->m_grab_point_index = d->m_curve.addPoint(newPoint);
359 d->m_draggedAwayPointIndex = -1;
363 (d->m_draggedAwayPointIndex >= 0))
367 setCursor(Qt::CrossCursor);
372 double leftX, rightX;
373 BPoint point = m_spline.points()[m_currentPointIndex];
374 switch (m_currentPointType) {
376 rightX = point.p.x();
377 if (m_currentPointIndex == 0)
380 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
381 x = qBound(leftX, x, rightX);
382 point.h1 = QPointF(x, y);
385 if (m_currentPointIndex == 0) {
388 /*if (d->m_curve.points().count() > 1)
389 * rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
392 } else if (m_currentPointIndex == m_spline.points().count() - 1) {
393 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
396 //// the 1E-4 addition so we can grab the dot later.
397 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
398 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
400 x = qBound(leftX, x, rightX);
401 y = qBound(0., y, 1.);
403 // move handles by same offset
404 point.h1 += QPointF(x, y) - point.p;
405 point.h2 += QPointF(x, y) - point.p;
407 point.p = QPointF(x, y);
411 if (m_currentPointIndex == m_spline.points().count() - 1)
414 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
415 x = qBound(leftX, x, rightX);
416 point.h2 = QPointF(x, y);
419 m_spline.setPoint(m_currentPointIndex, point);
421 /*if (removePoint && d->m_curve.points().count() > 2) {
422 d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
423 d->m_draggedAwayPointIndex = d->m_grab_point_index;
424 d->m_curve.removePoint(d->m_grab_point_index);
425 d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
428 d->setCurveModified();*/
429 emit currentPoint(point);
434 void BezierSplineEditor::leaveEvent(QEvent* event)
436 QWidget::leaveEvent(event);
439 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
441 double nearestDistanceSquared = 1000;
442 point_types selectedPoint;
443 int nearestIndex = -1;
446 double distanceSquared;
447 foreach(const BPoint & point, m_spline.points()) {
448 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
449 if (distanceSquared < nearestDistanceSquared) {
451 nearestDistanceSquared = distanceSquared;
452 selectedPoint = PTypeH1;
454 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
455 if (distanceSquared < nearestDistanceSquared) {
457 nearestDistanceSquared = distanceSquared;
458 selectedPoint = PTypeP;
460 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
461 if (distanceSquared < nearestDistanceSquared) {
463 nearestDistanceSquared = distanceSquared;
464 selectedPoint = PTypeH2;
469 if (nearestIndex >= 0) {
470 BPoint point = m_spline.points()[nearestIndex];
472 switch (selectedPoint) {
482 if (qAbs(p.x() - p2.x()) * wWidth < 5 && qAbs(p.y() - p2.y()) * wHeight < 5) {
483 *sel = selectedPoint;
491 #include "beziersplineeditor.moc"