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