]> git.sesse.net Git - kdenlive/blob - src/customruler.cpp
Scroll timeline when seeking near a border and also when dragging a clip, should...
[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 <QMouseEvent>
21 #include <QStylePainter>
22
23 #include <KDebug>
24 #include <KIcon>
25 #include <KCursor>
26 #include <KGlobalSettings>
27
28 #include "customruler.h"
29
30
31 static const int FIX_WIDTH = 24; /* widget width in pixel */
32 static const int LINE_END = (FIX_WIDTH - 3);
33 static const int END_MARK_LENGTH = (FIX_WIDTH - 8);
34 static const int BIG_MARK_LENGTH = (END_MARK_LENGTH * 3 / 4);
35 static const int BIG_MARK_X2 = LINE_END;
36 static const int BIG_MARK_X1 = (BIG_MARK_X2 - BIG_MARK_LENGTH);
37 static const int MIDDLE_MARK_LENGTH = (END_MARK_LENGTH / 2);
38 static const int MIDDLE_MARK_X2 = LINE_END;
39 static const int MIDDLE_MARK_X1 = (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH);
40 static const int LITTLE_MARK_LENGTH = (MIDDLE_MARK_LENGTH / 2);
41 static const int LITTLE_MARK_X2 = LINE_END;
42 static const int LITTLE_MARK_X1 = (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH);
43
44 static const int LABEL_SIZE = 9;
45 static const int END_LABEL_X = 4;
46 static const int END_LABEL_Y = (END_LABEL_X + LABEL_SIZE - 2);
47
48 static int littleMarkDistance;
49 static int mediumMarkDistance;
50 static int bigMarkDistance;
51
52 #include "definitions.h"
53
54 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000, 12000};
55
56 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent)
57         : QWidget(parent), m_timecode(tc), m_view(parent), m_duration(0), m_offset(0) {
58     setFont(KGlobalSettings::toolBarFont());
59     m_scale = 3;
60     littleMarkDistance = FRAME_SIZE;
61     mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
62     bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
63     m_zoneStart = 2 * m_timecode.fps();
64     m_zoneEnd = 10 * m_timecode.fps();
65     m_contextMenu = new QMenu(this);
66     QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
67     connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
68     QAction *editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
69     connect(editGuide, SIGNAL(triggered()), m_view, SLOT(slotEditGuide()));
70     QAction *delGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
71     connect(delGuide, SIGNAL(triggered()), m_view, SLOT(slotDeleteGuide()));
72     QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
73     connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
74     setMouseTracking(true);
75     setMinimumHeight(20);
76 }
77
78 void CustomRuler::setZone(QPoint p) {
79     int min = qMin(m_zoneStart, p.x());
80     int max = qMax(m_zoneEnd, p.y());
81     m_zoneStart = p.x();
82     m_zoneEnd = p.y();
83     update(min * m_factor - 2, 0, (max - min) * m_factor + 4, height());
84 }
85
86 // virtual
87 void CustomRuler::mousePressEvent(QMouseEvent * event) {
88     if (event->button() == Qt::RightButton) {
89         m_contextMenu->exec(event->globalPos());
90         return;
91     }
92     m_view->activateMonitor();
93     int pos = (int)((event->x() + offset()));
94     m_moveCursor = RULER_CURSOR;
95     if (event->y() > 10) {
96         if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
97         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
98         else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
99     }
100     if (m_moveCursor == RULER_CURSOR)
101         m_view->setCursorPos((int) pos / m_factor);
102 }
103
104 // virtual
105 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
106     if (event->buttons() == Qt::LeftButton) {
107         int pos = (int)((event->x() + offset()) / m_factor);
108         int zoneStart = m_zoneStart;
109         int zoneEnd = m_zoneEnd;
110         if (pos < 0) pos = 0;
111         if (m_moveCursor == RULER_CURSOR) {
112             m_view->setCursorPos(pos);
113             m_view->slotCheckPositionScrolling();
114             return;
115         } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
116         else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
117         else if (m_moveCursor == RULER_MIDDLE) {
118             int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
119             if (move + m_zoneStart < 0) move = - m_zoneStart;
120             m_zoneStart += move;
121             m_zoneEnd += move;
122         }
123         emit zoneMoved(m_zoneStart, m_zoneEnd);
124         m_view->setDocumentModified();
125
126         int min = qMin(m_zoneStart, zoneStart);
127         int max = qMax(m_zoneEnd, zoneEnd);
128         update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
129
130     } else {
131         int pos = (int)((event->x() + offset()));
132         if (event->y() <= 10) setCursor(Qt::ArrowCursor);
133         else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
134         else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
135         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor);
136         else setCursor(Qt::ArrowCursor);
137     }
138 }
139
140
141 // virtual
142 void CustomRuler::wheelEvent(QWheelEvent * e) {
143     int delta = 1;
144     if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
145     if (e->delta() < 0) delta = 0 - delta;
146     m_view->moveCursorPos(delta);
147 }
148
149 int CustomRuler::inPoint() const {
150     return m_zoneStart;
151 }
152
153 int CustomRuler::outPoint() const {
154     return m_zoneEnd;
155 }
156
157 void CustomRuler::slotMoveRuler(int newPos) {
158     m_offset = newPos;
159     update();
160 }
161
162 int CustomRuler::offset() const {
163     return m_offset;
164 }
165
166 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
167     if (qAbs(oldpos - newpos) * m_factor > 40) {
168         update(oldpos * m_factor - offset() - 6, 7, 17, 16);
169         update(newpos * m_factor - offset() - 6, 7, 17, 16);
170     } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 7, qAbs(oldpos - newpos) * m_factor + 17, 16);
171 }
172
173 void CustomRuler::setPixelPerMark(double rate) {
174     int scale = comboScale[(int) rate];
175     m_factor = 1.0 / (double) scale * FRAME_SIZE;
176     m_scale = 1.0 / (double) scale;
177     double fend = m_scale * littleMarkDistance;
178     switch ((int) rate) {
179     case 0:
180         m_textSpacing = fend;
181         break;
182     case 1:
183         m_textSpacing = fend * 5;
184         break;
185     case 2:
186     case 3:
187     case 4:
188         m_textSpacing = fend * m_timecode.fps();
189         break;
190     case 5:
191         m_textSpacing = fend * m_timecode.fps() * 5;
192         break;
193     case 6:
194         m_textSpacing = fend * m_timecode.fps() * 10;
195         break;
196     case 7:
197         m_textSpacing = fend * m_timecode.fps() * 30;
198         break;
199     case 8:
200     case 9:
201     case 10:
202         m_textSpacing = fend * m_timecode.fps() * 60;
203         break;
204     case 11:
205     case 12:
206         m_textSpacing = fend * m_timecode.fps() * 300;
207         break;
208     case 13:
209         m_textSpacing = fend * m_timecode.fps() * 600;
210         break;
211     }
212     update();
213 }
214
215 void CustomRuler::setDuration(int d) {
216     int oldduration = m_duration;
217     m_duration = d;
218     update(qMin(oldduration, m_duration) * m_factor - 1, 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
219 }
220
221 // virtual
222 void CustomRuler::paintEvent(QPaintEvent *e) {
223     QStylePainter p(this);
224     p.setClipRect(e->rect());
225
226     const int projectEnd = (int)(m_duration * m_factor);
227     p.fillRect(QRect(0, 0, projectEnd - m_offset, height()), QBrush(QColor(245, 245, 245)));
228
229     const int zoneStart = (int)(m_zoneStart * m_factor);
230     const int zoneEnd = (int)(m_zoneEnd * m_factor);
231
232     p.fillRect(QRect(zoneStart - offset(), height() / 2, zoneEnd - zoneStart, height() / 2), QBrush(QColor(133, 255, 143)));
233
234     const int value  = m_view->cursorPos() * m_factor - offset();
235     int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
236     const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
237     if (minval < 0) minval = 0;
238
239     double f, fend;
240     const int offsetmax = maxval * FRAME_SIZE;
241
242     QPalette palette;
243     p.setPen(palette.dark().color());
244     int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
245     offsetmin = offsetmin * m_textSpacing;
246     for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
247         QString lab = m_timecode.getTimecodeFromFrames((int)((f) / m_factor + 0.5));
248         p.drawText((int)f - m_offset + 2, LABEL_SIZE, lab);
249     }
250
251     if (true) {
252         offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
253         offsetmin = offsetmin * littleMarkDistance;
254         // draw the little marks
255         fend = m_scale * littleMarkDistance;
256         if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
257                 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
258     }
259     if (true) {
260         offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
261         offsetmin = offsetmin * mediumMarkDistance;
262         // draw medium marks
263         fend = m_scale * mediumMarkDistance;
264         if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
265                 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
266     }
267     if (true) {
268         offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
269         offsetmin = offsetmin * bigMarkDistance;
270         // draw big marks
271         fend = m_scale * bigMarkDistance;
272         if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
273                 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
274     }
275
276     // draw zone cursors
277     int off = offset();
278     if (zoneStart > 0) {
279         QPolygon pa(4);
280         pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
281         p.drawPolyline(pa);
282     }
283
284     if (zoneEnd > 0) {
285         QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
286         p.fillRect(rec, QColor(255, 255, 255, 150));
287         p.drawRect(rec);
288
289         QPolygon pa(4);
290         pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
291         p.drawPolyline(pa);
292     }
293
294     // draw pointer
295     QPolygon pa(3);
296     pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
297     p.setBrush(QBrush(Qt::yellow));
298     p.drawPolygon(pa);
299 }
300
301 #include "customruler.moc"