]> git.sesse.net Git - kdenlive/blob - src/smallruler.cpp
Reformat initializer lists in all constructors.
[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 "smallruler.h"
22
23 #include <KDebug>
24
25 #include <QMouseEvent>
26 #include <QStylePainter>
27
28 SmallRuler::SmallRuler(QWidget *parent) :
29         QWidget(parent),
30         m_scale(1),
31         m_maxval(25)
32 {
33     m_zoneStart = 10;
34     m_zoneEnd = 60;
35     m_zoneColor = QColor(133, 255, 143);
36 }
37
38 void SmallRuler::adjustScale(int maximum)
39 {
40     m_maxval = maximum;
41     m_scale = (double) width() / (double) maximum;
42     if (m_scale == 0) m_scale = 1;
43
44     if (m_scale > 0.5) {
45         m_small = 25;
46         m_medium = 5 * 25;
47     } else if (m_scale > 0.09) {
48         m_small = 5 * 25;
49         m_medium = 30 * 25;
50     } else {
51         m_small = 30 * 25;
52         m_medium = 60 * 25;
53     }
54     m_cursorPosition = m_cursorFramePosition * m_scale;
55     update();
56 }
57
58 void SmallRuler::setZone(int start, int end)
59 {
60     if (start != -1) {
61         if (end != -1 && start >= end) {
62             m_zoneEnd = qMin(m_maxval, end + (start - m_zoneStart));
63             m_zoneStart = start;
64         } else if (end == -1 && start >= m_zoneEnd) {
65             m_zoneEnd = qMin(m_maxval, m_zoneEnd + (start - m_zoneStart));
66             m_zoneStart = start;
67         } else m_zoneStart = start;
68     }
69     if (end != -1) {
70         if (start != -1 && end <= start) {
71             m_zoneStart = qMax(0, start - (m_zoneEnd - end));
72             m_zoneEnd = end;
73         } else if (start == -1 && end <= m_zoneStart) {
74             m_zoneStart = qMax(0, m_zoneStart - (m_zoneEnd - end));
75             m_zoneEnd = end;
76         } else m_zoneEnd = end;
77     }
78     update();
79 }
80
81 QPoint SmallRuler::zone()
82 {
83     return QPoint(m_zoneStart, m_zoneEnd);
84 }
85
86 // virtual
87 void SmallRuler::mousePressEvent(QMouseEvent * event)
88 {
89     const int pos = event->x() / m_scale;
90     if (event->button() == Qt::RightButton) {
91         // Right button clicked, move selection zone
92         if (qAbs(pos - m_zoneStart) < qAbs(pos - m_zoneEnd)) m_zoneStart = pos;
93         else m_zoneEnd = pos;
94         emit zoneChanged(QPoint(m_zoneStart, m_zoneEnd));
95         update();
96
97     } else emit seekRenderer((int) pos);
98 }
99
100 // virtual
101 void SmallRuler::mouseMoveEvent(QMouseEvent * event)
102 {
103     const int pos = event->x() / m_scale;
104     if (event->buttons() & Qt::LeftButton) emit seekRenderer((int) pos);
105 }
106
107 void SmallRuler::slotNewValue(int value)
108 {
109     m_cursorFramePosition = value;
110     int oldPos = m_cursorPosition;
111     m_cursorPosition = value * m_scale;
112     if (oldPos == m_cursorPosition) return;
113     const int offset = 6;
114     const int x = qMin(oldPos, m_cursorPosition);
115     const int w = qAbs(oldPos - m_cursorPosition);
116     update(x - offset, 9, w + 2 * offset, 6);
117 }
118
119 //virtual
120 void SmallRuler::resizeEvent(QResizeEvent *)
121 {
122     adjustScale(m_maxval);
123 }
124
125 // virtual
126 void SmallRuler::paintEvent(QPaintEvent *e)
127 {
128
129     QPainter p(this);
130     QRect r = e->rect();
131     p.setClipRect(r);
132
133     double f, fend;
134     p.setPen(palette().dark().color());
135
136     const int zoneStart = (int)(m_zoneStart * m_scale);
137     const int zoneEnd = (int)(m_zoneEnd * m_scale);
138
139     p.fillRect(zoneStart, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
140
141     if (r.top() < 9) {
142         // draw the little marks
143         fend = m_scale * m_small;
144         if (fend > 2) for (f = 0; f < width(); f += fend) {
145                 p.drawLine((int)f, 1, (int)f, 3);
146             }
147
148         // draw medium marks
149         fend = m_scale * m_medium;
150         if (fend > 2) for (f = 0; f < width(); f += fend) {
151                 p.drawLine((int)f, 1, (int)f, 5);
152             }
153     }
154
155     // draw pointer
156     QPolygon pa(3);
157     pa.setPoints(3, m_cursorPosition - 5, 14, m_cursorPosition + 5, 14, m_cursorPosition/*+0*/, 9);
158     p.setBrush(palette().dark().color());
159     p.drawPolygon(pa);
160 }
161
162 #include "smallruler.moc"