From b18385a95f25e13c767244b494f31bd4fc238143 Mon Sep 17 00:00:00 2001 From: manuel Date: Thu, 14 Apr 2011 02:37:31 +0200 Subject: encapsulate gapless audioplayer commit from yesterday into an own class (gaplessaudioplayer) added two new menu entrys: toggle sound + toggle ambient sound (2. very useful!) --- pacman-c++/client.cpp | 93 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 25 deletions(-) (limited to 'pacman-c++/client.cpp') diff --git a/pacman-c++/client.cpp b/pacman-c++/client.cpp index 5cc9279..fc0fdfc 100644 --- a/pacman-c++/client.cpp +++ b/pacman-c++/client.cpp @@ -4,63 +4,106 @@ #include "pacman.pb.h" Client::Client() + : m_ambientMuted(false) { m_settings = new QSettings(qApp->organizationName(), qApp->applicationName(), this); createMenu(); m_mainWidget = new MainWidget(this); + m_mainWidget->setAmbientMuted(m_ambientMuted); setCentralWidget(m_mainWidget); } void Client::createMenu() { - QAction *quitAction = new QAction("E&xit", this); - connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); - QMenu *fileMenu = menuBar()->addMenu("File"); - fileMenu->addAction(quitAction); + bool sound = AudioManager::self()->isWorking(); + bool muted = !sound || m_settings->value("muted", false).toBool(); + AudioManager::self()->setMuted(muted); + + /* toggle sound: corner icon */ ClickLabel *toggleSound = new ClickLabel("Toggle Sound", this); toggleSound->setToolTip("Toggle Sound"); toggleSound->setFixedWidth(20); toggleSound->setFixedHeight(16); toggleSound->setAlignment(Qt::AlignBottom); - - bool sound = AudioManager::self()->isWorking(); - bool muted = !sound || m_settings->value("muted", false).toBool(); - AudioManager::self()->setMuted(muted); - - QImage img(muted ? ":/soundoff" : ":/soundon"); - img.setColor(1, menuBar()->palette().color( - muted ? QPalette::Disabled : QPalette::Active, - QPalette::ButtonText).rgba()); - toggleSound->setPixmap(QPixmap::fromImage(img)); - + toggleSound->setPixmap(soundIcon(!muted)); if (sound) { connect(toggleSound, SIGNAL(clicked()), this, SLOT(toggleSound())); connect(AudioManager::self(), SIGNAL(mutedChanged(bool)), this, SLOT(mutedChanged(bool))); } - menuBar()->setCornerWidget(toggleSound); + + /* toggle sound: menu */ + QAction *toggleSoundAction = new QAction("Sound", this); + toggleSoundAction->setToolTip("Toggle Sound"); + toggleSoundAction->setCheckable(true); + toggleSoundAction->setChecked(!muted); + toggleSoundAction->setDisabled(!sound); + fileMenu->addAction(toggleSoundAction); + if (sound) + { + connect(toggleSoundAction, SIGNAL(triggered()), this, SLOT(toggleSound())); + connect(this, SIGNAL(setMuteActionsChecked(bool)), toggleSoundAction, SLOT(setChecked(bool))); + } + + /* toggle ambient sound: menu */ + m_ambientMuted = muted || m_settings->value("ambientMuted", false).toBool(); + QAction *toggleAmbientAction = new QAction("Ambient Sound", this); + toggleAmbientAction->setToolTip("Toggle Ambient Sound"); + toggleAmbientAction->setCheckable(true); + toggleAmbientAction->setChecked(!m_ambientMuted); + toggleAmbientAction->setDisabled(!sound); + fileMenu->addAction(toggleAmbientAction); + if (sound) + { + connect(toggleAmbientAction, SIGNAL(triggered(bool)), this, SLOT(enableAmbientSound(bool))); + connect(this, SIGNAL(setMuteActionsChecked(bool)), toggleAmbientAction, SLOT(setEnabled(bool))); + } + + /* exit entry */ + fileMenu->addSeparator(); + QAction *quitAction = new QAction("E&xit", this); + connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + fileMenu->addAction(quitAction); } -void Client::toggleSound() const +void Client::toggleSound() { if (!AudioManager::self()->isWorking()) return; - AudioManager::self()->setMuted(!AudioManager::self()->isMuted()); + bool muted = !AudioManager::self()->isMuted(); + AudioManager::self()->setMuted(muted); + /* mute ambient sound again if explicitly muted */ + if (!muted && m_ambientMuted) + m_mainWidget->setAmbientMuted(true); } -void Client::mutedChanged(bool muted) const +void Client::mutedChanged(bool muted) { - QImage img(muted ? ":/soundoff" : ":/soundon"); - img.setColor(1, menuBar()->palette().color( - muted ? QPalette::Disabled : QPalette::Active, - QPalette::ButtonText).rgba()); ClickLabel *tmp = qobject_cast(menuBar()->cornerWidget()); - tmp->setPixmap(QPixmap::fromImage(img)); - + tmp->setPixmap(soundIcon(!muted)); m_settings->setValue("muted", muted); + emit setMuteActionsChecked(!muted); +} + +void Client::enableAmbientSound(bool enabled) +{ + if (!AudioManager::self()->isWorking()) + return; + m_ambientMuted = !enabled; + m_mainWidget->setAmbientMuted(m_ambientMuted); + m_settings->setValue("ambientMuted", m_ambientMuted); +} + +QPixmap Client::soundIcon(bool enabled) const +{ + QImage img(enabled ? ":/soundon" : ":/soundoff"); + img.setColor(1, menuBar()->palette().color( + enabled ? QPalette::Active : QPalette::Disabled, + QPalette::ButtonText).rgba()); + return QPixmap::fromImage(img); } bool Constants::server = false; -- cgit v1.2.3