]> git.sesse.net Git - kdenlive/blob - src/customruler.cpp
Use current color scheme to draw timeline, fix ruler repaint issues
[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 const int LABEL_SIZE = 9;
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 {
62     setFont(KGlobalSettings::toolBarFont());
63     m_scale = 3;
64     m_zoneColor = QColor(133, 255, 143);
65     littleMarkDistance = FRAME_SIZE;
66     mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
67     bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
68     m_zoneStart = 0;
69     m_zoneEnd = 100;
70     m_contextMenu = new QMenu(this);
71     QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
72     connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
73     QAction *editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
74     connect(editGuide, SIGNAL(triggered()), m_view, SLOT(slotEditGuide()));
75     QAction *delGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
76     connect(delGuide, SIGNAL(triggered()), m_view, SLOT(slotDeleteGuide()));
77     QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
78     connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
79     setMouseTracking(true);
80     setMinimumHeight(20);
81 }
82
83 void CustomRuler::setZone(QPoint p)
84 {
85     int min = qMin(m_zoneStart, p.x());
86     int max = qMax(m_zoneEnd, p.y());
87     m_zoneStart = p.x();
88     m_zoneEnd = p.y();
89     update(min * m_factor - 2, 0, (max - min) * m_factor + 4, height());
90 }
91
92 // virtual
93 void CustomRuler::mousePressEvent(QMouseEvent * event)
94 {
95     if (event->button() == Qt::RightButton) {
96         m_contextMenu->exec(event->globalPos());
97         return;
98     }
99     m_view->activateMonitor();
100     int pos = (int)((event->x() + offset()));
101     m_moveCursor = RULER_CURSOR;
102     if (event->y() > 10) {
103         if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
104         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
105         else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
106     }
107     if (m_moveCursor == RULER_CURSOR)
108         m_view->setCursorPos((int) pos / m_factor);
109 }
110
111 // virtual
112 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
113 {
114     if (event->buttons() == Qt::LeftButton) {
115         int pos = (int)((event->x() + offset()) / m_factor);
116         int zoneStart = m_zoneStart;
117         int zoneEnd = m_zoneEnd;
118         if (pos < 0) pos = 0;
119         if (m_moveCursor == RULER_CURSOR) {
120             m_view->setCursorPos(pos);
121             m_view->slotCheckPositionScrolling();
122             return;
123         } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
124         else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
125         else if (m_moveCursor == RULER_MIDDLE) {
126             int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
127             if (move + m_zoneStart < 0) move = - m_zoneStart;
128             m_zoneStart += move;
129             m_zoneEnd += move;
130         }
131         emit zoneMoved(m_zoneStart, m_zoneEnd);
132         m_view->setDocumentModified();
133
134         int min = qMin(m_zoneStart, zoneStart);
135         int max = qMax(m_zoneEnd, zoneEnd);
136         update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
137
138     } else {
139         int pos = (int)((event->x() + offset()));
140         if (event->y() <= 10) setCursor(Qt::ArrowCursor);
141         else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
142         else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
143         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor);
144         else setCursor(Qt::ArrowCursor);
145     }
146 }
147
148
149 // virtual
150 void CustomRuler::wheelEvent(QWheelEvent * e)
151 {
152     int delta = 1;
153     if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
154     if (e->delta() < 0) delta = 0 - delta;
155     m_view->moveCursorPos(delta);
156 }
157
158 int CustomRuler::inPoint() const
159 {
160     return m_zoneStart;
161 }
162
163 int CustomRuler::outPoint() const
164 {
165     return m_zoneEnd;
166 }
167
168 void CustomRuler::slotMoveRuler(int newPos)
169 {
170     m_offset = newPos;
171     update();
172 }
173
174 int CustomRuler::offset() const
175 {
176     return m_offset;
177 }
178
179 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
180 {
181     if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) {
182         update(oldpos * m_factor - offset() - 6, 0, 17, height());
183         update(newpos * m_factor - offset() - 6, 0, 17, height());
184     } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 0, qAbs(oldpos - newpos) * m_factor + 17, height());
185 }
186
187 void CustomRuler::setPixelPerMark(double rate)
188 {
189     int scale = comboScale[(int) rate];
190     m_factor = 1.0 / (double) scale * FRAME_SIZE;
191     m_scale = 1.0 / (double) scale;
192     double fend = m_scale * littleMarkDistance;
193     if (rate > 8) {
194         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
195         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
196     }
197     else if (rate > 6) {
198         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
199         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
200     }
201     else if (rate > 3) {
202         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
203         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
204     }
205     else {
206         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
207         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
208     }
209     switch ((int) rate) {
210     case 0:
211         m_textSpacing = fend;
212         break;
213     case 1:
214         m_textSpacing = fend * 5;
215         break;
216     case 2:
217     case 3:
218     case 4:
219         m_textSpacing = fend * m_timecode.fps();
220         break;
221     case 5:
222         m_textSpacing = fend * m_timecode.fps() * 5;
223         break;
224     case 6:
225         m_textSpacing = fend * m_timecode.fps() * 10;
226         break;
227     case 7:
228         m_textSpacing = fend * m_timecode.fps() * 30;
229         break;
230     case 8:
231     case 9:
232         m_textSpacing = fend * m_timecode.fps() * 40;
233         break;
234     case 10:
235         m_textSpacing = fend * m_timecode.fps() * 80;
236         break;
237     case 11:
238     case 12:
239         m_textSpacing = fend * m_timecode.fps() * 400;
240         break;
241     case 13:
242         m_textSpacing = fend * m_timecode.fps() * 800;
243         break;
244     }
245     update();
246 }
247
248 void CustomRuler::setDuration(int d)
249 {
250     int oldduration = m_duration;
251     m_duration = d;
252     update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
253 }
254
255 // virtual
256 void CustomRuler::paintEvent(QPaintEvent *e)
257 {
258     QStylePainter p(this);
259     p.setClipRect(e->rect());
260     const int projectEnd = (int)(m_duration * m_factor);
261     p.fillRect(0, 0, projectEnd - m_offset, height(), palette().alternateBase().color());
262
263     const int zoneStart = (int)(m_zoneStart * m_factor);
264     const int zoneEnd = (int)(m_zoneEnd * m_factor);
265     const QRect zoneRect();
266
267     p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
268
269     const int value  = m_view->cursorPos() * m_factor - m_offset;
270     int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
271     const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
272     if (minval < 0) minval = 0;
273
274     double f, fend;
275     const int offsetmax = maxval * FRAME_SIZE;
276
277     p.setPen(palette().dark().color());
278
279     // draw time labels
280     int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
281     offsetmin = offsetmin * m_textSpacing;
282     for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
283         QString lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
284
285         p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
286     }
287
288     offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
289     offsetmin = offsetmin * littleMarkDistance;
290     // draw the little marks
291     fend = m_scale * littleMarkDistance;
292     if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
293         p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
294
295     offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
296     offsetmin = offsetmin * mediumMarkDistance;
297     // draw medium marks
298     fend = m_scale * mediumMarkDistance;
299     if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
300         p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
301
302     offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
303     offsetmin = offsetmin * bigMarkDistance;
304     // draw big marks
305     fend = m_scale * bigMarkDistance;
306     if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
307         p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
308
309     // draw zone cursors
310     int off = offset();
311     if (zoneStart > 0) {
312         QPolygon pa(4);
313         pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
314         p.drawPolyline(pa);
315     }
316
317     if (zoneEnd > 0) {
318         QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
319         p.fillRect(rec, QColor(255, 255, 255, 150));
320         p.drawRect(rec);
321
322         QPolygon pa(4);
323         pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
324         p.drawPolyline(pa);
325     }
326
327     // draw pointer
328     QPolygon pa(3);
329     pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16);
330     p.setBrush(palette().highlight());
331     p.drawPolygon(pa);
332 }
333
334 #include "customruler.moc"