annotate widgets/LevelPanToolButton.cpp @ 930:86df7de08e03 tonioni

A number of fixes to level-pan button (including mute toggle)
author Chris Cannam
date Wed, 25 Mar 2015 10:55:09 +0000
parents 20698aa6a517
children 7cf497b72da3
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@929 21
Chris@929 22 #include <iostream>
Chris@929 23 using std::cerr;
Chris@929 24 using std::endl;
Chris@929 25
Chris@929 26 LevelPanToolButton::LevelPanToolButton(QWidget *parent) :
Chris@929 27 QToolButton(parent),
Chris@930 28 m_pixels(32),
Chris@930 29 m_muted(false),
Chris@930 30 m_savedLevel(1.f)
Chris@929 31 {
Chris@929 32 m_lpw = new LevelPanWidget();
Chris@929 33
Chris@929 34 connect(m_lpw, SIGNAL(levelChanged(float)), this, SIGNAL(levelChanged(float)));
Chris@930 35 connect(m_lpw, SIGNAL(levelChanged(float)), this, SLOT(selfLevelChanged(float)));
Chris@929 36
Chris@929 37 connect(m_lpw, SIGNAL(panChanged(float)), this, SIGNAL(panChanged(float)));
Chris@929 38 connect(m_lpw, SIGNAL(panChanged(float)), this, SLOT(redraw()));
Chris@930 39
Chris@930 40 connect(this, SIGNAL(clicked(bool)), this, SLOT(selfClicked()));
Chris@929 41
Chris@929 42 QMenu *menu = new QMenu();
Chris@929 43 QWidgetAction *wa = new QWidgetAction(menu);
Chris@929 44 wa->setDefaultWidget(m_lpw);
Chris@929 45 menu->addAction(wa);
Chris@929 46
Chris@929 47 setPopupMode(MenuButtonPopup);
Chris@929 48 setMenu(menu);
Chris@929 49
Chris@929 50 redraw();
Chris@929 51 }
Chris@929 52
Chris@929 53 LevelPanToolButton::~LevelPanToolButton()
Chris@929 54 {
Chris@929 55 }
Chris@929 56
Chris@929 57 float
Chris@929 58 LevelPanToolButton::getLevel() const
Chris@929 59 {
Chris@929 60 return m_lpw->getLevel();
Chris@929 61 }
Chris@929 62
Chris@929 63 float
Chris@929 64 LevelPanToolButton::getPan() const
Chris@929 65 {
Chris@929 66 return m_lpw->getPan();
Chris@929 67 }
Chris@929 68
Chris@929 69 void
Chris@929 70 LevelPanToolButton::setImageSize(int pixels)
Chris@929 71 {
Chris@929 72 m_pixels = pixels;
Chris@929 73 redraw();
Chris@929 74 }
Chris@929 75
Chris@929 76 void
Chris@929 77 LevelPanToolButton::setLevel(float level)
Chris@929 78 {
Chris@929 79 m_lpw->setLevel(level);
Chris@930 80 redraw();
Chris@929 81 }
Chris@929 82
Chris@929 83 void
Chris@929 84 LevelPanToolButton::setPan(float pan)
Chris@929 85 {
Chris@929 86 m_lpw->setPan(pan);
Chris@930 87 redraw();
Chris@930 88 }
Chris@930 89
Chris@930 90 void
Chris@930 91 LevelPanToolButton::selfLevelChanged(float level)
Chris@930 92 {
Chris@930 93 if (level > 0.f) {
Chris@930 94 m_muted = false;
Chris@930 95 } else {
Chris@930 96 m_muted = true;
Chris@930 97 m_savedLevel = 1.f;
Chris@930 98 }
Chris@930 99 redraw();
Chris@930 100 }
Chris@930 101
Chris@930 102 void
Chris@930 103 LevelPanToolButton::selfClicked()
Chris@930 104 {
Chris@930 105 cerr << "selfClicked" << endl;
Chris@930 106
Chris@930 107 if (m_muted) {
Chris@930 108 m_muted = false;
Chris@930 109 m_lpw->setLevel(m_savedLevel);
Chris@930 110 } else {
Chris@930 111 m_savedLevel = m_lpw->getLevel();
Chris@930 112 m_muted = true;
Chris@930 113 m_lpw->setLevel(0.f);
Chris@930 114 }
Chris@930 115 redraw();
Chris@929 116 }
Chris@929 117
Chris@929 118 void
Chris@929 119 LevelPanToolButton::redraw()
Chris@929 120 {
Chris@929 121 QSize sz(m_pixels, m_pixels);
Chris@929 122
Chris@929 123 m_lpw->setFixedWidth(m_pixels * 4);
Chris@929 124 m_lpw->setFixedHeight(m_pixels * 4);
Chris@929 125
Chris@929 126 QPixmap px(sz);
Chris@929 127 px.fill(Qt::transparent);
Chris@929 128 m_lpw->renderTo(&px, QRectF(QPointF(), sz), false);
Chris@929 129 setIcon(px);
Chris@929 130 }
Chris@929 131