changeset 1589:27ea5d61b402

Provide context menu in LevelPanToolButton, + a bit of tidying
author Chris Cannam
date Tue, 31 Mar 2020 13:45:06 +0100
parents 0f36e0eca6b0
children e660a00bc3b9
files widgets/AudioDial.cpp widgets/LevelPanToolButton.cpp widgets/LevelPanToolButton.h widgets/LevelPanWidget.h widgets/Thumbwheel.cpp
diffstat 5 files changed, 78 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/widgets/AudioDial.cpp	Mon Mar 30 11:29:16 2020 +0100
+++ b/widgets/AudioDial.cpp	Tue Mar 31 13:45:06 2020 +0100
@@ -106,12 +106,10 @@
     if (!m_provideContextMenu) {
         return;
     }
-    
-    if (m_lastContextMenu) {
-        delete m_lastContextMenu;
-    }
-    
-    QMenu *m = new QMenu;
+
+    delete m_lastContextMenu;
+    m_lastContextMenu = new QMenu;
+    auto m = m_lastContextMenu;
 
     if (m_title == "") {
         MenuTitle::addTitle(m, tr("Dial"));
@@ -129,7 +127,6 @@
                  });
 
     m->popup(mapToGlobal(pos));
-    m_lastContextMenu = m;
 }
 
 void AudioDial::setRangeMapper(RangeMapper *mapper)
--- a/widgets/LevelPanToolButton.cpp	Mon Mar 30 11:29:16 2020 +0100
+++ b/widgets/LevelPanToolButton.cpp	Tue Mar 31 13:45:06 2020 +0100
@@ -21,8 +21,11 @@
 #include <QMouseEvent>
 #include <QStylePainter>
 #include <QStyleOptionToolButton>
+#include <QMenu>
 
 #include "base/Debug.h"
+#include "base/AudioLevel.h"
+#include "MenuTitle.h"
 
 #include <iostream>
 using std::cerr;
@@ -33,7 +36,9 @@
     m_pixels(32),
     m_pixelsBig(32 * 3),
     m_muted(false),
-    m_savedLevel(1.f)
+    m_savedLevel(1.f),
+    m_provideContextMenu(true),
+    m_lastContextMenu(nullptr)
 {
     m_lpw = new LevelPanWidget();
 
@@ -56,10 +61,15 @@
 
     setImageSize(m_pixels);
     setBigImageSize(m_pixelsBig);
+    
+    setContextMenuPolicy(Qt::CustomContextMenu);
+    connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
+            this, SLOT(contextMenuRequested(const QPoint &)));
 }
 
 LevelPanToolButton::~LevelPanToolButton()
 {
+    delete m_lastContextMenu;
 }
 
 void
@@ -110,6 +120,12 @@
 }
 
 void
+LevelPanToolButton::setProvideContextMenu(bool provide)
+{
+    m_provideContextMenu = provide;
+}
+
+void
 LevelPanToolButton::setBigImageSize(int pixels)
 {
     m_pixelsBig = pixels;
@@ -182,6 +198,44 @@
 }
 
 void
+LevelPanToolButton::contextMenuRequested(const QPoint &pos)
+{
+    if (!m_provideContextMenu) {
+        return;
+    }
+    
+    delete m_lastContextMenu;
+    m_lastContextMenu = new QMenu;
+    auto m = m_lastContextMenu;
+
+    QString title;
+
+    if (m_muted) {
+        title = tr("Muted");
+    } else {
+        // Pan is actually stereo balance in most applications...
+        auto level = AudioLevel::multiplier_to_dB(m_lpw->getLevel());
+        auto pan = m_lpw->getPan();
+        if (pan == 0) {
+            title = tr("Level: %1 dB - Balance: Middle").arg(level);
+        } else if (pan > 0) {
+            title = tr("Level: %1 dB - Balance: +%2").arg(level).arg(pan);
+        } else {
+            title = tr("Level: %1 dB - Balance: %2").arg(level).arg(pan);
+        }
+    }
+    
+    MenuTitle::addTitle(m, title);
+
+    m->addAction(tr("&Reset to Default"),
+                 [=]() {
+                     m_lpw->setToDefault();
+                 });
+
+    m->popup(mapToGlobal(pos));
+}
+
+void
 LevelPanToolButton::paintEvent(QPaintEvent *)
 {
     QStylePainter p(this);
--- a/widgets/LevelPanToolButton.h	Mon Mar 30 11:29:16 2020 +0100
+++ b/widgets/LevelPanToolButton.h	Tue Mar 31 13:45:06 2020 +0100
@@ -18,6 +18,7 @@
 #include <QToolButton>
 
 class LevelPanWidget;
+class QMenu;
 
 class LevelPanToolButton : public QToolButton
 {
@@ -37,6 +38,9 @@
     bool includesMute() const;
 
     void setImageSize(int pixels);
+
+    /// Specify whether a right-click context menu is provided
+    void setProvideContextMenu(bool);
                         
     void setBigImageSize(int pixels);
                         
@@ -55,6 +59,9 @@
 
     void setEnabled(bool enabled);
     
+protected slots:
+    void contextMenuRequested(const QPoint &);
+
 signals:
     void levelChanged(float);
     void panChanged(float);
@@ -78,6 +85,8 @@
     int m_pixelsBig;
     bool m_muted;
     float m_savedLevel;
+    bool m_provideContextMenu;
+    QMenu *m_lastContextMenu;
 };
 
 #endif
--- a/widgets/LevelPanWidget.h	Mon Mar 30 11:29:16 2020 +0100
+++ b/widgets/LevelPanWidget.h	Tue Mar 31 13:45:06 2020 +0100
@@ -71,7 +71,7 @@
     
     // public so it can be called from LevelPanToolButton (ew)
     void wheelEvent(QWheelEvent *ev) override;
-    
+
 signals:
     void levelChanged(float); // range [0,1]
     void panChanged(float); // range [-1,1]
--- a/widgets/Thumbwheel.cpp	Mon Mar 30 11:29:16 2020 +0100
+++ b/widgets/Thumbwheel.cpp	Tue Mar 31 13:45:06 2020 +0100
@@ -71,11 +71,9 @@
         return;
     }
     
-    if (m_lastContextMenu) {
-        delete m_lastContextMenu;
-    }
-    
-    QMenu *m = new QMenu;
+    delete m_lastContextMenu;
+    m_lastContextMenu = new QMenu;
+    auto m = m_lastContextMenu;
 
     if (m_title == "") {
         MenuTitle::addTitle(m, tr("Thumbwheel"));
@@ -93,7 +91,6 @@
                  });
 
     m->popup(mapToGlobal(pos));
-    m_lastContextMenu = m;
 }
 
 void
@@ -122,6 +119,12 @@
 }
 
 void
+Thumbwheel::setProvideContextMenu(bool provide)
+{
+    m_provideContextMenu = provide;
+}
+
+void
 Thumbwheel::setMinimumValue(int min)
 {
     if (m_min == min) return;