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