]> git.sesse.net Git - kdenlive/blob - src/customruler.cpp
default apps (video player, external image / sound editor) can now be defined in...
[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     } else {
135         int pos = (int)((event->x() + offset()));
136         if (event->y() <= 10) setCursor(Qt::ArrowCursor);
137         else if (qAbs(pos - m_zoneStart * pixelPerMark() * FRAME_SIZE) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
138         else if (qAbs(pos - m_zoneEnd * pixelPerMark() * FRAME_SIZE) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
139         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * pixelPerMark() * FRAME_SIZE) < 4) setCursor(Qt::SizeHorCursor);
140         else setCursor(Qt::ArrowCursor);
141     }
142 }
143
144
145 // virtual
146 void CustomRuler::wheelEvent(QWheelEvent * e) {
147     int delta = 1;
148     if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
149     if (e->delta() < 0) delta = 0 - delta;
150     m_view->moveCursorPos(delta);
151 }
152
153 int CustomRuler::inPoint() const {
154     return m_zoneStart;
155 }
156
157 int CustomRuler::outPoint() const {
158     return m_zoneEnd;
159 }
160
161 void CustomRuler::slotMoveRuler(int newPos) {
162     KRuler::slotNewOffset(newPos);
163 }
164
165 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
166     update(oldpos - offset() - 6, 2, 17, 16);
167     update(newpos - offset() - 6, 2, 17, 16);
168 }
169
170 void CustomRuler::setPixelPerMark(double rate) {
171     int scale = comboScale[(int) rate];
172     KRuler::setPixelPerMark(1.0 / scale);
173     double fend = pixelPerMark() * littleMarkDistance();
174     switch ((int) rate) {
175     case 0:
176         m_textSpacing = fend;
177         break;
178     case 1:
179         m_textSpacing = fend * 5;
180         break;
181     case 2:
182     case 3:
183     case 4:
184         m_textSpacing = fend * m_timecode.fps();
185         break;
186     case 5:
187         m_textSpacing = fend * m_timecode.fps() * 5;
188         break;
189     case 6:
190         m_textSpacing = fend * m_timecode.fps() * 10;
191         break;
192     case 7:
193         m_textSpacing = fend * m_timecode.fps() * 30;
194         break;
195     case 8:
196     case 9:
197     case 10:
198         m_textSpacing = fend * m_timecode.fps() * 60;
199         break;
200     case 11:
201     case 12:
202         m_textSpacing = fend * m_timecode.fps() * 300;
203         break;
204     case 13:
205         m_textSpacing = fend * m_timecode.fps() * 600;
206         break;
207     }
208 }
209
210 void CustomRuler::setDuration(int d) {
211     m_duration = d;
212     update();
213 }
214
215 // virtual
216 void CustomRuler::paintEvent(QPaintEvent *e) {
217     //  debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical");
218
219     QStylePainter p(this);
220     p.setClipRect(e->rect());
221
222     //p.fillRect(e->rect(), QBrush(QColor(200, 200, 200)));
223     //kDebug()<<"RULER ZONE: "<<m_zoneStart<<", OFF: "<<offset()<<", END: "<<m_zoneEnd<<", FACTOR: "<<pixelPerMark() * FRAME_SIZE;
224     int projectEnd = (int)(m_duration * pixelPerMark() * FRAME_SIZE);
225     p.fillRect(QRect(- offset(), e->rect().y(), projectEnd, e->rect().height()), QBrush(QColor(245, 245, 245)));
226
227
228     int zoneStart = (int)(m_zoneStart * pixelPerMark() * FRAME_SIZE);
229     int zoneEnd = (int)(m_zoneEnd * pixelPerMark() * FRAME_SIZE);
230
231     p.fillRect(QRect(zoneStart - offset(), e->rect().y() + e->rect().height() / 2, zoneEnd - zoneStart, e->rect().height() / 2), QBrush(QColor(133, 255, 143)));
232
233     int value  = m_view->cursorPos() - offset();
234     int minval = minimum();
235     int maxval = maximum() + offset() - endOffset();
236
237     //ioffsetval = value-offset;
238     //    pixelpm = (int)ppm;
239     //    left  = clip.left(),
240     //    right = clip.right();
241     double f, fend,
242     offsetmin = (double)(minval - offset()),
243                 offsetmax = (double)(maxval - offset()),
244                             fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
245     QRect bg = QRect((int)offsetmin, 0, (int)offsetmax, height());
246
247     QPalette palette;
248     //p.fillRect(bg, palette.light());
249     // draw labels
250     p.setPen(palette.dark().color());
251     // draw littlemarklabel
252
253     // draw mediummarklabel
254
255     // draw bigmarklabel
256
257     // draw endlabel
258     /*if (d->showEndL) {
259       if (d->dir == Qt::Horizontal) {
260         p.translate( fontOffset, 0 );
261         p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel );
262       }*/
263
264     // draw the tiny marks
265     //if (showTinyMarks())
266     /*{
267       fend =   pixelPerMark()*tinyMarkDistance();
268       if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
269           p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
270       }
271     }*/
272
273     for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
274         QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
275         p.drawText((int)f + 2, LABEL_SIZE, lab);
276     }
277
278     if (showLittleMarks()) {
279         // draw the little marks
280         fend = pixelPerMark() * littleMarkDistance();
281         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
282                 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
283     }
284     if (showMediumMarks()) {
285         // draw medium marks
286         fend = pixelPerMark() * mediumMarkDistance();
287         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
288                 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
289     }
290     if (showBigMarks()) {
291         // draw big marks
292         fend = pixelPerMark() * bigMarkDistance();
293         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
294                 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
295     }
296     /*   if (d->showem) {
297          // draw end marks
298          if (d->dir == Qt::Horizontal) {
299            p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
300            p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
301          }
302          else {
303            p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
304            p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
305          }
306        }*/
307
308
309     // draw zone cursors
310     int off = offset();
311     if (zoneStart > 0) {
312         QPolygon pa(4);
313         pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
314         p.drawPolyline(pa);
315     }
316
317     if (zoneEnd > 0) {
318         QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
319         p.fillRect(rec, QColor(255, 255, 255, 150));
320         p.drawRect(rec);
321
322         QPolygon pa(4);
323         pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
324         p.drawPolyline(pa);
325     }
326
327     // draw pointer
328     if (showPointer() && value >= 0) {
329         QPolygon pa(3);
330         pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
331         p.setBrush(QBrush(Qt::yellow));
332         p.drawPolygon(pa);
333     }
334
335
336
337 }
338
339 #include "customruler.moc"