]> git.sesse.net Git - kdenlive/blob - src/customruler.cpp
Add menu to timeline ruler to easily scroll to a guide
[kdenlive] / src / customruler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20 #include "customruler.h"
21 #include "kdenlivesettings.h"
22
23 #include <KDebug>
24 #include <KIcon>
25 #include <KCursor>
26 #include <KGlobalSettings>
27
28 #include <QApplication>
29 #include <QMouseEvent>
30 #include <QStylePainter>
31
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);
44
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);
48
49 static int littleMarkDistance;
50 static int mediumMarkDistance;
51 static int bigMarkDistance;
52
53 #include "definitions.h"
54
55 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 750, 1500, 3000, 6000, 12000};
56
57 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) :
58         QWidget(parent),
59         m_timecode(tc),
60         m_view(parent),
61         m_duration(0),
62         m_offset(0),
63         m_clickedGuide(-1),
64         m_mouseMove(NO_MOVE)
65 {
66     setFont(KGlobalSettings::toolBarFont());
67     QFontMetricsF fontMetrics(font());
68     LABEL_SIZE = fontMetrics.ascent() - 2;
69     m_scale = 3;
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;
74     m_zoneStart = 0;
75     m_zoneEnd = 100;
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);
88     setMinimumHeight(20);
89 }
90
91 void CustomRuler::updateProjectFps(Timecode t)
92 {
93     m_timecode = t;
94     mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
95     bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
96     update();
97 }
98
99 void CustomRuler::slotEditGuide()
100 {
101     m_view->slotEditGuide(m_clickedGuide);
102 }
103
104 void CustomRuler::slotDeleteGuide()
105 {
106     m_view->slotDeleteGuide(m_clickedGuide);
107 }
108
109 void CustomRuler::slotGoToGuide(QAction *act)
110 {
111     m_view->initCursorPos(act->data().toInt());
112 }
113
114 void CustomRuler::setZone(QPoint p)
115 {
116     m_zoneStart = p.x();
117     m_zoneEnd = p.y();
118     update();
119 }
120
121 void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
122 {
123     m_mouseMove = NO_MOVE;
124 }
125
126 // virtual
127 void CustomRuler::mousePressEvent(QMouseEvent * event)
128 {
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());
136         return;
137     }
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;
145     }
146     if (m_moveCursor == RULER_CURSOR) {
147         m_view->setCursorPos((int) pos / m_factor);
148         m_clickPoint = event->pos();
149         m_startRate = m_rate;
150     }
151 }
152
153 // virtual
154 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
155 {
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;
168                 } else return;
169             }
170             if (m_mouseMove == HORIZONTAL_MOVE) {
171                 m_view->setCursorPos(pos);
172                 m_view->slotCheckPositionScrolling();
173             } else {
174                 int verticalDiff = m_startRate - (diff.y()) / 7;
175                 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
176             }
177             return;
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;
183             m_zoneStart += move;
184             m_zoneEnd += move;
185         }
186         emit zoneMoved(m_zoneStart, m_zoneEnd);
187         m_view->setDocumentModified();
188
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());
192
193     } else {
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)));
208         } else {
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)));
212         }
213     }
214 }
215
216
217 // virtual
218 void CustomRuler::wheelEvent(QWheelEvent * e)
219 {
220     int delta = 1;
221     if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
222     if (e->delta() < 0) delta = 0 - delta;
223     m_view->moveCursorPos(delta);
224 }
225
226 int CustomRuler::inPoint() const
227 {
228     return m_zoneStart;
229 }
230
231 int CustomRuler::outPoint() const
232 {
233     return m_zoneEnd;
234 }
235
236 void CustomRuler::slotMoveRuler(int newPos)
237 {
238     m_offset = newPos;
239     update();
240 }
241
242 int CustomRuler::offset() const
243 {
244     return m_offset;
245 }
246
247 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
248 {
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());
253 }
254
255 void CustomRuler::setPixelPerMark(int rate)
256 {
257     int scale = comboScale[rate];
258     m_rate = rate;
259     m_factor = 1.0 / (double) scale * FRAME_SIZE;
260     m_scale = 1.0 / (double) scale;
261     double fend = m_scale * littleMarkDistance;
262     if (rate > 8) {
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;
271     } else {
272         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
273         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
274     }
275     switch (rate) {
276     case 0:
277         m_textSpacing = fend;
278         break;
279     case 1:
280         m_textSpacing = fend * 5;
281         break;
282     case 2:
283     case 3:
284     case 4:
285         m_textSpacing = fend * m_timecode.fps();
286         break;
287     case 5:
288         m_textSpacing = fend * m_timecode.fps() * 5;
289         break;
290     case 6:
291         m_textSpacing = fend * m_timecode.fps() * 10;
292         break;
293     case 7:
294         m_textSpacing = fend * m_timecode.fps() * 30;
295         break;
296     case 8:
297     case 9:
298         m_textSpacing = fend * m_timecode.fps() * 40;
299         break;
300     case 10:
301         m_textSpacing = fend * m_timecode.fps() * 80;
302         break;
303     case 11:
304     case 12:
305         m_textSpacing = fend * m_timecode.fps() * 400;
306         break;
307     case 13:
308         m_textSpacing = fend * m_timecode.fps() * 800;
309         break;
310     }
311     update();
312 }
313
314 void CustomRuler::setDuration(int d)
315 {
316     int oldduration = m_duration;
317     m_duration = d;
318     update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
319 }
320
321 // virtual
322 void CustomRuler::paintEvent(QPaintEvent *e)
323 {
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());
328
329     const int zoneStart = (int)(m_zoneStart * m_factor);
330     const int zoneEnd = (int)(m_zoneEnd * m_factor);
331     const QRect zoneRect();
332
333     p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
334
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;
339
340     double f, fend;
341     const int offsetmax = maxval * FRAME_SIZE;
342
343     p.setPen(palette().dark().color());
344
345     // draw time labels
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);
351     }
352
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);
359
360     offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
361     offsetmin = offsetmin * mediumMarkDistance;
362     // draw medium marks
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);
366
367     offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
368     offsetmin = offsetmin * bigMarkDistance;
369     // draw big marks
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);
373
374     // draw zone cursors
375     int off = offset();
376     if (zoneStart > 0) {
377         QPolygon pa(4);
378         pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
379         p.drawPolyline(pa);
380     }
381
382     if (zoneEnd > 0) {
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);
387         p.drawRect(rec);
388
389         QPolygon pa(4);
390         pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
391         p.drawPolyline(pa);
392     }
393
394     // draw pointer
395     QPolygon pa(3);
396     pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16);
397     p.setBrush(palette().highlight());
398     p.drawPolygon(pa);
399 }
400
401 #include "customruler.moc"