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()));
94 QAction *editGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Edit Guide"));
95 connect(editGuide, SIGNAL(triggered()), m_view, SLOT(slotEditGuide()));
99 void CustomRuler::mousePressEvent(QMouseEvent * event) {
100 if (event->button() == Qt::RightButton) {
101 m_contextMenu->exec(event->globalPos());
104 m_view->activateMonitor();
105 int pos = (int)((event->x() + offset()));
106 m_moveCursor = RULER_CURSOR;
107 if (event->y() > 10) {
108 if (qAbs(pos - m_zoneStart * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_START;
109 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_MIDDLE;
110 else if (qAbs(pos - m_zoneEnd * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_END;
112 if (m_moveCursor == RULER_CURSOR)
113 m_view->setCursorPos((int) pos / pixelPerMark() / FRAME_SIZE);
117 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
118 int pos = (int)((event->x() + offset()) / pixelPerMark() / FRAME_SIZE);
119 if (pos < 0) pos = 0;
120 if (m_moveCursor == RULER_CURSOR) {
121 m_view->setCursorPos(pos);
123 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
124 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
125 else if (m_moveCursor == RULER_MIDDLE) {
126 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
135 void CustomRuler::wheelEvent(QWheelEvent * e) {
137 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
138 if (e->delta() < 0) delta = 0 - delta;
139 m_view->moveCursorPos(delta);
142 int CustomRuler::inPoint() const {
146 int CustomRuler::outPoint() const {
150 void CustomRuler::slotMoveRuler(int newPos) {
151 KRuler::slotNewOffset(newPos);
154 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
155 update(oldpos - offset() - 6, 2, 17, 16);
156 update(newpos - offset() - 6, 2, 17, 16);
159 void CustomRuler::setPixelPerMark(double rate) {
160 int scale = comboScale[(int) rate];
161 KRuler::setPixelPerMark(1.0 / scale);
162 double fend = pixelPerMark() * littleMarkDistance();
163 switch ((int) rate) {
165 m_textSpacing = fend;
168 m_textSpacing = fend * 5;
173 m_textSpacing = fend * m_timecode.fps();
176 m_textSpacing = fend * m_timecode.fps() * 5;
179 m_textSpacing = fend * m_timecode.fps() * 10;
182 m_textSpacing = fend * m_timecode.fps() * 30;
187 m_textSpacing = fend * m_timecode.fps() * 60;
191 m_textSpacing = fend * m_timecode.fps() * 300;
194 m_textSpacing = fend * m_timecode.fps() * 600;
199 void CustomRuler::setDuration(int d) {
205 void CustomRuler::paintEvent(QPaintEvent *e) {
206 // debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical");
208 QStylePainter p(this);
209 p.setClipRect(e->rect());
211 //p.fillRect(e->rect(), QBrush(QColor(200, 200, 200)));
212 //kDebug()<<"RULER ZONE: "<<m_zoneStart<<", OFF: "<<offset()<<", END: "<<m_zoneEnd<<", FACTOR: "<<pixelPerMark() * FRAME_SIZE;
213 int projectEnd = (int)(m_duration * pixelPerMark() * FRAME_SIZE);
214 p.fillRect(QRect(- offset(), e->rect().y(), projectEnd, e->rect().height()), QBrush(QColor(245, 245, 245)));
217 int zoneStart = (int)(m_zoneStart * pixelPerMark() * FRAME_SIZE);
218 int zoneEnd = (int)(m_zoneEnd * pixelPerMark() * FRAME_SIZE);
220 p.fillRect(QRect(zoneStart - offset(), e->rect().y() + e->rect().height() / 2, zoneEnd - zoneStart, e->rect().height() / 2), QBrush(QColor(133, 255, 143)));
222 int value = m_view->cursorPos() - offset();
223 int minval = minimum();
224 int maxval = maximum() + offset() - endOffset();
226 //ioffsetval = value-offset;
227 // pixelpm = (int)ppm;
228 // left = clip.left(),
229 // right = clip.right();
231 offsetmin = (double)(minval - offset()),
232 offsetmax = (double)(maxval - offset()),
233 fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
234 QRect bg = QRect((int)offsetmin, 0, (int)offsetmax, height());
237 //p.fillRect(bg, palette.light());
239 p.setPen(palette.dark().color());
240 // draw littlemarklabel
242 // draw mediummarklabel
248 if (d->dir == Qt::Horizontal) {
249 p.translate( fontOffset, 0 );
250 p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel );
253 // draw the tiny marks
254 //if (showTinyMarks())
256 fend = pixelPerMark()*tinyMarkDistance();
257 if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
258 p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
262 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
263 QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
264 p.drawText((int)f + 2, LABEL_SIZE, lab);
267 if (showLittleMarks()) {
268 // draw the little marks
269 fend = pixelPerMark() * littleMarkDistance();
270 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
271 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
273 if (showMediumMarks()) {
275 fend = pixelPerMark() * mediumMarkDistance();
276 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
277 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
279 if (showBigMarks()) {
281 fend = pixelPerMark() * bigMarkDistance();
282 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
283 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
287 if (d->dir == Qt::Horizontal) {
288 p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
289 p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
292 p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
293 p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
302 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
307 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
308 p.fillRect(rec, QColor(255, 255, 255, 150));
312 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
317 if (showPointer() && value >= 0) {
319 pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
320 p.setBrush(QBrush(Qt::yellow));
328 #include "customruler.moc"