]> git.sesse.net Git - kdenlive/blob - src/customruler.cpp
start implementing timeline guides
[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 <KGlobalSettings>
26
27 #include "customruler.h"
28
29
30 #define INIT_VALUE 0
31 #define INIT_MIN_VALUE 0
32 #define INIT_MAX_VALUE 100
33 #define INIT_TINY_MARK_DISTANCE 1
34 #define INIT_LITTLE_MARK_DISTANCE 5
35 #define INIT_MIDDLE_MARK_DISTANCE (INIT_LITTLE_MARK_DISTANCE * 2)
36 #define INIT_BIG_MARK_DISTANCE (INIT_LITTLE_MARK_DISTANCE * 10)
37 #define INIT_SHOW_TINY_MARK false
38 #define INIT_SHOW_LITTLE_MARK true
39 #define INIT_SHOW_MEDIUM_MARK true
40 #define INIT_SHOW_BIG_MARK true
41 #define INIT_SHOW_END_MARK true
42 #define INIT_SHOW_POINTER true
43 #define INIT_SHOW_END_LABEL true
44
45 #define INIT_PIXEL_PER_MARK (double)10.0 /* distance between 2 base marks in pixel */
46 #define INIT_OFFSET (-20)
47 #define INIT_LENGTH_FIX true
48 #define INIT_END_OFFSET 0
49
50 #define FIX_WIDTH 24 /* widget width in pixel */
51 #define LINE_END (FIX_WIDTH - 3)
52 #define END_MARK_LENGTH (FIX_WIDTH - 8)
53 #define END_MARK_X2 LINE_END
54 #define END_MARK_X1 (END_MARK_X2 - END_MARK_LENGTH)
55 #define BIG_MARK_LENGTH (END_MARK_LENGTH*3/4)
56 #define BIG_MARK_X2 LINE_END
57 #define BIG_MARK_X1 (BIG_MARK_X2 - BIG_MARK_LENGTH)
58 #define MIDDLE_MARK_LENGTH (END_MARK_LENGTH/2)
59 #define MIDDLE_MARK_X2 LINE_END
60 #define MIDDLE_MARK_X1 (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH)
61 #define LITTLE_MARK_LENGTH (MIDDLE_MARK_LENGTH/2)
62 #define LITTLE_MARK_X2 LINE_END
63 #define LITTLE_MARK_X1 (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH)
64 #define BASE_MARK_LENGTH (LITTLE_MARK_LENGTH/2)
65 #define BASE_MARK_X2 LINE_END
66 #define BASE_MARK_X1 (BASE_MARK_X2 - 3) //BASE_MARK_LENGTH
67
68 #define LABEL_SIZE 9
69 #define END_LABEL_X 4
70 #define END_LABEL_Y (END_LABEL_X + LABEL_SIZE - 2)
71
72 #include "definitions.h"
73
74 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000, 12000};
75
76 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent)
77         : KRuler(parent), m_timecode(tc), m_view(parent), m_duration(0) {
78     setFont(KGlobalSettings::toolBarFont());
79     slotNewOffset(0);
80     setRulerMetricStyle(KRuler::Pixel);
81     setLength(1024);
82     setMaximum(1024);
83     setPixelPerMark(3);
84     setLittleMarkDistance(FRAME_SIZE);
85     setMediumMarkDistance(FRAME_SIZE * m_timecode.fps());
86     setBigMarkDistance(FRAME_SIZE * m_timecode.fps() * 60);
87     m_zoneStart = 2 * m_timecode.fps();
88     m_zoneEnd = 10 * m_timecode.fps();
89     m_contextMenu = new QMenu(this);
90     QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
91     connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
92 }
93
94 // virtual
95 void CustomRuler::mousePressEvent(QMouseEvent * event) {
96     if (event->button() == Qt::RightButton) {
97         m_contextMenu->exec(event->globalPos());
98         return;
99     }
100     m_view->activateMonitor();
101     int pos = (int)((event->x() + offset()));
102     m_moveCursor = RULER_CURSOR;
103     if (event->y() > 10) {
104         if (qAbs(pos - m_zoneStart * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_START;
105         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_MIDDLE;
106         else if (qAbs(pos - m_zoneEnd * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_END;
107     }
108     if (m_moveCursor == RULER_CURSOR)
109         m_view->setCursorPos((int) pos / pixelPerMark() / FRAME_SIZE);
110 }
111
112 // virtual
113 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
114     int pos = (int)((event->x() + offset()) / pixelPerMark() / FRAME_SIZE);
115     if (m_moveCursor == RULER_CURSOR) {
116         m_view->setCursorPos(pos);
117         return;
118     } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
119     else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
120     else if (m_moveCursor == RULER_MIDDLE) {
121         int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
122         m_zoneStart += move;
123         m_zoneEnd += move;
124     }
125     update();
126 }
127
128
129 // virtual
130 void CustomRuler::wheelEvent(QWheelEvent * e) {
131     int delta = 1;
132     if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
133     if (e->delta() < 0) delta = 0 - delta;
134     m_view->moveCursorPos(delta);
135 }
136
137 int CustomRuler::inPoint() const {
138     return m_zoneStart;
139 }
140
141 int CustomRuler::outPoint() const {
142     return m_zoneEnd;
143 }
144
145 void CustomRuler::slotMoveRuler(int newPos) {
146     KRuler::slotNewOffset(newPos);
147 }
148
149 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
150     update(oldpos - offset() - 6, 2, 17, 16);
151     update(newpos - offset() - 6, 2, 17, 16);
152 }
153
154 void CustomRuler::setPixelPerMark(double rate) {
155     int scale = comboScale[(int) rate];
156     KRuler::setPixelPerMark(1.0 / scale);
157     double fend = pixelPerMark() * littleMarkDistance();
158     switch ((int) rate) {
159     case 0:
160         m_textSpacing = fend;
161         break;
162     case 1:
163         m_textSpacing = fend * 5;
164         break;
165     case 2:
166     case 3:
167     case 4:
168         m_textSpacing = fend * m_timecode.fps();
169         break;
170     case 5:
171         m_textSpacing = fend * m_timecode.fps() * 5;
172         break;
173     case 6:
174         m_textSpacing = fend * m_timecode.fps() * 10;
175         break;
176     case 7:
177         m_textSpacing = fend * m_timecode.fps() * 30;
178         break;
179     case 8:
180     case 9:
181     case 10:
182         m_textSpacing = fend * m_timecode.fps() * 60;
183         break;
184     case 11:
185     case 12:
186         m_textSpacing = fend * m_timecode.fps() * 300;
187         break;
188     case 13:
189         m_textSpacing = fend * m_timecode.fps() * 600;
190         break;
191     }
192 }
193
194 void CustomRuler::setDuration(int d) {
195     m_duration = d;
196     update();
197 }
198
199 // virtual
200 void CustomRuler::paintEvent(QPaintEvent *e) {
201     //  debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical");
202
203     QStylePainter p(this);
204     p.setClipRect(e->rect());
205
206     //p.fillRect(e->rect(), QBrush(QColor(200, 200, 200)));
207     //kDebug()<<"RULER ZONE: "<<m_zoneStart<<", OFF: "<<offset()<<", END: "<<m_zoneEnd<<", FACTOR: "<<pixelPerMark() * FRAME_SIZE;
208     int projectEnd = (int)(m_duration * pixelPerMark() * FRAME_SIZE);
209     p.fillRect(QRect(- offset(), e->rect().y(), projectEnd, e->rect().height()), QBrush(QColor(245, 245, 245)));
210
211
212     int zoneStart = (int)(m_zoneStart * pixelPerMark() * FRAME_SIZE);
213     int zoneEnd = (int)(m_zoneEnd * pixelPerMark() * FRAME_SIZE);
214
215     p.fillRect(QRect(zoneStart - offset(), e->rect().y() + e->rect().height() / 2, zoneEnd - zoneStart, e->rect().height() / 2), QBrush(QColor(133, 255, 143)));
216
217     int value  = m_view->cursorPos() - offset() + 4;
218     int minval = minimum();
219     int maxval = maximum() + offset() - endOffset();
220
221     //ioffsetval = value-offset;
222     //    pixelpm = (int)ppm;
223     //    left  = clip.left(),
224     //    right = clip.right();
225     double f, fend,
226     offsetmin = (double)(minval - offset()),
227                 offsetmax = (double)(maxval - offset()),
228                             fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
229     QRect bg = QRect((int)offsetmin, 0, (int)offsetmax, height());
230
231     QPalette palette;
232     //p.fillRect(bg, palette.light());
233     // draw labels
234     p.setPen(palette.dark().color());
235     // draw littlemarklabel
236
237     // draw mediummarklabel
238
239     // draw bigmarklabel
240
241     // draw endlabel
242     /*if (d->showEndL) {
243       if (d->dir == Qt::Horizontal) {
244         p.translate( fontOffset, 0 );
245         p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel );
246       }*/
247
248     // draw the tiny marks
249     //if (showTinyMarks())
250     /*{
251       fend =   pixelPerMark()*tinyMarkDistance();
252       if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
253           p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
254       }
255     }*/
256
257     for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
258         QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
259         p.drawText((int)f + 2, LABEL_SIZE, lab);
260     }
261
262     if (showLittleMarks()) {
263         // draw the little marks
264         fend = pixelPerMark() * littleMarkDistance();
265         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
266                 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
267     }
268     if (showMediumMarks()) {
269         // draw medium marks
270         fend = pixelPerMark() * mediumMarkDistance();
271         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
272                 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
273     }
274     if (showBigMarks()) {
275         // draw big marks
276         fend = pixelPerMark() * bigMarkDistance();
277         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
278                 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
279     }
280     /*   if (d->showem) {
281          // draw end marks
282          if (d->dir == Qt::Horizontal) {
283            p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
284            p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
285          }
286          else {
287            p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
288            p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
289          }
290        }*/
291
292
293     // draw zone cursors
294     int off = offset();
295     if (zoneStart > 0) {
296         QPolygon pa(4);
297         pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
298         p.drawPolyline(pa);
299     }
300
301     if (zoneEnd > 0) {
302         QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
303         p.fillRect(rec, QColor(255, 255, 255, 150));
304         p.drawRect(rec);
305
306         QPolygon pa(4);
307         pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
308         p.drawPolyline(pa);
309     }
310
311     // draw pointer
312     if (showPointer() && value > 0) {
313         QPolygon pa(3);
314         pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
315         p.setBrush(QBrush(Qt::yellow));
316         p.drawPolygon(pa);
317     }
318
319
320
321 }
322
323 #include "customruler.moc"