]> git.sesse.net Git - kdenlive/blobdiff - src/customruler.cpp
Fix crash when enabling a proxy clip
[kdenlive] / src / customruler.cpp
index fad7268c699ff3dd933ca60d091c7a0c8f09f378..e173297466794337bbdcb9a7ea1734baeaf6f8c9 100644 (file)
  ***************************************************************************/
 
 #include "customruler.h"
+#include "kdenlivesettings.h"
 
 #include <KDebug>
 #include <KIcon>
 #include <KCursor>
 #include <KGlobalSettings>
+#include <KColorScheme>
 
+#include <QApplication>
 #include <QMouseEvent>
 #include <QStylePainter>
 
@@ -40,7 +43,8 @@ static const int LITTLE_MARK_LENGTH = (MIDDLE_MARK_LENGTH / 2);
 static const int LITTLE_MARK_X2 = LINE_END;
 static const int LITTLE_MARK_X1 = (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH);
 
-static const int LABEL_SIZE = 9;
+static int FRAME_SIZE;
+static int LABEL_SIZE;
 static const int END_LABEL_X = 4;
 static const int END_LABEL_Y = (END_LABEL_X + LABEL_SIZE - 2);
 
@@ -50,72 +54,147 @@ static int bigMarkDistance;
 
 #include "definitions.h"
 
-const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000, 12000};
-
-CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent)
-        : QWidget(parent), m_timecode(tc), m_view(parent), m_duration(0), m_offset(0)
+const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 750, 1500, 3000, 6000, 12000};
+
+CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) :
+        QWidget(parent),
+        m_timecode(tc),
+        m_view(parent),
+        m_duration(0),
+        m_offset(0),
+        m_clickedGuide(-1),
+        m_rate(-1),
+        m_mouseMove(NO_MOVE)
 {
     setFont(KGlobalSettings::toolBarFont());
+    QFontMetricsF fontMetrics(font());
+    LABEL_SIZE = fontMetrics.ascent() - 2;
+    updateFrameSize();
     m_scale = 3;
-    m_bgColor = QColor(245, 245, 245);
-    m_zoneColor = QColor(133, 255, 143);
-    littleMarkDistance = FRAME_SIZE;
-    mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
-    bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
+    m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
     m_zoneStart = 0;
     m_zoneEnd = 100;
     m_contextMenu = new QMenu(this);
     QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
     connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
-    QAction *editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
-    connect(editGuide, SIGNAL(triggered()), m_view, SLOT(slotEditGuide()));
-    QAction *delGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
-    connect(delGuide, SIGNAL(triggered()), m_view, SLOT(slotDeleteGuide()));
+    m_editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
+    connect(m_editGuide, SIGNAL(triggered()), this, SLOT(slotEditGuide()));
+    m_deleteGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
+    connect(m_deleteGuide , SIGNAL(triggered()), this, SLOT(slotDeleteGuide()));
     QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
     connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
+    m_goMenu = m_contextMenu->addMenu(i18n("Go To"));
+    connect(m_goMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotGoToGuide(QAction *)));
     setMouseTracking(true);
     setMinimumHeight(20);
 }
 
