]> git.sesse.net Git - kdenlive/blob - src/smallruler.cpp
Optimise monitor ruler (get rid of KRuler), it caused major slowdown (main timeline...
[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 #include "smallruler.h"
27
28
29 SmallRuler::SmallRuler(QWidget *parent)
30         : QWidget(parent), m_scale(1), m_maxval(25) {
31 }
32
33 void SmallRuler::adjustScale(int maximum) {
34     m_maxval = maximum;
35     m_scale = (double) width() / (double) maximum;
36     if (m_scale == 0) m_scale = 1;
37
38     if (m_scale > 0.5) {
39         m_small = 25;
40         m_medium = 5 * 25;
41     } else if (m_scale > 0.09) {
42         m_small = 5 * 25;
43         m_medium = 30 * 25;
44     } else {
45         m_small = 30 * 25;
46         m_medium = 60 * 25;
47     }
48     m_cursorPosition = m_cursorFramePosition * m_scale;
49     update();
50 }
51
52 // virtual
53 void SmallRuler::mousePressEvent(QMouseEvent * event) {
54     const int pos = event->x() / m_scale;
55     emit seekRenderer((int) pos);
56 }
57
58 // virtual
59 void SmallRuler::mouseMoveEvent(QMouseEvent * event) {
60     const int pos = event->x() / m_scale;
61     emit seekRenderer((int) pos);
62 }
63
64 void SmallRuler::slotNewValue(int value) {
65     m_cursorFramePosition = value;
66     int oldPos = m_cursorPosition;
67     m_cursorPosition = value * m_scale;
68     if (oldPos == m_cursorPosition) return;
69     const int offset = 6;
70     const int x = qMin(oldPos, m_cursorPosition);
71     const int w = qAbs(oldPos - m_cursorPosition);
72     update(x - offset, 9, w + 2 * offset, 6);
73 }
74
75 //virtual
76 void SmallRuler::resizeEvent(QResizeEvent *) {
77     adjustScale(m_maxval);
78 }
79
80 // virtual
81 void SmallRuler::paintEvent(QPaintEvent *e) {
82
83     QPainter p(this);
84     QRect r = e->rect();
85     p.setClipRect(r);
86
87     double f, fend;
88     p.setPen(palette().dark().color());
89
90     if (r.top() < 9) {
91         // draw the little marks
92         fend = m_scale * m_small;
93         if (fend > 2) for (f = 0; f < width(); f += fend) {
94                 p.drawLine((int)f, 1, (int)f, 3);
95             }
96
97         // draw medium marks
98         fend = m_scale * m_medium;
99         if (fend > 2) for (f = 0; f < width(); f += fend) {
100                 p.drawLine((int)f, 1, (int)f, 5);
101             }
102     }
103
104     // draw pointer
105     QPolygon pa(3);
106     pa.setPoints(3, m_cursorPosition - 5, 14, m_cursorPosition + 5, 14, m_cursorPosition/*+0*/, 9);
107     p.setBrush(palette().dark().color());
108     p.drawPolygon(pa);
109 }
110
111 #include "smallruler.moc"