]> git.sesse.net Git - kdenlive/blobdiff - src/customruler.cpp
Make monitor zone functional
[kdenlive] / src / customruler.cpp
index a56f74225e82e37f12fef038edfbb551f271d6d0..44563d23d697aff41d793041f837bc46ce8b82cb 100644 (file)
 #include <QStylePainter>
 
 #include <KDebug>
-
+#include <KIcon>
+#include <KCursor>
+#include <KGlobalSettings>
 
 #include "customruler.h"
 
 
-#define INIT_VALUE 0
-#define INIT_MIN_VALUE 0
-#define INIT_MAX_VALUE 100
-#define INIT_TINY_MARK_DISTANCE 1
-#define INIT_LITTLE_MARK_DISTANCE 5
-#define INIT_MIDDLE_MARK_DISTANCE (INIT_LITTLE_MARK_DISTANCE * 2)
-#define INIT_BIG_MARK_DISTANCE (INIT_LITTLE_MARK_DISTANCE * 10)
-#define INIT_SHOW_TINY_MARK false
-#define INIT_SHOW_LITTLE_MARK true
-#define INIT_SHOW_MEDIUM_MARK true
-#define INIT_SHOW_BIG_MARK true
-#define INIT_SHOW_END_MARK true
-#define INIT_SHOW_POINTER true
-#define INIT_SHOW_END_LABEL true
-
-#define INIT_PIXEL_PER_MARK (double)10.0 /* distance between 2 base marks in pixel */
-#define INIT_OFFSET (-20)
-#define INIT_LENGTH_FIX true
-#define INIT_END_OFFSET 0
-
-#define FIX_WIDTH 20 /* widget width in pixel */
-#define LINE_END (FIX_WIDTH - 3)
-#define END_MARK_LENGTH (FIX_WIDTH - 6)
-#define END_MARK_X2 LINE_END
-#define END_MARK_X1 (END_MARK_X2 - END_MARK_LENGTH)
-#define BIG_MARK_LENGTH (END_MARK_LENGTH*3/4)
-#define BIG_MARK_X2 LINE_END
-#define BIG_MARK_X1 (BIG_MARK_X2 - BIG_MARK_LENGTH)
-#define MIDDLE_MARK_LENGTH (END_MARK_LENGTH/2)
-#define MIDDLE_MARK_X2 LINE_END
-#define MIDDLE_MARK_X1 (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH)
-#define LITTLE_MARK_LENGTH (MIDDLE_MARK_LENGTH/2)
-#define LITTLE_MARK_X2 LINE_END
-#define LITTLE_MARK_X1 (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH)
-#define BASE_MARK_LENGTH (LITTLE_MARK_LENGTH/2)
-#define BASE_MARK_X2 LINE_END
-#define BASE_MARK_X1 (BASE_MARK_X2 - 3) //BASE_MARK_LENGTH
-
-#define LABEL_SIZE 8
-#define END_LABEL_X 4
-#define END_LABEL_Y (END_LABEL_X + LABEL_SIZE - 2)
+static const int FIX_WIDTH = 24; /* widget width in pixel */
+static const int LINE_END = (FIX_WIDTH - 3);
+static const int END_MARK_LENGTH = (FIX_WIDTH - 8);
+static const int BIG_MARK_LENGTH = (END_MARK_LENGTH * 3 / 4);
+static const int BIG_MARK_X2 = LINE_END;
+static const int BIG_MARK_X1 = (BIG_MARK_X2 - BIG_MARK_LENGTH);
+static const int MIDDLE_MARK_LENGTH = (END_MARK_LENGTH / 2);
+static const int MIDDLE_MARK_X2 = LINE_END;
+static const int MIDDLE_MARK_X1 = (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH);
+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 const int END_LABEL_X = 4;
+static const int END_LABEL_Y = (END_LABEL_X + LABEL_SIZE - 2);
 
 #include "definitions.h"
 
-const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000,
-                                        12000
-                                      };
+const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000, 12000};
 
 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent)
