]> git.sesse.net Git - kdenlive/commitdiff
Bezier Spline:
authorTill Theato <root@ttill.de>
Wed, 5 Jan 2011 00:21:16 +0000 (00:21 +0000)
committerTill Theato <root@ttill.de>
Wed, 5 Jan 2011 00:21:16 +0000 (00:21 +0000)
- 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
src/beziercurve/bpoint.cpp
src/beziercurve/bpoint.h
src/beziercurve/cubicbezierspline.cpp

index d50d87c78d337cd88906875d01f3f6298cb4a88d..c6183ee6406b39b9a301e1053285998531442839 100644 (file)
@@ -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;
         }
index c335b4cd69d24560847167ba748cee1395a4403b..2c5496555fec85873970f850f908ded8da5638d9 100644 (file)
@@ -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 &&
index 4619dc940b9d425faf3db59749619e112275bf58..388cec2019a984152eaac8685e2b99a29c1d297c 100644 (file)
@@ -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.
index db5ff6fd8b74ff2603e9bf4bb9a58e384555d05b..881d20af7e0e6bd8f53ce45daf38a3609114ecde 100644 (file)
@@ -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("|");
 }