comparison widgets/Thumbwheel.cpp @ 133:9e6b3e239b9d

* Add zoom thumbwheels to Pane. Implement horizontal thumbwheel, and vertical depending on layer type (supported for waveform and spectrogram, though wrong for log-scale spectrogram at the moment). * Add bare bones of a spectrum layer. * Add window icon * Add shortcut for "insert time instant" on laptops without keypad enter (";") * Delete FFT processing thread when it exits (at least, next time we're asked for something interesting) * Get audio file extensions from the file readers, and thus from libsndfile for the wave file reader -- leads to rather a wide combo box in file dialog though * Better refresh order for spectrogram (redraw centre section first)
author Chris Cannam
date Fri, 04 Aug 2006 17:01:37 +0000
parents 5d3a483856ff
children 793df5f0c6cb
comparison
equal deleted inserted replaced
132:5d3a483856ff 133:9e6b3e239b9d
21 #include <QPainter> 21 #include <QPainter>
22 22
23 #include <cmath> 23 #include <cmath>
24 #include <iostream> 24 #include <iostream>
25 25
26 Thumbwheel::Thumbwheel(int min, int max, int defaultValue, 26 Thumbwheel::Thumbwheel(Qt::Orientation orientation,
27 Qt::Orientation orientation,
28 QWidget *parent) : 27 QWidget *parent) :
29 QWidget(parent), 28 QWidget(parent),
30 m_min(min), 29 m_min(0),
31 m_max(max), 30 m_max(100),
32 m_default(defaultValue), 31 m_default(50),
33 m_value((min + max) / 2), 32 m_value(50),
34 m_orientation(orientation), 33 m_orientation(orientation),
34 m_speed(0.25),
35 m_tracking(true), 35 m_tracking(true),
36 m_showScale(true), 36 m_showScale(true),
37 m_clicked(false), 37 m_clicked(false),
38 m_atDefault(true),
38 m_clickValue(m_value) 39 m_clickValue(m_value)
39 { 40 {
40 if (max <= min) max = min + 1;
41 m_speed = float(max - min) / 300.f;
42 } 41 }
43 42
44 Thumbwheel::~Thumbwheel() 43 Thumbwheel::~Thumbwheel()
45 { 44 {
46 } 45 }
47 46
48 void 47 void
48 Thumbwheel::setMinimumValue(int min)
49 {
50 if (m_min == min) return;
51
52 m_min = min;
53 if (m_max <= m_min) m_max = m_min + 1;
54 if (m_value < m_min) m_value = m_min;
55 if (m_value > m_max) m_value = m_max;
56 }
57
58 int
59 Thumbwheel::getMinimumValue() const
60 {
61 return m_min;
62 }
63
64 void
65 Thumbwheel::setMaximumValue(int max)
66 {
67 if (m_max == max) return;
68
69 m_max = max;
70 if (m_min >= m_max) m_min = m_max - 1;
71 if (m_value < m_min) m_value = m_min;
72 if (m_value > m_max) m_value = m_max;
73 }
74
75 int
76 Thumbwheel::getMaximumValue() const
77 {
78 return m_max;
79 }
80
81 void
82 Thumbwheel::setDefaultValue(int deft)
83 {
84 if (m_default == deft) return;
85
86 m_default = deft;
87 if (m_atDefault) {
88 setValue(m_default);
89 emit valueChanged(getValue());
90 }
91 }
92
93 int
94 Thumbwheel::getDefaultValue() const
95 {
96 return m_default;
97 }
98
99 void
49 Thumbwheel::setValue(int value) 100 Thumbwheel::setValue(int value)
50 { 101 {
102 if (m_value == value) return;
103 m_atDefault = false;
104
51 if (value < m_min) value = m_min; 105 if (value < m_min) value = m_min;
52 if (value > m_max) value = m_max; 106 if (value > m_max) value = m_max;
53 m_value = value; 107 m_value = value;
54 update(); 108 update();
55 } 109 }
56 110
111 void
112 Thumbwheel::resetToDefault()
113 {
114 if (m_default == m_value) return;
115 setValue(m_default);
116 m_atDefault = true;
117 emit valueChanged(getValue());
118 }
119
57 int 120 int
58 Thumbwheel::getValue() const 121 Thumbwheel::getValue() const
59 { 122 {
60 return m_value; 123 return m_value;
61 } 124 }
97 } 160 }
98 161
99 void 162 void
100 Thumbwheel::mousePressEvent(QMouseEvent *e) 163 Thumbwheel::mousePressEvent(QMouseEvent *e)
101 { 164 {
102 m_clicked = true; 165 if (e->button() == Qt::LeftButton) {
103 m_clickPos = e->pos(); 166 m_clicked = true;
104 m_clickValue = m_value; 167 m_clickPos = e->pos();
168 m_clickValue = m_value;
169 } else if (e->button() == Qt::MidButton) {
170 resetToDefault();
171 }
105 } 172 }
106 173
107 void 174 void
108 Thumbwheel::mouseDoubleClickEvent(QMouseEvent *) 175 Thumbwheel::mouseDoubleClickEvent(QMouseEvent *)
109 { 176 {
110 setValue(m_default); 177 resetToDefault();
111 emit valueChanged(getValue());
112 } 178 }
113 179
114 void 180 void
115 Thumbwheel::mouseMoveEvent(QMouseEvent *e) 181 Thumbwheel::mouseMoveEvent(QMouseEvent *e)
116 { 182 {
183 if (!m_clicked) return;
117 int dist = 0; 184 int dist = 0;
118 if (m_orientation == Qt::Horizontal) { 185 if (m_orientation == Qt::Horizontal) {
119 dist = e->x() - m_clickPos.x(); 186 dist = e->x() - m_clickPos.x();
120 } else { 187 } else {
121 dist = e->y() - m_clickPos.y(); 188 dist = e->y() - m_clickPos.y();
130 } 197 }
131 198
132 void 199 void
133 Thumbwheel::mouseReleaseEvent(QMouseEvent *e) 200 Thumbwheel::mouseReleaseEvent(QMouseEvent *e)
134 { 201 {
202 if (!m_clicked) return;
135 bool reallyTracking = m_tracking; 203 bool reallyTracking = m_tracking;
136 m_tracking = true; 204 m_tracking = true;
137 mouseMoveEvent(e); 205 mouseMoveEvent(e);
138 m_tracking = reallyTracking; 206 m_tracking = reallyTracking;
207 m_clicked = false;
139 } 208 }
140 209
141 void 210 void
142 Thumbwheel::wheelEvent(QWheelEvent *e) 211 Thumbwheel::wheelEvent(QWheelEvent *e)
143 { 212 {
154 } 223 }
155 224
156 void 225 void
157 Thumbwheel::paintEvent(QPaintEvent *) 226 Thumbwheel::paintEvent(QPaintEvent *)
158 { 227 {
228 QPainter paint(this);
229 paint.fillRect(rect(), palette().background().color());
230 paint.setRenderHint(QPainter::Antialiasing, false);
231
232 int bw = 3;
233
234 for (int i = 0; i < bw; ++i) {
235 int grey = (i + 1) * (256 / (bw + 1));
236 QColor fc = QColor(grey, grey, grey);
237 paint.setPen(fc);
238 paint.drawRect(i, i, width() - i*2 - 1, height() - i*2 - 1);
239 }
240
241 paint.setClipRect(QRect(bw, bw, width() - bw*2, height() - bw*2));
242
159 float distance = float(m_value - m_min) / float(m_max - m_min); 243 float distance = float(m_value - m_min) / float(m_max - m_min);
160 float rotation = distance * 1.5f * M_PI; 244 float rotation = distance * 1.5f * M_PI;
161 245
162 // std::cerr << "value = " << m_value << ", min = " << m_min << ", max = " << m_max << ", rotation = " << rotation << std::endl; 246 // std::cerr << "value = " << m_value << ", min = " << m_min << ", max = " << m_max << ", rotation = " << rotation << std::endl;
163 247
164 int w = (m_orientation == Qt::Horizontal ? width() : height()); 248 int w = (m_orientation == Qt::Horizontal ? width() : height()) - bw*2;
165 249
166 // total number of notches on the entire wheel 250 // total number of notches on the entire wheel
167 int notches = 25; 251 int notches = 25;
168 252
169 // radius of the wheel including invisible part 253 // radius of the wheel including invisible part
170 int radius = w / 2 + 2; 254 int radius = w / 2 + 2;
171 255
172 QPainter paint(this);
173 paint.fillRect(rect(), palette().background().color());
174 paint.setRenderHint(QPainter::Antialiasing, true); 256 paint.setRenderHint(QPainter::Antialiasing, true);
175 257
176 for (int i = 0; i < notches; ++i) { 258 for (int i = 0; i < notches; ++i) {
177 259
178 float a0 = (2.f * M_PI * i) / notches + rotation; 260 float a0 = (2.f * M_PI * i) / notches + rotation;
188 if (x2 < 0 || x0 > w) continue; 270 if (x2 < 0 || x0 > w) continue;
189 271
190 if (x0 < 0) x0 = 0; 272 if (x0 < 0) x0 = 0;
191 if (x2 > w) x2 = w; 273 if (x2 > w) x2 = w;
192 274
275 x0 += bw;
276 x1 += bw;
277 x2 += bw;
278
193 int grey = lrintf(255 * depth); 279 int grey = lrintf(255 * depth);
194 QColor fc = QColor(grey, grey, grey); 280 QColor fc = QColor(grey, grey, grey);
195 QColor oc = palette().dark().color(); 281 QColor oc = palette().dark().color();
196 282
197 paint.setPen(oc); 283 paint.setPen(oc);
198 paint.setBrush(fc); 284 paint.setBrush(fc);
199 285
200 if (m_orientation == Qt::Horizontal) { 286 if (m_orientation == Qt::Horizontal) {
201 paint.drawRect(QRectF(x1, 0, x2 - x1, height())); 287 paint.drawRect(QRectF(x1, bw, x2 - x1, height() - bw*2));
202 } else { 288 } else {
203 paint.drawRect(QRectF(0, x1, width(), x2 - x1)); 289 paint.drawRect(QRectF(bw, x1, width() - bw*2, x2 - x1));
204 } 290 }
205 291
206 if (m_showScale) { 292 if (m_showScale) {
207 293
208 paint.setBrush(oc); 294 paint.setBrush(oc);
214 } else { 300 } else {
215 prop = 0.f; 301 prop = 0.f;
216 } 302 }
217 303
218 if (m_orientation == Qt::Horizontal) { 304 if (m_orientation == Qt::Horizontal) {
219 paint.drawRect(QRectF(x1, height() - height() * prop, 305 paint.drawRect(QRectF(x1, height() - (height() - bw*2) * prop - bw,
220 x2 - x1, height() * prop)); 306 x2 - x1, height() * prop));
221 } else { 307 } else {
222 paint.drawRect(QRectF(0, x1, width() * prop, x2 - x1)); 308 paint.drawRect(QRectF(bw, x1, (width() - bw*2) * prop, x2 - x1));
223 } 309 }
224 } 310 }
225 311
226 paint.setPen(oc); 312 paint.setPen(oc);
227 paint.setBrush(palette().background().color()); 313 paint.setBrush(palette().background().color());
228 314
229 if (m_orientation == Qt::Horizontal) { 315 if (m_orientation == Qt::Horizontal) {
230 paint.drawRect(QRectF(x0, 0, x1 - x0, height())); 316 paint.drawRect(QRectF(x0, bw, x1 - x0, height() - bw*2));
231 } else { 317 } else {
232 paint.drawRect(QRectF(0, x0, width(), x1 - x0)); 318 paint.drawRect(QRectF(bw, x0, width() - bw*2, x1 - x0));
233 } 319 }
234 } 320 }
235 } 321 }
236 322
237 QSize 323 QSize