-        : KRuler(parent), m_timecode(tc), m_view(parent) {
+        : KRuler(parent), m_timecode(tc), m_view(parent), m_duration(0) {
+    setFont(KGlobalSettings::toolBarFont());
     slotNewOffset(0);
     setRulerMetricStyle(KRuler::Pixel);
     setLength(1024);
     setMaximum(1024);
     setPixelPerMark(3);
     setLittleMarkDistance(FRAME_SIZE);
-    setMediumMarkDistance(FRAME_SIZE * 25);
-    setBigMarkDistance(FRAME_SIZE * 25 * 60);
-    m_zoneStart = 50;
-    m_zoneEnd = 250;
+    setMediumMarkDistance(FRAME_SIZE * m_timecode.fps());
+    setBigMarkDistance(FRAME_SIZE * m_timecode.fps() * 60);
+    m_zoneStart = 2 * m_timecode.fps();
+    m_zoneEnd = 10 * m_timecode.fps();
+    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()));
+    QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
+    connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
+    setMouseTracking(true);
+}
+
+void CustomRuler::setZone(QPoint p) {
+    m_zoneStart = p.x();
+    m_zoneEnd = p.y();
+    update();
 }
 
 // virtual
 void CustomRuler::mousePressEvent(QMouseEvent * event) {
+    if (event->button() == Qt::RightButton) {
+        m_contextMenu->exec(event->globalPos());
+        return;
+    }
     m_view->activateMonitor();
-    int pos = (event->x() + offset()) / pixelPerMark() / FRAME_SIZE;
+    int pos = (int)((event->x() + offset()));
     m_moveCursor = RULER_CURSOR;
     if (event->y() > 10) {
-        if (abs(pos - m_zoneStart) < 4) m_moveCursor = RULER_START;
-        else if (abs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2)) < 4) m_moveCursor = RULER_MIDDLE;
-        else if (abs(pos - m_zoneEnd) < 4) m_moveCursor = RULER_END;
+        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;
     }
     if (m_moveCursor == RULER_CURSOR)
-        m_view->setCursorPos(pos);
+        m_view->setCursorPos((int) pos / m_factor);
 }
 
 // virtual
 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
-    int pos = (event->x() + offset()) / pixelPerMark() / FRAME_SIZE;
-    if (m_moveCursor == RULER_CURSOR) {
-        m_view->setCursorPos(pos);
-        return;
-    } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
-    else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
-    else if (m_moveCursor == RULER_MIDDLE) {
-        int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
-        m_zoneStart += move;
-        m_zoneEnd += move;
+    if (event->buttons() == Qt::LeftButton) {
+        int pos = (int)((event->x() + offset()) / m_factor);
+        if (pos < 0) pos = 0;
+        if (m_moveCursor == RULER_CURSOR) {
+            m_view->setCursorPos(pos);
+            return;
+        } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
+        else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
+        else if (m_moveCursor == RULER_MIDDLE) {
+            int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
+            m_zoneStart += move;
+            m_zoneEnd += move;
+        }
+        emit zoneMoved(m_zoneStart, m_zoneEnd);
+        m_view->setDocumentModified();
+        update();
+    } 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);
     }
-    update();
 }
 
