annotate widgets/LevelPanToolButton.cpp @ 936:a61a779d73df tonioni

Experiment with simple button logic for LevelPanWidget (separating out playback toggle again)
author Chris Cannam
date Mon, 30 Mar 2015 14:28:04 +0100
parents 325a7aa69055
children 77110abca8a1
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@936 21 #include <QStylePainter>
Chris@936 22 #include <QStyleOptionToolButton>
Chris@929 23
Chris@929 24 #include <iostream>
Chris@929 25 using std::cerr;
Chris@929 26 using std::endl;
Chris@929 27
Chris@929 28 LevelPanToolButton::LevelPanToolButton(QWidget *parent) :
Chris@929 29 QToolButton(parent),
Chris@930 30 m_pixels(32),
Chris@930 31 m_muted(false),
Chris@930 32 m_savedLevel(1.f)
Chris@929 33 {
Chris@929 34 m_lpw = new LevelPanWidget();
Chris@929 35
Chris@929 36 connect(m_lpw, SIGNAL(levelChanged(float)), this, SIGNAL(levelChanged(float)));
Chris@930 37 connect(m_lpw, SIGNAL(levelChanged(float)), this, SLOT(selfLevelChanged(float)));
Chris@929 38
Chris@929 39 connect(m_lpw, SIGNAL(panChanged(float)), this, SIGNAL(panChanged(float)));
Chris@932 40 connect(m_lpw, SIGNAL(panChanged(float)), this, SLOT(update()));
Chris@930 41
Chris@930 42 connect(this, SIGNAL(clicked(bool)), this, SLOT(selfClicked()));
Chris@929 43
Chris@929 44 QMenu *menu = new QMenu();
Chris@929 45 QWidgetAction *wa = new QWidgetAction(menu);
Chris@929 46 wa->setDefaultWidget(m_lpw);
Chris@929 47 menu->addAction(wa);
Chris@929 48
Chris@936 49 setPopupMode(InstantPopup);
Chris@929 50 setMenu(menu);
Chris@929 51
Chris@932 52 setImageSize(m_pixels);
Chris@929 53 }
Chris@929 54
Chris@929 55 LevelPanToolButton::~LevelPanToolButton()
Chris@929 56 {
Chris@929 57 }
Chris@929 58
Chris@929 59 float
Chris@929 60 LevelPanToolButton::getLevel() const
Chris@929 61 {
Chris@929 62 return m_lpw->getLevel();
Chris@929 63 }
Chris@929 64
Chris@929 65 float
Chris@929 66 LevelPanToolButton::getPan() const
Chris@929 67 {
Chris@929 68 return m_lpw->getPan();
Chris@929 69 }
Chris@929 70
Chris@929 71 void
Chris@929 72 LevelPanToolButton::setImageSize(int pixels)
Chris@929 73 {
Chris@929 74 m_pixels = pixels;
Chris@932 75
Chris@932 76 QPixmap px(m_pixels, m_pixels);
Chris@932 77 px.fill(Qt::transparent);
Chris@932 78 setIcon(px);
Chris@932 79
Chris@933 80 m_lpw->setFixedWidth(m_pixels * 3);
Chris@933 81 m_lpw->setFixedHeight(m_pixels * 3);
Chris@929 82 }
Chris@929 83
Chris@929 84 void
Chris@929 85 LevelPanToolButton::setLevel(float level)
Chris@929 86 {
Chris@929 87 m_lpw->setLevel(level);
Chris@932 88 update();
Chris@929 89 }
Chris@929 90
Chris@929 91 void
Chris@929 92 LevelPanToolButton::setPan(float pan)
Chris@929 93 {
Chris@929 94 m_lpw->setPan(pan);
Chris@932 95 update();
Chris@930 96 }
Chris@930 97
Chris@930 98 void
Chris@933 99 LevelPanToolButton::setEnabled(bool enabled)
Chris@933 100 {
Chris@933 101 m_lpw->setEnabled(enabled);
Chris@933 102 QToolButton::setEnabled(enabled);
Chris@933 103 }
Chris@933 104
Chris@933 105 void
Chris@930 106 LevelPanToolButton::selfLevelChanged(float level)
Chris@930 107 {
Chris@930 108 if (level > 0.f) {
Chris@930 109 m_muted = false;
Chris@930 110 } else {
Chris@930 111 m_muted = true;
Chris@930 112 m_savedLevel = 1.f;
Chris@930 113 }
Chris@932 114 update();
Chris@930 115 }
Chris@930 116
Chris@930 117 void
Chris@930 118 LevelPanToolButton::selfClicked()
Chris@930 119 {
Chris@930 120 cerr << "selfClicked" << endl;
Chris@930 121
Chris@930 122 if (m_muted) {
Chris@930 123 m_muted = false;
Chris@930 124 m_lpw->setLevel(m_savedLevel);
Chris@931 125 emit levelChanged(m_savedLevel);
Chris@930 126 } else {
Chris@930 127 m_savedLevel = m_lpw->getLevel();
Chris@930 128 m_muted = true;
Chris@930 129 m_lpw->setLevel(0.f);
Chris@931 130 emit levelChanged(0.f);
Chris@930 131 }
Chris@932 132 update();
Chris@929 133 }
Chris@929 134
Chris@929 135 void
Chris@936 136 LevelPanToolButton::paintEvent(QPaintEvent *)
Chris@929 137 {
Chris@936 138 QStylePainter p(this);
Chris@936 139 QStyleOptionToolButton opt;
Chris@936 140 initStyleOption(&opt);
Chris@936 141 opt.features &= (~QStyleOptionToolButton::HasMenu);
Chris@936 142 p.drawComplexControl(QStyle::CC_ToolButton, opt);
Chris@936 143
Chris@934 144 if (m_pixels >= height()) {
Chris@934 145 setImageSize(height()-1);
Chris@934 146 }
Chris@936 147
Chris@932 148 double margin = (double(height()) - m_pixels) / 2.0;
Chris@932 149 m_lpw->renderTo(this, QRectF(margin, margin, m_pixels, m_pixels), false);
Chris@929 150 }
Chris@929 151
Chris@932 152