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>
26 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::paintEvent(QPaintEvent* event)
86 p.fillRect(rect(), palette().background());
88 int wWidth = width() - 1;
89 int wHeight = height() - 1;
90 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
92 wHeight -= 2 * offset;
97 /*p.setPen(QPen(Qt::gray, 1, Qt::SolidLine));
98 p.drawLine(QLineF(0, wHeight, wWidth, 0));*/
103 double prevY = wHeight - m_spline.value(0.) * wHeight;
106 double normalizedX = -1;
109 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
110 for (x = 0 ; x < wWidth ; ++x) {
111 normalizedX = x / (double)wWidth;
112 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
115 * Keep in mind that QLineF rounds doubles
116 * to ints mathematically, not just rounds down
119 p.drawLine(QLineF(prevX, prevY,
120 x, curY).translated(offset, offset));
124 p.drawLine(QLineF(prevX, prevY ,
125 x, wHeight - m_spline.value(1.0, true) * wHeight).translated(offset, offset));
130 int max = m_spline.points().count() - 1;
131 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
139 for (int i = 0; i <= max; ++i) {
140 point = m_spline.points().at(i);
141 if (i == m_currentPointIndex) {
142 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
144 p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight).translated(offset, offset));
146 p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
149 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
150 wHeight - 3 - point.p.y() * wHeight, 6, 6).translated(offset, offset));
152 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight).translated(offset, offset));
154 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
156 if ( i == m_currentPointIndex)
157 p.setBrush(QBrush(Qt::NoBrush));
161 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
163 m_spline.setPrecision(width());
164 QWidget::resizeEvent(event);
167 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
169 int wWidth = width() - 1;
170 int wHeight = height() - 1;
171 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
172 wWidth -= 2 * offset;
173 wHeight -= 2 * offset;
175 double x = (event->pos().x() - offset) / (double)(wWidth);
176 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
178 point_types selectedPoint;
179 int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
181 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
182 m_spline.removePoint(closestPointIndex);
183 setCursor(Qt::ArrowCursor);
185 if (closestPointIndex < m_currentPointIndex)
186 --m_currentPointIndex;
188 if (m_currentPointIndex >= 0)
189 emit currentPoint(m_spline.points()[m_currentPointIndex]);
191 emit currentPoint(BPoint());
194 } else if (event->button() != Qt::LeftButton) {
198 if (closestPointIndex < 0) {
200 po.p = QPointF(x, y);
201 po.h1 = QPointF(x-0.05, y-0.05);
202 po.h2 = QPointF(x+0.05, y+0.05);
203 m_currentPointIndex = m_spline.addPoint(po);
204 m_currentPointType = PTypeP;
205 /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
207 m_currentPointIndex = closestPointIndex;
208 m_currentPointType = selectedPoint;
211 BPoint point = m_spline.points()[m_currentPointIndex];
213 switch (m_currentPointType) {
224 m_grabOriginalX = p.x();
225 m_grabOriginalY = p.y();
226 m_grabOffsetX = p.x() - x;
227 m_grabOffsetY = p.y() - y;
229 switch (m_currentPointType) {
231 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
234 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
237 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
239 m_spline.setPoint(m_currentPointIndex, point);
241 //d->m_draggedAwayPointIndex = -1;
245 emit currentPoint(point);
249 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
251 if (event->button() != Qt::LeftButton)
254 setCursor(Qt::ArrowCursor);
260 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
262 int wWidth = width() - 1;
263 int wHeight = height() - 1;
264 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
265 wWidth -= 2 * offset;
266 wHeight -= 2 * offset;
268 double x = (event->pos().x() - offset) / (double)(wWidth);
269 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
271 if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
273 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
275 if (nearestPointIndex < 0)
276 setCursor(Qt::ArrowCursor);
278 setCursor(Qt::CrossCursor);
279 } else { // Else, drag the selected point
280 /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
281 event->pos().x() < -MOUSE_AWAY_THRES;
282 bool crossedVert = event->pos().y() - height() > MOUSE_AWAY_THRES ||
283 event->pos().y() < -MOUSE_AWAY_THRES;
285 bool removePoint = (crossedHoriz || crossedVert);
287 if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
288 // point is no longer dragged away so reinsert it
289 QPointF newPoint(d->m_draggedAwayPoint);
290 d->m_grab_point_index = d->m_curve.addPoint(newPoint);
291 d->m_draggedAwayPointIndex = -1;
295 (d->m_draggedAwayPointIndex >= 0))
299 setCursor(Qt::CrossCursor);
304 double leftX, rightX;
305 BPoint point = m_spline.points()[m_currentPointIndex];
306 switch (m_currentPointType) {
308 rightX = point.p.x();
309 if (m_currentPointIndex == 0)
312 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
313 x = qBound(leftX, x, rightX);
314 point.h1 = QPointF(x, y);
317 if (m_currentPointIndex == 0) {
320 /*if (d->m_curve.points().count() > 1)
321 * rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
324 } else if (m_currentPointIndex == m_spline.points().count() - 1) {
325 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
328 //// the 1E-4 addition so we can grab the dot later.
329 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
330 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
332 x = qBound(leftX, x, rightX);
333 y = qBound(0., y, 1.);
335 // move handles by same offset
336 point.h1 += QPointF(x, y) - point.p;
337 point.h2 += QPointF(x, y) - point.p;
339 point.p = QPointF(x, y);
343 if (m_currentPointIndex == m_spline.points().count() - 1)
346 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
347 x = qBound(leftX, x, rightX);
348 point.h2 = QPointF(x, y);
351 m_spline.setPoint(m_currentPointIndex, point);
353 /*if (removePoint && d->m_curve.points().count() > 2) {
354 d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
355 d->m_draggedAwayPointIndex = d->m_grab_point_index;
356 d->m_curve.removePoint(d->m_grab_point_index);
357 d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
360 d->setCurveModified();*/
361 emit currentPoint(point);
366 void BezierSplineEditor::leaveEvent(QEvent* event)
368 QWidget::leaveEvent(event);
371 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
373 double nearestDistanceSquared = 1000;
374 point_types selectedPoint;
375 int nearestIndex = -1;
378 double distanceSquared;
379 foreach(const BPoint & point, m_spline.points()) {
380 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
381 if (distanceSquared < nearestDistanceSquared) {
383 nearestDistanceSquared = distanceSquared;
384 selectedPoint = PTypeH1;
386 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
387 if (distanceSquared < nearestDistanceSquared) {
389 nearestDistanceSquared = distanceSquared;
390 selectedPoint = PTypeP;
392 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
393 if (distanceSquared < nearestDistanceSquared) {
395 nearestDistanceSquared = distanceSquared;
396 selectedPoint = PTypeH2;
401 if (nearestIndex >= 0) {
402 BPoint point = m_spline.points()[nearestIndex];
404 switch (selectedPoint) {
414 if (qAbs(p.x() - p2.x()) * wWidth < 5 && qAbs(p.y() - p2.y()) * wHeight < 5) {
415 *sel = selectedPoint;
423 #include "beziersplineeditor.moc"