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