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