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>
25 #include <KGlobalSettings>
27 #include "customruler.h"
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
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
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
70 #define END_LABEL_Y (END_LABEL_X + LABEL_SIZE - 2)
72 #include "definitions.h"
74 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000, 12000};
76 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent)
77 : KRuler(parent), m_timecode(tc), m_view(parent), m_duration(0) {
78 setFont(KGlobalSettings::toolBarFont());
80 setRulerMetricStyle(KRuler::Pixel);
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()));
97 void CustomRuler::mousePressEvent(QMouseEvent * event) {
98 if (event->button() == Qt::RightButton) {
99 m_contextMenu->exec(event->globalPos());
102 m_view->activateMonitor();
103 int pos = (int)((event->x() + offset()));
104 m_moveCursor = RULER_CURSOR;
105 if (event->y() > 10) {
106 if (qAbs(pos - m_zoneStart * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_START;
107 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_MIDDLE;
108 else if (qAbs(pos - m_zoneEnd * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_END;
110 if (m_moveCursor == RULER_CURSOR)
111 m_view->setCursorPos((int) pos / pixelPerMark() / FRAME_SIZE);
115 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
116 int pos = (int)((event->x() + offset()) / pixelPerMark() / FRAME_SIZE);
117 if (m_moveCursor == RULER_CURSOR) {
118 m_view->setCursorPos(pos);
120 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
121 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
122 else if (m_moveCursor == RULER_MIDDLE) {
123 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
132 void CustomRuler::wheelEvent(QWheelEvent * e) {
134 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
135 if (e->delta() < 0) delta = 0 - delta;
136 m_view->moveCursorPos(delta);
139 int CustomRuler::inPoint() const {
143 int CustomRuler::outPoint() const {
147 void CustomRuler::slotMoveRuler(int newPos) {
148 KRuler::slotNewOffset(newPos);
151 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
152 update(oldpos - offset() - 6, 2, 17, 16);
153 update(newpos - offset() - 6, 2, 17, 16);
156 void CustomRuler::setPixelPerMark(double rate) {
157 int scale = comboScale[(int) rate];
158 KRuler::setPixelPerMark(1.0 / scale);
159 double fend = pixelPerMark() * littleMarkDistance();
160 switch ((int) rate) {
162 m_textSpacing = fend;
165 m_textSpacing = fend * 5;
170 m_textSpacing = fend * m_timecode.fps();
173 m_textSpacing = fend * m_timecode.fps() * 5;
176 m_textSpacing = fend * m_timecode.fps() * 10;
179 m_textSpacing = fend * m_timecode.fps() * 30;
184 m_textSpacing = fend * m_timecode.fps() * 60;
188 m_textSpacing = fend * m_timecode.fps() * 300;
191 m_textSpacing = fend * m_timecode.fps() * 600;
196 void CustomRuler::setDuration(int d) {
202 void CustomRuler::paintEvent(QPaintEvent *e) {
203 // debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical");
205 QStylePainter p(this);
206 p.setClipRect(e->rect());
208 //p.fillRect(e->rect(), QBrush(QColor(200, 200, 200)));
209 //kDebug()<<"RULER ZONE: "<<m_zoneStart<<", OFF: "<<offset()<<", END: "<<m_zoneEnd<<", FACTOR: "<<pixelPerMark() * FRAME_SIZE;
210 int projectEnd = (int)(m_duration * pixelPerMark() * FRAME_SIZE);
211 p.fillRect(QRect(- offset(), e->rect().y(), projectEnd, e->rect().height()), QBrush(QColor(245, 245, 245)));
214 int zoneStart = (int)(m_zoneStart * pixelPerMark() * FRAME_SIZE);
215 int zoneEnd = (int)(m_zoneEnd * pixelPerMark() * FRAME_SIZE);
217 p.fillRect(QRect(zoneStart - offset(), e->rect().y() + e->rect().height() / 2, zoneEnd - zoneStart, e->rect().height() / 2), QBrush(QColor(133, 255, 143)));
219 int value = m_view->cursorPos() - offset() + 4;
220 int minval = minimum();
221 int maxval = maximum() + offset() - endOffset();
223 //ioffsetval = value-offset;
224 // pixelpm = (int)ppm;
225 // left = clip.left(),
226 // right = clip.right();
228 offsetmin = (double)(minval - offset()),
229 offsetmax = (double)(maxval - offset()),
230 fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
231 QRect bg = QRect((int)offsetmin, 0, (int)offsetmax, height());
234 //p.fillRect(bg, palette.light());
236 p.setPen(palette.dark().color());
237 // draw littlemarklabel
239 // draw mediummarklabel
245 if (d->dir == Qt::Horizontal) {
246 p.translate( fontOffset, 0 );
247 p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel );
250 // draw the tiny marks
251 //if (showTinyMarks())
253 fend = pixelPerMark()*tinyMarkDistance();
254 if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
255 p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
259 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
260 QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
261 p.drawText((int)f + 2, LABEL_SIZE, lab);
264 if (showLittleMarks()) {
265 // draw the little marks
266 fend = pixelPerMark() * littleMarkDistance();
267 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
268 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
270 if (showMediumMarks()) {
272 fend = pixelPerMark() * mediumMarkDistance();
273 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
274 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
276 if (showBigMarks()) {
278 fend = pixelPerMark() * bigMarkDistance();
279 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
280 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
284 if (d->dir == Qt::Horizontal) {
285 p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
286 p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
289 p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
290 p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
299 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
304 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
305 p.fillRect(rec, QColor(255, 255, 255, 150));
309 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
314 if (showPointer() && value > 0) {
316 pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
317 p.setBrush(QBrush(Qt::yellow));
325 #include "customruler.moc"