comparison widgets/Thumbwheel.cpp @ 187:e7cf6044c2a0

* better icon * support range mappers in thumbwheel * supply range mapper for vertical zoom from spectrogram * fix bug in fftmodel for scaled ffts * make the various widgets all respond to double-click for edit, middle-click for reset, ctrl-left-click for reset
author Chris Cannam
date Fri, 12 Jan 2007 14:49:18 +0000
parents 42118892f428
children dd573e090eed
comparison
equal deleted inserted replaced
186:8dd247c4c5f1 187:e7cf6044c2a0
13 COPYING included with this distribution for more information. 13 COPYING included with this distribution for more information.
14 */ 14 */
15 15
16 #include "Thumbwheel.h" 16 #include "Thumbwheel.h"
17 17
18 #include "base/RangeMapper.h"
19
18 #include <QMouseEvent> 20 #include <QMouseEvent>
19 #include <QPaintEvent> 21 #include <QPaintEvent>
20 #include <QWheelEvent> 22 #include <QWheelEvent>
23 #include <QInputDialog>
21 #include <QPainter> 24 #include <QPainter>
22 25
23 #include <cmath> 26 #include <cmath>
24 #include <iostream> 27 #include <iostream>
25 28
28 QWidget(parent), 31 QWidget(parent),
29 m_min(0), 32 m_min(0),
30 m_max(100), 33 m_max(100),
31 m_default(50), 34 m_default(50),
32 m_value(50), 35 m_value(50),
36 m_mappedValue(50),
37 m_noMappedUpdate(false),
33 m_rotation(0.5), 38 m_rotation(0.5),
34 m_orientation(orientation), 39 m_orientation(orientation),
35 m_speed(1.0), 40 m_speed(1.0),
36 m_tracking(true), 41 m_tracking(true),
37 m_showScale(true), 42 m_showScale(true),
38 m_clicked(false), 43 m_clicked(false),
39 m_atDefault(true), 44 m_atDefault(true),
40 m_clickRotation(m_rotation) 45 m_clickRotation(m_rotation),
46 m_showTooltip(true),
47 m_rangeMapper(0)
41 { 48 {
42 } 49 }
43 50
44 Thumbwheel::~Thumbwheel() 51 Thumbwheel::~Thumbwheel()
45 { 52 {
53 delete m_rangeMapper;
54 }
55
56 void
57 Thumbwheel::setRangeMapper(RangeMapper *mapper)
58 {
59 if (m_rangeMapper == mapper) return;
60
61 if (!m_rangeMapper && mapper) {
62 connect(this, SIGNAL(valueChanged(int)),
63 this, SLOT(updateMappedValue(int)));
64 }
65
66 delete m_rangeMapper;
67 m_rangeMapper = mapper;
68
69 updateMappedValue(getValue());
70 }
71
72 void
73 Thumbwheel::setShowToolTip(bool show)
74 {
75 m_showTooltip = show;
76 m_noMappedUpdate = true;
77 updateMappedValue(getValue());
78 m_noMappedUpdate = false;
46 } 79 }
47 80
48 void 81 void
49 Thumbwheel::setMinimumValue(int min) 82 Thumbwheel::setMinimumValue(int min)
50 { 83 {
96 m_atDefault = true; // setValue unsets this 129 m_atDefault = true; // setValue unsets this
97 emit valueChanged(getValue()); 130 emit valueChanged(getValue());
98 } 131 }
99 } 132 }
100 133
134 void
135 Thumbwheel::setMappedValue(float mappedValue)
136 {
137 if (m_rangeMapper) {
138 int newValue = m_rangeMapper->getPositionForValue(mappedValue);
139 m_mappedValue = mappedValue;
140 m_noMappedUpdate = true;
141 std::cerr << "Thumbwheel::setMappedValue(" << mappedValue << "): new value is " << newValue << std::endl;
142 if (newValue != getValue()) {
143 setValue(newValue);
144 }
145 emit valueChanged(newValue);
146 m_noMappedUpdate = false;
147 } else {
148 setValue(int(mappedValue));
149 emit valueChanged(int(mappedValue));
150 }
151 }
152
101 int 153 int
102 Thumbwheel::getDefaultValue() const 154 Thumbwheel::getDefaultValue() const
103 { 155 {
104 return m_default; 156 return m_default;
105 } 157 }
106 158
107 void 159 void
108 Thumbwheel::setValue(int value) 160 Thumbwheel::setValue(int value)
109 { 161 {
110 // std::cerr << "Thumbwheel::setValue(" << value << ") (from " << m_value 162 std::cerr << "Thumbwheel::setValue(" << value << ") (from " << m_value
111 // << ", rotation " << m_rotation << ")" << std::endl; 163 << ", rotation " << m_rotation << ")" << std::endl;
112 164
113 if (m_value != value) { 165 if (m_value != value) {
114 166
115 m_atDefault = false; 167 m_atDefault = false;
116 168
136 Thumbwheel::getValue() const 188 Thumbwheel::getValue() const
137 { 189 {
138 return m_value; 190 return m_value;
139 } 191 }
140 192
193 float
194 Thumbwheel::getMappedValue() const
195 {
196 if (m_rangeMapper) {
197 std::cerr << "Thumbwheel::getMappedValue(): value = " << getValue() << ", mappedValue = " << m_mappedValue << std::endl;
198 return m_mappedValue;
199 }
200 return getValue();
201 }
202
203 void
204 Thumbwheel::updateMappedValue(int value)
205 {
206 if (!m_noMappedUpdate) {
207 if (m_rangeMapper) {
208 m_mappedValue = m_rangeMapper->getValueForPosition(value);
209 } else {
210 m_mappedValue = value;
211 }
212 }
213
214 if (m_showTooltip) {
215 QString name = objectName();
216 QString unit = "";
217 QString text;
218 if (m_rangeMapper) unit = m_rangeMapper->getUnit();
219 if (name != "") {
220 text = tr("%1: %2%3").arg(name).arg(m_mappedValue).arg(unit);
221 } else {
222 text = tr("%2%3").arg(m_mappedValue).arg(unit);
223 }
224 setToolTip(text);
225 }
226 }
227
141 void 228 void
142 Thumbwheel::setSpeed(float speed) 229 Thumbwheel::setSpeed(float speed)
143 { 230 {
144 m_speed = speed; 231 m_speed = speed;
145 } 232 }
175 } 262 }
176 263
177 void 264 void
178 Thumbwheel::mousePressEvent(QMouseEvent *e) 265 Thumbwheel::mousePressEvent(QMouseEvent *e)
179 { 266 {
180 if (e->button() == Qt::LeftButton) { 267 if (e->button() == Qt::MidButton ||
268 ((e->button() == Qt::LeftButton) &&
269 (e->modifiers() & Qt::ControlModifier))) {
270 resetToDefault();
271 } else if (e->button() == Qt::LeftButton) {
181 m_clicked = true; 272 m_clicked = true;
182 m_clickPos = e->pos(); 273 m_clickPos = e->pos();
183 m_clickRotation = m_rotation; 274 m_clickRotation = m_rotation;
184 } else if (e->button() == Qt::MidButton) { 275 }
185 resetToDefault(); 276 }
186 } 277
187 } 278 void
188 279 Thumbwheel::mouseDoubleClickEvent(QMouseEvent *mouseEvent)
189 void 280 {
190 Thumbwheel::mouseDoubleClickEvent(QMouseEvent *) 281 //!!! needs a common base class with AudioDial
191 { 282
192 resetToDefault(); 283 if (mouseEvent->button() != Qt::LeftButton) {
193 } 284 return;
285 }
286
287 bool ok = false;
288
289 if (m_rangeMapper) {
290
291 float min = m_rangeMapper->getValueForPosition(m_min);
292 float max = m_rangeMapper->getValueForPosition(m_max);
293
294 if (min > max) {
295 float tmp = min;
296 min = max;
297 max = tmp;
298 }
299
300 QString unit = m_rangeMapper->getUnit();
301
302 QString text;
303 if (objectName() != "") {
304 if (unit != "") {
305 text = tr("New value for %1, from %2 to %3 %4:")
306 .arg(objectName()).arg(min).arg(max).arg(unit);
307 } else {
308 text = tr("New value for %1, from %2 to %3:")
309 .arg(objectName()).arg(min).arg(max);
310 }
311 } else {
312 if (unit != "") {
313 text = tr("Enter a new value from %1 to %2 %3:")
314 .arg(min).arg(max).arg(unit);
315 } else {
316 text = tr("Enter a new value from %1 to %2:")
317 .arg(min).arg(max);
318 }
319 }
320
321 float newValue = QInputDialog::getDouble
322 (this,
323 tr("Enter new value"),
324 text,
325 m_mappedValue,
326 min,
327 max,
328 4,
329 &ok);
330
331 if (ok) {
332 setMappedValue(newValue);
333 }
334
335 } else {
336
337 int newValue = QInputDialog::getInteger
338 (this,
339 tr("Enter new value"),
340 tr("Enter a new value from %1 to %2:")
341 .arg(m_min).arg(m_max),
342 getValue(), m_min, m_max, 1, &ok);
343
344 if (ok) {
345 setValue(newValue);
346 }
347 }
348 }
349
194 350
195 void 351 void
196 Thumbwheel::mouseMoveEvent(QMouseEvent *e) 352 Thumbwheel::mouseMoveEvent(QMouseEvent *e)
197 { 353 {
198 if (!m_clicked) return; 354 if (!m_clicked) return;