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