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>
28 #include <QApplication>
29 #include <QMouseEvent>
30 #include <QStylePainter>
32 static const int FIX_WIDTH = 24; /* widget width in pixel */
33 static const int LINE_END = (FIX_WIDTH - 3);
34 static const int END_MARK_LENGTH = (FIX_WIDTH - 8);
35 static const int BIG_MARK_LENGTH = (END_MARK_LENGTH * 3 / 4);
36 static const int BIG_MARK_X2 = LINE_END;
37 static const int BIG_MARK_X1 = (BIG_MARK_X2 - BIG_MARK_LENGTH);
38 static const int MIDDLE_MARK_LENGTH = (END_MARK_LENGTH / 2);
39 static const int MIDDLE_MARK_X2 = LINE_END;
40 static const int MIDDLE_MARK_X1 = (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH);
41 static const int LITTLE_MARK_LENGTH = (MIDDLE_MARK_LENGTH / 2);
42 static const int LITTLE_MARK_X2 = LINE_END;
43 static const int LITTLE_MARK_X1 = (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH);
45 static int LABEL_SIZE;
46 static const int END_LABEL_X = 4;
47 static const int END_LABEL_Y = (END_LABEL_X + LABEL_SIZE - 2);
49 static int littleMarkDistance;
50 static int mediumMarkDistance;
51 static int bigMarkDistance;
53 #include "definitions.h"
55 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 750, 1500, 3000, 6000, 12000};
57 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) :
66 setFont(KGlobalSettings::toolBarFont());
67 QFontMetricsF fontMetrics(font());
68 LABEL_SIZE = fontMetrics.ascent() - 2;
70 m_zoneColor = QColor(133, 255, 143);
71 littleMarkDistance = FRAME_SIZE;
72 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
73 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
76 m_contextMenu = new QMenu(this);
77 QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
78 connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
79 m_editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
80 connect(m_editGuide, SIGNAL(triggered()), this, SLOT(slotEditGuide()));
81 m_deleteGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
82 connect(m_deleteGuide , SIGNAL(triggered()), this, SLOT(slotDeleteGuide()));
83 QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
84 connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
85 m_goMenu = m_contextMenu->addMenu(i18n("Go To"));
86 connect(m_goMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotGoToGuide(QAction *)));
87 setMouseTracking(true);
91 void CustomRuler::updateProjectFps(Timecode t)
94 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
95 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
99 void CustomRuler::slotEditGuide()
101 m_view->slotEditGuide(m_clickedGuide);
104 void CustomRuler::slotDeleteGuide()
106 m_view->slotDeleteGuide(m_clickedGuide);
109 void CustomRuler::slotGoToGuide(QAction *act)
111 m_view->setCursorPos(act->data().toInt(), true);
112 m_view->initCursorPos(act->data().toInt());
115 void CustomRuler::setZone(QPoint p)
122 void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
124 m_mouseMove = NO_MOVE;
128 void CustomRuler::mousePressEvent(QMouseEvent * event)
130 int pos = (int)((event->x() + offset()));
131 if (event->button() == Qt::RightButton) {
132 m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
133 m_editGuide->setEnabled(m_clickedGuide > 0);
134 m_deleteGuide->setEnabled(m_clickedGuide > 0);
135 m_view->buildGuidesMenu(m_goMenu);
136 m_contextMenu->exec(event->globalPos());
139 setFocus(Qt::MouseFocusReason);
140 m_view->activateMonitor();
141 m_moveCursor = RULER_CURSOR;
142 if (event->y() > 10) {
143 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
144 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
145 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
147 if (m_moveCursor == RULER_CURSOR) {
148 m_view->setCursorPos((int) pos / m_factor);
149 m_clickPoint = event->pos();
150 m_startRate = m_rate;
155 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
157 if (event->buttons() == Qt::LeftButton) {
158 int pos = (int)((event->x() + offset()) / m_factor);
159 int zoneStart = m_zoneStart;
160 int zoneEnd = m_zoneEnd;
161 if (pos < 0) pos = 0;
162 if (m_moveCursor == RULER_CURSOR) {
163 QPoint diff = event->pos() - m_clickPoint;
164 if (m_mouseMove == NO_MOVE) {
165 if (!KdenliveSettings::verticalzoom() || qAbs(diff.x()) >= QApplication::startDragDistance()) {
166 m_mouseMove = HORIZONTAL_MOVE;
167 } else if (qAbs(diff.y()) >= QApplication::startDragDistance()) {
168 m_mouseMove = VERTICAL_MOVE;
171 if (m_mouseMove == HORIZONTAL_MOVE) {
172 m_view->setCursorPos(pos);
173 m_view->slotCheckPositionScrolling();
175 int verticalDiff = m_startRate - (diff.y()) / 7;
176 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
179 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
180 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
181 else if (m_moveCursor == RULER_MIDDLE) {
182 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
183 if (move + m_zoneStart < 0) move = - m_zoneStart;
187 emit zoneMoved(m_zoneStart, m_zoneEnd);
188 m_view->setDocumentModified();
190 int min = qMin(m_zoneStart, zoneStart);
191 int max = qMax(m_zoneEnd, zoneEnd);
192 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
195 int pos = (int)((event->x() + offset()));
196 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
197 else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
198 setCursor(KCursor("left_side", Qt::SizeHorCursor));
199 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
200 else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
201 } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
202 setCursor(KCursor("right_side", Qt::SizeHorCursor));
203 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
204 else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
205 } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
206 setCursor(Qt::SizeHorCursor);
207 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
208 else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
210 setCursor(Qt::ArrowCursor);
211 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
212 else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
219 void CustomRuler::wheelEvent(QWheelEvent * e)
222 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
223 if (e->delta() < 0) delta = 0 - delta;
224 m_view->moveCursorPos(delta);
227 int CustomRuler::inPoint() const
232 int CustomRuler::outPoint() const
237 void CustomRuler::slotMoveRuler(int newPos)
243 int CustomRuler::offset() const
248 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
250 if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) {
251 update(oldpos * m_factor - offset() - 6, 0, 17, height());
252 update(newpos * m_factor - offset() - 6, 0, 17, height());
253 } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 0, qAbs(oldpos - newpos) * m_factor + 17, height());
256 void CustomRuler::setPixelPerMark(int rate)
258 int scale = comboScale[rate];
260 m_factor = 1.0 / (double) scale * FRAME_SIZE;
261 m_scale = 1.0 / (double) scale;
262 double fend = m_scale * littleMarkDistance;
264 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
265 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
266 } else if (rate > 6) {
267 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
268 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
269 } else if (rate > 3) {
270 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
271 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
273 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
274 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
278 m_textSpacing = fend;
281 m_textSpacing = fend * 5;
286 m_textSpacing = fend * m_timecode.fps();
289 m_textSpacing = fend * m_timecode.fps() * 5;
292 m_textSpacing = fend * m_timecode.fps() * 10;
295 m_textSpacing = fend * m_timecode.fps() * 30;
299 m_textSpacing = fend * m_timecode.fps() * 40;
302 m_textSpacing = fend * m_timecode.fps() * 80;
306 m_textSpacing = fend * m_timecode.fps() * 400;
309 m_textSpacing = fend * m_timecode.fps() * 800;
315 void CustomRuler::setDuration(int d)
317 int oldduration = m_duration;
319 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
323 void CustomRuler::paintEvent(QPaintEvent *e)
325 QStylePainter p(this);
326 p.setClipRect(e->rect());
327 const int projectEnd = (int)(m_duration * m_factor);
328 p.fillRect(0, 0, projectEnd - m_offset, height(), palette().alternateBase().color());
330 const int zoneStart = (int)(m_zoneStart * m_factor);
331 const int zoneEnd = (int)(m_zoneEnd * m_factor);
332 const QRect zoneRect();
334 p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
336 const int value = m_view->cursorPos() * m_factor - m_offset;
337 int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
338 const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
343 const int offsetmax = maxval * FRAME_SIZE;
345 p.setPen(palette().text().color());
348 int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
349 offsetmin = offsetmin * m_textSpacing;
350 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
352 if (KdenliveSettings::frametimecode())
353 lab = QString::number((int)(f / m_factor + 0.5));
355 lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
356 p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
359 offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
360 offsetmin = offsetmin * littleMarkDistance;
361 // draw the little marks
362 fend = m_scale * littleMarkDistance;
364 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
365 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
368 offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
369 offsetmin = offsetmin * mediumMarkDistance;
371 fend = m_scale * mediumMarkDistance;
373 for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
374 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
377 offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
378 offsetmin = offsetmin * bigMarkDistance;
380 fend = m_scale * bigMarkDistance;
382 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
383 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
390 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
395 QColor center(Qt::white);
396 center.setAlpha(150);
397 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
398 p.fillRect(rec, center);
402 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
408 pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16);
409 p.setBrush(palette().highlight());
413 #include "customruler.moc"