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