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 #include "definitions.h"
49 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 750, 1500, 3000, 6000, 12000};
51 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) :
61 setFont(KGlobalSettings::toolBarFont());
62 QFontMetricsF fontMetrics(font());
63 // Define size variables
64 LABEL_SIZE = fontMetrics.ascent();
65 setMinimumHeight(LABEL_SIZE * 2);
66 setMaximumHeight(LABEL_SIZE * 2);
67 MAX_HEIGHT = height();
68 BIG_MARK_X = LABEL_SIZE + 1;
69 int mark_length = MAX_HEIGHT - BIG_MARK_X;
70 MIDDLE_MARK_X = BIG_MARK_X + mark_length / 2;
71 LITTLE_MARK_X = BIG_MARK_X + mark_length / 3;
74 m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
77 m_contextMenu = new QMenu(this);
78 QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
79 connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
80 m_editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
81 connect(m_editGuide, SIGNAL(triggered()), this, SLOT(slotEditGuide()));
82 m_deleteGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
83 connect(m_deleteGuide , SIGNAL(triggered()), this, SLOT(slotDeleteGuide()));
84 QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
85 connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
86 m_goMenu = m_contextMenu->addMenu(i18n("Go To"));
87 connect(m_goMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotGoToGuide(QAction *)));
88 setMouseTracking(true);
91 void CustomRuler::updatePalette()
93 m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
96 void CustomRuler::updateProjectFps(Timecode t)
99 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
100 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
104 void CustomRuler::updateFrameSize()
106 FRAME_SIZE = m_view->getFrameWidth();
107 littleMarkDistance = FRAME_SIZE;
108 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
109 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
110 updateProjectFps(m_timecode);
111 if (m_rate > 0) setPixelPerMark(m_rate);
114 void CustomRuler::slotEditGuide()
116 m_view->slotEditGuide(m_clickedGuide);
119 void CustomRuler::slotDeleteGuide()
121 m_view->slotDeleteGuide(m_clickedGuide);
124 void CustomRuler::slotGoToGuide(QAction *act)
126 m_view->setCursorPos(act->data().toInt(), true);
127 m_view->initCursorPos(act->data().toInt());
130 void CustomRuler::setZone(QPoint p)
137 void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
139 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END || m_moveCursor == RULER_MIDDLE) {
140 emit zoneMoved(m_zoneStart, m_zoneEnd);
141 m_view->setDocumentModified();
143 m_mouseMove = NO_MOVE;
148 void CustomRuler::mousePressEvent(QMouseEvent * event)
150 int pos = (int)((event->x() + offset()));
151 if (event->button() == Qt::RightButton) {
152 m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
153 m_editGuide->setEnabled(m_clickedGuide > 0);
154 m_deleteGuide->setEnabled(m_clickedGuide > 0);
155 m_view->buildGuidesMenu(m_goMenu);
156 m_contextMenu->exec(event->globalPos());
159 setFocus(Qt::MouseFocusReason);
160 m_view->activateMonitor();
161 m_moveCursor = RULER_CURSOR;
162 if (event->y() > 10) {
163 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
164 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
165 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
166 m_view->updateSnapPoints(NULL);
168 if (m_moveCursor == RULER_CURSOR) {
169 m_view->setCursorPos((int) pos / m_factor);
170 m_clickPoint = event->pos();
171 m_startRate = m_rate;
176 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
178 if (event->buttons() == Qt::LeftButton) {
180 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END) {
181 pos = m_view->getSnapPointForPos((int)((event->x() + offset()) / m_factor));
182 } else pos = (int)((event->x() + offset()) / m_factor);
183 int zoneStart = m_zoneStart;
184 int zoneEnd = m_zoneEnd;
185 if (pos < 0) pos = 0;
186 if (m_moveCursor == RULER_CURSOR) {
187 QPoint diff = event->pos() - m_clickPoint;
188 if (m_mouseMove == NO_MOVE) {
189 if (!KdenliveSettings::verticalzoom() || qAbs(diff.x()) >= QApplication::startDragDistance()) {
190 m_mouseMove = HORIZONTAL_MOVE;
191 } else if (qAbs(diff.y()) >= QApplication::startDragDistance()) {
192 m_mouseMove = VERTICAL_MOVE;
195 if (m_mouseMove == HORIZONTAL_MOVE) {
196 m_view->setCursorPos(pos);
197 m_view->slotCheckPositionScrolling();
199 int verticalDiff = m_startRate - (diff.y()) / 7;
200 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
203 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
204 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
205 else if (m_moveCursor == RULER_MIDDLE) {
206 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
207 if (move + m_zoneStart < 0) move = - m_zoneStart;
212 int min = qMin(m_zoneStart, zoneStart);
213 int max = qMax(m_zoneEnd, zoneEnd);
214 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
217 int pos = (int)((event->x() + offset()));
218 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
219 else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
220 setCursor(KCursor("left_side", Qt::SizeHorCursor));
221 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
222 else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
223 } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
224 setCursor(KCursor("right_side", Qt::SizeHorCursor));
225 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
226 else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
227 } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
228 setCursor(Qt::SizeHorCursor);
229 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
230 else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
232 setCursor(Qt::ArrowCursor);
233 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
234 else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
241 void CustomRuler::wheelEvent(QWheelEvent * e)
244 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
245 if (e->delta() < 0) delta = 0 - delta;
246 m_view->moveCursorPos(delta);
249 int CustomRuler::inPoint() const
254 int CustomRuler::outPoint() const
259 void CustomRuler::slotMoveRuler(int newPos)
265 int CustomRuler::offset() const
270 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
272 if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) {
273 update(oldpos * m_factor - offset() - 6, BIG_MARK_X, 14, MAX_HEIGHT - BIG_MARK_X);
274 update(newpos * m_factor - offset() - 6, BIG_MARK_X, 14, MAX_HEIGHT - BIG_MARK_X);
275 } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, BIG_MARK_X, qAbs(oldpos - newpos) * m_factor + 14, MAX_HEIGHT - BIG_MARK_X);
278 void CustomRuler::setPixelPerMark(int rate)
280 int scale = comboScale[rate];
282 m_factor = 1.0 / (double) scale * FRAME_SIZE;
283 m_scale = 1.0 / (double) scale;
284 double fend = m_scale * littleMarkDistance;
286 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
287 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
288 } else if (rate > 6) {
289 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
290 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
291 } else if (rate > 3) {
292 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
293 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
295 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
296 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
300 m_textSpacing = fend;
303 m_textSpacing = fend * 5;
308 m_textSpacing = fend * m_timecode.fps();
311 m_textSpacing = fend * m_timecode.fps() * 5;
314 m_textSpacing = fend * m_timecode.fps() * 10;
317 m_textSpacing = fend * m_timecode.fps() * 30;
321 m_textSpacing = fend * m_timecode.fps() * 40;
324 m_textSpacing = fend * m_timecode.fps() * 80;
328 m_textSpacing = fend * m_timecode.fps() * 400;
331 m_textSpacing = fend * m_timecode.fps() * 800;
337 void CustomRuler::setDuration(int d)
339 int oldduration = m_duration;
341 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
345 void CustomRuler::paintEvent(QPaintEvent *e)
347 QStylePainter p(this);
348 p.setClipRect(e->rect());
351 //p.fillRect(0, 0, m_duration * m_factor - m_offset, MAX_HEIGHT, palette().alternateBase().color());
353 // Draw zone background
354 const int zoneStart = (int)(m_zoneStart * m_factor);
355 const int zoneEnd = (int)(m_zoneEnd * m_factor);
356 p.fillRect(zoneStart - m_offset, LABEL_SIZE + 2, zoneEnd - zoneStart, MAX_HEIGHT - LABEL_SIZE - 2, m_zoneColor);
358 int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
359 const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
364 const int offsetmax = maxval * FRAME_SIZE;
367 p.setPen(palette().text().color());
370 if (e->rect().y() < LABEL_SIZE) {
371 offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
372 offsetmin = offsetmin * m_textSpacing;
373 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
375 if (KdenliveSettings::frametimecode())
376 lab = QString::number((int)(f / m_factor + 0.5));
378 lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
379 p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
383 offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
384 offsetmin = offsetmin * littleMarkDistance;
385 // draw the little marks
386 fend = m_scale * littleMarkDistance;
388 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
389 p.drawLine((int)f, LITTLE_MARK_X, (int)f, MAX_HEIGHT);
392 offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
393 offsetmin = offsetmin * mediumMarkDistance;
395 fend = m_scale * mediumMarkDistance;
397 for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
398 p.drawLine((int)f, MIDDLE_MARK_X, (int)f, MAX_HEIGHT);
401 offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
402 offsetmin = offsetmin * bigMarkDistance;
404 fend = m_scale * bigMarkDistance;
406 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
407 p.drawLine((int)f, BIG_MARK_X, (int)f, MAX_HEIGHT);
413 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);
418 QColor center(Qt::white);
419 center.setAlpha(150);
420 QRect rec(zoneStart - m_offset + (zoneEnd - zoneStart) / 2 - 4, LABEL_SIZE + 2, 8, MAX_HEIGHT - LABEL_SIZE - 3);
421 p.fillRect(rec, center);
425 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);
430 const int value = m_view->cursorPos() * m_factor - m_offset;
432 pa.setPoints(3, value - 6, BIG_MARK_X, value + 6, BIG_MARK_X, value, MAX_HEIGHT - 1);
433 p.setBrush(palette().highlight());
437 #include "customruler.moc"