diff widgets/LevelPanToolButton.cpp @ 1589:27ea5d61b402

Provide context menu in LevelPanToolButton, + a bit of tidying
author Chris Cannam
date Tue, 31 Mar 2020 13:45:06 +0100
parents 1589bc7528b7
children a798a7b5e215
line wrap: on
line diff
--- 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);