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