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 kDebug()<<"// GOT FRM SZ: "<<FRAME_SIZE;
104 littleMarkDistance = FRAME_SIZE;
105 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
106 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
107 updateProjectFps(m_timecode);
108 if (m_rate > 0) setPixelPerMark(m_rate);
111 void CustomRuler::slotEditGuide()
113 m_view->slotEditGuide(m_clickedGuide);
116 void CustomRuler::slotDeleteGuide()
118 m_view->slotDeleteGuide(m_clickedGuide);
121 void CustomRuler::slotGoToGuide(QAction *act)
123 m_view->setCursorPos(act->data().toInt(), true);
124 m_view->initCursorPos(act->data().toInt());
127 void CustomRuler::setZone(QPoint p)
134 void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
136 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END || m_moveCursor == RULER_MIDDLE) {
137 emit zoneMoved(m_zoneStart, m_zoneEnd);
138 m_view->setDocumentModified();
140 m_mouseMove = NO_MOVE;
145 void CustomRuler::mousePressEvent(QMouseEvent * event)
147 int pos = (int)((event->x() + offset()));
148 if (event->button() == Qt::RightButton) {
149 m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
150 m_editGuide->setEnabled(m_clickedGuide > 0);
151 m_deleteGuide->setEnabled(m_clickedGuide > 0);
152 m_view->buildGuidesMenu(m_goMenu);
153 m_contextMenu->exec(event->globalPos());
156 setFocus(Qt::MouseFocusReason);
157 m_view->activateMonitor();
158 m_moveCursor = RULER_CURSOR;
159 if (event->y() > 10) {
160 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
161 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
162 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
163 m_view->updateSnapPoints(NULL);
165 if (m_moveCursor == RULER_CURSOR) {
166 m_view->setCursorPos((int) pos / m_factor);
167 m_clickPoint = event->pos();
168 m_startRate = m_rate;
173 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
175 if (event->buttons() == Qt::LeftButton) {
177 if (m_moveCursor == RULER_START || m_moveCursor == RULER_END) {
178 pos = m_view->getSnapPointForPos((int)((event->x() + offset()) / m_factor));
179 } else pos = (int)((event->x() + offset()) / m_factor);
180 int zoneStart = m_zoneStart;
181 int zoneEnd = m_zoneEnd;
182 if (pos < 0) pos = 0;
183 if (m_moveCursor == RULER_CURSOR) {
184 QPoint diff = event->pos() - m_clickPoint;
185 if (m_mouseMove == NO_MOVE) {
186 if (!KdenliveSettings::verticalzoom() || qAbs(diff.x()) >= QApplication::startDragDistance()) {
187 m_mouseMove = HORIZONTAL_MOVE;
188 } else if (qAbs(diff.y()) >= QApplication::startDragDistance()) {
189 m_mouseMove = VERTICAL_MOVE;
192 if (m_mouseMove == HORIZONTAL_MOVE) {
193 m_view->setCursorPos(pos);
194 m_view->slotCheckPositionScrolling();
196 int verticalDiff = m_startRate - (diff.y()) / 7;
197 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
200 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
201 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
202 else if (m_moveCursor == RULER_MIDDLE) {
203 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
204 if (move + m_zoneStart < 0) move = - m_zoneStart;
209 int min = qMin(m_zoneStart, zoneStart);
210 int max = qMax(m_zoneEnd, zoneEnd);
211 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
214 int pos = (int)((event->x() + offset()));
215 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
216 else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
217 setCursor(KCursor("left_side", Qt::SizeHorCursor));
218 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
219 else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
220 } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
221 setCursor(KCursor("right_side", Qt::SizeHorCursor));
222 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
223 else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
224 } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
225 setCursor(Qt::SizeHorCursor);
226 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
227 else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
229 setCursor(Qt::ArrowCursor);
230 if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
231 else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
238 void CustomRuler::wheelEvent(QWheelEvent * e)
241 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
242 if (e->delta() < 0) delta = 0 - delta;
243 m_view->moveCursorPos(delta);
246 int CustomRuler::inPoint() const
251 int CustomRuler::outPoint() const
256 void CustomRuler::slotMoveRuler(int newPos)
262 int CustomRuler::offset() const
267 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
269 if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) {
270 update(oldpos * m_factor - offset() - 6, 0, 17, height());
271 update(newpos * m_factor - offset() - 6, 0, 17, height());
272 } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 0, qAbs(oldpos - newpos) * m_factor + 17, height());
275 void CustomRuler::setPixelPerMark(int rate)
277 int scale = comboScale[rate];
279 m_factor = 1.0 / (double) scale * FRAME_SIZE;
280 m_scale = 1.0 / (double) scale;
281 double fend = m_scale * littleMarkDistance;
283 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
284 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
285 } else if (rate > 6) {
286 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
287 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
288 } else if (rate > 3) {
289 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
290 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
292 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
293 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
297 m_textSpacing = fend;
300 m_textSpacing = fend * 5;
305 m_textSpacing = fend * m_timecode.fps();
308 m_textSpacing = fend * m_timecode.fps() * 5;
311 m_textSpacing = fend * m_timecode.fps() * 10;
314 m_textSpacing = fend * m_timecode.fps() * 30;
318 m_textSpacing = fend * m_timecode.fps() * 40;
321 m_textSpacing = fend * m_timecode.fps() * 80;
325 m_textSpacing = fend * m_timecode.fps() * 400;
328 m_textSpacing = fend * m_timecode.fps() * 800;
334 void CustomRuler::setDuration(int d)
336 int oldduration = m_duration;
338 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
342 void CustomRuler::paintEvent(QPaintEvent *e)
344 QStylePainter p(this);
345 p.setClipRect(e->rect());
346 const int projectEnd = (int)(m_duration * m_factor);
347 p.fillRect(0, 0, projectEnd - m_offset, height(), palette().alternateBase().color());
349 const int zoneStart = (int)(m_zoneStart * m_factor);
350 const int zoneEnd = (int)(m_zoneEnd * m_factor);
351 const QRect zoneRect();
353 p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
355 const int value = m_view->cursorPos() * m_factor - m_offset;
356 int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
357 const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
362 const int offsetmax = maxval * FRAME_SIZE;
364 p.setPen(palette().text().color());
367 int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
368 offsetmin = offsetmin * m_textSpacing;
369 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
371 if (KdenliveSettings::frametimecode())
372 lab = QString::number((int)(f / m_factor + 0.5));
374 lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
375 p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
378 offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
379 offsetmin = offsetmin * littleMarkDistance;
380 // draw the little marks
381 fend = m_scale * littleMarkDistance;
383 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
384 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
387 offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
388 offsetmin = offsetmin * mediumMarkDistance;
390 fend = m_scale * mediumMarkDistance;
392 for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
393 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
396 offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
397 offsetmin = offsetmin * bigMarkDistance;
399 fend = m_scale * bigMarkDistance;
401 for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
402 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
409 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
414 QColor center(Qt::white);
415 center.setAlpha(150);
416 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
417 p.fillRect(rec, center);
421 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
427 pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16);
428 p.setBrush(palette().highlight());
432 #include "customruler.moc"