]> git.sesse.net Git - kdenlive/blob - src/customruler.cpp
Rewrote the handling of timeline in QGraphicsView. Now we use the built in zoom featu...
[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 #include "definitions.h"
49
50 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000, 12000};
51
52 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent)
53         : KRuler(parent), m_timecode(tc), m_view(parent), m_duration(0) {
54     setFont(KGlobalSettings::toolBarFont());
55     slotNewOffset(0);
56     setRulerMetricStyle(KRuler::Pixel);
57     setLength(1024);
58     setMaximum(1024);
59     setPixelPerMark(3);
60     setLittleMarkDistance(FRAME_SIZE);
61     setMediumMarkDistance(FRAME_SIZE * m_timecode.fps());
62     setBigMarkDistance(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 }
76
77 // virtual
78 void CustomRuler::mousePressEvent(QMouseEvent * event) {
79     if (event->button() == Qt::RightButton) {
80         m_contextMenu->exec(event->globalPos());
81         return;
82     }
83     m_view->activateMonitor();
84     int pos = (int)((event->x() + offset()));
85     m_moveCursor = RULER_CURSOR;
86     if (event->y() > 10) {
87         if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
88         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
89         else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
90     }
91     if (m_moveCursor == RULER_CURSOR)
92         m_view->setCursorPos((int) pos / m_factor);
93 }
94
95 // virtual
96 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
97     if (event->buttons() == Qt::LeftButton) {
98         int pos = (int)((event->x() + offset()) / m_factor);
99         if (pos < 0) pos = 0;
100         if (m_moveCursor == RULER_CURSOR) {
101             m_view->setCursorPos(pos);
102             return;
103         } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
104         else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
105         else if (m_moveCursor == RULER_MIDDLE) {
106             int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
107             m_zoneStart += move;
108             m_zoneEnd += move;
109         }
110         update();
111     } else {
112         int pos = (int)((event->x() + offset()));
113         if (event->y() <= 10) setCursor(Qt::ArrowCursor);
114         else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
115         else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
116         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor);
117         else setCursor(Qt::ArrowCursor);
118     }
119 }
120
121
122 // virtual
123 void CustomRuler::wheelEvent(QWheelEvent * e) {
124     int delta = 1;
125     if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
126     if (e->delta() < 0) delta = 0 - delta;
127     m_view->moveCursorPos(delta);
128 }
129
130 int CustomRuler::inPoint() const {
131     return m_zoneStart;
132 }
133
134 int CustomRuler::outPoint() const {
135     return m_zoneEnd;
136 }
137
138 void CustomRuler::slotMoveRuler(int newPos) {
139     KRuler::slotNewOffset(newPos);
140 }
141
142 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
143     update(oldpos * m_factor - offset() - 6, 2, 17, 16);
144     update(newpos * m_factor - offset() - 6, 2, 17, 16);
145 }
146
147 void CustomRuler::setPixelPerMark(double rate) {
148     int scale = comboScale[(int) rate];
149     m_factor = 1.0 / (double) scale * FRAME_SIZE;
150     KRuler::setPixelPerMark(1.0 / scale);
151     double fend = pixelPerMark() * littleMarkDistance();
152     switch ((int) rate) {
153     case 0:
154         m_textSpacing = fend;
155         break;
156     case 1:
157         m_textSpacing = fend * 5;
158         break;
159     case 2:
160     case 3:
161     case 4:
162         m_textSpacing = fend * m_timecode.fps();
163         break;
164     case 5:
165         m_textSpacing = fend * m_timecode.fps() * 5;
166         break;
167     case 6:
168         m_textSpacing = fend * m_timecode.fps() * 10;
169         break;
170     case 7:
171         m_textSpacing = fend * m_timecode.fps() * 30;
172         break;
173     case 8:
174     case 9:
175     case 10:
176         m_textSpacing = fend * m_timecode.fps() * 60;
177         break;
178     case 11:
179     case 12:
180         m_textSpacing = fend * m_timecode.fps() * 300;
181         break;
182     case 13:
183         m_textSpacing = fend * m_timecode.fps() * 600;
184         break;
185     }
186 }
187
188 void CustomRuler::setDuration(int d) {
189     m_duration = d;
190     update();
191 }
192
193 // virtual
194 void CustomRuler::paintEvent(QPaintEvent *e) {
195     QStylePainter p(this);
196     p.setClipRect(e->rect());
197
198     const int projectEnd = (int)(m_duration * m_factor);
199     p.fillRect(QRect(- offset(), e->rect().y(), projectEnd, e->rect().height()), QBrush(QColor(245, 245, 245)));
200
201     const int zoneStart = (int)(m_zoneStart * m_factor);
202     const int zoneEnd = (int)(m_zoneEnd * m_factor);
203
204     p.fillRect(QRect(zoneStart - offset(), e->rect().y() + e->rect().height() / 2, zoneEnd - zoneStart, e->rect().height() / 2), QBrush(QColor(133, 255, 143)));
205
206     const int value  = m_view->cursorPos() * m_factor - offset();
207     const int minval = minimum();
208     const int maxval = maximum() + offset() - endOffset();
209
210     double f, fend,
211     offsetmin = (double)(minval - offset()),
212                 offsetmax = (double)(maxval - offset()),
213                             fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
214     QRect bg = QRect((int)offsetmin, 0, (int)offsetmax, height());
215
216     QPalette palette;
217     //p.fillRect(bg, palette.light());
218     // draw labels
219     p.setPen(palette.dark().color());
220     // draw littlemarklabel
221
222     // draw mediummarklabel
223
224     // draw bigmarklabel
225
226     // draw endlabel
227     /*if (d->showEndL) {
228       if (d->dir == Qt::Horizontal) {
229         p.translate( fontOffset, 0 );
230         p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel );
231       }*/
232
233     // draw the tiny marks
234     //if (showTinyMarks())
235     /*{
236       fend =   pixelPerMark()*tinyMarkDistance();
237       if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
238           p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
239       }
240     }*/
241
242     for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
243         QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / m_factor + 0.5));
244         p.drawText((int)f + 2, LABEL_SIZE, lab);
245     }
246
247     if (showLittleMarks()) {
248         // draw the little marks
249         fend = pixelPerMark() * littleMarkDistance();
250         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
251                 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
252     }
253     if (showMediumMarks()) {
254         // draw medium marks
255         fend = pixelPerMark() * mediumMarkDistance();
256         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
257                 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
258     }
259     if (showBigMarks()) {
260         // draw big marks
261         fend = pixelPerMark() * bigMarkDistance();
262         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
263                 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
264     }
265     /*   if (d->showem) {
266          // draw end marks
267          if (d->dir == Qt::Horizontal) {
268            p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
269            p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
270          }
271          else {
272            p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
273            p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
274          }
275        }*/
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"