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