]> git.sesse.net Git - kdenlive/blob - src/customruler.cpp
When user wants to display timecode in frames, use frames for all timecode display...
[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     setMouseTracking(true);
86     setMinimumHeight(20);
87 }
88
89 void CustomRuler::updateProjectFps(Timecode t)
90 {
91     m_timecode = t;
92     mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
93     bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
94     update();
95 }
96
97 void CustomRuler::slotEditGuide()
98 {
99     m_view->slotEditGuide(m_clickedGuide);
100 }
101
102 void CustomRuler::slotDeleteGuide()
103 {
104     m_view->slotDeleteGuide(m_clickedGuide);
105 }
106
107 void CustomRuler::setZone(QPoint p)
108 {
109     m_zoneStart = p.x();
110     m_zoneEnd = p.y();
111     update();
112 }
113
114 void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
115 {
116     m_mouseMove = NO_MOVE;
117 }
118
119 // virtual
120 void CustomRuler::mousePressEvent(QMouseEvent * event)
121 {
122     int pos = (int)((event->x() + offset()));
123     if (event->button() == Qt::RightButton) {
124         m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
125         m_editGuide->setEnabled(m_clickedGuide > 0);
126         m_deleteGuide->setEnabled(m_clickedGuide > 0);
127         m_contextMenu->exec(event->globalPos());
128         return;
129     }
130     setFocus(Qt::MouseFocusReason);
131     m_view->activateMonitor();
132     m_moveCursor = RULER_CURSOR;
133     if (event->y() > 10) {
134         if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
135         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
136         else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
137     }
138     if (m_moveCursor == RULER_CURSOR) {
139         m_view->setCursorPos((int) pos / m_factor);
140         m_clickPoint = event->pos();
141         m_startRate = m_rate;
142     }
143 }
144
145 // virtual
146 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
147 {
148     if (event->buttons() == Qt::LeftButton) {
149         int pos = (int)((event->x() + offset()) / m_factor);
150         int zoneStart = m_zoneStart;
151         int zoneEnd = m_zoneEnd;
152         if (pos < 0) pos = 0;
153         if (m_moveCursor == RULER_CURSOR) {
154             QPoint diff = event->pos() - m_clickPoint;
155             if (m_mouseMove == NO_MOVE) {
156                 if (!KdenliveSettings::verticalzoom() || qAbs(diff.x()) >= QApplication::startDragDistance()) {
157                     m_mouseMove = HORIZONTAL_MOVE;
158                 } else if (qAbs(diff.y()) >= QApplication::startDragDistance()) {
159                     m_mouseMove = VERTICAL_MOVE;
160                 } else return;
161             }
162             if (m_mouseMove == HORIZONTAL_MOVE) {
163                 m_view->setCursorPos(pos);
164                 m_view->slotCheckPositionScrolling();
165             } else {
166                 int verticalDiff = m_startRate - (diff.y()) / 7;
167                 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
168             }
169             return;
170         } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
171         else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
172         else if (m_moveCursor == RULER_MIDDLE) {
173             int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
174             if (move + m_zoneStart < 0) move = - m_zoneStart;
175             m_zoneStart += move;
176             m_zoneEnd += move;
177         }
178         emit zoneMoved(m_zoneStart, m_zoneEnd);
179         m_view->setDocumentModified();
180
181         int min = qMin(m_zoneStart, zoneStart);
182         int max = qMax(m_zoneEnd, zoneEnd);
183         update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
184
185     } else {
186         int pos = (int)((event->x() + offset()));
187         if (event->y() <= 10) setCursor(Qt::ArrowCursor);
188         else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
189             setCursor(KCursor("left_side", Qt::SizeHorCursor));
190             if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
191             else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
192         } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
193             setCursor(KCursor("right_side", Qt::SizeHorCursor));
194             if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
195             else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
196         } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
197             setCursor(Qt::SizeHorCursor);
198             if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
199             else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
200         } else {
201             setCursor(Qt::ArrowCursor);
202             if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
203             else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
204         }
205     }
206 }
207
208
209 // virtual
210 void CustomRuler::wheelEvent(QWheelEvent * e)
211 {
212     int delta = 1;
213     if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
214     if (e->delta() < 0) delta = 0 - delta;
215     m_view->moveCursorPos(delta);
216 }
217
218 int CustomRuler::inPoint() const
219 {
220     return m_zoneStart;
221 }
222
223 int CustomRuler::outPoint() const
224 {
225     return m_zoneEnd;
226 }
227
228 void CustomRuler::slotMoveRuler(int newPos)
229 {
230     m_offset = newPos;
231     update();
232 }
233
234 int CustomRuler::offset() const
235 {
236     return m_offset;
237 }
238
239 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
240 {
241     if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) {
242         update(oldpos * m_factor - offset() - 6, 0, 17, height());
243         update(newpos * m_factor - offset() - 6, 0, 17, height());
244     } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 0, qAbs(oldpos - newpos) * m_factor + 17, height());
245 }
246
247 void CustomRuler::setPixelPerMark(int rate)
248 {
249     int scale = comboScale[rate];
250     m_rate = rate;
251     m_factor = 1.0 / (double) scale * FRAME_SIZE;
252     m_scale = 1.0 / (double) scale;
253     double fend = m_scale * littleMarkDistance;
254     if (rate > 8) {
255         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
256         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
257     } else if (rate > 6) {
258         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
259         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
260     } else if (rate > 3) {
261         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
262         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
263     } else {
264         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
265         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
266     }
267     switch (rate) {
268     case 0:
269         m_textSpacing = fend;
270         break;
271     case 1:
272         m_textSpacing = fend * 5;
273         break;
274     case 2:
275     case 3:
276     case 4:
277         m_textSpacing = fend * m_timecode.fps();
278         break;
279     case 5:
280         m_textSpacing = fend * m_timecode.fps() * 5;
281         break;
282     case 6:
283         m_textSpacing = fend * m_timecode.fps() * 10;
284         break;
285     case 7:
286         m_textSpacing = fend * m_timecode.fps() * 30;
287         break;
288     case 8:
289     case 9:
290         m_textSpacing = fend * m_timecode.fps() * 40;
291         break;
292     case 10:
293         m_textSpacing = fend * m_timecode.fps() * 80;
294         break;
295     case 11:
296     case 12:
297         m_textSpacing = fend * m_timecode.fps() * 400;
298         break;
299     case 13:
300         m_textSpacing = fend * m_timecode.fps() * 800;
301         break;
302     }
303     update();
304 }
305
306 void CustomRuler::setDuration(int d)
307 {
308     int oldduration = m_duration;
309     m_duration = d;
310     update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
311 }
312
313 // virtual
314 void CustomRuler::paintEvent(QPaintEvent *e)
315 {
316     QStylePainter p(this);
317     p.setClipRect(e->rect());
318     const int projectEnd = (int)(m_duration * m_factor);
319     p.fillRect(0, 0, projectEnd - m_offset, height(), palette().alternateBase().color());
320
321     const int zoneStart = (int)(m_zoneStart * m_factor);
322     const int zoneEnd = (int)(m_zoneEnd * m_factor);
323     const QRect zoneRect();
324
325     p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
326
327     const int value  = m_view->cursorPos() * m_factor - m_offset;
328     int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
329     const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
330     if (minval < 0) minval = 0;
331
332     double f, fend;
333     const int offsetmax = maxval * FRAME_SIZE;
334
335     p.setPen(palette().dark().color());
336
337     // draw time labels
338     int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
339     offsetmin = offsetmin * m_textSpacing;
340     for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
341         QString lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
342         p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
343     }
344
345     offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
346     offsetmin = offsetmin * littleMarkDistance;
347     // draw the little marks
348     fend = m_scale * littleMarkDistance;
349     if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
350             p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
351
352     offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
353     offsetmin = offsetmin * mediumMarkDistance;
354     // draw medium marks
355     fend = m_scale * mediumMarkDistance;
356     if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
357             p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
358
359     offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
360     offsetmin = offsetmin * bigMarkDistance;
361     // draw big marks
362     fend = m_scale * bigMarkDistance;
363     if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
364             p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
365
366     // draw zone cursors
367     int off = offset();
368     if (zoneStart > 0) {
369         QPolygon pa(4);
370         pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
371         p.drawPolyline(pa);
372     }
373
374     if (zoneEnd > 0) {
375         QColor center(Qt::white);
376         center.setAlpha(150);
377         QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
378         p.fillRect(rec, center);
379         p.drawRect(rec);
380
381         QPolygon pa(4);
382         pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
383         p.drawPolyline(pa);
384     }
385
386     // draw pointer
387     QPolygon pa(3);
388     pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16);
389     p.setBrush(palette().highlight());
390     p.drawPolygon(pa);
391 }
392
393 #include "customruler.moc"