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