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