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