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);
141 /*p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
142 p.drawLine(QLineF(0, wHeight, wWidth, 0));*/
144 p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
149 if (m_zoomLevel != 0) {
150 p.drawRect(offset, offset, wWidth, wHeight);
156 if (m_gridLines != 0) {
157 double stepH = wWidth / (double)(m_gridLines + 1);
158 double stepV = wHeight / (double)(m_gridLines + 1);
159 for (int i = 1; i <= m_gridLines; ++i) {
160 p.drawLine(QLineF(i * stepH, 0, i * stepH, wHeight).translated(offset, offset));
161 p.drawLine(QLineF(0, i * stepV, wWidth, i * stepV).translated(offset, offset));
168 double prevY = wHeight - m_spline.value(0.) * wHeight;
171 double normalizedX = -1;
174 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
175 for (x = 0 ; x < wWidth ; ++x) {
176 normalizedX = x / (double)wWidth;
177 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
180 * Keep in mind that QLineF rounds doubles
181 * to ints mathematically, not just rounds down
184 p.drawLine(QLineF(prevX, prevY,
185 x, curY).translated(offset, offset));
189 p.drawLine(QLineF(prevX, prevY ,
190 x, wHeight - m_spline.value(1.0, true) * wHeight).translated(offset, offset));
195 int max = m_spline.points().count() - 1;
196 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
204 for (int i = 0; i <= max; ++i) {
205 point = m_spline.points().at(i);
206 if (i == m_currentPointIndex) {
207 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
209 p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight).translated(offset, offset));
211 p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
214 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
215 wHeight - 3 - point.p.y() * wHeight, 6, 6).translated(offset, offset));
217 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight).translated(offset, offset));
219 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
221 if ( i == m_currentPointIndex)
222 p.setBrush(QBrush(Qt::NoBrush));
226 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
228 m_spline.setPrecision(width());
229 m_pixmapIsDirty = true;
230 QWidget::resizeEvent(event);
233 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
235 int wWidth = width() - 1;
236 int wHeight = height() - 1;
237 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
238 wWidth -= 2 * offset;
239 wHeight -= 2 * offset;
241 double x = (event->pos().x() - offset) / (double)(wWidth);
242 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
244 point_types selectedPoint;
245 int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
247 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
248 m_spline.removePoint(closestPointIndex);
249 setCursor(Qt::ArrowCursor);
251 if (closestPointIndex < m_currentPointIndex)
252 --m_currentPointIndex;
254 if (m_currentPointIndex >= 0)
255 emit currentPoint(m_spline.points()[m_currentPointIndex]);
257 emit currentPoint(BPoint());
260 } else if (event->button() != Qt::LeftButton) {
264 if (closestPointIndex < 0) {
266 po.p = QPointF(x, y);
267 po.h1 = QPointF(x-0.05, y-0.05);
268 po.h2 = QPointF(x+0.05, y+0.05);
269 m_currentPointIndex = m_spline.addPoint(po);
270 m_currentPointType = PTypeP;
271 /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
273 m_currentPointIndex = closestPointIndex;
274 m_currentPointType = selectedPoint;
277 BPoint point = m_spline.points()[m_currentPointIndex];
279 switch (m_currentPointType) {
290 m_grabOriginalX = p.x();
291 m_grabOriginalY = p.y();
292 m_grabOffsetX = p.x() - x;
293 m_grabOffsetY = p.y() - y;
295 switch (m_currentPointType) {
297 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
300 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
303 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
305 m_spline.setPoint(m_currentPointIndex, point);
307 //d->m_draggedAwayPointIndex = -1;
311 emit currentPoint(point);
315 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
317 if (event->button() != Qt::LeftButton)
320 setCursor(Qt::ArrowCursor);
326 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
328 int wWidth = width() - 1;
329 int wHeight = height() - 1;
330 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
331 wWidth -= 2 * offset;
332 wHeight -= 2 * offset;
334 double x = (event->pos().x() - offset) / (double)(wWidth);
335 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
337 if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
339 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
341 if (nearestPointIndex < 0)
342 setCursor(Qt::ArrowCursor);
344 setCursor(Qt::CrossCursor);
345 } else { // Else, drag the selected point
346 /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
347 event->pos().x() < -MOUSE_AWAY_THRES;
348 bool crossedVert = event->pos().y() - height() > MOUSE_AWAY_THRES ||
349 event->pos().y() < -MOUSE_AWAY_THRES;
351 bool removePoint = (crossedHoriz || crossedVert);
353 if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
354 // point is no longer dragged away so reinsert it
355 QPointF newPoint(d->m_draggedAwayPoint);
356 d->m_grab_point_index = d->m_curve.addPoint(newPoint);
357 d->m_draggedAwayPointIndex = -1;
361 (d->m_draggedAwayPointIndex >= 0))
365 setCursor(Qt::CrossCursor);
370 double leftX, rightX;
371 BPoint point = m_spline.points()[m_currentPointIndex];
372 switch (m_currentPointType) {
374 rightX = point.p.x();
375 if (m_currentPointIndex == 0)
378 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
379 x = qBound(leftX, x, rightX);
380 point.h1 = QPointF(x, y);
383 if (m_currentPointIndex == 0) {
386 /*if (d->m_curve.points().count() > 1)
387 * rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
390 } else if (m_currentPointIndex == m_spline.points().count() - 1) {
391 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
394 //// the 1E-4 addition so we can grab the dot later.
395 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
396 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
398 x = qBound(leftX, x, rightX);
399 y = qBound(0., y, 1.);
401 // move handles by same offset
402 point.h1 += QPointF(x, y) - point.p;
403 point.h2 += QPointF(x, y) - point.p;
405 point.p = QPointF(x, y);
409 if (m_currentPointIndex == m_spline.points().count() - 1)
412 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
413 x = qBound(leftX, x, rightX);
414 point.h2 = QPointF(x, y);
417 m_spline.setPoint(m_currentPointIndex, point);
419 /*if (removePoint && d->m_curve.points().count() > 2) {
420 d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
421 d->m_draggedAwayPointIndex = d->m_grab_point_index;
422 d->m_curve.removePoint(d->m_grab_point_index);
423 d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
426 d->setCurveModified();*/
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 foreach(const BPoint & point, m_spline.points()) {
446 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
447 if (distanceSquared < nearestDistanceSquared) {
449 nearestDistanceSquared = distanceSquared;
450 selectedPoint = PTypeH1;
452 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
453 if (distanceSquared < nearestDistanceSquared) {
455 nearestDistanceSquared = distanceSquared;
456 selectedPoint = PTypeP;
458 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
459 if (distanceSquared < nearestDistanceSquared) {
461 nearestDistanceSquared = distanceSquared;
462 selectedPoint = PTypeH2;
467 if (nearestIndex >= 0) {
468 BPoint point = m_spline.points()[nearestIndex];
470 switch (selectedPoint) {
480 if (qAbs(p.x() - p2.x()) * wWidth < 5 && qAbs(p.y() - p2.y()) * wHeight < 5) {
481 *sel = selectedPoint;
489 #include "beziersplineeditor.moc"