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->initCursorPos(act->data().toInt());
114 void CustomRuler::setZone(QPoint p)
121 void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
123 m_mouseMove = NO_MOVE;
127 void CustomRuler::mousePressEvent(QMouseEvent * event)
129 int pos = (int)((event->x() + offset()));
130 if (event->button() == Qt::RightButton) {
131 m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
132 m_editGuide->setEnabled(m_clickedGuide > 0);
133 m_deleteGuide->setEnabled(m_clickedGuide > 0);
134 m_view->buildGuidesMenu(m_goMenu);
135 m_contextMenu->exec(event->globalPos());
138 setFocus(Qt::MouseFocusReason);
139 m_view->activateMonitor();
140 m_moveCursor = RULER_CURSOR;
141 if (event->y() > 10) {
142 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
143 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
144 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
146 if (m_moveCursor == RULER_CURSOR) {
147 m_view->setCursorPos((int) pos / m_factor);
148 m_clickPoint = event->pos();
149 m_startRate = m_rate;
154 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
156 if (event->buttons() == Qt::LeftButton) {
157 int pos = (int)((event->x() + offset()) / m_factor);
158 int zoneStart = m_zoneStart;
159 int zoneEnd = m_zoneEnd;
160 if (pos < 0) pos = 0;
161 if (m_moveCursor == RULER_CURSOR) {
162 QPoint diff = event->pos() - m_clickPoint;
163 if (m_mouseMove == NO_MOVE) {
164 if (!KdenliveSettings::verticalzoom() || qAbs(diff.x()) >= QApplication::startDragDistance()) {
165 m_mouseMove = HORIZONTAL_MOVE;
166 } else if (qAbs(diff.y()) >= QApplication::startDragDistance()) {
167 m_mouseMove = VERTICAL_MOVE;
170 if (m_mouseMove == HORIZONTAL_MOVE) {
171 m_view->setCursorPos(pos);
172 m_view->slotCheckPositionScrolling();
174 int verticalDiff = m_startRate - (diff.y()) / 7;
175 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
178 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
179 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
180 else if (m_moveCursor == RULER_MIDDLE) {
181 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
182 if (move + m_zoneStart < 0) move = - m_zoneStart;
186 emit zoneMoved(m_zoneStart, m_zoneEnd);
187 m_view->setDocumentModified();
189 int min = qMin(m_zoneStart, zoneStart);
190 int max = qMax(m_zoneEnd, zoneEnd);
191 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
194 int pos = (int)((event->x() + offset()));
195 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
196 else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
197 setCursor(KCursor("left_side", Qt::SizeHorCursor));
198 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
199 else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
200 } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
201 setCursor(KCursor("right_side", Qt::SizeHorCursor));
202 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
203 else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
204 } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
205 setCursor(Qt::SizeHorCursor);
206 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
207 else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
209 setCursor(Qt::ArrowCursor);
210 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
211 else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
218 void CustomRuler::wheelEvent(QWheelEvent * e)
221 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
222 if (e->delta() < 0) delta = 0 - delta;
223 m_view->moveCursorPos(delta);
226 int CustomRuler::inPoint() const
231 int CustomRuler::outPoint() const
236 void CustomRuler::slotMoveRuler(int newPos)
242 int CustomRuler::offset() const
247 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
249 if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) {
250 update(oldpos * m_factor - offset() - 6, 0, 17, height());
251 update(newpos * m_factor - offset() - 6, 0, 17, height());
252 } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 0, qAbs(oldpos - newpos) * m_factor + 17, height());
255 void CustomRuler::setPixelPerMark(int rate)
257 int scale = comboScale[rate];
259 m_factor = 1.0 / (double) scale * FRAME_SIZE;
260 m_scale = 1.0 / (double) scale;
261 double fend = m_scale * littleMarkDistance;
263 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
264 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
265 } else if (rate > 6) {
266 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
267 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
268 } else if (rate > 3) {
269 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
270 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
272 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
273 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
277 m_textSpacing = fend;
280 m_textSpacing = fend * 5;
285 m_textSpacing = fend * m_timecode.fps();
288 m_textSpacing = fend * m_timecode.fps() * 5;
291 m_textSpacing = fend * m_timecode.fps() * 10;
294 m_textSpacing = fend * m_timecode.fps() * 30;
298 m_textSpacing = fend * m_timecode.fps() * 40;
301 m_textSpacing = fend * m_timecode.fps() * 80;
305 m_textSpacing = fend * m_timecode.fps() * 400;
308 m_textSpacing = fend * m_timecode.fps() * 800;
314 void CustomRuler::setDuration(int d)
316 int oldduration = m_duration;
318 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
322 void CustomRuler::paintEvent(QPaintEvent *e)
324 QStylePainter p(this);
325 p.setClipRect(e->rect());
326 const int projectEnd = (int)(m_duration * m_factor);
327 p.fillRect(0, 0, projectEnd - m_offset, height(), palette().alternateBase().color());
329 const int zoneStart = (int)(m_zoneStart * m_factor);
330 const int zoneEnd = (int)(m_zoneEnd * m_factor);
331 const QRect zoneRect();
333 p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
335 const int value = m_view->cursorPos() * m_factor - m_offset;
336 int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
337 const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
338 if (minval < 0) minval = 0;
341 const int offsetmax = maxval * FRAME_SIZE;
343 p.setPen(palette().dark().color());
346 int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
347 offsetmin = offsetmin * m_textSpacing;
348 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
349 QString lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
350 p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
353 offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
354 offsetmin = offsetmin * littleMarkDistance;
355 // draw the little marks
356 fend = m_scale * littleMarkDistance;
357 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
358 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
360 offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
361 offsetmin = offsetmin * mediumMarkDistance;
363 fend = m_scale * mediumMarkDistance;
364 if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
365 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
367 offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
368 offsetmin = offsetmin * bigMarkDistance;
370 fend = m_scale * bigMarkDistance;
371 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
372 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
378 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
383 QColor center(Qt::white);
384 center.setAlpha(150);
385 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
386 p.fillRect(rec, center);
390 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
396 pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16);
397 p.setBrush(palette().highlight());
401 #include "customruler.moc"