comparison 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
comparison
equal deleted inserted replaced
1588:0f36e0eca6b0 1589:27ea5d61b402
19 #include <QWidgetAction> 19 #include <QWidgetAction>
20 #include <QImage> 20 #include <QImage>
21 #include <QMouseEvent> 21 #include <QMouseEvent>
22 #include <QStylePainter> 22 #include <QStylePainter>
23 #include <QStyleOptionToolButton> 23 #include <QStyleOptionToolButton>
24 #include <QMenu>
24 25
25 #include "base/Debug.h" 26 #include "base/Debug.h"
27 #include "base/AudioLevel.h"
28 #include "MenuTitle.h"
26 29
27 #include <iostream> 30 #include <iostream>
28 using std::cerr; 31 using std::cerr;
29 using std::endl; 32 using std::endl;
30 33
31 LevelPanToolButton::LevelPanToolButton(QWidget *parent) : 34 LevelPanToolButton::LevelPanToolButton(QWidget *parent) :
32 QToolButton(parent), 35 QToolButton(parent),
33 m_pixels(32), 36 m_pixels(32),
34 m_pixelsBig(32 * 3), 37 m_pixelsBig(32 * 3),
35 m_muted(false), 38 m_muted(false),
36 m_savedLevel(1.f) 39 m_savedLevel(1.f),
40 m_provideContextMenu(true),
41 m_lastContextMenu(nullptr)
37 { 42 {
38 m_lpw = new LevelPanWidget(); 43 m_lpw = new LevelPanWidget();
39 44
40 connect(m_lpw, SIGNAL(levelChanged(float)), this, SIGNAL(levelChanged(float))); 45 connect(m_lpw, SIGNAL(levelChanged(float)), this, SIGNAL(levelChanged(float)));
41 connect(m_lpw, SIGNAL(levelChanged(float)), this, SLOT(selfLevelChanged(float))); 46 connect(m_lpw, SIGNAL(levelChanged(float)), this, SLOT(selfLevelChanged(float)));
54 setMenu(menu); 59 setMenu(menu);
55 setToolTip(tr("Click to adjust level and pan")); 60 setToolTip(tr("Click to adjust level and pan"));
56 61
57 setImageSize(m_pixels); 62 setImageSize(m_pixels);
58 setBigImageSize(m_pixelsBig); 63 setBigImageSize(m_pixelsBig);
64
65 setContextMenuPolicy(Qt::CustomContextMenu);
66 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
67 this, SLOT(contextMenuRequested(const QPoint &)));
59 } 68 }
60 69
61 LevelPanToolButton::~LevelPanToolButton() 70 LevelPanToolButton::~LevelPanToolButton()
62 { 71 {
72 delete m_lastContextMenu;
63 } 73 }
64 74
65 void 75 void
66 LevelPanToolButton::mousePressEvent(QMouseEvent *e) 76 LevelPanToolButton::mousePressEvent(QMouseEvent *e)
67 { 77 {
108 px.fill(Qt::transparent); 118 px.fill(Qt::transparent);
109 setIcon(px); 119 setIcon(px);
110 } 120 }
111 121
112 void 122 void
123 LevelPanToolButton::setProvideContextMenu(bool provide)
124 {
125 m_provideContextMenu = provide;
126 }
127
128 void
113 LevelPanToolButton::setBigImageSize(int pixels) 129 LevelPanToolButton::setBigImageSize(int pixels)
114 { 130 {
115 m_pixelsBig = pixels; 131 m_pixelsBig = pixels;
116 132
117 m_lpw->setFixedWidth(m_pixelsBig); 133 m_lpw->setFixedWidth(m_pixelsBig);
177 m_muted = true; 193 m_muted = true;
178 m_lpw->setLevel(0.f); 194 m_lpw->setLevel(0.f);
179 emit levelChanged(0.f); 195 emit levelChanged(0.f);
180 } 196 }
181 update(); 197 update();
198 }
199
200 void
201 LevelPanToolButton::contextMenuRequested(const QPoint &pos)
202 {
203 if (!m_provideContextMenu) {
204 return;
205 }
206
207 delete m_lastContextMenu;
208 m_lastContextMenu = new QMenu;
209 auto m = m_lastContextMenu;
210
211 QString title;
212
213 if (m_muted) {
214 title = tr("Muted");
215 } else {
216 // Pan is actually stereo balance in most applications...
217 auto level = AudioLevel::multiplier_to_dB(m_lpw->getLevel());
218 auto pan = m_lpw->getPan();
219 if (pan == 0) {
220 title = tr("Level: %1 dB - Balance: Middle").arg(level);
221 } else if (pan > 0) {
222 title = tr("Level: %1 dB - Balance: +%2").arg(level).arg(pan);
223 } else {
224 title = tr("Level: %1 dB - Balance: %2").arg(level).arg(pan);
225 }
226 }
227
228 MenuTitle::addTitle(m, title);
229
230 m->addAction(tr("&Reset to Default"),
231 [=]() {
232 m_lpw->setToDefault();
233 });
234
235 m->popup(mapToGlobal(pos));
182 } 236 }
183 237
184 void 238 void
185 LevelPanToolButton::paintEvent(QPaintEvent *) 239 LevelPanToolButton::paintEvent(QPaintEvent *)
186 { 240 {