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 "beziersplinewidget.h"
22 #include <QMouseEvent>
26 BezierSplineWidget::BezierSplineWidget(QWidget* parent) :
29 m_currentPointIndex(-1)
31 //m_spline.setPrecision(width());
32 setMouseTracking(true);
33 setAutoFillBackground(false);
34 setAttribute(Qt::WA_OpaquePaintEvent);
35 setMinimumSize(150, 150);
36 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
39 CubicBezierSpline BezierSplineWidget::spline()
44 void BezierSplineWidget::setSpline(const CubicBezierSpline& spline)
47 m_spline.fromString(spline.toString());
50 void BezierSplineWidget::paintEvent(QPaintEvent* event)
56 p.fillRect(rect(), palette().background());
58 int wWidth = width() - 1;
59 int wHeight = height() - 1;
62 double prevY = wHeight - m_spline.value(0.) * wHeight;
65 double normalizedX = -1;
68 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
69 for (x = 0 ; x < wWidth ; ++x) {
70 normalizedX = x / (double)wWidth;
71 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
74 * Keep in mind that QLineF rounds doubles
75 * to ints mathematically, not just rounds down
78 p.drawLine(QLineF(prevX, prevY,
83 p.drawLine(QLineF(prevX, prevY ,
84 x, wHeight - m_spline.value(1.0, true) * wHeight));
86 // Drawing curve handles.
87 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
95 for (int i = 0; i < m_spline.points().count(); ++i) {
96 point = m_spline.points().at(i);
97 if (i == m_currentPointIndex)
98 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
100 p.drawPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight));
101 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
102 wHeight - 3 - point.p.y() * wHeight, 6, 6));
103 p.drawPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
105 if ( i == m_currentPointIndex)
106 p.setBrush(QBrush(Qt::NoBrush));
110 void BezierSplineWidget::resizeEvent(QResizeEvent* event)
112 m_spline.setPrecision(width());
113 QWidget::resizeEvent(event);
116 void BezierSplineWidget::mousePressEvent(QMouseEvent* event)
118 double x = event->pos().x() / (double)(width() - 1);
119 double y = 1.0 - event->pos().y() / (double)(height() - 1);
121 point_types selectedPoint;
122 int closest_point_index = nearestPointInRange(QPointF(x, y), width(), height(), &selectedPoint);
124 /*if (event->button() == Qt::RightButton && closest_point_index > 0 && closest_point_index < d->m_curve.points().count() - 1) {
125 d->m_curve.removePoint(closest_point_index);
126 setCursor(Qt::ArrowCursor);
127 d->setState(ST_NORMAL);
128 if (closest_point_index < d->m_grab_point_index)
129 --d->m_grab_point_index;
130 d->setCurveModified();
132 } else*/ if (event->button() != Qt::LeftButton) return;
134 if (closest_point_index < 0) {
136 po.p = QPointF(x, y);
137 po.h1 = QPointF(x-0.05, y-0.05);
138 po.h2 = QPointF(x+0.05, y+0.05);
139 m_currentPointIndex = m_spline.addPoint(po);
140 m_currentPointType = PTypeP;
141 if (m_currentPointIndex == -1) // x already in use
143 /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
145 m_currentPointIndex = closest_point_index;
146 m_currentPointType = selectedPoint;
149 BPoint point = m_spline.points()[m_currentPointIndex];
151 switch (m_currentPointType) {
162 m_grabOriginalX = p.x();
163 m_grabOriginalY = p.y();
164 m_grabOffsetX = p.x() - x;
165 m_grabOffsetY = p.y() - y;
167 switch (m_currentPointType) {
169 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
172 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
175 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
177 m_spline.setPoint(m_currentPointIndex, point);
179 //d->m_draggedAwayPointIndex = -1;
186 void BezierSplineWidget::mouseReleaseEvent(QMouseEvent* event)
188 if (event->button() != Qt::LeftButton)
191 setCursor(Qt::ArrowCursor);
197 void BezierSplineWidget::mouseMoveEvent(QMouseEvent* event)
199 double x = event->pos().x() / (double)(width() - 1);
200 double y = 1.0 - event->pos().y() / (double)(height() - 1);
202 if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
204 int nearestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &type);
206 if (nearestPointIndex < 0)
207 setCursor(Qt::ArrowCursor);
209 setCursor(Qt::CrossCursor);
210 } else { // Else, drag the selected point
211 /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
212 event->pos().x() < -MOUSE_AWAY_THRES;
213 bool crossedVert = event->pos().y() - height() > MOUSE_AWAY_THRES ||
214 event->pos().y() < -MOUSE_AWAY_THRES;
216 bool removePoint = (crossedHoriz || crossedVert);
218 if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
219 // point is no longer dragged away so reinsert it
220 QPointF newPoint(d->m_draggedAwayPoint);
221 d->m_grab_point_index = d->m_curve.addPoint(newPoint);
222 d->m_draggedAwayPointIndex = -1;
226 (d->m_draggedAwayPointIndex >= 0))
230 setCursor(Qt::CrossCursor);
235 double leftX, rightX;
236 BPoint point = m_spline.points()[m_currentPointIndex];
237 switch (m_currentPointType) {
239 rightX = point.p.x();
240 if (m_currentPointIndex == 0)
243 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
244 x = qBound(leftX, x, rightX);
245 point.h1 = QPointF(x, y);
248 if (m_currentPointIndex == 0) {
251 /*if (d->m_curve.points().count() > 1)
252 * rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
255 } else if (m_currentPointIndex == m_spline.points().count() - 1) {
256 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
259 //// the 1E-4 addition so we can grab the dot later.
260 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
261 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
263 x = qBound(leftX, x, rightX);
264 y = qBound(0., y, 1.);
265 point.p = QPointF(x, y);
269 if (m_currentPointIndex == m_spline.points().count() - 1)
272 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
273 x = qBound(leftX, x, rightX);
274 point.h2 = QPointF(x, y);
277 m_spline.setPoint(m_currentPointIndex, point);
279 /*if (removePoint && d->m_curve.points().count() > 2) {
280 d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
281 d->m_draggedAwayPointIndex = d->m_grab_point_index;
282 d->m_curve.removePoint(d->m_grab_point_index);
283 d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
286 d->setCurveModified();*/
291 void BezierSplineWidget::leaveEvent(QEvent* event)
293 QWidget::leaveEvent(event);
296 int BezierSplineWidget::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineWidget::point_types* sel)
298 double nearestDistanceSquared = 1000;
299 point_types selectedPoint;
300 int nearestIndex = -1;
303 double distanceSquared;
304 foreach(const BPoint & point, m_spline.points()) {
305 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
306 if (distanceSquared < nearestDistanceSquared) {
308 nearestDistanceSquared = distanceSquared;
309 selectedPoint = PTypeH1;
311 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
312 if (distanceSquared < nearestDistanceSquared) {
314 nearestDistanceSquared = distanceSquared;
315 selectedPoint = PTypeP;
317 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
318 if (distanceSquared < nearestDistanceSquared) {
320 nearestDistanceSquared = distanceSquared;
321 selectedPoint = PTypeH2;
326 if (nearestIndex >= 0) {
327 BPoint point = m_spline.points()[nearestIndex];
329 switch (selectedPoint) {
339 if (qAbs(p.x() - p2.x()) * (wWidth - 1) < 5 && qAbs(p.y() - p2.y()) * (wHeight - 1) < 5) {
340 *sel = selectedPoint;
348 #include "beziersplinewidget.moc"