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 "customruler.h"
30 #define INIT_MIN_VALUE 0
31 #define INIT_MAX_VALUE 100
32 #define INIT_TINY_MARK_DISTANCE 1
33 #define INIT_LITTLE_MARK_DISTANCE 5
34 #define INIT_MIDDLE_MARK_DISTANCE (INIT_LITTLE_MARK_DISTANCE * 2)
35 #define INIT_BIG_MARK_DISTANCE (INIT_LITTLE_MARK_DISTANCE * 10)
36 #define INIT_SHOW_TINY_MARK false
37 #define INIT_SHOW_LITTLE_MARK true
38 #define INIT_SHOW_MEDIUM_MARK true
39 #define INIT_SHOW_BIG_MARK true
40 #define INIT_SHOW_END_MARK true
41 #define INIT_SHOW_POINTER true
42 #define INIT_SHOW_END_LABEL true
44 #define INIT_PIXEL_PER_MARK (double)10.0 /* distance between 2 base marks in pixel */
45 #define INIT_OFFSET (-20)
46 #define INIT_LENGTH_FIX true
47 #define INIT_END_OFFSET 0
49 #define FIX_WIDTH 20 /* widget width in pixel */
50 #define LINE_END (FIX_WIDTH - 3)
51 #define END_MARK_LENGTH (FIX_WIDTH - 6)
52 #define END_MARK_X2 LINE_END
53 #define END_MARK_X1 (END_MARK_X2 - END_MARK_LENGTH)
54 #define BIG_MARK_LENGTH (END_MARK_LENGTH*3/4)
55 #define BIG_MARK_X2 LINE_END
56 #define BIG_MARK_X1 (BIG_MARK_X2 - BIG_MARK_LENGTH)
57 #define MIDDLE_MARK_LENGTH (END_MARK_LENGTH/2)
58 #define MIDDLE_MARK_X2 LINE_END
59 #define MIDDLE_MARK_X1 (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH)
60 #define LITTLE_MARK_LENGTH (MIDDLE_MARK_LENGTH/2)
61 #define LITTLE_MARK_X2 LINE_END
62 #define LITTLE_MARK_X1 (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH)
63 #define BASE_MARK_LENGTH (LITTLE_MARK_LENGTH/2)
64 #define BASE_MARK_X2 LINE_END
65 #define BASE_MARK_X1 (BASE_MARK_X2 - 3) //BASE_MARK_LENGTH
69 #define END_LABEL_Y (END_LABEL_X + LABEL_SIZE - 2)
71 #include "definitions.h"
73 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000,
77 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent)
78 : KRuler(parent), m_timecode(tc), m_view(parent) {
80 setRulerMetricStyle(KRuler::Pixel);
84 setLittleMarkDistance(FRAME_SIZE);
85 setMediumMarkDistance(FRAME_SIZE * 25);
86 setBigMarkDistance(FRAME_SIZE * 25 * 60);
92 void CustomRuler::mousePressEvent(QMouseEvent * event) {
93 int pos = (event->x() + offset()) / pixelPerMark() / FRAME_SIZE;
94 m_moveCursor = RULER_CURSOR;
95 if (event->y() > 10) {
96 if (abs(pos - m_zoneStart) < 4) m_moveCursor = RULER_START;
97 else if (abs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2)) < 4) m_moveCursor = RULER_MIDDLE;
98 else if (abs(pos - m_zoneEnd) < 4) m_moveCursor = RULER_END;
100 if (m_moveCursor == RULER_CURSOR)
101 m_view->setCursorPos(pos);
105 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
106 int pos = (event->x() + offset()) / pixelPerMark() / FRAME_SIZE;
107 if (m_moveCursor == RULER_CURSOR) {
108 m_view->setCursorPos(pos);
110 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
111 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
112 else if (m_moveCursor == RULER_MIDDLE) {
113 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
120 int CustomRuler::inPoint() {
124 int CustomRuler::outPoint() {
128 void CustomRuler::slotMoveRuler(int newPos) {
129 KRuler::slotNewOffset(newPos);
132 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
133 //TODO: optimize (redraw only around cursor positions
137 void CustomRuler::setPixelPerMark(double rate) {
138 int scale = comboScale[(int) rate];
139 KRuler::setPixelPerMark(1.0 / scale);
143 void CustomRuler::paintEvent(QPaintEvent *e) {
144 // debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical");
146 QStylePainter p(this);
147 p.setClipRect(e->rect());
148 p.fillRect(e->rect(), QBrush(QColor(255, 255, 255, 80)));
149 //kDebug()<<"RULER ZONE: "<<m_zoneStart<<", OFF: "<<offset()<<", END: "<<m_zoneEnd<<", FACTOR: "<<pixelPerMark() * FRAME_SIZE;
150 int zoneStart = (m_zoneStart) * pixelPerMark() * FRAME_SIZE;
151 int zoneEnd = (m_zoneEnd) * pixelPerMark() * FRAME_SIZE;
152 p.fillRect(QRect(zoneStart - offset(), e->rect().y() + e->rect().height() / 2, zoneEnd - zoneStart, e->rect().height() / 2), QBrush(QColor(133, 255, 143)));
154 int value = m_view->cursorPos() - offset() + 4;
155 int minval = minimum();
156 int maxval = maximum() + offset() - endOffset();
158 //ioffsetval = value-offset;
159 // pixelpm = (int)ppm;
160 // left = clip.left(),
161 // right = clip.right();
163 offsetmin = (double)(minval - offset()),
164 offsetmax = (double)(maxval - offset()),
165 fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
166 QRect bg = QRect(offsetmin, 0, offsetmax, height());
169 //p.fillRect(bg, palette.light());
171 QFont font = p.font();
172 font.setPointSize(LABEL_SIZE);
174 p.setPen(palette.dark().color());
175 // draw littlemarklabel
177 // draw mediummarklabel
183 if (d->dir == Qt::Horizontal) {
184 p.translate( fontOffset, 0 );
185 p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel );
188 // draw the tiny marks
189 //if (showTinyMarks())
191 fend = pixelPerMark()*tinyMarkDistance();
192 if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
193 p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
196 if (showLittleMarks()) {
197 // draw the little marks
198 fend = pixelPerMark() * littleMarkDistance();
199 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend) {
200 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
202 QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
203 p.drawText((int)f + 2, LABEL_SIZE, lab);
207 if (showMediumMarks()) {
209 fend = pixelPerMark() * mediumMarkDistance();
210 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend) {
211 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
213 QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
214 p.drawText((int)f + 2, LABEL_SIZE, lab);
218 if (showBigMarks()) {
220 fend = pixelPerMark() * bigMarkDistance();
221 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend) {
222 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
224 QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
225 p.drawText((int)f + 2, LABEL_SIZE, lab);
226 } else if (((int)(f - offsetmin)) % ((int)(fend * 5)) == 0) {
227 QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
228 p.drawText((int)f + 2, LABEL_SIZE, lab);
234 if (d->dir == Qt::Horizontal) {
235 p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
236 p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
239 p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
240 p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
249 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
254 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
255 p.fillRect(rec, QColor(255, 255, 255, 150));
259 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
264 if (showPointer() && value > 0) {
266 pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
267 p.setBrush(QBrush(Qt::yellow));
275 #include "customruler.moc"