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