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