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