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