X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fcustomruler.cpp;h=08370c21e1a973ae3719142c5fb85391067d694e;hb=56aee6aedeeed3efd10ada8fe3c229eddc01ef05;hp=b7aaa48135ee640fd46fd0c6b6c3000efaaf00f6;hpb=bb109649bb39246bc2e7bd6d3b59994afb33b3bc;p=kdenlive diff --git a/src/customruler.cpp b/src/customruler.cpp index b7aaa481..08370c21 100644 --- a/src/customruler.cpp +++ b/src/customruler.cpp @@ -24,53 +24,59 @@ #include #include #include +#include #include #include #include -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 int MAX_HEIGHT; +// Width of a frame in pixels +static int FRAME_SIZE; +// Height of the timecode text static int LABEL_SIZE; -static const int END_LABEL_X = 4; -static const int END_LABEL_Y = (END_LABEL_X + LABEL_SIZE - 2); + +static int BIG_MARK_X; +static int MIDDLE_MARK_X; +static int LITTLE_MARK_X; static int littleMarkDistance; static int mediumMarkDistance; static int bigMarkDistance; +#define SEEK_INACTIVE (-1) + #include "definitions.h" const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 750, 1500, 3000, 6000, 12000}; -CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) : +CustomRuler::CustomRuler(const Timecode &tc, CustomTrackView *parent) : QWidget(parent), m_timecode(tc), m_view(parent), m_duration(0), m_offset(0), + m_lastSeekPosition(SEEK_INACTIVE), m_clickedGuide(-1), - m_mouseMove(NO_MOVE) + m_rate(-1), + m_mouseMove(NO_MOVE), + m_cursorColor(palette().text()) { setFont(KGlobalSettings::toolBarFont()); QFontMetricsF fontMetrics(font()); - LABEL_SIZE = fontMetrics.ascent() - 2; + // Define size variables + LABEL_SIZE = fontMetrics.ascent(); + setMinimumHeight(LABEL_SIZE * 2); + setMaximumHeight(LABEL_SIZE * 2); + MAX_HEIGHT = height(); + BIG_MARK_X = LABEL_SIZE + 1; + int mark_length = MAX_HEIGHT - BIG_MARK_X; + MIDDLE_MARK_X = BIG_MARK_X + mark_length / 2; + LITTLE_MARK_X = BIG_MARK_X + mark_length / 3; + updateFrameSize(); m_scale = 3; - 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::FocusColor, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color(); + m_zoneColor.setAlpha(180); m_zoneStart = 0; m_zoneEnd = 100; m_contextMenu = new QMenu(this); @@ -83,12 +89,17 @@ CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) : 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 *))); + connect(m_goMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotGoToGuide(QAction*))); setMouseTracking(true); - setMinimumHeight(20); } -void CustomRuler::updateProjectFps(Timecode t) +void CustomRuler::updatePalette() +{ + m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::FocusColor, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color(); + m_zoneColor.setAlpha(180); +} + +void CustomRuler::updateProjectFps(const Timecode &t) { m_timecode = t; mediumMarkDistance = FRAME_SIZE * m_timecode.fps(); @@ -96,6 +107,16 @@ void CustomRuler::updateProjectFps(Timecode t) 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); @@ -108,10 +129,11 @@ void CustomRuler::slotDeleteGuide() void CustomRuler::slotGoToGuide(QAction *act) { + m_view->seekCursorPos(act->data().toInt()); m_view->initCursorPos(act->data().toInt()); } -void CustomRuler::setZone(QPoint p) +void CustomRuler::setZone(const QPoint &p) { m_zoneStart = p.x(); m_zoneEnd = p.y(); @@ -120,7 +142,12 @@ void CustomRuler::setZone(QPoint p) 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 @@ -131,7 +158,7 @@ void CustomRuler::mousePressEvent(QMouseEvent * event) 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_view->buildGuidesMenu(m_goMenu); m_contextMenu->exec(event->globalPos()); return; } @@ -140,11 +167,12 @@ void CustomRuler::mousePressEvent(QMouseEvent * event) 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_zoneStart + (m_zoneEnd - m_zoneStart) / 2.0) * 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) { - m_view->setCursorPos((int) pos / m_factor); + m_view->seekCursorPos((int) pos / m_factor); m_clickPoint = event->pos(); m_startRate = m_rate; } @@ -153,45 +181,59 @@ void CustomRuler::mousePressEvent(QMouseEvent * event) // virtual void CustomRuler::mouseMoveEvent(QMouseEvent * event) { + int mappedXPos = (int)((event->x() + offset()) / m_factor); + emit mousePosition(mappedXPos); 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(mappedXPos); + } else pos = mappedXPos; int zoneStart = m_zoneStart; int zoneEnd = m_zoneEnd; if (pos < 0) pos = 0; if (m_moveCursor == RULER_CURSOR) { QPoint diff = event->pos() - m_clickPoint; if (m_mouseMove == NO_MOVE) { - if (!KdenliveSettings::verticalzoom() || qAbs(diff.x()) >= QApplication::startDragDistance()) { + if (qAbs(diff.x()) >= QApplication::startDragDistance()) { m_mouseMove = HORIZONTAL_MOVE; - } else if (qAbs(diff.y()) >= QApplication::startDragDistance()) { + } else if (KdenliveSettings::verticalzoom() && qAbs(diff.y()) >= QApplication::startDragDistance()) { m_mouseMove = VERTICAL_MOVE; } else return; } if (m_mouseMove == HORIZONTAL_MOVE) { - m_view->setCursorPos(pos); - m_view->slotCheckPositionScrolling(); + if (pos != m_lastSeekPosition && pos != m_view->cursorPos()) { + m_view->seekCursorPos(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; + } else if (m_moveCursor == RULER_START) m_zoneStart = qMin(pos, m_zoneEnd - 1); + else if (m_moveCursor == RULER_END) m_zoneEnd = qMax(pos, m_zoneStart + 1); else if (m_moveCursor == RULER_MIDDLE) { int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2); if (move + m_zoneStart < 0) move = - m_zoneStart; 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); update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height()); } else { - int pos = (int)((event->x() + offset())); + int pos = (int)((event->x() + m_offset)); + if (m_cursorColor == palette().text() && qAbs(pos - m_view->cursorPos() * m_factor) < 7) { + // Mouse is over cursor + m_cursorColor = palette().link(); + update(m_view->cursorPos() * m_factor - m_offset - 10, LABEL_SIZE + 2, 20, MAX_HEIGHT - LABEL_SIZE - 2); + } + else if (m_cursorColor == palette().link() && qAbs(pos - m_view->cursorPos() * m_factor) >= 7) { + m_cursorColor = palette().text(); + update(m_view->cursorPos() * m_factor - m_offset - 10, LABEL_SIZE + 2, 20, MAX_HEIGHT - LABEL_SIZE - 2); + } + if (event->y() <= 10) setCursor(Qt::ArrowCursor); else if (qAbs(pos - m_zoneStart * m_factor) < 4) { setCursor(KCursor("left_side", Qt::SizeHorCursor)); @@ -201,7 +243,7 @@ void CustomRuler::mouseMoveEvent(QMouseEvent * event) 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) { + } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2.0) * 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))); @@ -214,10 +256,21 @@ void CustomRuler::mouseMoveEvent(QMouseEvent * event) } +// virtual +void CustomRuler::leaveEvent(QEvent * event) +{ + QWidget::leaveEvent(event); + if (m_cursorColor == palette().link()) { + m_cursorColor = palette().text(); + update(); + } +} + // virtual void CustomRuler::wheelEvent(QWheelEvent * e) { int delta = 1; + m_view->activateMonitor(); if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps(); if (e->delta() < 0) delta = 0 - delta; m_view->moveCursorPos(delta); @@ -235,8 +288,10 @@ int CustomRuler::outPoint() const void CustomRuler::slotMoveRuler(int newPos) { - m_offset = newPos; - update(); + if (m_offset != newPos) { + m_offset = newPos; + update(); + } } int CustomRuler::offset() const @@ -246,10 +301,41 @@ int CustomRuler::offset() const void CustomRuler::slotCursorMoved(int oldpos, int newpos) { - 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()); + int min = qMin(oldpos, newpos); + int max = qMax(oldpos, newpos); + if (m_lastSeekPosition != SEEK_INACTIVE) { + if (m_lastSeekPosition == newpos) { + m_lastSeekPosition = SEEK_INACTIVE; + } + else { + min = qMin(min, m_lastSeekPosition); + max = qMax(max, m_lastSeekPosition); + } + } + update(min * m_factor - m_offset - 6, BIG_MARK_X, (max - min) * m_factor + 14, MAX_HEIGHT - BIG_MARK_X); +} + +void CustomRuler::updateRuler() +{ + // Update requested seek position + int min = SEEK_INACTIVE; + int max = SEEK_INACTIVE; + if (m_lastSeekPosition != SEEK_INACTIVE) { + min = max = m_lastSeekPosition; + } + m_lastSeekPosition = m_view->seekPosition(); + if (m_lastSeekPosition != SEEK_INACTIVE) { + if (min == SEEK_INACTIVE) { + min = max = m_lastSeekPosition; + } + else { + min = qMin(min, m_lastSeekPosition); + max = qMax(max, m_lastSeekPosition); + } + } + if (min != SEEK_INACTIVE) { + update(min * m_factor - offset() - 3, BIG_MARK_X, (max - min) * m_factor + 6, MAX_HEIGHT - BIG_MARK_X); + } } void CustomRuler::setPixelPerMark(int rate) @@ -322,80 +408,97 @@ void CustomRuler::setDuration(int d) 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(), palette().alternateBase().color()); + const QRect &paintRect = e->rect(); + p.setClipRect(paintRect); + // Draw zone background const int zoneStart = (int)(m_zoneStart * m_factor); const int zoneEnd = (int)(m_zoneEnd * m_factor); - const QRect zoneRect(); - - p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor); - - 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; + p.fillRect(zoneStart - m_offset, LABEL_SIZE + 2, zoneEnd - zoneStart, MAX_HEIGHT - LABEL_SIZE - 2, m_zoneColor); + + int minval = (paintRect.left() + m_offset) / FRAME_SIZE - 1; + const int maxval = (paintRect.right() + m_offset) / FRAME_SIZE + 1; + if (minval < 0) + minval = 0; double f, fend; const int offsetmax = maxval * FRAME_SIZE; + int offsetmin; - 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(f - m_offset + 2, LABEL_SIZE, lab); + if (paintRect.y() < LABEL_SIZE) { + offsetmin = (paintRect.left() + m_offset) / m_textSpacing; + offsetmin = offsetmin * m_textSpacing; + for (f = offsetmin; f < offsetmax; f += m_textSpacing) { + 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); + } } - - offsetmin = (e->rect().left() + m_offset) / littleMarkDistance; + p.setPen(palette().dark().color()); + offsetmin = (paintRect.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 (fend > 5) { + for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend) + p.drawLine((int)f, LITTLE_MARK_X, (int)f, MAX_HEIGHT); + } - offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance; + offsetmin = (paintRect.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 (fend > 5) { + for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend) + p.drawLine((int)f, MIDDLE_MARK_X, (int)f, MAX_HEIGHT); + } - offsetmin = (e->rect().left() + m_offset) / bigMarkDistance; + offsetmin = (paintRect.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); + if (fend > 5) { + for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend) + p.drawLine((int)f, BIG_MARK_X, (int)f, MAX_HEIGHT); + } // draw zone cursors - int off = offset(); if (zoneStart > 0) { QPolygon pa(4); - pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18); + pa.setPoints(4, zoneStart - m_offset + 3, LABEL_SIZE + 2, zoneStart - m_offset, LABEL_SIZE + 2, zoneStart - m_offset, MAX_HEIGHT - 1, zoneStart - m_offset + 3, MAX_HEIGHT - 1); p.drawPolyline(pa); } if (zoneEnd > 0) { QColor center(Qt::white); center.setAlpha(150); - QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9); + QRect rec(zoneStart - m_offset + (zoneEnd - zoneStart) / 2 - 4, LABEL_SIZE + 2, 8, MAX_HEIGHT - LABEL_SIZE - 3); p.fillRect(rec, center); p.drawRect(rec); QPolygon pa(4); - pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18); + pa.setPoints(4, zoneEnd - m_offset - 3, LABEL_SIZE + 2, zoneEnd - m_offset, LABEL_SIZE + 2, zoneEnd - m_offset, MAX_HEIGHT - 1, zoneEnd - m_offset - 3, MAX_HEIGHT - 1); p.drawPolyline(pa); } - + // draw pointer + const int value = m_view->cursorPos() * m_factor - m_offset; QPolygon pa(3); - pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16); - p.setBrush(palette().highlight()); + pa.setPoints(3, value - 6, LABEL_SIZE + 3, value + 6, LABEL_SIZE + 3, value, MAX_HEIGHT); + p.setBrush(m_cursorColor); + p.setPen(Qt::NoPen); p.drawPolygon(pa); + + if (m_lastSeekPosition != SEEK_INACTIVE && m_lastSeekPosition != m_view->cursorPos()) { + p.fillRect(m_lastSeekPosition * m_factor - m_offset - 1, BIG_MARK_X + 1, 3, MAX_HEIGHT - BIG_MARK_X - 1, palette().linkVisited()); + } + } #include "customruler.moc"