From 51cb2de5a320ba77695fd3c3d3f585b9d4a64207 Mon Sep 17 00:00:00 2001 From: Till Theato Date: Wed, 5 Jan 2011 00:21:16 +0000 Subject: [PATCH] Bezier Spline: - add operator [] to BPoint to make it easier to access the point of the handles using the type of selected qpoint - cleanup svn path=/trunk/kdenlive/; revision=5267 --- src/beziercurve/beziersplineeditor.cpp | 74 ++++++-------------------- src/beziercurve/bpoint.cpp | 10 ++++ src/beziercurve/bpoint.h | 4 ++ src/beziercurve/cubicbezierspline.cpp | 6 +-- 4 files changed, 33 insertions(+), 61 deletions(-) diff --git a/src/beziercurve/beziersplineeditor.cpp b/src/beziercurve/beziersplineeditor.cpp index d50d87c7..c6183ee6 100644 --- a/src/beziercurve/beziersplineeditor.cpp +++ b/src/beziercurve/beziersplineeditor.cpp @@ -288,11 +288,9 @@ void BezierSplineEditor::mousePressEvent(QMouseEvent* event) } if (closestPointIndex < 0) { - BPoint po; - po.p = QPointF(x, y); - po.h1 = QPointF(x-0.05, y-0.05); - po.h2 = QPointF(x+0.05, y+0.05); - m_currentPointIndex = m_spline.addPoint(po); + m_currentPointIndex = m_spline.addPoint(BPoint(QPointF(x-0.05, y-0.05), + QPointF(x, y), + QPointF(x+0.05, y+0.05))); m_currentPointType = PTypeP; } else { m_currentPointIndex = closestPointIndex; @@ -300,36 +298,17 @@ void BezierSplineEditor::mousePressEvent(QMouseEvent* event) } BPoint point = m_spline.points()[m_currentPointIndex]; - QPointF p; - switch (m_currentPointType) { - case PTypeH1: - p = point.h1; - break; - case PTypeP: - p = point.p; - break; - case PTypeH2: - p = point.h2; - } m_grabPOriginal = point; if (m_currentPointIndex > 0) m_grabPPrevious = m_spline.points()[m_currentPointIndex - 1]; if (m_currentPointIndex < m_spline.points().count() - 1) m_grabPNext = m_spline.points()[m_currentPointIndex + 1]; - m_grabOffsetX = p.x() - x; - m_grabOffsetY = p.y() - y; + m_grabOffsetX = point[(int)m_currentPointType].x() - x; + m_grabOffsetY = point[(int)m_currentPointType].y() - y; + + point[(int)m_currentPointType] = QPointF(x + m_grabOffsetX, y + m_grabOffsetY); - switch (m_currentPointType) { - case PTypeH1: - point.h1 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY); - break; - case PTypeP: - point.p = QPointF(x + m_grabOffsetX, y + m_grabOffsetY); - break; - case PTypeH2: - point.h2 = QPointF(x + m_grabOffsetX, y + m_grabOffsetY); - } m_spline.setPoint(m_currentPointIndex, point); m_mode = ModeDrag; @@ -372,7 +351,7 @@ void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event) } else { // Else, drag the selected point setCursor(Qt::CrossCursor); - + x += m_grabOffsetX; y += m_grabOffsetY; @@ -469,41 +448,20 @@ int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight, double distanceSquared; // find out distance using the Pythagorean theorem foreach(const BPoint & point, m_spline.points()) { - distanceSquared = pow(point.h1.x() - p.x(), 2) + pow(point.h1.y() - p.y(), 2); - if (distanceSquared < nearestDistanceSquared) { - nearestIndex = i; - nearestDistanceSquared = distanceSquared; - selectedPoint = PTypeH1; - } - distanceSquared = pow(point.p.x() - p.x(), 2) + pow(point.p.y() - p.y(), 2); - if (distanceSquared < nearestDistanceSquared) { - nearestIndex = i; - nearestDistanceSquared = distanceSquared; - selectedPoint = PTypeP; - } - distanceSquared = pow(point.h2.x() - p.x(), 2) + pow(point.h2.y() - p.y(), 2); - if (distanceSquared < nearestDistanceSquared) { - nearestIndex = i; - nearestDistanceSquared = distanceSquared; - selectedPoint = PTypeH2; + for (int j = 0; j < 3; ++j) { + distanceSquared = pow(point[j].x() - p.x(), 2) + pow(point[j].y() - p.y(), 2); + if (distanceSquared < nearestDistanceSquared) { + nearestIndex = i; + nearestDistanceSquared = distanceSquared; + selectedPoint = (point_types)j; + } } ++i; } if (nearestIndex >= 0) { BPoint point = m_spline.points()[nearestIndex]; - QPointF p2; - switch (selectedPoint) { - case PTypeH1: - p2 = point.h1; - break; - case PTypeP: - p2 = point.p; - break; - case PTypeH2: - p2 = point.h2; - } - if (qAbs(p.x() - p2.x()) * wWidth < 5 && qAbs(p.y() - p2.y()) * wHeight < 5) { + if (qAbs(p.x() - point[(int)selectedPoint].x()) * wWidth < 5 && qAbs(p.y() - point[(int)selectedPoint].y()) * wHeight < 5) { *sel = selectedPoint; return nearestIndex; } diff --git a/src/beziercurve/bpoint.cpp b/src/beziercurve/bpoint.cpp index c335b4cd..2c549655 100644 --- a/src/beziercurve/bpoint.cpp +++ b/src/beziercurve/bpoint.cpp @@ -35,6 +35,16 @@ BPoint::BPoint(QPointF handle1, QPointF point, QPointF handle2) : autoSetLinked(); } +QPointF &BPoint::operator[](int i) +{ + return i == 0 ? h1 : (i == 1 ? p : h2); +} + +const QPointF &BPoint::operator[](int i) const +{ + return i == 0 ? h1 : (i == 1 ? p : h2); +} + bool BPoint::operator==(const BPoint& point) const { return point.h1 == h1 && diff --git a/src/beziercurve/bpoint.h b/src/beziercurve/bpoint.h index 4619dc94..388cec20 100644 --- a/src/beziercurve/bpoint.h +++ b/src/beziercurve/bpoint.h @@ -33,6 +33,10 @@ public: /** @brief Sets up according to the params. Linking detecting is done using autoSetLinked(). */ BPoint(QPointF handle1, QPointF point, QPointF handle2); + /** @brief Returns h1 if i = 0, p if i = 1, h2 if i = 2. */ + QPointF &operator[](int i); + /** @brief Returns h1 if i = 0, p if i = 1, h2 if i = 2. */ + const QPointF &operator[](int i) const; bool operator==(const BPoint &point) const; /** @brief Sets p to @param point. diff --git a/src/beziercurve/cubicbezierspline.cpp b/src/beziercurve/cubicbezierspline.cpp index db5ff6fd..881d20af 100644 --- a/src/beziercurve/cubicbezierspline.cpp +++ b/src/beziercurve/cubicbezierspline.cpp @@ -77,9 +77,9 @@ QString CubicBezierSpline::toString() const { QStringList spline; foreach(const BPoint &p, m_points) { - spline << (QString::number(p.h1.x()) + ";" + QString::number(p.h1.y()) - + "#" + QString::number(p.p.x()) + ";" + QString::number(p.p.y()) - + "#" + QString::number(p.h2.x()) + ";" + QString::number(p.h2.y())); + spline << QString("%1;%2#%3;%4#%5;%6").arg(p.h1.x()).arg(p.h1.y()) + .arg(p.p.x()).arg(p.p.y()) + .arg(p.h2.x()).arg(p.h2.y()); } return spline.join("|"); } -- 2.39.2