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 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
138 for (int i = 0; i < m_spline.points().count(); ++i) {
139 point = m_spline.points().at(i);
140 if (i == m_currentPointIndex) {
141 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
142 p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight).translated(offset, offset));
143 p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
146 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
147 wHeight - 3 - point.p.y() * wHeight, 6, 6).translated(offset, offset));
148 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight).translated(offset, offset));
149 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
151 if ( i == m_currentPointIndex)
152 p.setBrush(QBrush(Qt::NoBrush));
156 void BezierSplineEditor::resizeEvent(QResizeEvent* event)
158 m_spline.setPrecision(width());
159 QWidget::resizeEvent(event);
162 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
164 int wWidth = width() - 1;
165 int wHeight = height() - 1;
166 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
167 wWidth -= 2 * offset;
168 wHeight -= 2 * offset;
170 double x = (event->pos().x() - offset) / (double)(wWidth);
171 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
173 point_types selectedPoint;
174 int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
176 if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
177 m_spline.removePoint(closestPointIndex);
178 setCursor(Qt::ArrowCursor);
180 if (closestPointIndex < m_currentPointIndex)
181 --m_currentPointIndex;
183 if (m_currentPointIndex >= 0)
184 emit currentPoint(m_spline.points()[m_currentPointIndex]);
186 emit currentPoint(BPoint());
189 } else if (event->button() != Qt::LeftButton) {
193 if (closestPointIndex < 0) {
195 po.p = QPointF(x, y);
196 po.h1 = QPointF(x-0.05, y-0.05);
197 po.h2 = QPointF(x+0.05, y+0.05);
198 m_currentPointIndex = m_spline.addPoint(po);
199 m_currentPointType = PTypeP;
200 /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
202 m_currentPointIndex = closestPointIndex;
203 m_currentPointType = selectedPoint;
206 BPoint point = m_spline.points()[m_currentPointIndex];
208 switch (m_currentPointType) {
219 m_grabOriginalX = p.x();
220 m_grabOriginalY = p.y();
221 m_grabOffsetX = p.x() - x;
222 m_grabOffsetY = p.y() - y;
224 switch (m_currentPointType) {
226 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
229 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
232 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
234 m_spline.setPoint(m_currentPointIndex, point);
236 //d->m_draggedAwayPointIndex = -1;
240 emit currentPoint(point);
244 void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
246 if (event->button() != Qt::LeftButton)
249 setCursor(Qt::ArrowCursor);
255 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
257 int wWidth = width() - 1;
258 int wHeight = height() - 1;
259 int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
260 wWidth -= 2 * offset;
261 wHeight -= 2 * offset;
263 double x = (event->pos().x() - offset) / (double)(wWidth);
264 double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
266 if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
268 int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
270 if (nearestPointIndex < 0)
271 setCursor(Qt::ArrowCursor);
273 setCursor(Qt::CrossCursor);
274 } else { // Else, drag the selected point
275 /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
276 event->pos().x() < -MOUSE_AWAY_THRES;
277 bool crossedVert = event->pos().y() - height() > MOUSE_AWAY_THRES ||
278 event->pos().y() < -MOUSE_AWAY_THRES;
280 bool removePoint = (crossedHoriz || crossedVert);
282 if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
283 // point is no longer dragged away so reinsert it
284 QPointF newPoint(d->m_draggedAwayPoint);
285 d->m_grab_point_index = d->m_curve.addPoint(newPoint);
286 d->m_draggedAwayPointIndex = -1;
290 (d->m_draggedAwayPointIndex >= 0))
294 setCursor(Qt::CrossCursor);
299 double leftX, rightX;
300 BPoint point = m_spline.points()[m_currentPointIndex];
301 switch (m_currentPointType) {
303 rightX = point.p.x();
304 if (m_currentPointIndex == 0)
307 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
308 x = qBound(leftX, x, rightX);
309 point.h1 = QPointF(x, y);
312 if (m_currentPointIndex == 0) {
315 /*if (d->m_curve.points().count() > 1)
316 * rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
319 } else if (m_currentPointIndex == m_spline.points().count() - 1) {
320 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
323 //// the 1E-4 addition so we can grab the dot later.
324 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
325 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
327 x = qBound(leftX, x, rightX);
328 y = qBound(0., y, 1.);
330 // move handles by same offset
331 point.h1 += QPointF(x, y) - point.p;
332 point.h2 += QPointF(x, y) - point.p;
334 point.p = QPointF(x, y);
338 if (m_currentPointIndex == m_spline.points().count() - 1)
341 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
342 x = qBound(leftX, x, rightX);
343 point.h2 = QPointF(x, y);
346 m_spline.setPoint(m_currentPointIndex, point);
348 /*if (removePoint && d->m_curve.points().count() > 2) {
349 d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
350 d->m_draggedAwayPointIndex = d->m_grab_point_index;
351 d->m_curve.removePoint(d->m_grab_point_index);
352 d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
355 d->setCurveModified();*/
356 emit currentPoint(point);
361 void BezierSplineEditor::leaveEvent(QEvent* event)
363 QWidget::leaveEvent(event);
366 int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineEditor::point_types* sel)
368 double nearestDistanceSquared = 1000;
369 point_types selectedPoint;
370 int nearestIndex = -1;
373 double distanceSquared;
374 foreach(const BPoint & point, m_spline.points()) {
375 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
376 if (distanceSquared < nearestDistanceSquared) {
378 nearestDistanceSquared = distanceSquared;
379 selectedPoint = PTypeH1;
381 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
382 if (distanceSquared < nearestDistanceSquared) {
384 nearestDistanceSquared = distanceSquared;
385 selectedPoint = PTypeP;
387 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
388 if (distanceSquared < nearestDistanceSquared) {
390 nearestDistanceSquared = distanceSquared;
391 selectedPoint = PTypeH2;
396 if (nearestIndex >= 0) {
397 BPoint point = m_spline.points()[nearestIndex];
399 switch (selectedPoint) {
409 if (qAbs(p.x() - p2.x()) * wWidth < 5 && qAbs(p.y() - p2.y()) * wHeight < 5) {
410 *sel = selectedPoint;
418 #include "beziersplineeditor.moc"