annotate widgets/LevelPanToolButton.cpp @ 932:37bb4b416c52 tonioni

Render direct to widget (necessary to avoid pixel doubling on OS/X)
author Chris Cannam
date Wed, 25 Mar 2015 11:27:46 +0000
parents 7cf497b72da3
children 9022635c615a
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@932 38 connect(m_lpw, SIGNAL(panChanged(float)), this, SLOT(update()));
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@932 50 setImageSize(m_pixels);
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@932 73
Chris@932 74 QPixmap px(m_pixels, m_pixels);
Chris@932 75 px.fill(Qt::transparent);
Chris@932 76 setIcon(px);
Chris@932 77
Chris@932 78 m_lpw->setFixedWidth(m_pixels * 4);
Chris@932 79 m_lpw->setFixedHeight(m_pixels * 4);
Chris@929 80 }
Chris@929 81
Chris@929 82 void
Chris@929 83 LevelPanToolButton::setLevel(float level)
Chris@929 84 {
Chris@929 85 m_lpw->setLevel(level);
Chris@932 86 update();
Chris@929 87 }
Chris@929 88
Chris@929 89 void
Chris@929 90 LevelPanToolButton::setPan(float pan)
Chris@929 91 {
Chris@929 92 m_lpw->setPan(pan);
Chris@932 93 update();
Chris@930 94 }
Chris@930 95
Chris@930 96 void
Chris@930 97 LevelPanToolButton::selfLevelChanged(float level)
Chris@930 98 {
Chris@930 99 if (level > 0.f) {
Chris@930 100 m_muted = false;
Chris@930 101 } else {
Chris@930 102 m_muted = true;
Chris@930 103 m_savedLevel = 1.f;
Chris@930 104 }
Chris@932 105 update();
Chris@930 106 }
Chris@930 107
Chris@930 108 void
Chris@930 109 LevelPanToolButton::selfClicked()
Chris@930 110 {
Chris@930 111 cerr << "selfClicked" << endl;
Chris@930 112
Chris@930 113 if (m_muted) {
Chris@930 114 m_muted = false;
Chris@930 115 m_lpw->setLevel(m_savedLevel);
Chris@931 116 emit levelChanged(m_savedLevel);
Chris@930 117 } else {
Chris@930 118 m_savedLevel = m_lpw->getLevel();
Chris@930 119 m_muted = true;
Chris@930 120 m_lpw->setLevel(0.f);
Chris@931 121 emit levelChanged(0.f);
Chris@930 122 }
Chris@932 123 update();
Chris@929 124 }
Chris@929 125
Chris@929 126 void
Chris@932 127 LevelPanToolButton::paintEvent(QPaintEvent *e)
Chris@929 128 {
Chris@932 129 QToolButton::paintEvent(e);
Chris@932 130 double margin = (double(height()) - m_pixels) / 2.0;
Chris@932 131 m_lpw->renderTo(this, QRectF(margin, margin, m_pixels, m_pixels), false);
Chris@929 132 }
Chris@929 133
Chris@932 134