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