comparison widgets/Thumbwheel.cpp @ 132:5d3a483856ff

* Add Thumbwheel widget for all our zooming needs * Use QSettings to save/restore window size and position -- precursor to switching our preferences to QSettings as well -- wish I'd noticed it sooner * Only suspend writes (not reads from the underlying cache objects) from the fft caches when repainting the spectrogram -- performance should now be significantly better
author Chris Cannam
date Thu, 03 Aug 2006 15:40:11 +0000
parents
children 9e6b3e239b9d
comparison
equal deleted inserted replaced
131:eaae73b6bd28 132:5d3a483856ff
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "Thumbwheel.h"
17
18 #include <QMouseEvent>
19 #include <QPaintEvent>
20 #include <QWheelEvent>
21 #include <QPainter>
22
23 #include <cmath>
24 #include <iostream>
25
26 Thumbwheel::Thumbwheel(int min, int max, int defaultValue,
27 Qt::Orientation orientation,
28 QWidget *parent) :
29 QWidget(parent),
30 m_min(min),
31 m_max(max),
32 m_default(defaultValue),
33 m_value((min + max) / 2),
34 m_orientation(orientation),
35 m_tracking(true),
36 m_showScale(true),
37 m_clicked(false),
38 m_clickValue(m_value)
39 {
40 if (max <= min) max = min + 1;
41 m_speed = float(max - min) / 300.f;
42 }
43
44 Thumbwheel::~Thumbwheel()
45 {
46 }
47
48 void
49 Thumbwheel::setValue(int value)
50 {
51 if (value < m_min) value = m_min;
52 if (value > m_max) value = m_max;
53 m_value = value;
54 update();
55 }
56
57 int
58 Thumbwheel::getValue() const
59 {
60 return m_value;
61 }
62
63 void
64 Thumbwheel::setSpeed(float speed)
65 {
66 m_speed = speed;
67 }
68
69 float
70 Thumbwheel::getSpeed() const
71 {
72 return m_speed;
73 }
74
75 void
76 Thumbwheel::setTracking(bool tracking)
77 {
78 m_tracking = tracking;
79 }
80
81 bool
82 Thumbwheel::getTracking() const
83 {
84 return m_tracking;
85 }
86
87 void
88 Thumbwheel::setShowScale(bool showScale)
89 {
90 m_showScale = showScale;
91 }
92
93 bool
94 Thumbwheel::getShowScale() const
95 {
96 return m_showScale;
97 }
98
99 void
100 Thumbwheel::mousePressEvent(QMouseEvent *e)
101 {
102 m_clicked = true;
103 m_clickPos = e->pos();
104 m_clickValue = m_value;
105 }
106
107 void
108 Thumbwheel::mouseDoubleClickEvent(QMouseEvent *)
109 {
110 setValue(m_default);
111 emit valueChanged(getValue());
112 }
113
114 void
115 Thumbwheel::mouseMoveEvent(QMouseEvent *e)
116 {
117 int dist = 0;
118 if (m_orientation == Qt::Horizontal) {
119 dist = e->x() - m_clickPos.x();
120 } else {
121 dist = e->y() - m_clickPos.y();
122 }
123 int value = m_clickValue + lrintf(m_speed * dist);
124 if (value < m_min) value = m_min;
125 if (value > m_max) value = m_max;
126 if (value != m_value) {
127 setValue(value);
128 if (m_tracking) emit valueChanged(getValue());
129 }
130 }
131
132 void
133 Thumbwheel::mouseReleaseEvent(QMouseEvent *e)
134 {
135 bool reallyTracking = m_tracking;
136 m_tracking = true;
137 mouseMoveEvent(e);
138 m_tracking = reallyTracking;
139 }
140
141 void
142 Thumbwheel::wheelEvent(QWheelEvent *e)
143 {
144 int step = lrintf(m_speed);
145 if (step == 0) step = 1;
146
147 if (e->delta() > 0) {
148 setValue(m_value + step);
149 } else {
150 setValue(m_value - step);
151 }
152
153 emit valueChanged(getValue());
154 }
155
156 void
157 Thumbwheel::paintEvent(QPaintEvent *)
158 {
159 float distance = float(m_value - m_min) / float(m_max - m_min);
160 float rotation = distance * 1.5f * M_PI;
161
162 // std::cerr << "value = " << m_value << ", min = " << m_min << ", max = " << m_max << ", rotation = " << rotation << std::endl;
163
164 int w = (m_orientation == Qt::Horizontal ? width() : height());
165
166 // total number of notches on the entire wheel
167 int notches = 25;
168
169 // radius of the wheel including invisible part
170 int radius = w / 2 + 2;
171
172 QPainter paint(this);
173 paint.fillRect(rect(), palette().background().color());
174 paint.setRenderHint(QPainter::Antialiasing, true);
175
176 for (int i = 0; i < notches; ++i) {
177
178 float a0 = (2.f * M_PI * i) / notches + rotation;
179 float a1 = a0 + M_PI / (notches * 2);
180 float a2 = (2.f * M_PI * (i + 1)) / notches + rotation;
181
182 float depth = cosf((a0 + a2) / 2);
183 if (depth < 0) continue;
184
185 float x0 = radius * sinf(a0) + w/2;
186 float x1 = radius * sinf(a1) + w/2;
187 float x2 = radius * sinf(a2) + w/2;
188 if (x2 < 0 || x0 > w) continue;
189
190 if (x0 < 0) x0 = 0;
191 if (x2 > w) x2 = w;
192
193 int grey = lrintf(255 * depth);
194 QColor fc = QColor(grey, grey, grey);
195 QColor oc = palette().dark().color();
196
197 paint.setPen(oc);
198 paint.setBrush(fc);
199
200 if (m_orientation == Qt::Horizontal) {
201 paint.drawRect(QRectF(x1, 0, x2 - x1, height()));
202 } else {
203 paint.drawRect(QRectF(0, x1, width(), x2 - x1));
204 }
205
206 if (m_showScale) {
207
208 paint.setBrush(oc);
209
210 float prop;
211 if (i >= notches / 4) {
212 prop = float(notches - (((i - float(notches) / 4.f) * 4.f) / 3.f))
213 / notches;
214 } else {
215 prop = 0.f;
216 }
217
218 if (m_orientation == Qt::Horizontal) {
219 paint.drawRect(QRectF(x1, height() - height() * prop,
220 x2 - x1, height() * prop));
221 } else {
222 paint.drawRect(QRectF(0, x1, width() * prop, x2 - x1));
223 }
224 }
225
226 paint.setPen(oc);
227 paint.setBrush(palette().background().color());
228
229 if (m_orientation == Qt::Horizontal) {
230 paint.drawRect(QRectF(x0, 0, x1 - x0, height()));
231 } else {
232 paint.drawRect(QRectF(0, x0, width(), x1 - x0));
233 }
234 }
235 }
236
237 QSize
238 Thumbwheel::sizeHint() const
239 {
240 if (m_orientation == Qt::Horizontal) {
241 return QSize(80, 12);
242 } else {
243 return QSize(12, 80);
244 }
245 }
246