]> git.sesse.net Git - kdenlive/blob - src/customruler.cpp
0e5fb1048c9d306c53325f2adfbc1c4f834dfc39
[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 #include <KColorScheme>
28
29 #include <QApplication>
30 #include <QMouseEvent>
31 #include <QStylePainter>
32
33 static int MAX_HEIGHT;
34 // Width of a frame in pixels
35 static int FRAME_SIZE;
36 // Height of the timecode text
37 static int LABEL_SIZE;
38
39 static int BIG_MARK_X;
40 static int MIDDLE_MARK_X;
41 static int LITTLE_MARK_X;
42
43 static int littleMarkDistance;
44 static int mediumMarkDistance;
45 static int bigMarkDistance;
46
47 #define SEEK_INACTIVE (-1)
48
49 #include "definitions.h"
50
51 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 750, 1500, 3000, 6000, 12000};
52
53 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) :
54         QWidget(parent),
55         m_timecode(tc),
56         m_view(parent),
57         m_duration(0),
58         m_offset(0),
59         m_lastSeekPosition(SEEK_INACTIVE),
60         m_clickedGuide(-1),
61         m_rate(-1),
62         m_mouseMove(NO_MOVE),
63         m_cursorColor(palette().text())
64 {
65     setFont(KGlobalSettings::toolBarFont());
66     QFontMetricsF fontMetrics(font());
67     // Define size variables
68     LABEL_SIZE = fontMetrics.ascent();
69     setMinimumHeight(LABEL_SIZE * 2);
70     setMaximumHeight(LABEL_SIZE * 2);
71     MAX_HEIGHT = height();
72     BIG_MARK_X = LABEL_SIZE + 1;
73     int mark_length = MAX_HEIGHT - BIG_MARK_X;
74     MIDDLE_MARK_X = BIG_MARK_X + mark_length / 2;
75     LITTLE_MARK_X = BIG_MARK_X + mark_length / 3;
76     updateFrameSize();
77     m_scale = 3;
78     m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
79     m_zoneStart = 0;
80     m_zoneEnd = 100;
81     m_contextMenu = new QMenu(this);
82     QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
83     connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
84     m_editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
85     connect(m_editGuide, SIGNAL(triggered()), this, SLOT(slotEditGuide()));
86     m_deleteGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
87     connect(m_deleteGuide , SIGNAL(triggered()), this, SLOT(slotDeleteGuide()));
88     QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
89     connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
90     m_goMenu = m_contextMenu->addMenu(i18n("Go To"));
91     connect(m_goMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotGoToGuide(QAction *)));
92     setMouseTracking(true);
93 }
94
95 void CustomRuler::updatePalette()
96 {
97     m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
98 }
99
100 void CustomRuler::updateProjectFps(Timecode t)
101 {
102     m_timecode = t;
103     mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
104     bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
105     update();
106 }
107
108 void CustomRuler::updateFrameSize()
109 {
110     FRAME_SIZE = m_view->getFrameWidth();
111     littleMarkDistance = FRAME_SIZE;
112     mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
113     bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
114     updateProjectFps(m_timecode);
115     if (m_rate > 0) setPixelPerMark(m_rate);
116 }
117
118 void CustomRuler::slotEditGuide()
119 {
120     m_view->slotEditGuide(m_clickedGuide);
121 }
122
123 void CustomRuler::slotDeleteGuide()
124 {
125     m_view->slotDeleteGuide(m_clickedGuide);
126 }
127
128 void CustomRuler::slotGoToGuide(QAction *act)
129 {
130     m_view->seekCursorPos(act->data().toInt());
131     m_view->initCursorPos(act->data().toInt());
132 }
133
134 void CustomRuler::setZone(QPoint p)
135 {
136     m_zoneStart = p.x();
137     m_zoneEnd = p.y();
138     update();
139 }
140
141 void CustomRuler::mouseReleaseEvent(QMouseEvent * /*event*/)
142 {
143     if (m_moveCursor == RULER_START || m_moveCursor == RULER_END || m_moveCursor == RULER_MIDDLE) {
144         emit zoneMoved(m_zoneStart, m_zoneEnd);
145         m_view->setDocumentModified();
146     }
147     m_mouseMove = NO_MOVE;
148
149 }
150
151 // virtual
152 void CustomRuler::mousePressEvent(QMouseEvent * event)
153 {
154     int pos = (int)((event->x() + offset()));
155     if (event->button() == Qt::RightButton) {
156         m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
157         m_editGuide->setEnabled(m_clickedGuide > 0);
158         m_deleteGuide->setEnabled(m_clickedGuide > 0);
159         m_view->buildGuidesMenu(m_goMenu);
160         m_contextMenu->exec(event->globalPos());
161         return;
162     }
163     setFocus(Qt::MouseFocusReason);
164     m_view->activateMonitor();
165     m_moveCursor = RULER_CURSOR;
166     if (event->y() > 10) {
167         if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
168         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
169         else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
170         m_view->updateSnapPoints(NULL);
171     }
172     if (m_moveCursor == RULER_CURSOR) {
173         m_view->seekCursorPos((int) pos / m_factor);
174         m_clickPoint = event->pos();
175         m_startRate = m_rate;
176     }
177 }
178
179 // virtual
180 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
181 {
182     if (event->buttons() == Qt::LeftButton) {
183         int pos;
184         if (m_moveCursor == RULER_START || m_moveCursor == RULER_END) {
185             pos = m_view->getSnapPointForPos((int)((event->x() + offset()) / m_factor));
186         } else pos = (int)((event->x() + offset()) / m_factor);
187         int zoneStart = m_zoneStart;
188         int zoneEnd = m_zoneEnd;
189         if (pos < 0) pos = 0;
190         if (m_moveCursor == RULER_CURSOR) {
191             QPoint diff = event->pos() - m_clickPoint;
192             if (m_mouseMove == NO_MOVE) {
193                 if (qAbs(diff.x()) >= QApplication::startDragDistance()) {
194                     m_mouseMove = HORIZONTAL_MOVE;
195                 } else if (KdenliveSettings::verticalzoom() && qAbs(diff.y()) >= QApplication::startDragDistance()) {
196                     m_mouseMove = VERTICAL_MOVE;
197                 } else return;
198             }
199             if (m_mouseMove == HORIZONTAL_MOVE) {
200                 if (pos != m_lastSeekPosition && pos != m_view->cursorPos()) {
201                     m_view->seekCursorPos(pos);
202                     m_view->slotCheckPositionScrolling();
203                 }
204             } else {
205                 int verticalDiff = m_startRate - (diff.y()) / 7;
206                 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
207             }
208             return;
209         } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
210         else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
211         else if (m_moveCursor == RULER_MIDDLE) {
212             int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
213             if (move + m_zoneStart < 0) move = - m_zoneStart;
214             m_zoneStart += move;
215             m_zoneEnd += move;
216         }
217         int min = qMin(m_zoneStart, zoneStart);
218         int max = qMax(m_zoneEnd, zoneEnd);
219         update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
220
221     } else {
222         int pos = (int)((event->x() + m_offset));
223         if (m_cursorColor == palette().text() && qAbs(pos - m_view->cursorPos() * m_factor) < 7) {
224             // Mouse is over cursor
225             m_cursorColor = palette().highlight();
226             update(m_view->cursorPos() * m_factor - m_offset - 10, 0, 20, height());
227         }
228         else if (m_cursorColor == palette().highlight() && qAbs(pos - m_view->cursorPos() * m_factor) >= 7) {
229             m_cursorColor = palette().text();
230             update(m_view->cursorPos() * m_factor - m_offset - 10, 0, 20, height());
231         }
232         
233         if (event->y() <= 10) setCursor(Qt::ArrowCursor);
234         else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
235             setCursor(KCursor("left_side", Qt::SizeHorCursor));
236             if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
237             else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
238         } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
239             setCursor(KCursor("right_side", Qt::SizeHorCursor));
240             if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
241             else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
242         } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
243             setCursor(Qt::SizeHorCursor);
244             if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
245             else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
246         } else {
247             setCursor(Qt::ArrowCursor);
248             if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
249             else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
250         }
251     }
252 }
253
254
255 // virtual
256 void CustomRuler::leaveEvent(QEvent * event)
257 {
258     QWidget::leaveEvent(event);
259     if (m_cursorColor == palette().highlight()) {
260         m_cursorColor = palette().text();
261         update();
262     }
263 }
264
265 // virtual
266 void CustomRuler::wheelEvent(QWheelEvent * e)
267 {
268     int delta = 1;
269     if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
270     if (e->delta() < 0) delta = 0 - delta;
271     m_view->moveCursorPos(delta);
272 }
273
274 int CustomRuler::inPoint() const
275 {
276     return m_zoneStart;
277 }
278
279 int CustomRuler::outPoint() const
280 {
281     return m_zoneEnd;
282 }
283
284 void CustomRuler::slotMoveRuler(int newPos)
285 {
286     m_offset = newPos;
287     update();
288 }
289
290 int CustomRuler::offset() const
291 {
292     return m_offset;
293 }
294
295 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
296 {
297     int min = qMin(oldpos, newpos);
298     int max = qMax(oldpos, newpos);
299     if (m_lastSeekPosition != SEEK_INACTIVE) {
300         if (m_lastSeekPosition == newpos) {
301             m_lastSeekPosition = SEEK_INACTIVE;
302         }
303         else {
304             min = qMin(min, m_lastSeekPosition);
305             max = qMax(max, m_lastSeekPosition);
306         }
307     }
308     update(min * m_factor - m_offset - 6, BIG_MARK_X, (max - min) * m_factor + 14, MAX_HEIGHT - BIG_MARK_X);
309 }
310
311 void CustomRuler::updateRuler()
312 {
313     // Update requested seek position
314     int min = SEEK_INACTIVE;
315     int max = SEEK_INACTIVE;
316     if (m_lastSeekPosition != SEEK_INACTIVE) {
317         min = max = m_lastSeekPosition;
318     }
319     m_lastSeekPosition = m_view->seekPosition();
320     if (m_lastSeekPosition != SEEK_INACTIVE) {
321         if (min == SEEK_INACTIVE) {
322             min = max = m_lastSeekPosition;
323         }
324         else {
325             min = qMin(min, m_lastSeekPosition);
326             max = qMax(max, m_lastSeekPosition);
327         }
328     }
329     if (min != SEEK_INACTIVE) {
330         update(min * m_factor - offset() - 3, BIG_MARK_X, (max - min) * m_factor + 6, MAX_HEIGHT - BIG_MARK_X);
331     }
332 }
333
334 void CustomRuler::setPixelPerMark(int rate)
335 {
336     int scale = comboScale[rate];
337     m_rate = rate;
338     m_factor = 1.0 / (double) scale * FRAME_SIZE;
339     m_scale = 1.0 / (double) scale;
340     double fend = m_scale * littleMarkDistance;
341     if (rate > 8) {
342         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
343         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
344     } else if (rate > 6) {
345         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
346         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
347     } else if (rate > 3) {
348         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
349         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
350     } else {
351         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
352         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
353     }
354     switch (rate) {
355     case 0:
356         m_textSpacing = fend;
357         break;
358     case 1:
359         m_textSpacing = fend * 5;
360         break;
361     case 2:
362     case 3:
363     case 4:
364         m_textSpacing = fend * m_timecode.fps();
365         break;
366     case 5:
367         m_textSpacing = fend * m_timecode.fps() * 5;
368         break;
369     case 6:
370         m_textSpacing = fend * m_timecode.fps() * 10;
371         break;
372     case 7:
373         m_textSpacing = fend * m_timecode.fps() * 30;
374         break;
375     case 8:
376     case 9:
377         m_textSpacing = fend * m_timecode.fps() * 40;
378         break;
379     case 10:
380         m_textSpacing = fend * m_timecode.fps() * 80;
381         break;
382     case 11:
383     case 12:
384         m_textSpacing = fend * m_timecode.fps() * 400;
385         break;
386     case 13:
387         m_textSpacing = fend * m_timecode.fps() * 800;
388         break;
389     }
390     update();
391 }
392
393 void CustomRuler::setDuration(int d)
394 {
395     int oldduration = m_duration;
396     m_duration = d;
397     update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
398 }
399
400 // virtual
401 void CustomRuler::paintEvent(QPaintEvent *e)
402 {
403     QStylePainter p(this);
404     const QRect &paintRect = e->rect();
405     p.setClipRect(paintRect);
406
407     // Draw zone background
408     const int zoneStart = (int)(m_zoneStart * m_factor);
409     const int zoneEnd = (int)(m_zoneEnd * m_factor);
410     p.fillRect(zoneStart - m_offset, LABEL_SIZE + 2, zoneEnd - zoneStart, MAX_HEIGHT - LABEL_SIZE - 2, m_zoneColor);
411     
412     int minval = (paintRect.left() + m_offset) / FRAME_SIZE - 1;
413     const int maxval = (paintRect.right() + m_offset) / FRAME_SIZE + 1;
414     if (minval < 0)
415         minval = 0;
416
417     double f, fend;
418     const int offsetmax = maxval * FRAME_SIZE;
419     int offsetmin;
420
421     p.setPen(palette().text().color());
422
423     // draw time labels
424     if (paintRect.y() < LABEL_SIZE) {
425         offsetmin = (paintRect.left() + m_offset) / m_textSpacing;
426         offsetmin = offsetmin * m_textSpacing;
427         for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
428             QString lab;
429             if (KdenliveSettings::frametimecode())
430                 lab = QString::number((int)(f / m_factor + 0.5));
431             else
432                 lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
433             p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
434         }
435     }
436
437     offsetmin = (paintRect.left() + m_offset) / littleMarkDistance;
438     offsetmin = offsetmin * littleMarkDistance;
439     // draw the little marks
440     fend = m_scale * littleMarkDistance;
441     if (fend > 5) {
442         for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
443             p.drawLine((int)f, LITTLE_MARK_X, (int)f, MAX_HEIGHT);
444     }
445
446     offsetmin = (paintRect.left() + m_offset) / mediumMarkDistance;
447     offsetmin = offsetmin * mediumMarkDistance;
448     // draw medium marks
449     fend = m_scale * mediumMarkDistance;
450     if (fend > 5) {
451         for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
452             p.drawLine((int)f, MIDDLE_MARK_X, (int)f, MAX_HEIGHT);
453     }
454
455     offsetmin = (paintRect.left() + m_offset) / bigMarkDistance;
456     offsetmin = offsetmin * bigMarkDistance;
457     // draw big marks
458     fend = m_scale * bigMarkDistance;
459     if (fend > 5) {
460         for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
461             p.drawLine((int)f, BIG_MARK_X, (int)f, MAX_HEIGHT);
462     }
463
464     // draw zone cursors
465     if (zoneStart > 0) {
466         QPolygon pa(4);
467         pa.setPoints(4, zoneStart - m_offset + 3, LABEL_SIZE + 2, zoneStart - m_offset, LABEL_SIZE + 2, zoneStart - m_offset, MAX_HEIGHT - 1, zoneStart - m_offset + 3, MAX_HEIGHT - 1);
468         p.drawPolyline(pa);
469     }
470
471     if (zoneEnd > 0) {
472         QColor center(Qt::white);
473         center.setAlpha(150);
474         QRect rec(zoneStart - m_offset + (zoneEnd - zoneStart) / 2 - 4, LABEL_SIZE + 2, 8, MAX_HEIGHT - LABEL_SIZE - 3);
475         p.fillRect(rec, center);
476         p.drawRect(rec);
477
478         QPolygon pa(4);
479         pa.setPoints(4, zoneEnd - m_offset - 3, LABEL_SIZE + 2, zoneEnd - m_offset, LABEL_SIZE + 2, zoneEnd - m_offset, MAX_HEIGHT - 1, zoneEnd - m_offset - 3, MAX_HEIGHT - 1);
480         p.drawPolyline(pa);
481     }
482     
483     // draw pointer
484     const int value  =  m_view->cursorPos() * m_factor - m_offset;
485     QPolygon pa(3);
486     pa.setPoints(3, value - 6, BIG_MARK_X, value + 6, BIG_MARK_X, value, MAX_HEIGHT - 1);
487     p.setBrush(m_cursorColor);
488     p.setPen(Qt::NoPen);
489     p.drawPolygon(pa);
490     
491     if (m_lastSeekPosition != SEEK_INACTIVE && m_lastSeekPosition != m_view->cursorPos()) {
492         p.fillRect(m_lastSeekPosition * m_factor - m_offset - 1, BIG_MARK_X, 3, MAX_HEIGHT - 1, palette().highlight());
493     }
494
495 }
496
497 #include "customruler.moc"