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 setMouseTracking(true);
32 setAutoFillBackground(false);
33 setAttribute(Qt::WA_OpaquePaintEvent);
34 setMinimumSize(150, 150);
35 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
38 CubicBezierSpline BezierSplineWidget::spline()
43 void BezierSplineWidget::setSpline(const CubicBezierSpline& spline)
46 m_spline.fromString(spline.toString());
49 void BezierSplineWidget::paintEvent(QPaintEvent* event)
55 p.fillRect(rect(), palette().background());
57 int wWidth = width() - 1;
58 int wHeight = height() - 1;
63 double prevY = wHeight - m_spline.value(0.) * wHeight;
66 double normalizedX = -1;
69 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
70 for (x = 0 ; x < wWidth ; ++x) {
71 normalizedX = x / (double)wWidth;
72 curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
75 * Keep in mind that QLineF rounds doubles
76 * to ints mathematically, not just rounds down
79 p.drawLine(QLineF(prevX, prevY,
84 p.drawLine(QLineF(prevX, prevY ,
85 x, wHeight - m_spline.value(1.0, true) * wHeight));
90 p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
98 for (int i = 0; i < m_spline.points().count(); ++i) {
99 point = m_spline.points().at(i);
100 if (i == m_currentPointIndex)
101 p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
103 p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight));
104 p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
105 wHeight - 3 - point.p.y() * wHeight, 6, 6));
106 p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
108 if ( i == m_currentPointIndex)
109 p.setBrush(QBrush(Qt::NoBrush));
113 void BezierSplineWidget::resizeEvent(QResizeEvent* event)
115 m_spline.setPrecision(width());
116 QWidget::resizeEvent(event);
119 void BezierSplineWidget::mousePressEvent(QMouseEvent* event)
121 double x = event->pos().x() / (double)(width() - 1);
122 double y = 1.0 - event->pos().y() / (double)(height() - 1);
124 point_types selectedPoint;
125 int closest_point_index = nearestPointInRange(QPointF(x, y), width(), height(), &selectedPoint);
127 /*if (event->button() == Qt::RightButton && closest_point_index > 0 && closest_point_index < d->m_curve.points().count() - 1) {
128 d->m_curve.removePoint(closest_point_index);
129 setCursor(Qt::ArrowCursor);
130 d->setState(ST_NORMAL);
131 if (closest_point_index < d->m_grab_point_index)
132 --d->m_grab_point_index;
133 d->setCurveModified();
135 } else*/ if (event->button() != Qt::LeftButton) return;
137 if (closest_point_index < 0) {
139 po.p = QPointF(x, y);
140 po.h1 = QPointF(x-0.05, y-0.05);
141 po.h2 = QPointF(x+0.05, y+0.05);
142 m_currentPointIndex = m_spline.addPoint(po);
143 m_currentPointType = PTypeP;
144 if (m_currentPointIndex == -1) // x already in use
146 /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/
148 m_currentPointIndex = closest_point_index;
149 m_currentPointType = selectedPoint;
152 BPoint point = m_spline.points()[m_currentPointIndex];
154 switch (m_currentPointType) {
165 m_grabOriginalX = p.x();
166 m_grabOriginalY = p.y();
167 m_grabOffsetX = p.x() - x;
168 m_grabOffsetY = p.y() - y;
170 switch (m_currentPointType) {
172 point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
175 point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
178 point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY);
180 m_spline.setPoint(m_currentPointIndex, point);
182 //d->m_draggedAwayPointIndex = -1;
189 void BezierSplineWidget::mouseReleaseEvent(QMouseEvent* event)
191 if (event->button() != Qt::LeftButton)
194 setCursor(Qt::ArrowCursor);
200 void BezierSplineWidget::mouseMoveEvent(QMouseEvent* event)
202 double x = event->pos().x() / (double)(width() - 1);
203 double y = 1.0 - event->pos().y() / (double)(height() - 1);
205 if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
207 int nearestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &type);
209 if (nearestPointIndex < 0)
210 setCursor(Qt::ArrowCursor);
212 setCursor(Qt::CrossCursor);
213 } else { // Else, drag the selected point
214 /*bool crossedHoriz = event->pos().x() - width() > MOUSE_AWAY_THRES ||
215 event->pos().x() < -MOUSE_AWAY_THRES;
216 bool crossedVert = event->pos().y() - height() > MOUSE_AWAY_THRES ||
217 event->pos().y() < -MOUSE_AWAY_THRES;
219 bool removePoint = (crossedHoriz || crossedVert);
221 if (!removePoint && d->m_draggedAwayPointIndex >= 0) {
222 // point is no longer dragged away so reinsert it
223 QPointF newPoint(d->m_draggedAwayPoint);
224 d->m_grab_point_index = d->m_curve.addPoint(newPoint);
225 d->m_draggedAwayPointIndex = -1;
229 (d->m_draggedAwayPointIndex >= 0))
233 setCursor(Qt::CrossCursor);
238 double leftX, rightX;
239 BPoint point = m_spline.points()[m_currentPointIndex];
240 switch (m_currentPointType) {
242 rightX = point.p.x();
243 if (m_currentPointIndex == 0)
246 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();
247 x = qBound(leftX, x, rightX);
248 point.h1 = QPointF(x, y);
251 if (m_currentPointIndex == 0) {
254 /*if (d->m_curve.points().count() > 1)
255 * rightX = d->m_curve.points()[d->m_grab_point_index + 1].x() - POINT_AREA;
258 } else if (m_currentPointIndex == m_spline.points().count() - 1) {
259 leftX = 1.0;//m_spline.points()[m_currentPointIndex - 1].p.x();
262 //// the 1E-4 addition so we can grab the dot later.
263 leftX = m_spline.points()[m_currentPointIndex - 1].p.x();// + POINT_AREA;
264 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();// - POINT_AREA;
266 x = qBound(leftX, x, rightX);
267 y = qBound(0., y, 1.);
268 point.p = QPointF(x, y);
272 if (m_currentPointIndex == m_spline.points().count() - 1)
275 rightX = m_spline.points()[m_currentPointIndex + 1].p.x();
276 x = qBound(leftX, x, rightX);
277 point.h2 = QPointF(x, y);
280 m_spline.setPoint(m_currentPointIndex, point);
282 /*if (removePoint && d->m_curve.points().count() > 2) {
283 d->m_draggedAwayPoint = d->m_curve.points()[d->m_grab_point_index];
284 d->m_draggedAwayPointIndex = d->m_grab_point_index;
285 d->m_curve.removePoint(d->m_grab_point_index);
286 d->m_grab_point_index = bounds(d->m_grab_point_index, 0, d->m_curve.points().count() - 1);
289 d->setCurveModified();*/
294 void BezierSplineWidget::leaveEvent(QEvent* event)
296 QWidget::leaveEvent(event);
299 int BezierSplineWidget::nearestPointInRange(QPointF p, int wWidth, int wHeight, BezierSplineWidget::point_types* sel)
301 double nearestDistanceSquared = 1000;
302 point_types selectedPoint;
303 int nearestIndex = -1;
306 double distanceSquared;
307 foreach(const BPoint & point, m_spline.points()) {
308 distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2);
309 if (distanceSquared < nearestDistanceSquared) {
311 nearestDistanceSquared = distanceSquared;
312 selectedPoint = PTypeH1;
314 distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2);
315 if (distanceSquared < nearestDistanceSquared) {
317 nearestDistanceSquared = distanceSquared;
318 selectedPoint = PTypeP;
320 distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2);
321 if (distanceSquared < nearestDistanceSquared) {
323 nearestDistanceSquared = distanceSquared;
324 selectedPoint = PTypeH2;
329 if (nearestIndex >= 0) {
330 BPoint point = m_spline.points()[nearestIndex];
332 switch (selectedPoint) {
342 if (qAbs(p.x() - p2.x()) * (wWidth - 1) < 5 && qAbs(p.y() - p2.y()) * (wHeight - 1) < 5) {
343 *sel = selectedPoint;
351 #include "beziersplinewidget.moc"