+void CustomRuler::updateProjectFps(Timecode t)
+{
+    m_timecode = t;
+    mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
+    bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
+    update();
+}
+
+void CustomRuler::updateFrameSize()
+{
+    FRAME_SIZE = m_view->getFrameWidth();
+    littleMarkDistance = FRAME_SIZE;
+    mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
+    bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
+    updateProjectFps(m_timecode);
+    if (m_rate > 0) setPixelPerMark(m_rate);
+}
+
+void CustomRuler::slotEditGuide()
+{
+    m_view->slotEditGuide(m_clickedGuide);
+}
+
+void CustomRuler::slotDeleteGuide()
+{
+    m_view->slotDeleteGuide(m_clickedGuide);
+}
+
+void CustomRuler::slotGoToGuide(QAction *act)
+{
+    m_view->setCursorPos(act->data().toInt(), true);
+    m_view->initCursorPos(act->data().toInt());
+}
+
 void CustomRuler::setZone(QPoint p)
 {
-    int min = qMin(m_zoneStart, p.x());
-    int max = qMax(m_zoneEnd, p.y());
     m_zoneStart = p.x();
     m_zoneEnd = p.y();
-    update(min * m_factor - 2, 0, (max - min) * m_factor + 4, height());
+    update();
+}
+
+void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
+{
+    if (m_moveCursor == RULER_START || m_moveCursor == RULER_END || m_moveCursor == RULER_MIDDLE) {
+        emit zoneMoved(m_zoneStart, m_zoneEnd);
+        m_view->setDocumentModified();
+    }
+    m_mouseMove = NO_MOVE;
+
 }
 
 // virtual
 void CustomRuler::mousePressEvent(QMouseEvent * event)
 {
+    int pos = (int)((event->x() + offset()));
     if (event->button() == Qt::RightButton) {
+        m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
+        m_editGuide->setEnabled(m_clickedGuide > 0);
+        m_deleteGuide->setEnabled(m_clickedGuide > 0);
+        m_view->buildGuidesMenu(m_goMenu);
         m_contextMenu->exec(event->globalPos());
         return;
     }
+    setFocus(Qt::MouseFocusReason);
     m_view->activateMonitor();
-    int pos = (int)((event->x() + offset()));
     m_moveCursor = RULER_CURSOR;
     if (event->y() > 10) {
         if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
         else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
+        m_view->updateSnapPoints(NULL);
     }
-    if (m_moveCursor == RULER_CURSOR)
+    if (m_moveCursor == RULER_CURSOR) {
         m_view->setCursorPos((int) pos / m_factor);
+        m_clickPoint = event->pos();
+        m_startRate = m_rate;
+    }
 }
 
 // virtual
 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
 {
     if (event->buttons() == Qt::LeftButton) {
-        int pos = (int)((event->x() + offset()) / m_factor);
+        int pos;
+        if (m_moveCursor == RULER_START || m_moveCursor == RULER_END) {
+            pos = m_view->getSnapPointForPos((int)((event->x() + offset()) / m_factor));
+        } else pos = (int)((event->x() + offset()) / m_factor);
         int zoneStart = m_zoneStart;
         int zoneEnd = m_zoneEnd;
         if (pos < 0) pos = 0;
         if (m_moveCursor == RULER_CURSOR) {
-            m_view->setCursorPos(pos);
-            m_view->slotCheckPositionScrolling();
+            QPoint diff = event->pos() - m_clickPoint;
+            if (m_mouseMove == NO_MOVE) {
+                if (!KdenliveSettings::verticalzoom() || qAbs(diff.x()) >= QApplication::startDragDistance()) {
+                    m_mouseMove = HORIZONTAL_MOVE;
+                } else if (qAbs(diff.y()) >= QApplication::startDragDistance()) {
+                    m_mouseMove = VERTICAL_MOVE;
+                } else return;
+            }
+            if (m_mouseMove == HORIZONTAL_MOVE) {
+                m_view->setCursorPos(pos);
+                m_view->slotCheckPositionScrolling();
+            } else {
+                int verticalDiff = m_startRate - (diff.y()) / 7;
+                if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
+            }
             return;
         } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
         else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
@@ -125,8 +204,6 @@ void CustomRuler::mouseMoveEvent(QMouseEvent * event)
             m_zoneStart += move;
             m_zoneEnd += move;
         }
-        emit zoneMoved(m_zoneStart, m_zoneEnd);
-        m_view->setDocumentModified();
 
         int min = qMin(m_zoneStart, zoneStart);
         int max = qMax(m_zoneEnd, zoneEnd);
@@ -135,10 +212,23 @@ void CustomRuler::mouseMoveEvent(QMouseEvent * event)
     } else {
         int pos = (int)((event->x() + offset()));
         if (event->y() <= 10) setCursor(Qt::ArrowCursor);
-        else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
-        else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
-        else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor);
-        else setCursor(Qt::ArrowCursor);
+        else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
+            setCursor(KCursor("left_side", Qt::SizeHorCursor));
+            if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
+            else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
+        } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
+            setCursor(KCursor("right_side", Qt::SizeHorCursor));
+            if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
+            else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
+        } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
+            setCursor(Qt::SizeHorCursor);
+            if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
+            else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
+        } else {
+            setCursor(Qt::ArrowCursor);
+            if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
+            else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
+        }
     }
 }
 
@@ -175,19 +265,33 @@ int CustomRuler::offset() const
 
 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
 {
-    if (qAbs(oldpos - newpos) * m_factor > 40) {
-        update(oldpos * m_factor - offset() - 6, 7, 17, 16);
-        update(newpos * m_factor - offset() - 6, 7, 17, 16);
-    } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 7, qAbs(oldpos - newpos) * m_factor + 17, 16);
+    if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) {
+        update(oldpos * m_factor - offset() - 6, 0, 17, height());
+        update(newpos * m_factor - offset() - 6, 0, 17, height());
+    } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 0, qAbs(oldpos - newpos) * m_factor + 17, height());
 }
 
