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 LABEL_SIZE;
47 static const int END_LABEL_X = 4;
48 static const int END_LABEL_Y = (END_LABEL_X + LABEL_SIZE - 2);
50 static int littleMarkDistance;
51 static int mediumMarkDistance;
52 static int bigMarkDistance;
54 #include "definitions.h"
56 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 750, 1500, 3000, 6000, 12000};
58 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) :
67 setFont(KGlobalSettings::toolBarFont());
68 QFontMetricsF fontMetrics(font());
69 LABEL_SIZE = fontMetrics.ascent() - 2;
71 m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
72 littleMarkDistance = FRAME_SIZE;
73 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
74 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
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::slotEditGuide()
102 m_view->slotEditGuide(m_clickedGuide);
105 void CustomRuler::slotDeleteGuide()
107 m_view->slotDeleteGuide(m_clickedGuide);
110 void CustomRuler::slotGoToGuide(QAction *act)
112 m_view->setCursorPos(act->data().toInt(), true);
113 m_view->initCursorPos(act->data().toInt());
116 void CustomRuler::setZone(QPoint p)
123 void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
125 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END || m_moveCursor == RULER_MIDDLE) {
126 emit zoneMoved(m_zoneStart, m_zoneEnd);
127 m_view->setDocumentModified();
129 m_mouseMove = NO_MOVE;
134 void CustomRuler::mousePressEvent(QMouseEvent * event)
136 int pos = (int)((event->x() + offset()));
137 if (event->button() == Qt::RightButton) {
138 m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
139 m_editGuide->setEnabled(m_clickedGuide > 0);
140 m_deleteGuide->setEnabled(m_clickedGuide > 0);
141 m_view->buildGuidesMenu(m_goMenu);
142 m_contextMenu->exec(event->globalPos());
145 setFocus(Qt::MouseFocusReason);
146 m_view->activateMonitor();
147 m_moveCursor = RULER_CURSOR;
148 if (event->y() > 10) {
149 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
150 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
151 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
152 m_view->updateSnapPoints(NULL);
154 if (m_moveCursor == RULER_CURSOR) {
155 m_view->setCursorPos((int) pos / m_factor);
156 m_clickPoint = event->pos();
157 m_startRate = m_rate;
162 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
164 if (event->buttons() == Qt::LeftButton) {
166 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END) {
167 pos = m_view->getSnapPointForPos((int)((event->x() + offset()) / m_factor));
168 } else pos = (int)((event->x() + offset()) / m_factor);
169 int zoneStart = m_zoneStart;
170 int zoneEnd = m_zoneEnd;
171 if (pos < 0) pos = 0;
172 if (m_moveCursor == RULER_CURSOR) {
173 QPoint diff = event->pos() - m_clickPoint;
174 if (m_mouseMove == NO_MOVE) {
175 if (!KdenliveSettings::verticalzoom() || qAbs(diff.x()) >= QApplication::startDragDistance()) {
176 m_mouseMove = HORIZONTAL_MOVE;
177 } else if (qAbs(diff.y()) >= QApplication::startDragDistance()) {
178 m_mouseMove = VERTICAL_MOVE;
181 if (m_mouseMove == HORIZONTAL_MOVE) {
182 m_view->setCursorPos(pos);
183 m_view->slotCheckPositionScrolling();
185 int verticalDiff = m_startRate - (diff.y()) / 7;
186 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
189 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
190 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
191 else if (m_moveCursor == RULER_MIDDLE) {
192 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
193 if (move + m_zoneStart < 0) move = - m_zoneStart;
198 int min = qMin(m_zoneStart, zoneStart);
199 int max = qMax(m_zoneEnd, zoneEnd);
200 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
203 int pos = (int)((event->x() + offset()));
204 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
205 else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
206 setCursor(KCursor("left_side", Qt::SizeHorCursor));
207 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
208 else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
209 } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
210 setCursor(KCursor("right_side", Qt::SizeHorCursor));
211 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
212 else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
213 } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
214 setCursor(Qt::SizeHorCursor);
215 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
216 else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
218 setCursor(Qt::ArrowCursor);
219 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
220 else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
227 void CustomRuler::wheelEvent(QWheelEvent * e)
230 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
231 if (e->delta() < 0) delta = 0 - delta;
232 m_view->moveCursorPos(delta);
235 int CustomRuler::inPoint() const
240 int CustomRuler::outPoint() const
245 void CustomRuler::slotMoveRuler(int newPos)
251 int CustomRuler::offset() const
256 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
258 if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) {
259 update(oldpos * m_factor - offset() - 6, 0, 17, height());
260 update(newpos * m_factor - offset() - 6, 0, 17, height());
261 } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 0, qAbs(oldpos - newpos) * m_factor + 17, height());
264 void CustomRuler::setPixelPerMark(int rate)
266 int scale = comboScale[rate];
268 m_factor = 1.0 / (double) scale * FRAME_SIZE;
269 m_scale = 1.0 / (double) scale;
270 double fend = m_scale * littleMarkDistance;
272 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
273 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
274 } else if (rate > 6) {
275 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
276 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
277 } else if (rate > 3) {
278 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
279 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
281 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
282 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
286 m_textSpacing = fend;
289 m_textSpacing = fend * 5;
294 m_textSpacing = fend * m_timecode.fps();
297 m_textSpacing = fend * m_timecode.fps() * 5;
300 m_textSpacing = fend * m_timecode.fps() * 10;
303 m_textSpacing = fend * m_timecode.fps() * 30;
307 m_textSpacing = fend * m_timecode.fps() * 40;
310 m_textSpacing = fend * m_timecode.fps() * 80;
314 m_textSpacing = fend * m_timecode.fps() * 400;
317 m_textSpacing = fend * m_timecode.fps() * 800;
323 void CustomRuler::setDuration(int d)
325 int oldduration = m_duration;
327 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
331 void CustomRuler::paintEvent(QPaintEvent *e)
333 QStylePainter p(this);
334 p.setClipRect(e->rect());
335 const int projectEnd = (int)(m_duration * m_factor);
336 p.fillRect(0, 0, projectEnd - m_offset, height(), palette().alternateBase().color());
338 const int zoneStart = (int)(m_zoneStart * m_factor);
339 const int zoneEnd = (int)(m_zoneEnd * m_factor);
340 const QRect zoneRect();
342 p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
344 const int value = m_view->cursorPos() * m_factor - m_offset;
345 int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
346 const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
351 const int offsetmax = maxval * FRAME_SIZE;
353 p.setPen(palette().text().color());
356 int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
357 offsetmin = offsetmin * m_textSpacing;
358 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
360 if (KdenliveSettings::frametimecode())
361 lab = QString::number((int)(f / m_factor + 0.5));
363 lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
364 p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
367 offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
368 offsetmin = offsetmin * littleMarkDistance;
369 // draw the little marks
370 fend = m_scale * littleMarkDistance;
372 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
373 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
376 offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
377 offsetmin = offsetmin * mediumMarkDistance;
379 fend = m_scale * mediumMarkDistance;
381 for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
382 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
385 offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
386 offsetmin = offsetmin * bigMarkDistance;
388 fend = m_scale * bigMarkDistance;
390 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
391 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
398 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
403 QColor center(Qt::white);
404 center.setAlpha(150);
405 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
406 p.fillRect(rec, center);
410 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
416 pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16);
417 p.setBrush(palette().highlight());
421 #include "customruler.moc"