annotate widgets/LevelPanToolButton.cpp @ 1588:0f36e0eca6b0

Add right-button context menu to panner widget
author Chris Cannam
date Mon, 30 Mar 2020 11:29:16 +0100
parents 1589bc7528b7
children 27ea5d61b402
rev   line source
Chris@929 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@929 2
Chris@929 3 /*
Chris@929 4 Sonic Visualiser
Chris@929 5 An audio file viewer and annotation editor.
Chris@929 6 Centre for Digital Music, Queen Mary, University of London.
Chris@929 7
Chris@929 8 This program is free software; you can redistribute it and/or
Chris@929 9 modify it under the terms of the GNU General Public License as
Chris@929 10 published by the Free Software Foundation; either version 2 of the
Chris@929 11 License, or (at your option) any later version. See the file
Chris@929 12 COPYING included with this distribution for more information.
Chris@929 13 */
Chris@929 14
Chris@929 15 #include "LevelPanToolButton.h"
Chris@929 16 #include "LevelPanWidget.h"
Chris@929 17
Chris@929 18 #include <QMenu>
Chris@929 19 #include <QWidgetAction>
Chris@929 20 #include <QImage>
Chris@1300 21 #include <QMouseEvent>
Chris@936 22 #include <QStylePainter>
Chris@936 23 #include <QStyleOptionToolButton>
Chris@929 24
Chris@1300 25 #include "base/Debug.h"
Chris@1300 26
Chris@929 27 #include <iostream>
Chris@929 28 using std::cerr;
Chris@929 29 using std::endl;
Chris@929 30
Chris@929 31 LevelPanToolButton::LevelPanToolButton(QWidget *parent) :
Chris@929 32 QToolButton(parent),
Chris@930 33 m_pixels(32),
Chris@942 34 m_pixelsBig(32 * 3),
Chris@930 35 m_muted(false),
Chris@930 36 m_savedLevel(1.f)
Chris@929 37 {
Chris@929 38 m_lpw = new LevelPanWidget();
Chris@929 39
Chris@929 40 connect(m_lpw, SIGNAL(levelChanged(float)), this, SIGNAL(levelChanged(float)));
Chris@930 41 connect(m_lpw, SIGNAL(levelChanged(float)), this, SLOT(selfLevelChanged(float)));
Chris@929 42
Chris@929 43 connect(m_lpw, SIGNAL(panChanged(float)), this, SIGNAL(panChanged(float)));
Chris@932 44 connect(m_lpw, SIGNAL(panChanged(float)), this, SLOT(update()));
Chris@930 45
Chris@930 46 connect(this, SIGNAL(clicked(bool)), this, SLOT(selfClicked()));
Chris@929 47
Chris@929 48 QMenu *menu = new QMenu();
Chris@929 49 QWidgetAction *wa = new QWidgetAction(menu);
Chris@929 50 wa->setDefaultWidget(m_lpw);
Chris@929 51 menu->addAction(wa);
Chris@929 52
Chris@936 53 setPopupMode(InstantPopup);
Chris@929 54 setMenu(menu);
Chris@1191 55 setToolTip(tr("Click to adjust level and pan"));
Chris@929 56
Chris@932 57 setImageSize(m_pixels);
Chris@942 58 setBigImageSize(m_pixelsBig);
Chris@929 59 }
Chris@929 60
Chris@929 61 LevelPanToolButton::~LevelPanToolButton()
Chris@929 62 {
Chris@929 63 }
Chris@929 64
Chris@1200 65 void
Chris@1300 66 LevelPanToolButton::mousePressEvent(QMouseEvent *e)
Chris@1300 67 {
Chris@1300 68 if (e->button() == Qt::MidButton ||
Chris@1300 69 ((e->button() == Qt::LeftButton) &&
Chris@1300 70 (e->modifiers() & Qt::ControlModifier))) {
Chris@1300 71 m_lpw->setToDefault();
Chris@1300 72 e->accept();
Chris@1300 73 } else {
Chris@1300 74 QToolButton::mousePressEvent(e);
Chris@1300 75 }
Chris@1300 76 }
Chris@1300 77
Chris@1300 78 void
Chris@1200 79 LevelPanToolButton::wheelEvent(QWheelEvent *e)
Chris@1200 80 {
Chris@1200 81 m_lpw->wheelEvent(e);
Chris@1200 82 }
Chris@1200 83
Chris@929 84 float
Chris@929 85 LevelPanToolButton::getLevel() const
Chris@929 86 {
Chris@929 87 return m_lpw->getLevel();
Chris@929 88 }
Chris@929 89
Chris@929 90 float
Chris@929 91 LevelPanToolButton::getPan() const
Chris@929 92 {
Chris@929 93 return m_lpw->getPan();
Chris@929 94 }
Chris@929 95
Chris@940 96 bool
Chris@940 97 LevelPanToolButton::includesMute() const
Chris@940 98 {
Chris@940 99 return m_lpw->includesMute();
Chris@940 100 }
Chris@940 101
Chris@929 102 void
Chris@929 103 LevelPanToolButton::setImageSize(int pixels)
Chris@929 104 {
Chris@929 105 m_pixels = pixels;
Chris@932 106
Chris@932 107 QPixmap px(m_pixels, m_pixels);
Chris@932 108 px.fill(Qt::transparent);
Chris@932 109 setIcon(px);
Chris@942 110 }
Chris@932 111
Chris@942 112 void
Chris@942 113 LevelPanToolButton::setBigImageSize(int pixels)
Chris@942 114 {
Chris@942 115 m_pixelsBig = pixels;
Chris@942 116
Chris@942 117 m_lpw->setFixedWidth(m_pixelsBig);
Chris@942 118 m_lpw->setFixedHeight(m_pixelsBig);
Chris@929 119 }
Chris@929 120
Chris@929 121 void
Chris@929 122 LevelPanToolButton::setLevel(float level)
Chris@929 123 {
Chris@929 124 m_lpw->setLevel(level);
Chris@932 125 update();
Chris@929 126 }
Chris@929 127
Chris@929 128 void
Chris@929 129 LevelPanToolButton::setPan(float pan)
Chris@929 130 {
Chris@929 131 m_lpw->setPan(pan);
Chris@932 132 update();
Chris@930 133 }
Chris@930 134
Chris@930 135 void
Chris@1187 136 LevelPanToolButton::setMonitoringLevels(float left, float right)
Chris@1187 137 {
Chris@1187 138 m_lpw->setMonitoringLevels(left, right);
Chris@1187 139 update();
Chris@1187 140 }
Chris@1187 141
Chris@1187 142 void
Chris@940 143 LevelPanToolButton::setIncludeMute(bool include)
Chris@940 144 {
Chris@940 145 m_lpw->setIncludeMute(include);
Chris@940 146 update();
Chris@940 147 }
Chris@940 148
Chris@940 149 void
Chris@933 150 LevelPanToolButton::setEnabled(bool enabled)
Chris@933 151 {
Chris@933 152 m_lpw->setEnabled(enabled);
Chris@933 153 QToolButton::setEnabled(enabled);
Chris@933 154 }
Chris@933 155
Chris@933 156 void
Chris@930 157 LevelPanToolButton::selfLevelChanged(float level)
Chris@930 158 {
Chris@930 159 if (level > 0.f) {
Chris@1266 160 m_muted = false;
Chris@930 161 } else {
Chris@1266 162 m_muted = true;
Chris@1266 163 m_savedLevel = 1.f;
Chris@930 164 }
Chris@932 165 update();
Chris@930 166 }
Chris@930 167
Chris@930 168 void
Chris@930 169 LevelPanToolButton::selfClicked()
Chris@930 170 {
Chris@930 171 if (m_muted) {
Chris@1266 172 m_muted = false;
Chris@1266 173 m_lpw->setLevel(m_savedLevel);
Chris@1266 174 emit levelChanged(m_savedLevel);
Chris@930 175 } else {
Chris@1266 176 m_savedLevel = m_lpw->getLevel();
Chris@1266 177 m_muted = true;
Chris@1266 178 m_lpw->setLevel(0.f);
Chris@1266 179 emit levelChanged(0.f);
Chris@930 180 }
Chris@932 181 update();
Chris@929 182 }
Chris@929 183
Chris@929 184 void
Chris@936 185 LevelPanToolButton::paintEvent(QPaintEvent *)
Chris@929 186 {
Chris@936 187 QStylePainter p(this);
Chris@936 188 QStyleOptionToolButton opt;
Chris@936 189 initStyleOption(&opt);
Chris@936 190 opt.features &= (~QStyleOptionToolButton::HasMenu);
Chris@936 191 p.drawComplexControl(QStyle::CC_ToolButton, opt);
Chris@936 192
Chris@934 193 if (m_pixels >= height()) {
Chris@934 194 setImageSize(height()-1);
Chris@934 195 }
Chris@936 196
Chris@932 197 double margin = (double(height()) - m_pixels) / 2.0;
Chris@932 198 m_lpw->renderTo(this, QRectF(margin, margin, m_pixels, m_pixels), false);
Chris@929 199 }
Chris@929 200
Chris@1180 201 void
Chris@1180 202 LevelPanToolButton::enterEvent(QEvent *e)
Chris@1180 203 {
Chris@1180 204 QToolButton::enterEvent(e);
Chris@1180 205 emit mouseEntered();
Chris@1180 206 }
Chris@932 207
Chris@1180 208 void
Chris@1180 209 LevelPanToolButton::leaveEvent(QEvent *e)
Chris@1180 210 {
Chris@1180 211 QToolButton::enterEvent(e);
Chris@1180 212 emit mouseLeft();
Chris@1180 213 }
Chris@1180 214
Chris@1180 215