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