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