Chris@929: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@929: Chris@929: /* Chris@929: Sonic Visualiser Chris@929: An audio file viewer and annotation editor. Chris@929: Centre for Digital Music, Queen Mary, University of London. Chris@929: Chris@929: This program is free software; you can redistribute it and/or Chris@929: modify it under the terms of the GNU General Public License as Chris@929: published by the Free Software Foundation; either version 2 of the Chris@929: License, or (at your option) any later version. See the file Chris@929: COPYING included with this distribution for more information. Chris@929: */ Chris@929: Chris@929: #include "LevelPanToolButton.h" Chris@929: #include "LevelPanWidget.h" Chris@929: Chris@929: #include Chris@929: #include Chris@929: #include Chris@929: Chris@929: #include Chris@929: using std::cerr; Chris@929: using std::endl; Chris@929: Chris@929: LevelPanToolButton::LevelPanToolButton(QWidget *parent) : Chris@929: QToolButton(parent), Chris@930: m_pixels(32), Chris@930: m_muted(false), Chris@930: m_savedLevel(1.f) Chris@929: { Chris@929: m_lpw = new LevelPanWidget(); Chris@929: Chris@929: connect(m_lpw, SIGNAL(levelChanged(float)), this, SIGNAL(levelChanged(float))); Chris@930: connect(m_lpw, SIGNAL(levelChanged(float)), this, SLOT(selfLevelChanged(float))); Chris@929: Chris@929: connect(m_lpw, SIGNAL(panChanged(float)), this, SIGNAL(panChanged(float))); Chris@932: connect(m_lpw, SIGNAL(panChanged(float)), this, SLOT(update())); Chris@930: Chris@930: connect(this, SIGNAL(clicked(bool)), this, SLOT(selfClicked())); Chris@929: Chris@929: QMenu *menu = new QMenu(); Chris@929: QWidgetAction *wa = new QWidgetAction(menu); Chris@929: wa->setDefaultWidget(m_lpw); Chris@929: menu->addAction(wa); Chris@929: Chris@929: setPopupMode(MenuButtonPopup); Chris@929: setMenu(menu); Chris@929: Chris@932: setImageSize(m_pixels); Chris@929: } Chris@929: Chris@929: LevelPanToolButton::~LevelPanToolButton() Chris@929: { Chris@929: } Chris@929: Chris@929: float Chris@929: LevelPanToolButton::getLevel() const Chris@929: { Chris@929: return m_lpw->getLevel(); Chris@929: } Chris@929: Chris@929: float Chris@929: LevelPanToolButton::getPan() const Chris@929: { Chris@929: return m_lpw->getPan(); Chris@929: } Chris@929: Chris@929: void Chris@929: LevelPanToolButton::setImageSize(int pixels) Chris@929: { Chris@929: m_pixels = pixels; Chris@932: Chris@932: QPixmap px(m_pixels, m_pixels); Chris@932: px.fill(Qt::transparent); Chris@932: setIcon(px); Chris@932: Chris@933: m_lpw->setFixedWidth(m_pixels * 3); Chris@933: m_lpw->setFixedHeight(m_pixels * 3); Chris@929: } Chris@929: Chris@929: void Chris@929: LevelPanToolButton::setLevel(float level) Chris@929: { Chris@929: m_lpw->setLevel(level); Chris@932: update(); Chris@929: } Chris@929: Chris@929: void Chris@929: LevelPanToolButton::setPan(float pan) Chris@929: { Chris@929: m_lpw->setPan(pan); Chris@932: update(); Chris@930: } Chris@930: Chris@930: void Chris@933: LevelPanToolButton::setEnabled(bool enabled) Chris@933: { Chris@933: m_lpw->setEnabled(enabled); Chris@933: QToolButton::setEnabled(enabled); Chris@933: } Chris@933: Chris@933: void Chris@930: LevelPanToolButton::selfLevelChanged(float level) Chris@930: { Chris@930: if (level > 0.f) { Chris@930: m_muted = false; Chris@930: } else { Chris@930: m_muted = true; Chris@930: m_savedLevel = 1.f; Chris@930: } Chris@932: update(); Chris@930: } Chris@930: Chris@930: void Chris@930: LevelPanToolButton::selfClicked() Chris@930: { Chris@930: cerr << "selfClicked" << endl; Chris@930: Chris@930: if (m_muted) { Chris@930: m_muted = false; Chris@930: m_lpw->setLevel(m_savedLevel); Chris@931: emit levelChanged(m_savedLevel); Chris@930: } else { Chris@930: m_savedLevel = m_lpw->getLevel(); Chris@930: m_muted = true; Chris@930: m_lpw->setLevel(0.f); Chris@931: emit levelChanged(0.f); Chris@930: } Chris@932: update(); Chris@929: } Chris@929: Chris@929: void Chris@932: LevelPanToolButton::paintEvent(QPaintEvent *e) Chris@929: { Chris@932: QToolButton::paintEvent(e); Chris@934: if (m_pixels >= height()) { Chris@934: setImageSize(height()-1); Chris@934: } Chris@932: double margin = (double(height()) - m_pixels) / 2.0; Chris@932: m_lpw->renderTo(this, QRectF(margin, margin, m_pixels, m_pixels), false); Chris@929: } Chris@929: Chris@932: