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_currentPointIndex(-1)
32 setMouseTracking(true);
33 setAutoFillBackground(false);
34 setAttribute(Qt::WA_OpaquePaintEvent);
35 setMinimumSize(150, 150);
36 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
39 CubicBezierSpline BezierSplineEditor::spline()
44 void BezierSplineEditor::setSpline(const CubicBezierSpline& spline)
47 m_spline.fromString(spline.toString());
50 BPoint BezierSplineEditor::getCurrentPoint()
52 if (m_currentPointIndex >= 0)
53 return m_spline.points()[m_currentPointIndex];
58 void BezierSplineEditor::updateCurrentPoint(const BPoint& p)
60 if (m_currentPointIndex >= 0) {
61 m_spline.setPoint(m_currentPointIndex, p);
62 // during validation the point might have changed
63 emit currentPoint(m_spline.points()[m_currentPointIndex]);
68 void BezierSplineEditor::slotZoomIn()
70 m_zoomLevel = qMax(m_zoomLevel-1, 0);
74 void BezierSplineEditor::slotZoomOut()
76 m_zoomLevel = qMin(m_zoomLevel+1, 3);
80 void BezierSplineEditor::slotGridChange()
82 m_gridLines = (m_gridLines + 1) % 9;
86 void BezierSplineEditor::paintEvent(QPaintEvent* event)
92 p.fillRect(rect(), palette().background());
94 int wWidth = width() - 1;
95 int wHeight = height() - 1;
96 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
98 wHeight -= 2 * offset;
103 /*p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
104 p.drawLine(QLineF(0, wHeight, wWidth, 0));*/
106 p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
111 if (m_zoomLevel != 0) {
112 p.drawRect(offset, offset, wWidth, wHeight);
118 if (m_gridLines != 0) {
119 double stepH = wWidth / (double)(m_gridLines + 1);
120 double stepV = wHeight / (double)(m_gridLines + 1);
121 for (int i = 1; i <= m_gridLines; ++i) {
122 p.drawLine(QLineF(i * stepH, 0, i * stepH, wHeight).translated(offset, offset));
123 p.drawLine(QLineF(0, i * stepV, wWidth, i * stepV).translated(offset, offset));
130 double prevY = wHeight - m_spline.value(0.) * wHeight;
133 double normalizedX = -1;
136 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
137 for (x = 0 ; x < wWidth ; ++x) {
138 normalizedX = x / (double)wWidth;
139 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
142 * Keep in mind that QLineF rounds doubles
143 * to ints mathematically, not just rounds down
146 p.drawLine(QLineF(prevX, prevY,
147 x, curY).translated(offset, offset));
151 p.drawLine(QLineF(prevX, prevY ,
152 x, wHeight - m_spline.value(1.0, true) * wHeight).translated(offset, offset));
157 int max = m_spline.points().count() - 1;
158 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
166 for (int i = 0; i <= max; ++i) {
167 point = m_spline.points().at(i);
168 if (i == m_currentPointIndex) {
169 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
171 p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight).translated(offset, offset));
173 p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
176 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
177 wHeight - 3 - point.p.y() * wHeight, 6, 6).translated(offset, offset));
179 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight).translated(offset, offset));
181 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
183 if ( i == m_currentPointIndex)
184 p.setBrush(QBrush(Qt::NoBrush));
188 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
190 m_spline.setPrecision(width());
191 QWidget::resizeEvent(event);
194 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
196 int wWidth = width() - 1;
197 int wHeight = height() - 1;
198 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
199 wWidth -= 2 * offset;
200 wHeight -= 2 * offset;
202 double x = (event->pos().x() - offset) / (double)(wWidth);
203 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
205 point_types selectedPoint;
206 int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
208 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
209 m_spline.removePoint(closestPointIndex);
210 setCursor(Qt::ArrowCursor);
212 if (closestPointIndex < m_currentPointIndex)
213 --m_currentPointIndex;
215 if (m_currentPointIndex >= 0)
216 emit currentPoint(m_spline.points()[m_currentPointIndex]);
218 emit currentPoint(BPoint());
221 } else if (event->button() != Qt::LeftButton) {
225 if (closestPointIndex < 0) {
227 po.p = QPointF(x, y);
228 po.h1 = QPointF(x-0.05, y-0.05);
229 po.h2 = QPointF(x+0.05, y+0.05);
230 m_currentPointIndex = m_spline.addPoint(po);
231 m_currentPointType = PTypeP;
232 /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
234 m_currentPointIndex = closestPointIndex;
235 m_currentPointType = selectedPoint;
238 BPoint point = m_spline.points()[m_currentPointIndex];
240 switch (m_currentPointType) {
251 m_grabOriginalX = p.x();
252 m_grabOriginalY = p.y();
253 m_grabOffsetX = p.x() - x;
254 m_grabOffsetY = p.y() - y;
256 switch (m_currentPointType) {
258 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
261 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
264 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
266 m_spline.setPoint(m_currentPointIndex, point);
268 //d->m_draggedAwayPointIndex = -1;
272 emit currentPoint(point);
276 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
278 if (event->button() != Qt::LeftButton)
281 setCursor(Qt::ArrowCursor);
287 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
289 int wWidth = width() - 1;
290 int wHeight = height() - 1;
291 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
292 wWidth -= 2 * offset;
293 wHeight -= 2 * offset;
295 double x = (event->pos().x() - offset) / (double)(wWidth);
296 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
298 if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
300 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
302 if (nearestPointIndex < 0)
303 setCursor(Qt::ArrowCursor);
305 setCursor(Qt::CrossCursor);
306 } else { // Else, drag the selected point
307 /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
308 event->pos().x() < -MOUSE_AWAY_THRES;
309 bool crossedVert = event->pos().y() - height() > MOUSE_AWAY_THRES ||
310 event->pos().y() < -MOUSE_AWAY_THRES;
312 bool removePoint = (crossedHoriz || crossedVert);
314 if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
315 // point is no longer dragged away so reinsert it
316 QPointF newPoint(d->m_draggedAwayPoint);
317 d->m_grab_point_index = d->m_curve.addPoint(newPoint);
318 d->m_draggedAwayPointIndex = -1;
322 (d->m_draggedAwayPointIndex >= 0))
326 setCursor(Qt::CrossCursor);
331 double leftX, rightX;
332 BPoint point = m_spline.points()[m_currentPointIndex];
333 switch (m_currentPointType) {
335 rightX = point.p.x();
336 if (m_currentPointIndex == 0)
339 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
340 x = qBound(leftX, x, rightX);
341 point.h1 = QPointF(x, y);
344 if (m_currentPointIndex == 0) {
347 /*if (d->m_curve.points().count() > 1)
348 * rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
351 } else if (m_currentPointIndex == m_spline.points().count() - 1) {
352 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
355 //// the 1E-4 addition so we can grab the dot later.
356 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
357 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
359 x = qBound(leftX, x, rightX);
360 y = qBound(0., y, 1.);
362 // move handles by same offset
363 point.h1 += QPointF(x, y) - point.p;
364 point.h2 += QPointF(x, y) - point.p;
366 point.p = QPointF(x, y);
370 if (m_currentPointIndex == m_spline.points().count() - 1)
373 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
374 x = qBound(leftX, x, rightX);
375 point.h2 = QPointF(x, y);
378 m_spline.setPoint(m_currentPointIndex, point);
380 /*if (removePoint && d->m_curve.points().count() > 2) {
381 d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
382 d->m_draggedAwayPointIndex = d->m_grab_point_index;
383 d->m_curve.removePoint(d->m_grab_point_index);
384 d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
387 d->setCurveModified();*/
388 emit currentPoint(point);
393 void BezierSplineEditor::leaveEvent(QEvent* event)
395 QWidget::leaveEvent(event);
398 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
400 double nearestDistanceSquared = 1000;
401 point_types selectedPoint;
402 int nearestIndex = -1;
405 double distanceSquared;
406 foreach(const BPoint & point, m_spline.points()) {
407 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
408 if (distanceSquared < nearestDistanceSquared) {
410 nearestDistanceSquared = distanceSquared;
411 selectedPoint = PTypeH1;
413 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
414 if (distanceSquared < nearestDistanceSquared) {
416 nearestDistanceSquared = distanceSquared;
417 selectedPoint = PTypeP;
419 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
420 if (distanceSquared < nearestDistanceSquared) {
422 nearestDistanceSquared = distanceSquared;
423 selectedPoint = PTypeH2;
428 if (nearestIndex >= 0) {
429 BPoint point = m_spline.points()[nearestIndex];
431 switch (selectedPoint) {
441 if (qAbs(p.x() - p2.x()) * wWidth < 5 && qAbs(p.y() - p2.y()) * wHeight < 5) {
442 *sel = selectedPoint;
450 #include "beziersplineeditor.moc"