]> git.sesse.net Git - kdenlive/blobdiff - src/smallruler.cpp
* New config option for preview: disable B Frame decoding on h.264:
[kdenlive] / src / smallruler.cpp
index 5d9d23caf42d5657c7e81d96ad33a69fddc28685..fdd6fd9fcf08e330c9bbe2f58d5aa31f871cfde7 100644 (file)
 
 #include <KDebug>
 
-#define LITTLE_MARK_X2 8
-#define LITTLE_MARK_X1 5
-#define MIDDLE_MARK_X1 3
-#define MIDDLE_MARK_X2 8
-
-#define LABEL_SIZE 8
-
 #include "smallruler.h"
 
 
 SmallRuler::SmallRuler(QWidget *parent)
-        : KRuler(parent) {
-    setShowPointer(true);
-    setShowBigMarks(false);
-    setShowTinyMarks(false);
-    slotNewOffset(0);
-    setRulerMetricStyle(KRuler::Custom);
-    setLengthFixed(true);
+        : QWidget(parent), m_scale(1), m_maxval(25) {
+    m_zoneStart = 10;
+    m_zoneEnd = 60;
 }
 
-void SmallRuler::setPixelPerMark(double rate) {
-    kDebug() << " RULER SET RATE: " << rate;
-    if (rate > 0.5) {
-        setLittleMarkDistance(25);
-        setMediumMarkDistance(5 * 25);
-    } else if (rate > 0.09) {
-        setLittleMarkDistance(5 * 25);
-        setMediumMarkDistance(30 * 25);
+void SmallRuler::adjustScale(int maximum) {
+    m_maxval = maximum;
+    m_scale = (double) width() / (double) maximum;
+    if (m_scale == 0) m_scale = 1;
+
+    if (m_scale > 0.5) {
+        m_small = 25;
+        m_medium = 5 * 25;
+    } else if (m_scale > 0.09) {
+        m_small = 5 * 25;
+        m_medium = 30 * 25;
     } else {
-        setLittleMarkDistance(30 * 25);
-        setMediumMarkDistance(60 * 25);
+        m_small = 30 * 25;
+        m_medium = 60 * 25;
+    }
+    m_cursorPosition = m_cursorFramePosition * m_scale;
+    update();
+}
+
+void SmallRuler::setZone(int start, int end) {
+    if (start != -1) {
+        if (end != -1 && start >= end) {
+            m_zoneEnd = qMin(m_maxval, end + (start - m_zoneStart));
+            m_zoneStart = start;
+        } else if (end == -1 && start >= m_zoneEnd) {
+            m_zoneEnd = qMin(m_maxval, m_zoneEnd + (start - m_zoneStart));
+            m_zoneStart = start;
+        } else m_zoneStart = start;
+    }
+    if (end != -1) {
+        if (start != -1 && end <= start) {
+            m_zoneStart = qMax(0, start - (m_zoneEnd - end));
+            m_zoneEnd = end;
+        } else if (start == -1 && end <= m_zoneStart) {
+            m_zoneStart = qMax(0, m_zoneStart - (m_zoneEnd - end));
+            m_zoneEnd = end;
+        } else m_zoneEnd = end;
     }
+    update();
+}
 
-    KRuler::setPixelPerMark(rate);
+QPoint SmallRuler::zone() {
+    return QPoint(m_zoneStart, m_zoneEnd);
 }
 
 // virtual
 void SmallRuler::mousePressEvent(QMouseEvent * event) {
-    int pos = event->x();
-    //slotNewValue( pos );
-    emit seekRenderer(pos);
-    kDebug() << pos;
+    const int pos = event->x() / m_scale;
+    if (event->button() == Qt::RightButton) {
+        // Right button clicked, move selection zone
+        if (qAbs(pos - m_zoneStart) < qAbs(pos - m_zoneEnd)) m_zoneStart = pos;
+        else m_zoneEnd = pos;
+        emit zoneChanged(QPoint(m_zoneStart, m_zoneEnd));
+        update();
+
+    } else emit seekRenderer((int) pos);
 }
 
 // virtual
 void SmallRuler::mouseMoveEvent(QMouseEvent * event) {
-    int pos = event->x();
-    //slotNewValue( pos );
-    emit seekRenderer(pos);
-    kDebug() << pos;
+    const int pos = event->x() / m_scale;
+    if (event->buttons() & Qt::LeftButton) emit seekRenderer((int) pos);
 }
 
-void SmallRuler::slotNewValue(int _value) {
-    m_cursorPosition = _value / pixelPerMark();
-    KRuler::slotNewValue(_value);
+void SmallRuler::slotNewValue(int value) {
+    m_cursorFramePosition = value;
+    int oldPos = m_cursorPosition;
+    m_cursorPosition = value * m_scale;
+    if (oldPos == m_cursorPosition) return;
+    const int offset = 6;
+    const int x = qMin(oldPos, m_cursorPosition);
+    const int w = qAbs(oldPos - m_cursorPosition);
+    update(x - offset, 9, w + 2 * offset, 6);
+}
+
+//virtual
+void SmallRuler::resizeEvent(QResizeEvent *) {
+    adjustScale(m_maxval);
 }
 
 // virtual
 void SmallRuler::paintEvent(QPaintEvent *e) {
-    //  debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical");
-
-    QStylePainter p(this);
-    p.fillRect(e->rect(), QBrush(QColor(Qt::white)));
 
+    QPainter p(this);
+    QRect r = e->rect();
+    p.setClipRect(r);
 
-    int value  = this->value(),
-                 minval = minimum(),
-                          maxval;
-    maxval = maximum()
-             + offset() - endOffset();
+    double f, fend;
+    p.setPen(palette().dark().color());
 
-    //ioffsetval = value-offset;
-    //    pixelpm = (int)ppm;
-    //    left  = clip.left(),
-    //    right = clip.right();
-    double f, fend,
-    offsetmin = (double)(minval - offset()),
-                offsetmax = (double)(maxval - offset()),
-                            fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
+    const int zoneStart = (int)(m_zoneStart * m_scale);
+    const int zoneEnd = (int)(m_zoneEnd * m_scale);
 
-    // draw labels
-    QFont font = p.font();
-    font.setPointSize(LABEL_SIZE);
-    p.setFont(font);
+    p.fillRect(QRect(zoneStart, height() / 2, zoneEnd - zoneStart, height() / 2), QBrush(QColor(133, 255, 143)));
 
-    if (showLittleMarks()) {
+    if (r.top() < 9) {
         // draw the little marks
-        fend = pixelPerMark() * littleMarkDistance();
-        if (fend > 2) for (f = offsetmin; f < offsetmax; f += fend) {
-                p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
+        fend = m_scale * m_small;
+        if (fend > 2) for (f = 0; f < width(); f += fend) {
+                p.drawLine((int)f, 1, (int)f, 3);
             }
-    }
-    if (showMediumMarks()) {
+
         // draw medium marks
-        fend = pixelPerMark() * mediumMarkDistance();
-        if (fend > 2) for (f = offsetmin; f < offsetmax; f += fend) {
-                p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
+        fend = m_scale * m_medium;
+        if (fend > 2) for (f = 0; f < width(); f += fend) {
+                p.drawLine((int)f, 1, (int)f, 5);
             }
     }
 
-    /*   if (d->showem) {
-         // draw end marks
-         if (d->dir == Qt::Horizontal) {
-           p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
-           p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
-         }
-         else {
-           p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
-           p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
-         }
-       }*/
-
     // draw pointer
-    if (showPointer()) {
-        QPolygon pa(3);
-        pa.setPoints(3, value - 6, 0, value + 6, 0, value/*+0*/, 8);
-        p.setBrush(QBrush(Qt::yellow));
-        p.drawPolygon(pa);
-    }
-
+    QPolygon pa(3);
+    pa.setPoints(3, m_cursorPosition - 5, 14, m_cursorPosition + 5, 14, m_cursorPosition/*+0*/, 9);
+    p.setBrush(palette().dark().color());
+    p.drawPolygon(pa);
 }
 
 #include "smallruler.moc"