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::FocusColor, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
79 m_zoneColor.setAlpha(180);
82 m_contextMenu = new QMenu(this);
83 QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
84 connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
85 m_editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
86 connect(m_editGuide, SIGNAL(triggered()), this, SLOT(slotEditGuide()));
87 m_deleteGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
88 connect(m_deleteGuide , SIGNAL(triggered()), this, SLOT(slotDeleteGuide()));
89 QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
90 connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
91 m_goMenu = m_contextMenu->addMenu(i18n("Go To"));
92 connect(m_goMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotGoToGuide(QAction *)));
93 setMouseTracking(true);
96 void CustomRuler::updatePalette()
98 m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::FocusColor, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
99 m_zoneColor.setAlpha(180);
102 void CustomRuler::updateProjectFps(Timecode t)
105 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
106 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
110 void CustomRuler::updateFrameSize()
112 FRAME_SIZE = m_view->getFrameWidth();
113 littleMarkDistance = FRAME_SIZE;
114 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
115 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
116 updateProjectFps(m_timecode);
117 if (m_rate > 0) setPixelPerMark(m_rate);
120 void CustomRuler::slotEditGuide()
122 m_view->slotEditGuide(m_clickedGuide);
125 void CustomRuler::slotDeleteGuide()
127 m_view->slotDeleteGuide(m_clickedGuide);
130 void CustomRuler::slotGoToGuide(QAction *act)
132 m_view->seekCursorPos(act->data().toInt());
133 m_view->initCursorPos(act->data().toInt());
136 void CustomRuler::setZone(QPoint p)
143 void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
145 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END || m_moveCursor == RULER_MIDDLE) {
146 emit zoneMoved(m_zoneStart, m_zoneEnd);
147 m_view->setDocumentModified();
149 m_mouseMove = NO_MOVE;
154 void CustomRuler::mousePressEvent(QMouseEvent * event)
156 int pos = (int)((event->x() + offset()));
157 if (event->button() == Qt::RightButton) {
158 m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
159 m_editGuide->setEnabled(m_clickedGuide > 0);
160 m_deleteGuide->setEnabled(m_clickedGuide > 0);
161 m_view->buildGuidesMenu(m_goMenu);
162 m_contextMenu->exec(event->globalPos());
165 setFocus(Qt::MouseFocusReason);
166 m_view->activateMonitor();
167 m_moveCursor = RULER_CURSOR;
168 if (event->y() > 10) {
169 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
170 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2.0) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
171 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
172 m_view->updateSnapPoints(NULL);
174 if (m_moveCursor == RULER_CURSOR) {
175 m_view->seekCursorPos((int) pos / m_factor);
176 m_clickPoint = event->pos();
177 m_startRate = m_rate;
182 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
184 if (event->buttons() == Qt::LeftButton) {
186 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END) {
187 pos = m_view->getSnapPointForPos((int)((event->x() + offset()) / m_factor));
188 } else pos = (int)((event->x() + offset()) / m_factor);
189 int zoneStart = m_zoneStart;
190 int zoneEnd = m_zoneEnd;
191 if (pos < 0) pos = 0;
192 if (m_moveCursor == RULER_CURSOR) {
193 QPoint diff = event->pos() - m_clickPoint;
194 if (m_mouseMove == NO_MOVE) {
195 if (qAbs(diff.x()) >= QApplication::startDragDistance()) {
196 m_mouseMove = HORIZONTAL_MOVE;
197 } else if (KdenliveSettings::verticalzoom() && qAbs(diff.y()) >= QApplication::startDragDistance()) {
198 m_mouseMove = VERTICAL_MOVE;
201 if (m_mouseMove == HORIZONTAL_MOVE) {
202 if (pos != m_lastSeekPosition && pos != m_view->cursorPos()) {
203 m_view->seekCursorPos(pos);
204 m_view->slotCheckPositionScrolling();
207 int verticalDiff = m_startRate - (diff.y()) / 7;
208 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
211 } else if (m_moveCursor == RULER_START) m_zoneStart = qMin(pos, m_zoneEnd - 1);
212 else if (m_moveCursor == RULER_END) m_zoneEnd = qMax(pos, m_zoneStart + 1);
213 else if (m_moveCursor == RULER_MIDDLE) {
214 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
215 if (move + m_zoneStart < 0) move = - m_zoneStart;
219 int min = qMin(m_zoneStart, zoneStart);
220 int max = qMax(m_zoneEnd, zoneEnd);
221 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
224 int pos = (int)((event->x() + m_offset));
225 if (m_cursorColor == palette().text() && qAbs(pos - m_view->cursorPos() * m_factor) < 7) {
226 // Mouse is over cursor
227 m_cursorColor = palette().link();
228 update(m_view->cursorPos() * m_factor - m_offset - 10, LABEL_SIZE + 2, 20, MAX_HEIGHT - LABEL_SIZE - 2);
230 else if (m_cursorColor == palette().link() && qAbs(pos - m_view->cursorPos() * m_factor) >= 7) {
231 m_cursorColor = palette().text();
232 update(m_view->cursorPos() * m_factor - m_offset - 10, LABEL_SIZE + 2, 20, MAX_HEIGHT - LABEL_SIZE - 2);
235 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
236 else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
237 setCursor(KCursor("left_side", Qt::SizeHorCursor));
238 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
239 else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
240 } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
241 setCursor(KCursor("right_side", Qt::SizeHorCursor));
242 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
243 else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
244 } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2.0) * m_factor) < 4) {
245 setCursor(Qt::SizeHorCursor);
246 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
247 else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
249 setCursor(Qt::ArrowCursor);
250 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
251 else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
258 void CustomRuler::leaveEvent(QEvent * event)
260 QWidget::leaveEvent(event);
261 if (m_cursorColor == palette().link()) {
262 m_cursorColor = palette().text();
268 void CustomRuler::wheelEvent(QWheelEvent * e)
271 m_view->activateMonitor();
272 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
273 if (e->delta() < 0) delta = 0 - delta;
274 m_view->moveCursorPos(delta);
277 int CustomRuler::inPoint() const
282 int CustomRuler::outPoint() const
287 void CustomRuler::slotMoveRuler(int newPos)
293 int CustomRuler::offset() const
298 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
300 int min = qMin(oldpos, newpos);
301 int max = qMax(oldpos, newpos);
302 if (m_lastSeekPosition != SEEK_INACTIVE) {
303 if (m_lastSeekPosition == newpos) {
304 m_lastSeekPosition = SEEK_INACTIVE;
307 min = qMin(min, m_lastSeekPosition);
308 max = qMax(max, m_lastSeekPosition);
311 update(min * m_factor - m_offset - 6, BIG_MARK_X, (max - min) * m_factor + 14, MAX_HEIGHT - BIG_MARK_X);
314 void CustomRuler::updateRuler()
316 // Update requested seek position
317 int min = SEEK_INACTIVE;
318 int max = SEEK_INACTIVE;
319 if (m_lastSeekPosition != SEEK_INACTIVE) {
320 min = max = m_lastSeekPosition;
322 m_lastSeekPosition = m_view->seekPosition();
323 if (m_lastSeekPosition != SEEK_INACTIVE) {
324 if (min == SEEK_INACTIVE) {
325 min = max = m_lastSeekPosition;
328 min = qMin(min, m_lastSeekPosition);
329 max = qMax(max, m_lastSeekPosition);
332 if (min != SEEK_INACTIVE) {
333 update(min * m_factor - offset() - 3, BIG_MARK_X, (max - min) * m_factor + 6, MAX_HEIGHT - BIG_MARK_X);
337 void CustomRuler::setPixelPerMark(int rate)
339 int scale = comboScale[rate];
341 m_factor = 1.0 / (double) scale * FRAME_SIZE;
342 m_scale = 1.0 / (double) scale;
343 double fend = m_scale * littleMarkDistance;
345 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
346 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
347 } else if (rate > 6) {
348 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
349 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
350 } else if (rate > 3) {
351 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
352 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
354 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
355 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
359 m_textSpacing = fend;
362 m_textSpacing = fend * 5;
367 m_textSpacing = fend * m_timecode.fps();
370 m_textSpacing = fend * m_timecode.fps() * 5;
373 m_textSpacing = fend * m_timecode.fps() * 10;
376 m_textSpacing = fend * m_timecode.fps() * 30;
380 m_textSpacing = fend * m_timecode.fps() * 40;
383 m_textSpacing = fend * m_timecode.fps() * 80;
387 m_textSpacing = fend * m_timecode.fps() * 400;
390 m_textSpacing = fend * m_timecode.fps() * 800;
396 void CustomRuler::setDuration(int d)
398 int oldduration = m_duration;
400 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
404 void CustomRuler::paintEvent(QPaintEvent *e)
406 QStylePainter p(this);
407 const QRect &paintRect = e->rect();
408 p.setClipRect(paintRect);
410 // Draw zone background
411 const int zoneStart = (int)(m_zoneStart * m_factor);
412 const int zoneEnd = (int)(m_zoneEnd * m_factor);
413 p.fillRect(zoneStart - m_offset, LABEL_SIZE + 2, zoneEnd - zoneStart, MAX_HEIGHT - LABEL_SIZE - 2, m_zoneColor);
415 int minval = (paintRect.left() + m_offset) / FRAME_SIZE - 1;
416 const int maxval = (paintRect.right() + m_offset) / FRAME_SIZE + 1;
421 const int offsetmax = maxval * FRAME_SIZE;
424 p.setPen(palette().text().color());
427 if (paintRect.y() < LABEL_SIZE) {
428 offsetmin = (paintRect.left() + m_offset) / m_textSpacing;
429 offsetmin = offsetmin * m_textSpacing;
430 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
432 if (KdenliveSettings::frametimecode())
433 lab = QString::number((int)(f / m_factor + 0.5));
435 lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
436 p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
439 p.setPen(palette().dark().color());
440 offsetmin = (paintRect.left() + m_offset) / littleMarkDistance;
441 offsetmin = offsetmin * littleMarkDistance;
442 // draw the little marks
443 fend = m_scale * littleMarkDistance;
445 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
446 p.drawLine((int)f, LITTLE_MARK_X, (int)f, MAX_HEIGHT);
449 offsetmin = (paintRect.left() + m_offset) / mediumMarkDistance;
450 offsetmin = offsetmin * mediumMarkDistance;
452 fend = m_scale * mediumMarkDistance;
454 for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
455 p.drawLine((int)f, MIDDLE_MARK_X, (int)f, MAX_HEIGHT);
458 offsetmin = (paintRect.left() + m_offset) / bigMarkDistance;
459 offsetmin = offsetmin * bigMarkDistance;
461 fend = m_scale * bigMarkDistance;
463 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
464 p.drawLine((int)f, BIG_MARK_X, (int)f, MAX_HEIGHT);
470 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);
475 QColor center(Qt::white);
476 center.setAlpha(150);
477 QRect rec(zoneStart - m_offset + (zoneEnd - zoneStart) / 2 - 4, LABEL_SIZE + 2, 8, MAX_HEIGHT - LABEL_SIZE - 3);
478 p.fillRect(rec, center);
482 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);
487 const int value = m_view->cursorPos() * m_factor - m_offset;
489 pa.setPoints(3, value - 6, LABEL_SIZE + 3, value + 6, LABEL_SIZE + 3, value, MAX_HEIGHT);
490 p.setBrush(m_cursorColor);
494 if (m_lastSeekPosition != SEEK_INACTIVE && m_lastSeekPosition != m_view->cursorPos()) {
495 p.fillRect(m_lastSeekPosition * m_factor - m_offset - 1, BIG_MARK_X + 1, 3, MAX_HEIGHT - BIG_MARK_X - 1, palette().linkVisited());
500 #include "customruler.moc"