changeset 1584:e5464dc2f6cf

Add optional context menu to AudioDial
author Chris Cannam
date Thu, 26 Mar 2020 12:10:55 +0000
parents 2e720fdcab0a
children 073ef72e8e60
files widgets/AudioDial.cpp widgets/AudioDial.h
diffstat 2 files changed, 88 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/widgets/AudioDial.cpp	Thu Mar 26 11:16:47 2020 +0000
+++ b/widgets/AudioDial.cpp	Thu Mar 26 12:10:55 2020 +0000
@@ -49,9 +49,12 @@
 #include <QMouseEvent>
 #include <QPaintEvent>
 #include <QInputDialog>
+#include <QMenu>
 
 #include "base/Profiler.h"
 
+#include "MenuTitle.h"
+
 
 
 
@@ -68,8 +71,6 @@
 #define AUDIO_DIAL_RANGE (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN)
 
 
-//static int dialsExtant = 0;
-
 
 // Constructor.
 AudioDial::AudioDial(QWidget *parent) :
@@ -81,11 +82,15 @@
     m_mappedValue(0),
     m_noMappedUpdate(false),
     m_showTooltip(true),
+    m_provideContextMenu(true),
+    m_lastContextMenu(nullptr),
     m_rangeMapper(nullptr)
 {
     m_mouseDial = false;
     m_mousePressed = false;
-//    ++dialsExtant;
+    setContextMenuPolicy(Qt::CustomContextMenu);
+    connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
+            this, SLOT(contextMenuRequested(const QPoint &)));
 }
 
 
@@ -93,14 +98,42 @@
 AudioDial::~AudioDial (void)
 {
     delete m_rangeMapper;
-//    --dialsExtant;
+    delete m_lastContextMenu;
 }
 
+void AudioDial::contextMenuRequested(const QPoint &pos)
+{
+    if (!m_provideContextMenu) {
+        return;
+    }
+    
+    if (m_lastContextMenu) {
+        delete m_lastContextMenu;
+    }
+    
+    QMenu *m = new QMenu;
+
+    if (m_title == "") {
+        MenuTitle::addTitle(m, tr("Dial"));
+    } else {        
+        MenuTitle::addTitle(m, m_title);
+    }
+
+    m->addAction(tr("&Edit..."),
+                 [=]() {
+                     edit();
+                 });
+    m->addAction(tr("&Reset to Default"),
+                 [=]() {
+                     setToDefault();
+                 });
+
+    m->popup(mapToGlobal(pos));
+    m_lastContextMenu = m;
+}
 
 void AudioDial::setRangeMapper(RangeMapper *mapper)
 {
-//    cerr << "AudioDial[" << this << "][\"" << objectName() << "\"::setRangeMapper(" << mapper << ") [current is " << m_rangeMapper << "] (have " << dialsExtant << " dials extant)" << endl;
-
     if (m_rangeMapper == mapper) return;
 
     if (!m_rangeMapper && mapper) {
@@ -422,6 +455,12 @@
 }
 
 
+void AudioDial::setProvideContextMenu(bool provide)
+{
+    m_provideContextMenu = provide;
+}
+
+
 double AudioDial::mappedValue() const
 {
     if (m_rangeMapper) {
@@ -442,31 +481,36 @@
         }
     }
 
+    QString name = objectName();
+    QString label;
+    if (m_rangeMapper) {
+        label = m_rangeMapper->getLabel(value);
+    }
+    QString text;
+    if (label != "") {
+        if (name != "") {
+            text = tr("%1: %2").arg(name).arg(label);
+        } else {
+            text = label;
+        }
+    } else {
+        QString unit = "";
+        if (m_rangeMapper) {
+            unit = m_rangeMapper->getUnit();
+        }
+        if (name != "") {
+            text = tr("%1: %2%3").arg(name).arg(m_mappedValue).arg(unit);
+        } else {
+            text = tr("%2%3").arg(m_mappedValue).arg(unit);
+        }
+    }
+
+    m_title = text;
+
     if (m_showTooltip) {
-        QString name = objectName();
-        QString label;
-        if (m_rangeMapper) {
-            label = m_rangeMapper->getLabel(value);
-        }
-        QString text;
-        if (label != "") {
-            if (name != "") {
-                text = tr("%1: %2").arg(name).arg(label);
-            } else {
-                text = label;
-            }
-        } else {
-            QString unit = "";
-            if (m_rangeMapper) {
-                unit = m_rangeMapper->getUnit();
-            }
-            if (name != "") {
-                text = tr("%1: %2%3").arg(name).arg(m_mappedValue).arg(unit);
-            } else {
-                text = tr("%2%3").arg(m_mappedValue).arg(unit);
-            }
-        }
         setToolTip(text);
+    } else {
+        setToolTip("");
     }
 }
 
@@ -509,6 +553,11 @@
         return;
     }
 
+    edit();
+}
+
+void AudioDial::edit()
+{
     bool ok = false;
 
     if (m_rangeMapper) {
--- a/widgets/AudioDial.h	Thu Mar 26 11:16:47 2020 +0000
+++ b/widgets/AudioDial.h	Thu Mar 26 12:10:55 2020 +0000
@@ -42,6 +42,7 @@
 #include <map>
 
 class RangeMapper;
+class QMenu;
 
 /**
  * AudioDial is a nicer-looking QDial that by default reacts to mouse
@@ -78,6 +79,7 @@
     int defaultValue() const { return m_defaultValue; }
 
     void setShowToolTip(bool show);
+    void setProvideContextMenu(bool provide);
 
 signals:
     void mouseEntered();
@@ -113,6 +115,8 @@
 
     void setToDefault();
 
+    void edit();
+
 protected:
     void drawTick(QPainter &paint, double angle, int size, bool internal);
     void paintEvent(QPaintEvent *) override;
@@ -127,6 +131,7 @@
 
 protected slots:
     void updateMappedValue(int value);
+    void contextMenuRequested(const QPoint &);
 
 private:
     QColor m_knobColor;
@@ -143,6 +148,11 @@
     QPoint m_posMouse;
 
     bool m_showTooltip;
+    bool m_provideContextMenu;
+
+    QString m_title;
+
+    QMenu *m_lastContextMenu;
 
     RangeMapper *m_rangeMapper;
 };