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),
64 setFont(KGlobalSettings::toolBarFont());
65 QFontMetricsF fontMetrics(font());
66 // Define size variables
67 LABEL_SIZE = fontMetrics.ascent();
68 setMinimumHeight(LABEL_SIZE * 2);
69 setMaximumHeight(LABEL_SIZE * 2);
70 MAX_HEIGHT = height();
71 BIG_MARK_X = LABEL_SIZE + 1;
72 int mark_length = MAX_HEIGHT - BIG_MARK_X;
73 MIDDLE_MARK_X = BIG_MARK_X + mark_length / 2;
74 LITTLE_MARK_X = BIG_MARK_X + mark_length / 3;
77 m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
80 m_contextMenu = new QMenu(this);
81 QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
82 connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
83 m_editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
84 connect(m_editGuide, SIGNAL(triggered()), this, SLOT(slotEditGuide()));
85 m_deleteGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
86 connect(m_deleteGuide , SIGNAL(triggered()), this, SLOT(slotDeleteGuide()));
87 QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
88 connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
89 m_goMenu = m_contextMenu->addMenu(i18n("Go To"));
90 connect(m_goMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotGoToGuide(QAction *)));
91 setMouseTracking(true);
94 void CustomRuler::updatePalette()
96 m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
99 void CustomRuler::updateProjectFps(Timecode t)
102 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
103 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
107 void CustomRuler::updateFrameSize()
109 FRAME_SIZE = m_view->getFrameWidth();
110 littleMarkDistance = FRAME_SIZE;
111 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
112 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
113 updateProjectFps(m_timecode);
114 if (m_rate > 0) setPixelPerMark(m_rate);
117 void CustomRuler::slotEditGuide()
119 m_view->slotEditGuide(m_clickedGuide);
122 void CustomRuler::slotDeleteGuide()
124 m_view->slotDeleteGuide(m_clickedGuide);
127 void CustomRuler::slotGoToGuide(QAction *act)
129 m_view->seekCursorPos(act->data().toInt());
130 m_view->initCursorPos(act->data().toInt());
133 void CustomRuler::setZone(QPoint p)
140 void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
142 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END || m_moveCursor == RULER_MIDDLE) {
143 emit zoneMoved(m_zoneStart, m_zoneEnd);
144 m_view->setDocumentModified();
146 m_mouseMove = NO_MOVE;
151 void CustomRuler::mousePressEvent(QMouseEvent * event)
153 int pos = (int)((event->x() + offset()));
154 if (event->button() == Qt::RightButton) {
155 m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
156 m_editGuide->setEnabled(m_clickedGuide > 0);
157 m_deleteGuide->setEnabled(m_clickedGuide > 0);
158 m_view->buildGuidesMenu(m_goMenu);
159 m_contextMenu->exec(event->globalPos());
162 setFocus(Qt::MouseFocusReason);
163 m_view->activateMonitor();
164 m_moveCursor = RULER_CURSOR;
165 if (event->y() > 10) {
166 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
167 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
168 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
169 m_view->updateSnapPoints(NULL);
171 if (m_moveCursor == RULER_CURSOR) {
172 m_view->seekCursorPos((int) pos / m_factor);
173 m_clickPoint = event->pos();
174 m_startRate = m_rate;
179 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
181 if (event->buttons() == Qt::LeftButton) {
183 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END) {
184 pos = m_view->getSnapPointForPos((int)((event->x() + offset()) / m_factor));
185 } else pos = (int)((event->x() + offset()) / m_factor);
186 int zoneStart = m_zoneStart;
187 int zoneEnd = m_zoneEnd;
188 if (pos < 0) pos = 0;
189 if (m_moveCursor == RULER_CURSOR) {
190 QPoint diff = event->pos() - m_clickPoint;
191 if (m_mouseMove == NO_MOVE) {
192 if (!KdenliveSettings::verticalzoom() || qAbs(diff.x()) >= QApplication::startDragDistance()) {
193 m_mouseMove = HORIZONTAL_MOVE;
194 } else if (qAbs(diff.y()) >= QApplication::startDragDistance()) {
195 m_mouseMove = VERTICAL_MOVE;
198 if (m_mouseMove == HORIZONTAL_MOVE) {
199 m_view->seekCursorPos(pos);
200 m_view->slotCheckPositionScrolling();
202 int verticalDiff = m_startRate - (diff.y()) / 7;
203 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
206 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
207 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
208 else if (m_moveCursor == RULER_MIDDLE) {
209 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
210 if (move + m_zoneStart < 0) move = - m_zoneStart;
214 int min = qMin(m_zoneStart, zoneStart);
215 int max = qMax(m_zoneEnd, zoneEnd);
216 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
219 int pos = (int)((event->x() + offset()));
220 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
221 else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
222 setCursor(KCursor("left_side", Qt::SizeHorCursor));
223 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
224 else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
225 } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
226 setCursor(KCursor("right_side", Qt::SizeHorCursor));
227 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
228 else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
229 } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
230 setCursor(Qt::SizeHorCursor);
231 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
232 else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
234 setCursor(Qt::ArrowCursor);
235 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
236 else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
243 void CustomRuler::wheelEvent(QWheelEvent * e)
246 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
247 if (e->delta() < 0) delta = 0 - delta;
248 m_view->moveCursorPos(delta);
251 int CustomRuler::inPoint() const
256 int CustomRuler::outPoint() const
261 void CustomRuler::slotMoveRuler(int newPos)
267 int CustomRuler::offset() const
272 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
274 int min = qMin(oldpos, newpos);
275 int max = qMax(oldpos, newpos);
276 if (m_lastSeekPosition != SEEK_INACTIVE) {
277 if (m_lastSeekPosition == newpos) {
278 m_lastSeekPosition = SEEK_INACTIVE;
281 min = qMin(min, m_lastSeekPosition);
282 max = qMax(max, m_lastSeekPosition);
285 update(min * m_factor - offset() - 6, BIG_MARK_X, (max - min) * m_factor + 14, MAX_HEIGHT - BIG_MARK_X);
288 void CustomRuler::updateRuler()
290 // Update requested seek position
291 int min = SEEK_INACTIVE;
292 int max = SEEK_INACTIVE;
293 if (m_lastSeekPosition != SEEK_INACTIVE) {
294 min = max = m_lastSeekPosition;
296 m_lastSeekPosition = m_view->seekPosition();
297 if (m_lastSeekPosition != SEEK_INACTIVE) {
298 if (min == SEEK_INACTIVE) {
299 min = max = m_lastSeekPosition;
302 min = qMin(min, m_lastSeekPosition);
303 max = qMax(max, m_lastSeekPosition);
306 if (min != SEEK_INACTIVE) {
307 update(min * m_factor - offset() - 3, BIG_MARK_X, (max - min) * m_factor + 6, MAX_HEIGHT - BIG_MARK_X);
311 void CustomRuler::setPixelPerMark(int rate)
313 int scale = comboScale[rate];
315 m_factor = 1.0 / (double) scale * FRAME_SIZE;
316 m_scale = 1.0 / (double) scale;
317 double fend = m_scale * littleMarkDistance;
319 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
320 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
321 } else if (rate > 6) {
322 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
323 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
324 } else if (rate > 3) {
325 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
326 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
328 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
329 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
333 m_textSpacing = fend;
336 m_textSpacing = fend * 5;
341 m_textSpacing = fend * m_timecode.fps();
344 m_textSpacing = fend * m_timecode.fps() * 5;
347 m_textSpacing = fend * m_timecode.fps() * 10;
350 m_textSpacing = fend * m_timecode.fps() * 30;
354 m_textSpacing = fend * m_timecode.fps() * 40;
357 m_textSpacing = fend * m_timecode.fps() * 80;
361 m_textSpacing = fend * m_timecode.fps() * 400;
364 m_textSpacing = fend * m_timecode.fps() * 800;
370 void CustomRuler::setDuration(int d)
372 int oldduration = m_duration;
374 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
378 void CustomRuler::paintEvent(QPaintEvent *e)
380 QStylePainter p(this);
381 const QRect &paintRect = e->rect();
382 p.setClipRect(paintRect);
384 // Draw zone background
385 const int zoneStart = (int)(m_zoneStart * m_factor);
386 const int zoneEnd = (int)(m_zoneEnd * m_factor);
387 p.fillRect(zoneStart - m_offset, LABEL_SIZE + 2, zoneEnd - zoneStart, MAX_HEIGHT - LABEL_SIZE - 2, m_zoneColor);
389 int minval = (paintRect.left() + m_offset) / FRAME_SIZE - 1;
390 const int maxval = (paintRect.right() + m_offset) / FRAME_SIZE + 1;
395 const int offsetmax = maxval * FRAME_SIZE;
398 p.setPen(palette().text().color());
401 if (paintRect.y() < LABEL_SIZE) {
402 offsetmin = (paintRect.left() + m_offset) / m_textSpacing;
403 offsetmin = offsetmin * m_textSpacing;
404 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
406 if (KdenliveSettings::frametimecode())
407 lab = QString::number((int)(f / m_factor + 0.5));
409 lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
410 p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
414 offsetmin = (paintRect.left() + m_offset) / littleMarkDistance;
415 offsetmin = offsetmin * littleMarkDistance;
416 // draw the little marks
417 fend = m_scale * littleMarkDistance;
419 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
420 p.drawLine((int)f, LITTLE_MARK_X, (int)f, MAX_HEIGHT);
423 offsetmin = (paintRect.left() + m_offset) / mediumMarkDistance;
424 offsetmin = offsetmin * mediumMarkDistance;
426 fend = m_scale * mediumMarkDistance;
428 for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
429 p.drawLine((int)f, MIDDLE_MARK_X, (int)f, MAX_HEIGHT);
432 offsetmin = (paintRect.left() + m_offset) / bigMarkDistance;
433 offsetmin = offsetmin * bigMarkDistance;
435 fend = m_scale * bigMarkDistance;
437 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
438 p.drawLine((int)f, BIG_MARK_X, (int)f, MAX_HEIGHT);
444 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);
449 QColor center(Qt::white);
450 center.setAlpha(150);
451 QRect rec(zoneStart - m_offset + (zoneEnd - zoneStart) / 2 - 4, LABEL_SIZE + 2, 8, MAX_HEIGHT - LABEL_SIZE - 3);
452 p.fillRect(rec, center);
456 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);
461 const int value = m_view->cursorPos() * m_factor - m_offset;
463 pa.setPoints(3, value - 6, BIG_MARK_X, value + 6, BIG_MARK_X, value, MAX_HEIGHT - 1);
464 p.setBrush(palette().text());
468 if (m_lastSeekPosition != SEEK_INACTIVE && m_lastSeekPosition != m_view->cursorPos()) {
469 p.fillRect(m_lastSeekPosition * m_factor - m_offset - 1, BIG_MARK_X, 3, MAX_HEIGHT - 1, palette().highlight());
474 #include "customruler.moc"