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 const int FIX_WIDTH = 24; /* widget width in pixel */
34 static const int LINE_END = (FIX_WIDTH - 3);
35 static const int END_MARK_LENGTH = (FIX_WIDTH - 8);
36 static const int BIG_MARK_LENGTH = (END_MARK_LENGTH * 3 / 4);
37 static const int BIG_MARK_X2 = LINE_END;
38 static const int BIG_MARK_X1 = (BIG_MARK_X2 - BIG_MARK_LENGTH);
39 static const int MIDDLE_MARK_LENGTH = (END_MARK_LENGTH / 2);
40 static const int MIDDLE_MARK_X2 = LINE_END;
41 static const int MIDDLE_MARK_X1 = (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH);
42 static const int LITTLE_MARK_LENGTH = (MIDDLE_MARK_LENGTH / 2);
43 static const int LITTLE_MARK_X2 = LINE_END;
44 static const int LITTLE_MARK_X1 = (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH);
46 static int FRAME_SIZE;
47 static int LABEL_SIZE;
48 static const int END_LABEL_X = 4;
49 static const int END_LABEL_Y = (END_LABEL_X + LABEL_SIZE - 2);
51 static int littleMarkDistance;
52 static int mediumMarkDistance;
53 static int bigMarkDistance;
55 #include "definitions.h"
57 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 750, 1500, 3000, 6000, 12000};
59 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) :
69 setFont(KGlobalSettings::toolBarFont());
70 QFontMetricsF fontMetrics(font());
71 LABEL_SIZE = fontMetrics.ascent() - 2;
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);
92 void CustomRuler::updateProjectFps(Timecode t)
95 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
96 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
100 void CustomRuler::updateFrameSize()
102 FRAME_SIZE = m_view->getFrameWidth();
103 littleMarkDistance = FRAME_SIZE;
104 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
105 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
106 updateProjectFps(m_timecode);
107 if (m_rate > 0) setPixelPerMark(m_rate);
110 void CustomRuler::slotEditGuide()
112 m_view->slotEditGuide(m_clickedGuide);
115 void CustomRuler::slotDeleteGuide()
117 m_view->slotDeleteGuide(m_clickedGuide);
120 void CustomRuler::slotGoToGuide(QAction *act)
122 m_view->setCursorPos(act->data().toInt(), true);
123 m_view->initCursorPos(act->data().toInt());
126 void CustomRuler::setZone(QPoint p)
133 void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
135 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END || m_moveCursor == RULER_MIDDLE) {
136 emit zoneMoved(m_zoneStart, m_zoneEnd);
137 m_view->setDocumentModified();
139 m_mouseMove = NO_MOVE;
144 void CustomRuler::mousePressEvent(QMouseEvent * event)
146 int pos = (int)((event->x() + offset()));
147 if (event->button() == Qt::RightButton) {
148 m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
149 m_editGuide->setEnabled(m_clickedGuide > 0);
150 m_deleteGuide->setEnabled(m_clickedGuide > 0);
151 m_view->buildGuidesMenu(m_goMenu);
152 m_contextMenu->exec(event->globalPos());
155 setFocus(Qt::MouseFocusReason);
156 m_view->activateMonitor();
157 m_moveCursor = RULER_CURSOR;
158 if (event->y() > 10) {
159 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
160 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
161 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
162 m_view->updateSnapPoints(NULL);
164 if (m_moveCursor == RULER_CURSOR) {
165 m_view->setCursorPos((int) pos / m_factor);
166 m_clickPoint = event->pos();
167 m_startRate = m_rate;
172 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
174 if (event->buttons() == Qt::LeftButton) {
176 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END) {
177 pos = m_view->getSnapPointForPos((int)((event->x() + offset()) / m_factor));
178 } else pos = (int)((event->x() + offset()) / m_factor);
179 int zoneStart = m_zoneStart;
180 int zoneEnd = m_zoneEnd;
181 if (pos < 0) pos = 0;
182 if (m_moveCursor == RULER_CURSOR) {
183 QPoint diff = event->pos() - m_clickPoint;
184 if (m_mouseMove == NO_MOVE) {
185 if (!KdenliveSettings::verticalzoom() || qAbs(diff.x()) >= QApplication::startDragDistance()) {
186 m_mouseMove = HORIZONTAL_MOVE;
187 } else if (qAbs(diff.y()) >= QApplication::startDragDistance()) {
188 m_mouseMove = VERTICAL_MOVE;
191 if (m_mouseMove == HORIZONTAL_MOVE) {
192 m_view->setCursorPos(pos);
193 m_view->slotCheckPositionScrolling();
195 int verticalDiff = m_startRate - (diff.y()) / 7;
196 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
199 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
200 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
201 else if (m_moveCursor == RULER_MIDDLE) {
202 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
203 if (move + m_zoneStart < 0) move = - m_zoneStart;
208 int min = qMin(m_zoneStart, zoneStart);
209 int max = qMax(m_zoneEnd, zoneEnd);
210 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
213 int pos = (int)((event->x() + offset()));
214 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
215 else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
216 setCursor(KCursor("left_side", Qt::SizeHorCursor));
217 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
218 else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
219 } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
220 setCursor(KCursor("right_side", Qt::SizeHorCursor));
221 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
222 else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
223 } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
224 setCursor(Qt::SizeHorCursor);
225 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
226 else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
228 setCursor(Qt::ArrowCursor);
229 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
230 else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
237 void CustomRuler::wheelEvent(QWheelEvent * e)
240 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
241 if (e->delta() < 0) delta = 0 - delta;
242 m_view->moveCursorPos(delta);
245 int CustomRuler::inPoint() const
250 int CustomRuler::outPoint() const
255 void CustomRuler::slotMoveRuler(int newPos)
261 int CustomRuler::offset() const
266 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
268 if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) {
269 update(oldpos * m_factor - offset() - 6, 0, 17, height());
270 update(newpos * m_factor - offset() - 6, 0, 17, height());
271 } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 0, qAbs(oldpos - newpos) * m_factor + 17, height());
274 void CustomRuler::setPixelPerMark(int rate)
276 int scale = comboScale[rate];
278 m_factor = 1.0 / (double) scale * FRAME_SIZE;
279 m_scale = 1.0 / (double) scale;
280 double fend = m_scale * littleMarkDistance;
282 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
283 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
284 } else if (rate > 6) {
285 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
286 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
287 } else if (rate > 3) {
288 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
289 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
291 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
292 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
296 m_textSpacing = fend;
299 m_textSpacing = fend * 5;
304 m_textSpacing = fend * m_timecode.fps();
307 m_textSpacing = fend * m_timecode.fps() * 5;
310 m_textSpacing = fend * m_timecode.fps() * 10;
313 m_textSpacing = fend * m_timecode.fps() * 30;
317 m_textSpacing = fend * m_timecode.fps() * 40;
320 m_textSpacing = fend * m_timecode.fps() * 80;
324 m_textSpacing = fend * m_timecode.fps() * 400;
327 m_textSpacing = fend * m_timecode.fps() * 800;
333 void CustomRuler::setDuration(int d)
335 int oldduration = m_duration;
337 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
341 void CustomRuler::paintEvent(QPaintEvent *e)
343 QStylePainter p(this);
344 p.setClipRect(e->rect());
345 const int projectEnd = (int)(m_duration * m_factor);
346 p.fillRect(0, 0, projectEnd - m_offset, height(), palette().alternateBase().color());
348 const int zoneStart = (int)(m_zoneStart * m_factor);
349 const int zoneEnd = (int)(m_zoneEnd * m_factor);
350 const QRect zoneRect();
352 p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
354 const int value = m_view->cursorPos() * m_factor - m_offset;
355 int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
356 const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
361 const int offsetmax = maxval * FRAME_SIZE;
363 p.setPen(palette().text().color());
366 int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
367 offsetmin = offsetmin * m_textSpacing;
368 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
370 if (KdenliveSettings::frametimecode())
371 lab = QString::number((int)(f / m_factor + 0.5));
373 lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
374 p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
377 offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
378 offsetmin = offsetmin * littleMarkDistance;
379 // draw the little marks
380 fend = m_scale * littleMarkDistance;
382 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
383 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
386 offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
387 offsetmin = offsetmin * mediumMarkDistance;
389 fend = m_scale * mediumMarkDistance;
391 for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
392 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
395 offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
396 offsetmin = offsetmin * bigMarkDistance;
398 fend = m_scale * bigMarkDistance;
400 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
401 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
408 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
413 QColor center(Qt::white);
414 center.setAlpha(150);
415 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
416 p.fillRect(rec, center);
420 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
426 pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16);
427 p.setBrush(palette().highlight());
431 #include "customruler.moc"