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