comparison widgets/Thumbwheel.cpp @ 1586:bbc3f537564c

Add context menu to Thumbwheel
author Chris Cannam
date Fri, 27 Mar 2020 11:04:56 +0000
parents d39db4673676
children 27ea5d61b402
comparison
equal deleted inserted replaced
1585:073ef72e8e60 1586:bbc3f537564c
22 #include <QPaintEvent> 22 #include <QPaintEvent>
23 #include <QWheelEvent> 23 #include <QWheelEvent>
24 #include <QInputDialog> 24 #include <QInputDialog>
25 #include <QPainter> 25 #include <QPainter>
26 #include <QPainterPath> 26 #include <QPainterPath>
27 #include <QMenu>
28
29 #include "MenuTitle.h"
27 30
28 #include <cmath> 31 #include <cmath>
29 #include <iostream> 32 #include <iostream>
30 33
31 Thumbwheel::Thumbwheel(Qt::Orientation orientation, 34 Thumbwheel::Thumbwheel(Qt::Orientation orientation,
44 m_showScale(true), 47 m_showScale(true),
45 m_clicked(false), 48 m_clicked(false),
46 m_atDefault(true), 49 m_atDefault(true),
47 m_clickRotation(m_rotation), 50 m_clickRotation(m_rotation),
48 m_showTooltip(true), 51 m_showTooltip(true),
52 m_provideContextMenu(true),
53 m_lastContextMenu(nullptr),
49 m_rangeMapper(nullptr) 54 m_rangeMapper(nullptr)
50 { 55 {
56 setContextMenuPolicy(Qt::CustomContextMenu);
57 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
58 this, SLOT(contextMenuRequested(const QPoint &)));
51 } 59 }
52 60
53 Thumbwheel::~Thumbwheel() 61 Thumbwheel::~Thumbwheel()
54 { 62 {
55 delete m_rangeMapper; 63 delete m_rangeMapper;
64 delete m_lastContextMenu;
65 }
66
67 void
68 Thumbwheel::contextMenuRequested(const QPoint &pos)
69 {
70 if (!m_provideContextMenu) {
71 return;
72 }
73
74 if (m_lastContextMenu) {
75 delete m_lastContextMenu;
76 }
77
78 QMenu *m = new QMenu;
79
80 if (m_title == "") {
81 MenuTitle::addTitle(m, tr("Thumbwheel"));
82 } else {
83 MenuTitle::addTitle(m, m_title);
84 }
85
86 m->addAction(tr("&Edit..."),
87 [=]() {
88 edit();
89 });
90 m->addAction(tr("&Reset to Default"),
91 [=]() {
92 resetToDefault();
93 });
94
95 m->popup(mapToGlobal(pos));
96 m_lastContextMenu = m;
56 } 97 }
57 98
58 void 99 void
59 Thumbwheel::setRangeMapper(RangeMapper *mapper) 100 Thumbwheel::setRangeMapper(RangeMapper *mapper)
60 { 101 {
181 m_value = value; 222 m_value = value;
182 } 223 }
183 224
184 m_rotation = float(m_value - m_min) / float(m_max - m_min); 225 m_rotation = float(m_value - m_min) / float(m_max - m_min);
185 m_cache = QImage(); 226 m_cache = QImage();
186 if (isVisible()) update(); 227
228 if (isVisible()) {
229 update();
230 updateTitle();
231 }
187 } 232 }
188 233
189 void 234 void
190 Thumbwheel::resetToDefault() 235 Thumbwheel::resetToDefault()
191 { 236 {
221 } else { 266 } else {
222 m_mappedValue = value; 267 m_mappedValue = value;
223 } 268 }
224 } 269 }
225 270
271 updateTitle();
272 }
273
274 void
275 Thumbwheel::updateTitle()
276 {
277 QString name = objectName();
278 QString unit = "";
279 QString text;
280 double mappedValue = getMappedValue();
281
282 if (m_rangeMapper) unit = m_rangeMapper->getUnit();
283 if (name != "") {
284 text = tr("%1: %2%3").arg(name).arg(mappedValue).arg(unit);
285 } else {
286 text = tr("%2%3").arg(mappedValue).arg(unit);
287 }
288
289 m_title = text;
290
226 if (m_showTooltip) { 291 if (m_showTooltip) {
227 QString name = objectName();
228 QString unit = "";
229 QString text;
230 if (m_rangeMapper) unit = m_rangeMapper->getUnit();
231 if (name != "") {
232 text = tr("%1: %2%3").arg(name).arg(m_mappedValue).arg(unit);
233 } else {
234 text = tr("%2%3").arg(m_mappedValue).arg(unit);
235 }
236 setToolTip(text); 292 setToolTip(text);
293 } else {
294 setToolTip("");
237 } 295 }
238 } 296 }
239 297
240 void 298 void
241 Thumbwheel::scroll(bool up) 299 Thumbwheel::scroll(bool up)
321 379
322 if (mouseEvent->button() != Qt::LeftButton) { 380 if (mouseEvent->button() != Qt::LeftButton) {
323 return; 381 return;
324 } 382 }
325 383
384 edit();
385 }
386
387 void
388 Thumbwheel::edit()
389 {
326 bool ok = false; 390 bool ok = false;
327 391
328 if (m_rangeMapper) { 392 if (m_rangeMapper) {
329 393
330 double min = m_rangeMapper->getValueForPosition(m_min); 394 double min = m_rangeMapper->getValueForPosition(m_min);