+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());
+ // 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 = 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()));
+ 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);
+}
+
+void CustomRuler::updatePalette()
+{
+ m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
+}
+
+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)
+{
+ m_zoneStart = p.x();
+ m_zoneEnd = p.y();
+ 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();
+ 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) {
+ 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;
+ 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) {
+ 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;
+ 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;
+ }
+
+ 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()));
+ if (event->y() <= 10) 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)));
+ }
+ }
+}
+
+
+// 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() const
+{
+ return m_zoneEnd;
+}
+
+void CustomRuler::slotMoveRuler(int newPos)
+{
+ m_offset = newPos;
+ update();
+}
+
+int CustomRuler::offset() const
+{
+ return m_offset;
+}
+
+void CustomRuler::slotCursorMoved(int oldpos, int newpos)
+{
+ if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) {
+ update(oldpos * m_factor - offset() - 6, BIG_MARK_X, 14, MAX_HEIGHT - BIG_MARK_X);
+ update(newpos * m_factor - offset() - 6, BIG_MARK_X, 14, MAX_HEIGHT - BIG_MARK_X);
+ } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, BIG_MARK_X, qAbs(oldpos - newpos) * m_factor + 14, MAX_HEIGHT - BIG_MARK_X);
+}