-void CustomRuler::setPixelPerMark(double rate)
+void CustomRuler::setPixelPerMark(int rate)
 {
-    int scale = comboScale[(int) rate];
+    int scale = comboScale[rate];
+    m_rate = rate;
     m_factor = 1.0 / (double) scale * FRAME_SIZE;
     m_scale = 1.0 / (double) scale;
     double fend = m_scale * littleMarkDistance;
-    switch ((int) rate) {
+    if (rate > 8) {
+        mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
+        bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
+    } else if (rate > 6) {
+        mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
+        bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
+    } else if (rate > 3) {
+        mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
+        bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
+    } else {
+        mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
+        bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
+    }
+    switch (rate) {
     case 0:
         m_textSpacing = fend;
         break;
@@ -210,15 +314,17 @@ void CustomRuler::setPixelPerMark(double rate)
         break;
     case 8:
     case 9:
+        m_textSpacing = fend * m_timecode.fps() * 40;
+        break;
     case 10:
-        m_textSpacing = fend * m_timecode.fps() * 60;
+        m_textSpacing = fend * m_timecode.fps() * 80;
         break;
     case 11:
     case 12:
-        m_textSpacing = fend * m_timecode.fps() * 300;
+        m_textSpacing = fend * m_timecode.fps() * 400;
         break;
     case 13:
-        m_textSpacing = fend * m_timecode.fps() * 600;
+        m_textSpacing = fend * m_timecode.fps() * 800;
         break;
     }
     update();
@@ -236,56 +342,63 @@ void CustomRuler::paintEvent(QPaintEvent *e)
 {
     QStylePainter p(this);
     p.setClipRect(e->rect());
-
     const int projectEnd = (int)(m_duration * m_factor);
-    p.fillRect(0, 0, projectEnd - m_offset, height(), m_bgColor);
+    p.fillRect(0, 0, projectEnd - m_offset, height(), palette().alternateBase().color());
 
     const int zoneStart = (int)(m_zoneStart * m_factor);
     const int zoneEnd = (int)(m_zoneEnd * m_factor);
     const QRect zoneRect();
 
-    p.fillRect(zoneStart - offset(), height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
+    p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
 
-    const int value  = m_view->cursorPos() * m_factor - offset();
+    const int value  = m_view->cursorPos() * m_factor - m_offset;
     int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
     const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
-    if (minval < 0) minval = 0;
+    if (minval < 0)
+        minval = 0;
 
     double f, fend;
     const int offsetmax = maxval * FRAME_SIZE;
 
-    QPalette palette;
-    p.setPen(palette.dark().color());
+    p.setPen(palette().text().color());
+
+    // draw time labels
     int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
     offsetmin = offsetmin * m_textSpacing;
     for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
-        QString lab = m_timecode.getTimecodeFromFrames((int)((f) / m_factor + 0.5));
-        p.drawText((int)f - m_offset + 2, LABEL_SIZE, lab);
+        QString lab;
+        if (KdenliveSettings::frametimecode())
+            lab = QString::number((int)(f / m_factor + 0.5));
+        else
+            lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
+        p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
     }
 
-    if (true) {
-        offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
-        offsetmin = offsetmin * littleMarkDistance;
-        // draw the little marks
-        fend = m_scale * littleMarkDistance;
-        if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
-                p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
+    offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
+    offsetmin = offsetmin * littleMarkDistance;
+    // draw the little marks
+    fend = m_scale * littleMarkDistance;
+    if (fend > 5) {
+        for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
+            p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
     }
-    if (true) {
-        offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
-        offsetmin = offsetmin * mediumMarkDistance;
-        // draw medium marks
-        fend = m_scale * mediumMarkDistance;
-        if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
-                p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
+
+    offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
+    offsetmin = offsetmin * mediumMarkDistance;
+    // draw medium marks
+    fend = m_scale * mediumMarkDistance;
+    if (fend > 5) {
+        for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
+            p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
     }
-    if (true) {
-        offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
-        offsetmin = offsetmin * bigMarkDistance;
-        // draw big marks
-        fend = m_scale * bigMarkDistance;
-        if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
-                p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
+
+    offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
+    offsetmin = offsetmin * bigMarkDistance;
+    // draw big marks
+    fend = m_scale * bigMarkDistance;
+    if (fend > 5) {
+        for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
+            p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
     }
 
     // draw zone cursors
@@ -297,8 +410,10 @@ void CustomRuler::paintEvent(QPaintEvent *e)
     }
 
     if (zoneEnd > 0) {
+        QColor center(Qt::white);
+        center.setAlpha(150);
         QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
-        p.fillRect(rec, QColor(255, 255, 255, 150));
+        p.fillRect(rec, center);
         p.drawRect(rec);
 
         QPolygon pa(4);
@@ -308,8 +423,8 @@ void CustomRuler::paintEvent(QPaintEvent *e)
 
     // draw pointer
     QPolygon pa(3);
-    pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
-    p.setBrush(QBrush(Qt::yellow));
+    pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16);
+    p.setBrush(palette().highlight());
     p.drawPolygon(pa);
 }