1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "customruler.h"
21 #include "kdenlivesettings.h"
26 #include <KGlobalSettings>
27 #include <KColorScheme>
29 #include <QApplication>
30 #include <QMouseEvent>
31 #include <QStylePainter>
33 static int MAX_HEIGHT;
34 // Width of a frame in pixels
35 static int FRAME_SIZE;
36 // Height of the timecode text
37 static int LABEL_SIZE;
39 static int BIG_MARK_X;
40 static int MIDDLE_MARK_X;
41 static int LITTLE_MARK_X;
43 static int littleMarkDistance;
44 static int mediumMarkDistance;
45 static int bigMarkDistance;
47 #define SEEK_INACTIVE (-1)
49 #include "definitions.h"
51 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 750, 1500, 3000, 6000, 12000};
53 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) :
59 m_lastSeekPosition(SEEK_INACTIVE),
63 m_cursorColor(palette().text())
65 setFont(KGlobalSettings::toolBarFont());
66 QFontMetricsF fontMetrics(font());
67 // Define size variables
68 LABEL_SIZE = fontMetrics.ascent();
69 setMinimumHeight(LABEL_SIZE * 2);
70 setMaximumHeight(LABEL_SIZE * 2);
71 MAX_HEIGHT = height();
72 BIG_MARK_X = LABEL_SIZE + 1;
73 int mark_length = MAX_HEIGHT - BIG_MARK_X;
74 MIDDLE_MARK_X = BIG_MARK_X + mark_length / 2;
75 LITTLE_MARK_X = BIG_MARK_X + mark_length / 3;
78 m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
81 m_contextMenu = new QMenu(this);
82 QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
83 connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
84 m_editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
85 connect(m_editGuide, SIGNAL(triggered()), this, SLOT(slotEditGuide()));
86 m_deleteGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
87 connect(m_deleteGuide , SIGNAL(triggered()), this, SLOT(slotDeleteGuide()));
88 QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
89 connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
90 m_goMenu = m_contextMenu->addMenu(i18n("Go To"));
91 connect(m_goMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotGoToGuide(QAction *)));
92 setMouseTracking(true);
95 void CustomRuler::updatePalette()
97 m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
100 void CustomRuler::updateProjectFps(Timecode t)
103 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
104 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
108 void CustomRuler::updateFrameSize()
110 FRAME_SIZE = m_view->getFrameWidth();
111 littleMarkDistance = FRAME_SIZE;
112 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
113 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
114 updateProjectFps(m_timecode);
115 if (m_rate > 0) setPixelPerMark(m_rate);
118 void CustomRuler::slotEditGuide()
120 m_view->slotEditGuide(m_clickedGuide);
123 void CustomRuler::slotDeleteGuide()
125 m_view->slotDeleteGuide(m_clickedGuide);
128 void CustomRuler::slotGoToGuide(QAction *act)
130 m_view->seekCursorPos(act->data().toInt());
131 m_view->initCursorPos(act->data().toInt());
134 void CustomRuler::setZone(QPoint p)
141 void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
143 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END || m_moveCursor == RULER_MIDDLE) {
144 emit zoneMoved(m_zoneStart, m_zoneEnd);
145 m_view->setDocumentModified();
147 m_mouseMove = NO_MOVE;
152 void CustomRuler::mousePressEvent(QMouseEvent * event)
154 int pos = (int)((event->x() + offset()));
155 if (event->button() == Qt::RightButton) {
156 m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
157 m_editGuide->setEnabled(m_clickedGuide > 0);
158 m_deleteGuide->setEnabled(m_clickedGuide > 0);
159 m_view->buildGuidesMenu(m_goMenu);
160 m_contextMenu->exec(event->globalPos());
163 setFocus(Qt::MouseFocusReason);
164 m_view->activateMonitor();
165 m_moveCursor = RULER_CURSOR;
166 if (event->y() > 10) {
167 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
168 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
169 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
170 m_view->updateSnapPoints(NULL);
172 if (m_moveCursor == RULER_CURSOR) {
173 m_view->seekCursorPos((int) pos / m_factor);
174 m_clickPoint = event->pos();
175 m_startRate = m_rate;
180 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
182 if (event->buttons() == Qt::LeftButton) {
184 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END) {
185 pos = m_view->getSnapPointForPos((int)((event->x() + offset()) / m_factor));
186 } else pos = (int)((event->x() + offset()) / m_factor);
187 int zoneStart = m_zoneStart;
188 int zoneEnd = m_zoneEnd;
189 if (pos < 0) pos = 0;
190 if (m_moveCursor == RULER_CURSOR) {
191 QPoint diff = event->pos() - m_clickPoint;
192 if (m_mouseMove == NO_MOVE) {
193 if (!KdenliveSettings::verticalzoom() || qAbs(diff.x()) >= QApplication::startDragDistance()) {
194 m_mouseMove = HORIZONTAL_MOVE;
195 } else if (qAbs(diff.y()) >= QApplication::startDragDistance()) {
196 m_mouseMove = VERTICAL_MOVE;
199 if (m_mouseMove == HORIZONTAL_MOVE) {
200 m_view->seekCursorPos(pos);
201 m_view->slotCheckPositionScrolling();
203 int verticalDiff = m_startRate - (diff.y()) / 7;
204 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
207 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
208 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
209 else if (m_moveCursor == RULER_MIDDLE) {
210 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
211 if (move + m_zoneStart < 0) move = - m_zoneStart;
215 int min = qMin(m_zoneStart, zoneStart);
216 int max = qMax(m_zoneEnd, zoneEnd);
217 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
220 int pos = (int)((event->x() + m_offset));
221 if (m_cursorColor == palette().text() && qAbs(pos - m_view->cursorPos() * m_factor) < 7) {
222 // Mouse is over cursor
223 m_cursorColor = palette().highlight();
224 update(m_view->cursorPos() * m_factor - m_offset - 10, 0, 20, height());
226 else if (m_cursorColor == palette().highlight() && qAbs(pos - m_view->cursorPos() * m_factor) >= 7) {
227 m_cursorColor = palette().text();
228 update(m_view->cursorPos() * m_factor - m_offset - 10, 0, 20, height());
231 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
232 else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
233 setCursor(KCursor("left_side", Qt::SizeHorCursor));
234 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
235 else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
236 } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
237 setCursor(KCursor("right_side", Qt::SizeHorCursor));
238 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
239 else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
240 } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
241 setCursor(Qt::SizeHorCursor);
242 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
243 else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
245 setCursor(Qt::ArrowCursor);
246 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
247 else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
254 void CustomRuler::leaveEvent(QEvent * event)
256 QWidget::leaveEvent(event);
257 if (m_cursorColor == palette().highlight()) {
258 m_cursorColor = palette().text();
264 void CustomRuler::wheelEvent(QWheelEvent * e)
267 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
268 if (e->delta() < 0) delta = 0 - delta;
269 m_view->moveCursorPos(delta);
272 int CustomRuler::inPoint() const
277 int CustomRuler::outPoint() const
282 void CustomRuler::slotMoveRuler(int newPos)
288 int CustomRuler::offset() const
293 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
295 int min = qMin(oldpos, newpos);
296 int max = qMax(oldpos, newpos);
297 if (m_lastSeekPosition != SEEK_INACTIVE) {
298 if (m_lastSeekPosition == newpos) {
299 m_lastSeekPosition = SEEK_INACTIVE;
302 min = qMin(min, m_lastSeekPosition);
303 max = qMax(max, m_lastSeekPosition);
306 update(min * m_factor - m_offset - 6, BIG_MARK_X, (max - min) * m_factor + 14, MAX_HEIGHT - BIG_MARK_X);
309 void CustomRuler::updateRuler()
311 // Update requested seek position
312 int min = SEEK_INACTIVE;
313 int max = SEEK_INACTIVE;
314 if (m_lastSeekPosition != SEEK_INACTIVE) {
315 min = max = m_lastSeekPosition;
317 m_lastSeekPosition = m_view->seekPosition();
318 if (m_lastSeekPosition != SEEK_INACTIVE) {
319 if (min == SEEK_INACTIVE) {
320 min = max = m_lastSeekPosition;
323 min = qMin(min, m_lastSeekPosition);
324 max = qMax(max, m_lastSeekPosition);
327 if (min != SEEK_INACTIVE) {
328 update(min * m_factor - offset() - 3, BIG_MARK_X, (max - min) * m_factor + 6, MAX_HEIGHT - BIG_MARK_X);
332 void CustomRuler::setPixelPerMark(int rate)
334 int scale = comboScale[rate];
336 m_factor = 1.0 / (double) scale * FRAME_SIZE;
337 m_scale = 1.0 / (double) scale;
338 double fend = m_scale * littleMarkDistance;
340 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
341 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
342 } else if (rate > 6) {
343 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
344 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
345 } else if (rate > 3) {
346 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
347 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
349 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
350 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
354 m_textSpacing = fend;
357 m_textSpacing = fend * 5;
362 m_textSpacing = fend * m_timecode.fps();
365 m_textSpacing = fend * m_timecode.fps() * 5;
368 m_textSpacing = fend * m_timecode.fps() * 10;
371 m_textSpacing = fend * m_timecode.fps() * 30;
375 m_textSpacing = fend * m_timecode.fps() * 40;
378 m_textSpacing = fend * m_timecode.fps() * 80;
382 m_textSpacing = fend * m_timecode.fps() * 400;
385 m_textSpacing = fend * m_timecode.fps() * 800;
391 void CustomRuler::setDuration(int d)
393 int oldduration = m_duration;
395 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
399 void CustomRuler::paintEvent(QPaintEvent *e)
401 QStylePainter p(this);
402 const QRect &paintRect = e->rect();
403 p.setClipRect(paintRect);
405 // Draw zone background
406 const int zoneStart = (int)(m_zoneStart * m_factor);
407 const int zoneEnd = (int)(m_zoneEnd * m_factor);
408 p.fillRect(zoneStart - m_offset, LABEL_SIZE + 2, zoneEnd - zoneStart, MAX_HEIGHT - LABEL_SIZE - 2, m_zoneColor);
410 int minval = (paintRect.left() + m_offset) / FRAME_SIZE - 1;
411 const int maxval = (paintRect.right() + m_offset) / FRAME_SIZE + 1;
416 const int offsetmax = maxval * FRAME_SIZE;
419 p.setPen(palette().text().color());
422 if (paintRect.y() < LABEL_SIZE) {
423 offsetmin = (paintRect.left() + m_offset) / m_textSpacing;
424 offsetmin = offsetmin * m_textSpacing;
425 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
427 if (KdenliveSettings::frametimecode())
428 lab = QString::number((int)(f / m_factor + 0.5));
430 lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
431 p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
435 offsetmin = (paintRect.left() + m_offset) / littleMarkDistance;
436 offsetmin = offsetmin * littleMarkDistance;
437 // draw the little marks
438 fend = m_scale * littleMarkDistance;
440 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
441 p.drawLine((int)f, LITTLE_MARK_X, (int)f, MAX_HEIGHT);
444 offsetmin = (paintRect.left() + m_offset) / mediumMarkDistance;
445 offsetmin = offsetmin * mediumMarkDistance;
447 fend = m_scale * mediumMarkDistance;
449 for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
450 p.drawLine((int)f, MIDDLE_MARK_X, (int)f, MAX_HEIGHT);
453 offsetmin = (paintRect.left() + m_offset) / bigMarkDistance;
454 offsetmin = offsetmin * bigMarkDistance;
456 fend = m_scale * bigMarkDistance;
458 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
459 p.drawLine((int)f, BIG_MARK_X, (int)f, MAX_HEIGHT);
465 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);
470 QColor center(Qt::white);
471 center.setAlpha(150);
472 QRect rec(zoneStart - m_offset + (zoneEnd - zoneStart) / 2 - 4, LABEL_SIZE + 2, 8, MAX_HEIGHT - LABEL_SIZE - 3);
473 p.fillRect(rec, center);
477 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);
482 const int value = m_view->cursorPos() * m_factor - m_offset;
484 pa.setPoints(3, value - 6, BIG_MARK_X, value + 6, BIG_MARK_X, value, MAX_HEIGHT - 1);
485 p.setBrush(m_cursorColor);
489 if (m_lastSeekPosition != SEEK_INACTIVE && m_lastSeekPosition != m_view->cursorPos()) {
490 p.fillRect(m_lastSeekPosition * m_factor - m_offset - 1, BIG_MARK_X, 3, MAX_HEIGHT - 1, palette().highlight());
495 #include "customruler.moc"