-int CustomRuler::inPoint() {
+
+// virtual
+void CustomRuler::wheelEvent(QWheelEvent * e) {
+    int delta = 1;
+    if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
+    if (e->delta() < 0) delta = 0 - delta;
+    m_view->moveCursorPos(delta);
+}
+
+int CustomRuler::inPoint() const {
     return m_zoneStart;
 }
 
-int CustomRuler::outPoint() {
+int CustomRuler::outPoint() const {
     return m_zoneEnd;
 }
 
@@ -131,47 +148,82 @@ void CustomRuler::slotMoveRuler(int newPos) {
 }
 
 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
-    //TODO: optimize (redraw only around cursor positions
-    update();
+    update(oldpos * m_factor - offset() - 6, 2, 17, 16);
+    update(newpos * m_factor - offset() - 6, 2, 17, 16);
 }
 
 void CustomRuler::setPixelPerMark(double rate) {
     int scale = comboScale[(int) rate];
+    m_factor = 1.0 / (double) scale * FRAME_SIZE;
     KRuler::setPixelPerMark(1.0 / scale);
+    double fend = pixelPerMark() * littleMarkDistance();
+    switch ((int) rate) {
+    case 0:
+        m_textSpacing = fend;
+        break;
+    case 1:
+        m_textSpacing = fend * 5;
+        break;
+    case 2:
+    case 3:
+    case 4:
+        m_textSpacing = fend * m_timecode.fps();
+        break;
+    case 5:
+        m_textSpacing = fend * m_timecode.fps() * 5;
+        break;
+    case 6:
+        m_textSpacing = fend * m_timecode.fps() * 10;
+        break;
+    case 7:
+        m_textSpacing = fend * m_timecode.fps() * 30;
+        break;
+    case 8:
+    case 9:
+    case 10:
+        m_textSpacing = fend * m_timecode.fps() * 60;
+        break;
+    case 11:
+    case 12:
+        m_textSpacing = fend * m_timecode.fps() * 300;
+        break;
+    case 13:
+        m_textSpacing = fend * m_timecode.fps() * 600;
+        break;
+    }
+}
+
+void CustomRuler::setDuration(int d) {
+    m_duration = d;
+    update();
 }
 
 // virtual
 void CustomRuler::paintEvent(QPaintEvent *e) {
-    //  debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical");
-
     QStylePainter p(this);
     p.setClipRect(e->rect());
-    p.fillRect(e->rect(), QBrush(QColor(255, 255, 255, 80)));
-    //kDebug()<<"RULER ZONE: "<<m_zoneStart<<", OFF: "<<offset()<<", END: "<<m_zoneEnd<<", FACTOR: "<<pixelPerMark() * FRAME_SIZE;
-    int zoneStart = (m_zoneStart) * pixelPerMark() * FRAME_SIZE;
-    int zoneEnd = (m_zoneEnd) * pixelPerMark() * FRAME_SIZE;
-    p.fillRect(QRect(zoneStart - offset(), e->rect().y() + e->rect().height() / 2, zoneEnd - zoneStart, e->rect().height() / 2), QBrush(QColor(133, 255, 143)));
-
-    int value  = m_view->cursorPos() - offset() + 4;
-    int minval = minimum();
-    int maxval = maximum() + offset() - endOffset();
-
-    //ioffsetval = value-offset;
-    //    pixelpm = (int)ppm;
-    //    left  = clip.left(),
-    //    right = clip.right();
+
+    const int projectEnd = (int)(m_duration * m_factor);
+    p.fillRect(QRect(0, 0, projectEnd - offset(), height()), QBrush(QColor(245, 245, 245)));
+
+    const int zoneStart = (int)(m_zoneStart * m_factor);
+    const int zoneEnd = (int)(m_zoneEnd * m_factor);
+
+    p.fillRect(QRect(zoneStart - offset(), height() / 2, zoneEnd - zoneStart, height() / 2), QBrush(QColor(133, 255, 143)));
+
+    const int value  = m_view->cursorPos() * m_factor - offset();
+    const int minval = minimum();
+    const int maxval = maximum() + offset() - endOffset();
+
     double f, fend,
     offsetmin = (double)(minval - offset()),
                 offsetmax = (double)(maxval - offset()),
                             fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
-    QRect bg = QRect(offsetmin, 0, offsetmax, height());
+    QRect bg = QRect((int)offsetmin, 0, (int)offsetmax, height());
 
     QPalette palette;
     //p.fillRect(bg, palette.light());
     // draw labels
-    QFont font = p.font();
-    font.setPointSize(LABEL_SIZE);
-    p.setFont(font);
     p.setPen(palette.dark().color());
     // draw littlemarklabel
 
@@ -194,41 +246,29 @@ void CustomRuler::paintEvent(QPaintEvent *e) {
           p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
       }
     }*/
+
+    for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
+        QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / m_factor + 0.5));
+        p.drawText((int)f + 2, LABEL_SIZE, lab);
+    }
+
     if (showLittleMarks()) {
         // draw the little marks
         fend = pixelPerMark() * littleMarkDistance();
-        if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend) {
+        if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
                 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
-                if (fend > 60) {
-                    QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
-                    p.drawText((int)f + 2, LABEL_SIZE, lab);
-                }
-            }
     }
     if (showMediumMarks()) {
         // draw medium marks
         fend = pixelPerMark() * mediumMarkDistance();
-        if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend) {
+        if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
                 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
-                if (fend > 60) {
-                    QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
-                    p.drawText((int)f + 2, LABEL_SIZE, lab);
-                }
-            }
     }
     if (showBigMarks()) {
         // draw big marks
         fend = pixelPerMark() * bigMarkDistance();
-        if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend) {
+        if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
                 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
-                if (fend > 60) {
-                    QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
-                    p.drawText((int)f + 2, LABEL_SIZE, lab);
-                } else if (((int)(f - offsetmin)) % ((int)(fend * 5)) == 0) {
-                    QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
-                    p.drawText((int)f + 2, LABEL_SIZE, lab);
-                }
-            }
     }
     /*   if (d->showem) {
          // draw end marks
@@ -262,15 +302,10 @@ void CustomRuler::paintEvent(QPaintEvent *e) {
     }
 
     // draw pointer
-    if (showPointer() && value > 0) {
-        QPolygon pa(3);
-        pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
-        p.setBrush(QBrush(Qt::yellow));
-        p.drawPolygon(pa);
-    }
-
-
-
+    QPolygon pa(3);
+    pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
+    p.setBrush(QBrush(Qt::yellow));
+    p.drawPolygon(pa);
 }
 
 #include "customruler.moc"