]> git.sesse.net Git - kdenlive/blob - src/customruler.cpp
Fix zone snapping and color 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 #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         m_view->updateSnapPoints(NULL);
148     }
149     if (m_moveCursor == RULER_CURSOR) {
150         m_view->setCursorPos((int) pos / m_factor);
151         m_clickPoint = event->pos();
152         m_startRate = m_rate;
153     }
154 }
155
156 // virtual
157 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
158 {
159     if (event->buttons() == Qt::LeftButton) {
160         int pos;
161         if (m_moveCursor == RULER_START || m_moveCursor == RULER_END) {
162             pos = m_view->getSnapPointForPos((int)((event->x() + offset()) / m_factor));
163         } else pos = (int)((event->x() + offset()) / m_factor);
164         int zoneStart = m_zoneStart;
165         int zoneEnd = m_zoneEnd;
166         if (pos < 0) pos = 0;
167         if (m_moveCursor == RULER_CURSOR) {
168             QPoint diff = event->pos() - m_clickPoint;
169             if (m_mouseMove == NO_MOVE) {
170                 if (!KdenliveSettings::verticalzoom() || qAbs(diff.x()) >= QApplication::startDragDistance()) {
171                     m_mouseMove = HORIZONTAL_MOVE;
172                 } else if (qAbs(diff.y()) >= QApplication::startDragDistance()) {
173                     m_mouseMove = VERTICAL_MOVE;
174                 } else return;
175             }
176             if (m_mouseMove == HORIZONTAL_MOVE) {
177                 m_view->setCursorPos(pos);
178                 m_view->slotCheckPositionScrolling();
179             } else {
180                 int verticalDiff = m_startRate - (diff.y()) / 7;
181                 if (verticalDiff != m_rate) emit adjustZoom(verticalDiff);
182             }
183             return;
184         } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
185         else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
186         else if (m_moveCursor == RULER_MIDDLE) {
187             int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
188             if (move + m_zoneStart < 0) move = - m_zoneStart;
189             m_zoneStart += move;
190             m_zoneEnd += move;
191         }
192         emit zoneMoved(m_zoneStart, m_zoneEnd);
193         m_view->setDocumentModified();
194
195         int min = qMin(m_zoneStart, zoneStart);
196         int max = qMax(m_zoneEnd, zoneEnd);
197         update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
198
199     } else {
200         int pos = (int)((event->x() + offset()));
201         if (event->y() <= 10) setCursor(Qt::ArrowCursor);
202         else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
203             setCursor(KCursor("left_side", Qt::SizeHorCursor));
204             if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone start: %1", m_zoneStart));
205             else setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
206         } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
207             setCursor(KCursor("right_side", Qt::SizeHorCursor));
208             if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone end: %1", m_zoneEnd));
209             else setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
210         } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
211             setCursor(Qt::SizeHorCursor);
212             if (KdenliveSettings::frametimecode()) setToolTip(i18n("Zone duration: %1", m_zoneEnd - m_zoneStart));
213             else setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
214         } else {
215             setCursor(Qt::ArrowCursor);
216             if (KdenliveSettings::frametimecode()) setToolTip(i18n("Position: %1", (int)(pos / m_factor)));
217             else setToolTip(i18n("Position: %1", m_timecode.getTimecodeFromFrames(pos / m_factor)));
218         }
219     }
220 }
221
222
223 // virtual
224 void CustomRuler::wheelEvent(QWheelEvent * e)
225 {
226     int delta = 1;
227     if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
228     if (e->delta() < 0) delta = 0 - delta;
229     m_view->moveCursorPos(delta);
230 }
231
232 int CustomRuler::inPoint() const
233 {
234     return m_zoneStart;
235 }
236
237 int CustomRuler::outPoint() const
238 {
239     return m_zoneEnd;
240 }
241
242 void CustomRuler::slotMoveRuler(int newPos)
243 {
244     m_offset = newPos;
245     update();
246 }
247
248 int CustomRuler::offset() const
249 {
250     return m_offset;
251 }
252
253 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
254 {
255     if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) {
256         update(oldpos * m_factor - offset() - 6, 0, 17, height());
257         update(newpos * m_factor - offset() - 6, 0, 17, height());
258     } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 0, qAbs(oldpos - newpos) * m_factor + 17, height());
259 }
260
261 void CustomRuler::setPixelPerMark(int rate)
262 {
263     int scale = comboScale[rate];
264     m_rate = rate;
265     m_factor = 1.0 / (double) scale * FRAME_SIZE;
266     m_scale = 1.0 / (double) scale;
267     double fend = m_scale * littleMarkDistance;
268     if (rate > 8) {
269         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
270         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
271     } else if (rate > 6) {
272         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
273         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
274     } else if (rate > 3) {
275         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
276         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
277     } else {
278         mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
279         bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
280     }
281     switch (rate) {
282     case 0:
283         m_textSpacing = fend;
284         break;
285     case 1:
286         m_textSpacing = fend * 5;
287         break;
288     case 2:
289     case 3:
290     case 4:
291         m_textSpacing = fend * m_timecode.fps();
292         break;
293     case 5:
294         m_textSpacing = fend * m_timecode.fps() * 5;
295         break;
296     case 6:
297         m_textSpacing = fend * m_timecode.fps() * 10;
298         break;
299     case 7:
300         m_textSpacing = fend * m_timecode.fps() * 30;
301         break;
302     case 8:
303     case 9:
304         m_textSpacing = fend * m_timecode.fps() * 40;
305         break;
306     case 10:
307         m_textSpacing = fend * m_timecode.fps() * 80;
308         break;
309     case 11:
310     case 12:
311         m_textSpacing = fend * m_timecode.fps() * 400;
312         break;
313     case 13:
314         m_textSpacing = fend * m_timecode.fps() * 800;
315         break;
316     }
317     update();
318 }
319
320 void CustomRuler::setDuration(int d)
321 {
322     int oldduration = m_duration;
323     m_duration = d;
324     update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
325 }
326
327 // virtual
328 void CustomRuler::paintEvent(QPaintEvent *e)
329 {
330     QStylePainter p(this);
331     p.setClipRect(e->rect());
332     const int projectEnd = (int)(m_duration * m_factor);
333     p.fillRect(0, 0, projectEnd - m_offset, height(), palette().alternateBase().color());
334
335     const int zoneStart = (int)(m_zoneStart * m_factor);
336     const int zoneEnd = (int)(m_zoneEnd * m_factor);
337     const QRect zoneRect();
338
339     p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
340
341     const int value  = m_view->cursorPos() * m_factor - m_offset;
342     int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
343     const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
344     if (minval < 0)
345         minval = 0;
346
347     double f, fend;
348     const int offsetmax = maxval * FRAME_SIZE;
349
350     p.setPen(palette().text().color());
351
352     // draw time labels
353     int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
354     offsetmin = offsetmin * m_textSpacing;
355     for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
356         QString lab;
357         if (KdenliveSettings::frametimecode())
358             lab = QString::number((int)(f / m_factor + 0.5));
359         else
360             lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
361         p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
362     }
363
364     offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
365     offsetmin = offsetmin * littleMarkDistance;
366     // draw the little marks
367     fend = m_scale * littleMarkDistance;
368     if (fend > 5) {
369         for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
370             p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
371     }
372
373     offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
374     offsetmin = offsetmin * mediumMarkDistance;
375     // draw medium marks
376     fend = m_scale * mediumMarkDistance;
377     if (fend > 5) {
378         for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
379             p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
380     }
381
382     offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
383     offsetmin = offsetmin * bigMarkDistance;
384     // draw big marks
385     fend = m_scale * bigMarkDistance;
386     if (fend > 5) {
387         for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
388             p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
389     }
390
391     // draw zone cursors
392     int off = offset();
393     if (zoneStart > 0) {
394         QPolygon pa(4);
395         pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
396         p.drawPolyline(pa);
397     }
398
399     if (zoneEnd > 0) {
400         QColor center(Qt::white);
401         center.setAlpha(150);
402         QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
403         p.fillRect(rec, center);
404         p.drawRect(rec);
405
406         QPolygon pa(4);
407         pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
408         p.drawPolyline(pa);
409     }
410
411     // draw pointer
412     QPolygon pa(3);
413     pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16);
414     p.setBrush(palette().highlight());
415     p.drawPolygon(pa);
416 }
417
418 #include "customruler.moc"