comparison widgets/AudioDial.cpp @ 1584:e5464dc2f6cf

Add optional context menu to AudioDial
author Chris Cannam
date Thu, 26 Mar 2020 12:10:55 +0000
parents bdf284b29722
children 27ea5d61b402
comparison
equal deleted inserted replaced
1583:2e720fdcab0a 1584:e5464dc2f6cf
47 #include <QPixmap> 47 #include <QPixmap>
48 #include <QColormap> 48 #include <QColormap>
49 #include <QMouseEvent> 49 #include <QMouseEvent>
50 #include <QPaintEvent> 50 #include <QPaintEvent>
51 #include <QInputDialog> 51 #include <QInputDialog>
52 #include <QMenu>
52 53
53 #include "base/Profiler.h" 54 #include "base/Profiler.h"
55
56 #include "MenuTitle.h"
54 57
55 58
56 59
57 60
58 61
65 68
66 #define AUDIO_DIAL_MIN (0.25 * M_PI) 69 #define AUDIO_DIAL_MIN (0.25 * M_PI)
67 #define AUDIO_DIAL_MAX (1.75 * M_PI) 70 #define AUDIO_DIAL_MAX (1.75 * M_PI)
68 #define AUDIO_DIAL_RANGE (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) 71 #define AUDIO_DIAL_RANGE (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN)
69 72
70
71 //static int dialsExtant = 0;
72 73
73 74
74 // Constructor. 75 // Constructor.
75 AudioDial::AudioDial(QWidget *parent) : 76 AudioDial::AudioDial(QWidget *parent) :
76 QDial(parent), 77 QDial(parent),
79 m_defaultValue(0), 80 m_defaultValue(0),
80 m_defaultMappedValue(0), 81 m_defaultMappedValue(0),
81 m_mappedValue(0), 82 m_mappedValue(0),
82 m_noMappedUpdate(false), 83 m_noMappedUpdate(false),
83 m_showTooltip(true), 84 m_showTooltip(true),
85 m_provideContextMenu(true),
86 m_lastContextMenu(nullptr),
84 m_rangeMapper(nullptr) 87 m_rangeMapper(nullptr)
85 { 88 {
86 m_mouseDial = false; 89 m_mouseDial = false;
87 m_mousePressed = false; 90 m_mousePressed = false;
88 // ++dialsExtant; 91 setContextMenuPolicy(Qt::CustomContextMenu);
92 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
93 this, SLOT(contextMenuRequested(const QPoint &)));
89 } 94 }
90 95
91 96
92 // Destructor. 97 // Destructor.
93 AudioDial::~AudioDial (void) 98 AudioDial::~AudioDial (void)
94 { 99 {
95 delete m_rangeMapper; 100 delete m_rangeMapper;
96 // --dialsExtant; 101 delete m_lastContextMenu;
97 } 102 }
98 103
104 void AudioDial::contextMenuRequested(const QPoint &pos)
105 {
106 if (!m_provideContextMenu) {
107 return;
108 }
109
110 if (m_lastContextMenu) {
111 delete m_lastContextMenu;
112 }
113
114 QMenu *m = new QMenu;
115
116 if (m_title == "") {
117 MenuTitle::addTitle(m, tr("Dial"));
118 } else {
119 MenuTitle::addTitle(m, m_title);
120 }
121
122 m->addAction(tr("&Edit..."),
123 [=]() {
124 edit();
125 });
126 m->addAction(tr("&Reset to Default"),
127 [=]() {
128 setToDefault();
129 });
130
131 m->popup(mapToGlobal(pos));
132 m_lastContextMenu = m;
133 }
99 134
100 void AudioDial::setRangeMapper(RangeMapper *mapper) 135 void AudioDial::setRangeMapper(RangeMapper *mapper)
101 { 136 {
102 // cerr << "AudioDial[" << this << "][\"" << objectName() << "\"::setRangeMapper(" << mapper << ") [current is " << m_rangeMapper << "] (have " << dialsExtant << " dials extant)" << endl;
103
104 if (m_rangeMapper == mapper) return; 137 if (m_rangeMapper == mapper) return;
105 138
106 if (!m_rangeMapper && mapper) { 139 if (!m_rangeMapper && mapper) {
107 connect(this, SIGNAL(valueChanged(int)), 140 connect(this, SIGNAL(valueChanged(int)),
108 this, SLOT(updateMappedValue(int))); 141 this, SLOT(updateMappedValue(int)));
420 updateMappedValue(value()); 453 updateMappedValue(value());
421 m_noMappedUpdate = false; 454 m_noMappedUpdate = false;
422 } 455 }
423 456
424 457
458 void AudioDial::setProvideContextMenu(bool provide)
459 {
460 m_provideContextMenu = provide;
461 }
462
463
425 double AudioDial::mappedValue() const 464 double AudioDial::mappedValue() const
426 { 465 {
427 if (m_rangeMapper) { 466 if (m_rangeMapper) {
428 // SVDEBUG << "AudioDial::mappedValue(): value = " << value() << ", mappedValue = " << m_mappedValue << endl; 467 // SVDEBUG << "AudioDial::mappedValue(): value = " << value() << ", mappedValue = " << m_mappedValue << endl;
429 return m_mappedValue; 468 return m_mappedValue;
440 } else { 479 } else {
441 m_mappedValue = value; 480 m_mappedValue = value;
442 } 481 }
443 } 482 }
444 483
484 QString name = objectName();
485 QString label;
486 if (m_rangeMapper) {
487 label = m_rangeMapper->getLabel(value);
488 }
489 QString text;
490 if (label != "") {
491 if (name != "") {
492 text = tr("%1: %2").arg(name).arg(label);
493 } else {
494 text = label;
495 }
496 } else {
497 QString unit = "";
498 if (m_rangeMapper) {
499 unit = m_rangeMapper->getUnit();
500 }
501 if (name != "") {
502 text = tr("%1: %2%3").arg(name).arg(m_mappedValue).arg(unit);
503 } else {
504 text = tr("%2%3").arg(m_mappedValue).arg(unit);
505 }
506 }
507
508 m_title = text;
509
445 if (m_showTooltip) { 510 if (m_showTooltip) {
446 QString name = objectName();
447 QString label;
448 if (m_rangeMapper) {
449 label = m_rangeMapper->getLabel(value);
450 }
451 QString text;
452 if (label != "") {
453 if (name != "") {
454 text = tr("%1: %2").arg(name).arg(label);
455 } else {
456 text = label;
457 }
458 } else {
459 QString unit = "";
460 if (m_rangeMapper) {
461 unit = m_rangeMapper->getUnit();
462 }
463 if (name != "") {
464 text = tr("%1: %2%3").arg(name).arg(m_mappedValue).arg(unit);
465 } else {
466 text = tr("%2%3").arg(m_mappedValue).arg(unit);
467 }
468 }
469 setToolTip(text); 511 setToolTip(text);
512 } else {
513 setToolTip("");
470 } 514 }
471 } 515 }
472 516
473 void 517 void
474 AudioDial::setToDefault() 518 AudioDial::setToDefault()
507 QDial::mouseDoubleClickEvent(mouseEvent); 551 QDial::mouseDoubleClickEvent(mouseEvent);
508 } else if (mouseEvent->button() != Qt::LeftButton) { 552 } else if (mouseEvent->button() != Qt::LeftButton) {
509 return; 553 return;
510 } 554 }
511 555
556 edit();
557 }
558
559 void AudioDial::edit()
560 {
512 bool ok = false; 561 bool ok = false;
513 562
514 if (m_rangeMapper) { 563 if (m_rangeMapper) {
515 564
516 double min = m_rangeMapper->getValueForPosition(minimum()); 565 double min = m_rangeMapper->getValueForPosition(minimum());