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[] =
74 { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000,
77 CustomRuler::CustomRuler(Timecode tc, QWidget *parent)
78 : KRuler(parent), m_timecode(tc)
81 setRulerMetricStyle(KRuler::Pixel);
85 setLittleMarkDistance (FRAME_SIZE);
86 setMediumMarkDistance (FRAME_SIZE * 25);
87 setBigMarkDistance (FRAME_SIZE * 25 * 60);
91 void CustomRuler::mousePressEvent ( QMouseEvent * event )
99 void CustomRuler::mouseMoveEvent ( QMouseEvent * event )
101 int pos = event->x();
106 void CustomRuler::slotNewValue ( int _value )
108 m_cursorPosition = _value / pixelPerMark();
109 emit cursorMoved(m_cursorPosition / FRAME_SIZE);
110 KRuler::slotNewValue(_value);
113 void CustomRuler::setPixelPerMark (double rate)
115 int scale = comboScale[(int) rate];
116 int newPos = m_cursorPosition * (1.0 / scale);
117 KRuler::setPixelPerMark(1.0 / scale);
118 KRuler::slotNewValue( newPos );
122 void CustomRuler::paintEvent(QPaintEvent * /*e*/)
124 // debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical");
126 QStylePainter p(this);
129 int value = this->value(),
132 maxval = maximum() + offset() - endOffset();
134 //ioffsetval = value-offset;
135 // pixelpm = (int)ppm;
136 // left = clip.left(),
137 // right = clip.right();
139 offsetmin=(double)(minval-offset()),
140 offsetmax=(double)(maxval-offset()),
141 fontOffset = (((double)minval)>offsetmin)?(double)minval:offsetmin;
142 QRect bg = QRect(offsetmin, 0, offsetmax, height());
145 //p.fillRect(bg, palette.light());
147 QFont font = p.font();
148 font.setPointSize(LABEL_SIZE);
150 p.setPen(palette.dark().color());
151 // draw littlemarklabel
153 // draw mediummarklabel
159 if (d->dir == Qt::Horizontal) {
160 p.translate( fontOffset, 0 );
161 p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel );
164 // draw the tiny marks
165 //if (showTinyMarks())
167 fend = pixelPerMark()*tinyMarkDistance();
168 if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
169 p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
172 if (showLittleMarks()) {
173 // draw the little marks
174 fend = pixelPerMark()*littleMarkDistance();
175 if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
176 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
178 QString lab = m_timecode.getTimecodeFromFrames((int) ((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
179 p.drawText( (int)f + 2, LABEL_SIZE, lab );
183 if (showMediumMarks()) {
185 fend = pixelPerMark()*mediumMarkDistance();
186 if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
187 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
189 QString lab = m_timecode.getTimecodeFromFrames((int) ((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5) );
190 p.drawText( (int)f + 2, LABEL_SIZE, lab );
194 if (showBigMarks()) {
196 fend = pixelPerMark()*bigMarkDistance();
197 if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
198 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
200 QString lab = m_timecode.getTimecodeFromFrames((int) ((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5) );
201 p.drawText( (int)f + 2, LABEL_SIZE, lab );
203 else if (((int) (f - offsetmin)) % ((int)(fend * 5)) == 0) {
204 QString lab = m_timecode.getTimecodeFromFrames((int) ((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5) );
205 p.drawText( (int)f + 2, LABEL_SIZE, lab );
211 if (d->dir == Qt::Horizontal) {
212 p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
213 p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
216 p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
217 p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
224 pa.setPoints(3, value-6, 9, value+6, 9, value/*+0*/, 16);
225 p.setBrush( QBrush(Qt::yellow) );
231 #include "customruler.moc"