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