]> git.sesse.net Git - kdenlive/blob - src/customruler.cpp
Do not allow render zone to go below 0, might fix:
[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             return;
114         } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
115         else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
116         else if (m_moveCursor == RULER_MIDDLE) {
117             int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
118             if (move + m_zoneStart < 0) move = - m_zoneStart;
119             m_zoneStart += move;
120             m_zoneEnd += move;
121         }
122         emit zoneMoved(m_zoneStart, m_zoneEnd);
123         m_view->setDocumentModified();
124
125         int min = qMin(m_zoneStart, zoneStart);
126         int max = qMax(m_zoneEnd, zoneEnd);
127         update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
128
129     } else {
130         int pos = (int)((event->x() + offset()));
131         if (event->y() <= 10) setCursor(Qt::ArrowCursor);
132         else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
133         else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
134         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor);
135         else setCursor(Qt::ArrowCursor);
136     }
137 }
138
139
140 // virtual
141 void CustomRuler::wheelEvent(QWheelEvent * e) {
142     int delta = 1;
143     if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
144     if (e->delta() < 0) delta = 0 - delta;
145     m_view->moveCursorPos(delta);
146 }
147
148 int CustomRuler::inPoint() const {
149     return m_zoneStart;
150 }
151
152 int CustomRuler::outPoint() const {
153     return m_zoneEnd;
154 }
155
156 void CustomRuler::slotMoveRuler(int newPos) {
157     m_offset = newPos;
158     update();
159 }
160
161 int CustomRuler::offset() const {
162     return m_offset;
163 }
164
165 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
166     if (qAbs(oldpos - newpos) * m_factor > 40) {
167         update(oldpos * m_factor - offset() - 6, 7, 17, 16);
168         update(newpos * m_factor - offset() - 6, 7, 17, 16);
169     } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 7, qAbs(oldpos - newpos) * m_factor + 17, 16);
170 }
171
172 void CustomRuler::setPixelPerMark(double rate) {
173     int scale = comboScale[(int) rate];
174     m_factor = 1.0 / (double) scale * FRAME_SIZE;
175     m_scale = 1.0 / (double) scale;
176     double fend = m_scale * littleMarkDistance;
177     switch ((int) rate) {
178     case 0:
179         m_textSpacing = fend;
180         break;
181     case 1:
182         m_textSpacing = fend * 5;
183         break;
184     case 2:
185     case 3:
186     case 4:
187         m_textSpacing = fend * m_timecode.fps();
188         break;
189     case 5:
190         m_textSpacing = fend * m_timecode.fps() * 5;
191         break;
192     case 6:
193         m_textSpacing = fend * m_timecode.fps() * 10;
194         break;
195     case 7:
196         m_textSpacing = fend * m_timecode.fps() * 30;
197         break;
198     case 8:
199     case 9:
200     case 10:
201         m_textSpacing = fend * m_timecode.fps() * 60;
202         break;
203     case 11:
204     case 12:
205         m_textSpacing = fend * m_timecode.fps() * 300;
206         break;
207     case 13:
208         m_textSpacing = fend * m_timecode.fps() * 600;
209         break;
210     }
211     update();
212 }
213
214 void CustomRuler::setDuration(int d) {
215     int oldduration = m_duration;
216     m_duration = d;
217     update(qMin(oldduration, m_duration) * m_factor - 1, 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
218 }
219
220 // virtual
221 void CustomRuler::paintEvent(QPaintEvent *e) {
222     QStylePainter p(this);
223     p.setClipRect(e->rect());
224
225     const int projectEnd = (int)(m_duration * m_factor);
226     p.fillRect(QRect(0, 0, projectEnd - m_offset, height()), QBrush(QColor(245, 245, 245)));
227
228     const int zoneStart = (int)(m_zoneStart * m_factor);
229     const int zoneEnd = (int)(m_zoneEnd * m_factor);
230
231     p.fillRect(QRect(zoneStart - offset(), height() / 2, zoneEnd - zoneStart, height() / 2), QBrush(QColor(133, 255, 143)));
232
233     const int value  = m_view->cursorPos() * m_factor - offset();
234     int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
235     const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
236     if (minval < 0) minval = 0;
237
238     double f, fend;
239     const int offsetmax = maxval * FRAME_SIZE;
240
241     QPalette palette;
242     p.setPen(palette.dark().color());
243     int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
244     offsetmin = offsetmin * m_textSpacing;
245     for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
246         QString lab = m_timecode.getTimecodeFromFrames((int)((f) / m_factor + 0.5));
247         p.drawText((int)f - m_offset + 2, LABEL_SIZE, lab);
248     }
249
250     if (true) {
251         offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
252         offsetmin = offsetmin * littleMarkDistance;
253         // draw the little marks
254         fend = m_scale * littleMarkDistance;
255         if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
256                 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
257     }
258     if (true) {
259         offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
260         offsetmin = offsetmin * mediumMarkDistance;
261         // draw medium marks
262         fend = m_scale * mediumMarkDistance;
263         if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
264                 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
265     }
266     if (true) {
267         offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
268         offsetmin = offsetmin * bigMarkDistance;
269         // draw big marks
270         fend = m_scale * bigMarkDistance;
271         if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
272                 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
273     }
274
275     // draw zone cursors
276     int off = offset();
277     if (zoneStart > 0) {
278         QPolygon pa(4);
279         pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
280         p.drawPolyline(pa);
281     }
282
283     if (zoneEnd > 0) {
284         QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
285         p.fillRect(rec, QColor(255, 255, 255, 150));
286         p.drawRect(rec);
287
288         QPolygon pa(4);
289         pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
290         p.drawPolyline(pa);
291     }
292
293     // draw pointer
294     QPolygon pa(3);
295     pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
296     p.setBrush(QBrush(Qt::yellow));
297     p.drawPolygon(pa);
298 }
299
300 #include "customruler.moc"