]> git.sesse.net Git - kdenlive/blob - src/smallruler.cpp
b9e420225a4c07adc661284d9ef3405b8eb0ab42
[kdenlive] / src / smallruler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include <QMouseEvent>
22 #include <QStylePainter>
23
24 #include <KDebug>
25
26 #define LITTLE_MARK_X2 8
27 #define LITTLE_MARK_X1 5
28 #define MIDDLE_MARK_X1 3
29 #define MIDDLE_MARK_X2 8
30
31 #define LABEL_SIZE 8
32
33 #include "smallruler.h"
34
35
36 SmallRuler::SmallRuler(QWidget *parent)
37     : KRuler(parent)
38 {
39   setShowPointer(true);
40   setShowBigMarks(false);
41   setShowTinyMarks(false);
42   slotNewOffset(0);
43   setRulerMetricStyle(KRuler::Custom);
44   setLengthFixed(true);
45 }
46
47 void SmallRuler::setPixelPerMark ( double rate )
48 {
49   kDebug()<<" RULER SET RATE: "<<rate;
50   if (rate > 0.5) {
51     setLittleMarkDistance(25);
52     setMediumMarkDistance(5 * 25);
53   }
54   else if (rate > 0.09) {
55     setLittleMarkDistance(5 * 25);
56     setMediumMarkDistance(30 * 25);
57   }
58   else {
59     setLittleMarkDistance(30 * 25);
60     setMediumMarkDistance(60 * 25);
61   }
62   
63   KRuler::setPixelPerMark( rate );
64 }
65
66 // virtual 
67 void SmallRuler::mousePressEvent ( QMouseEvent * event )
68 {
69   int pos = event->x();
70   //slotNewValue( pos );
71   emit seekRenderer(pos);
72   kDebug()<<pos;
73 }
74
75 // virtual
76 void SmallRuler::mouseMoveEvent ( QMouseEvent * event )
77 {
78   int pos = event->x();
79   //slotNewValue( pos );
80   emit seekRenderer(pos);
81   kDebug()<<pos;
82 }
83
84 void SmallRuler::slotNewValue ( int _value )
85 {
86   m_cursorPosition = _value / pixelPerMark();
87   KRuler::slotNewValue(_value);
88 }
89
90 // virtual
91 void SmallRuler::paintEvent(QPaintEvent *e)
92  {
93    //  debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical");
94  
95    QStylePainter p(this);
96    p.fillRect(e->rect(), QBrush(QColor(Qt::white)));
97  
98   
99    int value  = this->value(),
100      minval = minimum(),
101      maxval;
102      maxval = maximum()
103      + offset() - endOffset();
104
105      //ioffsetval = value-offset;
106      //    pixelpm = (int)ppm;
107    //    left  = clip.left(),
108    //    right = clip.right();
109    double f, fend,
110      offsetmin=(double)(minval-offset()),
111      offsetmax=(double)(maxval-offset()),
112      fontOffset = (((double)minval)>offsetmin)?(double)minval:offsetmin;
113  
114    // draw labels
115    QFont font = p.font();
116    font.setPointSize(LABEL_SIZE);
117    p.setFont( font );
118
119    if (showLittleMarks()) {
120      // draw the little marks
121      fend = pixelPerMark()*littleMarkDistance();
122      if (fend > 2) for ( f=offsetmin; f<offsetmax; f+=fend ) {
123          p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
124      }
125    }
126    if (showMediumMarks()) {
127      // draw medium marks
128      fend = pixelPerMark()*mediumMarkDistance();
129      if (fend > 2) for ( f=offsetmin; f<offsetmax; f+=fend ) {
130          p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
131      }
132    }
133
134 /*   if (d->showem) {
135      // draw end marks
136      if (d->dir == Qt::Horizontal) {
137        p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
138        p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
139      }
140      else {
141        p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
142        p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
143      }
144    }*/
145  
146    // draw pointer
147    if (showPointer()) {
148      QPolygon pa(3);
149        pa.setPoints(3, value-6, 0, value+6, 0, value/*+0*/, 8);
150      p.setBrush( QBrush(Qt::yellow) );
151      p.drawPolygon( pa );
152    }
153  
154  }
155
156 #include "smallruler.moc"