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