]> git.sesse.net Git - kdenlive/commitdiff
Bezier Spline Widget: Make it possible to zoom out (because handles can have values...
authorTill Theato <root@ttill.de>
Sat, 1 Jan 2011 22:41:35 +0000 (22:41 +0000)
committerTill Theato <root@ttill.de>
Sat, 1 Jan 2011 22:41:35 +0000 (22:41 +0000)
svn path=/trunk/kdenlive/; revision=5235

src/beziercurve/beziersplineeditor.cpp
src/beziercurve/beziersplineeditor.h
src/beziercurve/beziersplinewidget.cpp
src/widgets/bezierspline_ui.ui

index 94cbbdaf0e0b56eb0b568f89c3023be6778a4c6e..9846ee1fb36debe40f5b79b18e087b6e4f584e60 100644 (file)
@@ -25,6 +25,7 @@
 
 BezierSplineEditor::BezierSplineEditor(QWidget* parent) :
         QWidget(parent),
+        m_zoomLevel(0),
         m_mode(ModeNormal),
         m_currentPointIndex(-1)
 {
@@ -64,6 +65,18 @@ void BezierSplineEditor::updateCurrentPoint(const BPoint& p)
     }
 }
 
+void BezierSplineEditor::slotZoomIn()
+{
+    m_zoomLevel = qMax(m_zoomLevel-1, 0);
+    update();
+}
+
+void BezierSplineEditor::slotZoomOut()
+{
+    m_zoomLevel = qMin(m_zoomLevel+1, 3);
+    update();
+}
+
 void BezierSplineEditor::paintEvent(QPaintEvent* event)
 {
     Q_UNUSED(event);
@@ -72,8 +85,11 @@ void BezierSplineEditor::paintEvent(QPaintEvent* event)
 
     p.fillRect(rect(), palette().background());
 
-    int    wWidth = width() - 1;
-    int    wHeight = height() - 1;
+    int wWidth = width() - 1;
+    int wHeight = height() - 1;
+    int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
+    wWidth -= 2 * offset;
+    wHeight -= 2 * offset;
 
     /*
      * Standard line
@@ -94,19 +110,19 @@ void BezierSplineEditor::paintEvent(QPaintEvent* event)
     for (x = 0 ; x < wWidth ; ++x) {
         normalizedX = x / (double)wWidth;
         curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
-        
+
         /*
          * Keep in mind that QLineF rounds doubles
          * to ints mathematically, not just rounds down
          * like in C
          */
         p.drawLine(QLineF(prevX, prevY,
-                          x, curY));
+                          x, curY).translated(offset, offset));
         prevX = x;
         prevY = curY;
     }
     p.drawLine(QLineF(prevX, prevY ,
-                      x, wHeight - m_spline.value(1.0, true) * wHeight));
+                      x, wHeight - m_spline.value(1.0, true) * wHeight).translated(offset, offset));
 
     /*
      * Points + Handles
@@ -123,14 +139,14 @@ void BezierSplineEditor::paintEvent(QPaintEvent* event)
         point = m_spline.points().at(i);
         if (i == m_currentPointIndex) {
             p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
-            p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight));
-            p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
+            p.drawLine(QLineF(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight, point.p.x() * wWidth, wHeight - point.p.y() * wHeight).translated(offset, offset));
+            p.drawLine(QLineF(point.p.x() * wWidth, wHeight - point.p.y() * wHeight, point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
         }
 
         p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
-                     wHeight - 3 - point.p.y() * wHeight, 6, 6));
-        p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight));
-        p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
+                             wHeight - 3 - point.p.y() * wHeight, 6, 6).translated(offset, offset));
+        p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight).translated(offset, offset));
+        p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight).translated(offset, offset));
 
         if ( i == m_currentPointIndex)
             p.setBrush(QBrush(Qt::NoBrush));
@@ -145,11 +161,17 @@ void BezierSplineEditor::resizeEvent(QResizeEvent* event)
 
 void BezierSplineEditor::mousePressEvent(QMouseEvent* event)
 {
-    double x = event->pos().x() / (double)(width() - 1);
-    double y = 1.0 - event->pos().y() / (double)(height() - 1);
+    int wWidth = width() - 1;
+    int wHeight = height() - 1;
+    int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
+    wWidth -= 2 * offset;
+    wHeight -= 2 * offset;
+
+    double x = (event->pos().x() - offset) / (double)(wWidth);
+    double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
 
     point_types selectedPoint;
-    int closestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &selectedPoint);
+    int closestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &selectedPoint);
 
     if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
         m_spline.removePoint(closestPointIndex);
@@ -232,12 +254,18 @@ void BezierSplineEditor::mouseReleaseEvent(QMouseEvent* event)
 
 void BezierSplineEditor::mouseMoveEvent(QMouseEvent* event)
 {
-    double x = event->pos().x() / (double)(width() - 1);
-    double y = 1.0 - event->pos().y() / (double)(height() - 1);
+    int wWidth = width() - 1;
+    int wHeight = height() - 1;
+    int offset = 1/8. * m_zoomLevel * (wWidth > wHeight ? wWidth : wHeight);
+    wWidth -= 2 * offset;
+    wHeight -= 2 * offset;
+
+    double x = (event->pos().x() - offset) / (double)(wWidth);
+    double y = 1.0 - (event->pos().y() - offset) / (double)(wHeight);
     
     if (m_mode == ModeNormal) { // If no point is selected set the the cursor shape if on top
         point_types type;
-        int nearestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &type);
+        int nearestPointIndex = nearestPointInRange(QPointF(x, y), wWidth, wHeight, &type);
         
         if (nearestPointIndex < 0)
             setCursor(Qt::ArrowCursor);
@@ -378,7 +406,7 @@ int BezierSplineEditor::nearestPointInRange(QPointF p, int wWidth, int wHeight,
         case PTypeH2:
             p2 = point.h2;
         }
-        if (qAbs(p.x() - p2.x()) * (wWidth - 1) < 5 && qAbs(p.y() - p2.y()) * (wHeight - 1) < 5) {
+        if (qAbs(p.x() - p2.x()) * wWidth < 5 && qAbs(p.y() - p2.y()) * wHeight < 5) {
             *sel = selectedPoint;
             return nearestIndex;
         }
index 1c8229d0b8b17bc7ed5e848fba48677c72707a99..68fb7b6ed6c66bf4a4a352fc4e9c2c55b16b3a7e 100644 (file)
@@ -37,6 +37,10 @@ public:
     BPoint getCurrentPoint();
     void updateCurrentPoint(const BPoint &p);
 
+public slots:
+    void slotZoomIn();
+    void slotZoomOut();
+
 protected:
     //void keyPressEvent(QKeyEvent *event);
     void paintEvent(QPaintEvent *event);
@@ -51,6 +55,7 @@ private:
     enum modes { ModeDrag, ModeNormal };
     enum point_types { PTypeH1, PTypeP, PTypeH2 };
     modes m_mode;
+    int m_zoomLevel;
     int m_currentPointIndex;
     point_types m_currentPointType;
     double m_grabOffsetX;
index e784e04459a0959942aa8dfdb5acfce392ae794b..b2491c75724dd13939691e9f93d27717e453a7ed 100644 (file)
@@ -33,6 +33,8 @@ BezierSplineWidget::BezierSplineWidget(const QString& spline, QWidget* parent) :
 
     m_ui.buttonLinkHandles->setIcon(KIcon("insert-link"));
     m_ui.buttonLinkHandles->setEnabled(false);
+    m_ui.buttonZoomIn->setIcon(KIcon("zoom-in"));
+    m_ui.buttonZoomOut->setIcon(KIcon("zoom-out"));
     m_ui.widgetPoint->setEnabled(false);
 
     CubicBezierSpline s;
@@ -48,6 +50,9 @@ BezierSplineWidget::BezierSplineWidget(const QString& spline, QWidget* parent) :
     connect(m_ui.spinH1Y, SIGNAL(editingFinished()), this, SLOT(slotUpdateSpline()));
     connect(m_ui.spinH2X, SIGNAL(editingFinished()), this, SLOT(slotUpdateSpline()));
     connect(m_ui.spinH2Y, SIGNAL(editingFinished()), this, SLOT(slotUpdateSpline()));
+
+    connect(m_ui.buttonZoomIn, SIGNAL(clicked()), &m_edit, SLOT(slotZoomIn()));
+    connect(m_ui.buttonZoomOut, SIGNAL(clicked()), &m_edit, SLOT(slotZoomOut()));
 }
 
 QString BezierSplineWidget::spline()
index 6e9f4cb8987a2397da0b0c09e755aafd4849fda5..e0cbb66a29c7991e4f4b1f86709c88b9165bcb45 100644 (file)
@@ -6,15 +6,15 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>471</width>
-    <height>84</height>
+    <width>336</width>
+    <height>94</height>
    </rect>
   </property>
   <property name="windowTitle">
    <string>Form</string>
   </property>
   <layout class="QGridLayout" name="gridLayout">
-   <item row="1" column="5">
+   <item row="1" column="5" colspan="3">
     <widget class="QWidget" name="widgetPoint" native="true">
      <layout class="QGridLayout" name="gridLayout_2">
       <item row="0" column="0" colspan="9">
      </layout>
     </widget>
    </item>
+   <item row="2" column="5">
+    <widget class="QToolButton" name="buttonZoomIn">
+     <property name="text">
+      <string>...</string>
+     </property>
+     <property name="autoRaise">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
+   <item row="2" column="6">
+    <widget class="QToolButton" name="buttonZoomOut">
+     <property name="text">
+      <string>...</string>
+     </property>
+     <property name="autoRaise">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
+   <item row="2" column="7">
+    <spacer name="spacer">
+     <property name="orientation">
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <property name="sizeHint" stdset="0">
+      <size>
+       <width>40</width>
+       <height>20</height>
+      </size>
+     </property>
+    </spacer>
+   </item>
   </layout>
  </widget>
  <resources/>