1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
20 #include <QMouseEvent>
21 #include <QStylePainter>
26 #include <KGlobalSettings>
28 #include "customruler.h"
32 #define INIT_MIN_VALUE 0
33 #define INIT_MAX_VALUE 100
34 #define INIT_TINY_MARK_DISTANCE 1
35 #define INIT_LITTLE_MARK_DISTANCE 5
36 #define INIT_MIDDLE_MARK_DISTANCE (INIT_LITTLE_MARK_DISTANCE * 2)
37 #define INIT_BIG_MARK_DISTANCE (INIT_LITTLE_MARK_DISTANCE * 10)
38 #define INIT_SHOW_TINY_MARK false
39 #define INIT_SHOW_LITTLE_MARK true
40 #define INIT_SHOW_MEDIUM_MARK true
41 #define INIT_SHOW_BIG_MARK true
42 #define INIT_SHOW_END_MARK true
43 #define INIT_SHOW_POINTER true
44 #define INIT_SHOW_END_LABEL true
46 #define INIT_PIXEL_PER_MARK (double)10.0 /* distance between 2 base marks in pixel */
47 #define INIT_OFFSET (-20)
48 #define INIT_LENGTH_FIX true
49 #define INIT_END_OFFSET 0
51 #define FIX_WIDTH 24 /* widget width in pixel */
52 #define LINE_END (FIX_WIDTH - 3)
53 #define END_MARK_LENGTH (FIX_WIDTH - 8)
54 #define END_MARK_X2 LINE_END
55 #define END_MARK_X1 (END_MARK_X2 - END_MARK_LENGTH)
56 #define BIG_MARK_LENGTH (END_MARK_LENGTH*3/4)
57 #define BIG_MARK_X2 LINE_END
58 #define BIG_MARK_X1 (BIG_MARK_X2 - BIG_MARK_LENGTH)
59 #define MIDDLE_MARK_LENGTH (END_MARK_LENGTH/2)
60 #define MIDDLE_MARK_X2 LINE_END
61 #define MIDDLE_MARK_X1 (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH)
62 #define LITTLE_MARK_LENGTH (MIDDLE_MARK_LENGTH/2)
63 #define LITTLE_MARK_X2 LINE_END
64 #define LITTLE_MARK_X1 (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH)
65 #define BASE_MARK_LENGTH (LITTLE_MARK_LENGTH/2)
66 #define BASE_MARK_X2 LINE_END
67 #define BASE_MARK_X1 (BASE_MARK_X2 - 3) //BASE_MARK_LENGTH
71 #define END_LABEL_Y (END_LABEL_X + LABEL_SIZE - 2)
73 #include "definitions.h"
75 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000, 12000};
77 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent)
78 : KRuler(parent), m_timecode(tc), m_view(parent), m_duration(0) {
79 setFont(KGlobalSettings::toolBarFont());
81 setRulerMetricStyle(KRuler::Pixel);
85 setLittleMarkDistance(FRAME_SIZE);
86 setMediumMarkDistance(FRAME_SIZE * m_timecode.fps());
87 setBigMarkDistance(FRAME_SIZE * m_timecode.fps() * 60);
88 m_zoneStart = 2 * m_timecode.fps();
89 m_zoneEnd = 10 * m_timecode.fps();
90 m_contextMenu = new QMenu(this);
91 QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
92 connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
93 QAction *delGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Delete Guide"));
94 connect(delGuide, SIGNAL(triggered()), m_view, SLOT(slotDeleteGuide()));
95 QAction *editGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Edit Guide"));
96 connect(editGuide, SIGNAL(triggered()), m_view, SLOT(slotEditGuide()));
97 setMouseTracking(true);
101 void CustomRuler::mousePressEvent(QMouseEvent * event) {
102 if (event->button() == Qt::RightButton) {
103 m_contextMenu->exec(event->globalPos());
106 m_view->activateMonitor();
107 int pos = (int)((event->x() + offset()));
108 m_moveCursor = RULER_CURSOR;
109 if (event->y() > 10) {
110 if (qAbs(pos - m_zoneStart * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_START;
111 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_MIDDLE;
112 else if (qAbs(pos - m_zoneEnd * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_END;
114 if (m_moveCursor == RULER_CURSOR)
115 m_view->setCursorPos((int) pos / pixelPerMark() / FRAME_SIZE);
119 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
120 if (event->buttons() == Qt::LeftButton) {
121 int pos = (int)((event->x() + offset()) / pixelPerMark() / FRAME_SIZE);
122 if (pos < 0) pos = 0;
123 if (m_moveCursor == RULER_CURSOR) {
124 m_view->setCursorPos(pos);
126 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
127 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
128 else if (m_moveCursor == RULER_MIDDLE) {
129 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
135 int pos = (int)((event->x() + offset()));
136 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
137 else if (qAbs(pos - m_zoneStart * pixelPerMark() * FRAME_SIZE) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
138 else if (qAbs(pos - m_zoneEnd * pixelPerMark() * FRAME_SIZE) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
139 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * pixelPerMark() * FRAME_SIZE) < 4) setCursor(Qt::SizeHorCursor);
140 else setCursor(Qt::ArrowCursor);
146 void CustomRuler::wheelEvent(QWheelEvent * e) {
148 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
149 if (e->delta() < 0) delta = 0 - delta;
150 m_view->moveCursorPos(delta);
153 int CustomRuler::inPoint() const {
157 int CustomRuler::outPoint() const {
161 void CustomRuler::slotMoveRuler(int newPos) {
162 KRuler::slotNewOffset(newPos);
165 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
166 update(oldpos - offset() - 6, 2, 17, 16);
167 update(newpos - offset() - 6, 2, 17, 16);
170 void CustomRuler::setPixelPerMark(double rate) {
171 int scale = comboScale[(int) rate];
172 KRuler::setPixelPerMark(1.0 / scale);
173 double fend = pixelPerMark() * littleMarkDistance();
174 switch ((int) rate) {
176 m_textSpacing = fend;
179 m_textSpacing = fend * 5;
184 m_textSpacing = fend * m_timecode.fps();
187 m_textSpacing = fend * m_timecode.fps() * 5;
190 m_textSpacing = fend * m_timecode.fps() * 10;
193 m_textSpacing = fend * m_timecode.fps() * 30;
198 m_textSpacing = fend * m_timecode.fps() * 60;
202 m_textSpacing = fend * m_timecode.fps() * 300;
205 m_textSpacing = fend * m_timecode.fps() * 600;
210 void CustomRuler::setDuration(int d) {
216 void CustomRuler::paintEvent(QPaintEvent *e) {
217 // debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical");
219 QStylePainter p(this);
220 p.setClipRect(e->rect());
222 //p.fillRect(e->rect(), QBrush(QColor(200, 200, 200)));
223 //kDebug()<<"RULER ZONE: "<<m_zoneStart<<", OFF: "<<offset()<<", END: "<<m_zoneEnd<<", FACTOR: "<<pixelPerMark() * FRAME_SIZE;
224 int projectEnd = (int)(m_duration * pixelPerMark() * FRAME_SIZE);
225 p.fillRect(QRect(- offset(), e->rect().y(), projectEnd, e->rect().height()), QBrush(QColor(245, 245, 245)));
228 int zoneStart = (int)(m_zoneStart * pixelPerMark() * FRAME_SIZE);
229 int zoneEnd = (int)(m_zoneEnd * pixelPerMark() * FRAME_SIZE);
231 p.fillRect(QRect(zoneStart - offset(), e->rect().y() + e->rect().height() / 2, zoneEnd - zoneStart, e->rect().height() / 2), QBrush(QColor(133, 255, 143)));
233 int value = m_view->cursorPos() - offset();
234 int minval = minimum();
235 int maxval = maximum() + offset() - endOffset();
237 //ioffsetval = value-offset;
238 // pixelpm = (int)ppm;
239 // left = clip.left(),
240 // right = clip.right();
242 offsetmin = (double)(minval - offset()),
243 offsetmax = (double)(maxval - offset()),
244 fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
245 QRect bg = QRect((int)offsetmin, 0, (int)offsetmax, height());
248 //p.fillRect(bg, palette.light());
250 p.setPen(palette.dark().color());
251 // draw littlemarklabel
253 // draw mediummarklabel
259 if (d->dir == Qt::Horizontal) {
260 p.translate( fontOffset, 0 );
261 p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel );
264 // draw the tiny marks
265 //if (showTinyMarks())
267 fend = pixelPerMark()*tinyMarkDistance();
268 if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
269 p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
273 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
274 QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
275 p.drawText((int)f + 2, LABEL_SIZE, lab);
278 if (showLittleMarks()) {
279 // draw the little marks
280 fend = pixelPerMark() * littleMarkDistance();
281 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
282 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
284 if (showMediumMarks()) {
286 fend = pixelPerMark() * mediumMarkDistance();
287 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
288 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
290 if (showBigMarks()) {
292 fend = pixelPerMark() * bigMarkDistance();
293 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
294 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
298 if (d->dir == Qt::Horizontal) {
299 p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
300 p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
303 p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
304 p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
313 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
318 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
319 p.fillRect(rec, QColor(255, 255, 255, 150));
323 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
328 if (showPointer() && value >= 0) {
330 pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
331 p.setBrush(QBrush(Qt::yellow));
339 #include "customruler.moc"