From 4c8c448a6de8b33e4a64271d6b2d0d25e00043ab Mon Sep 17 00:00:00 2001 From: manuel Date: Sat, 9 Apr 2011 16:01:36 +0200 Subject: store sound muted in local settings --- pacman-c++/audioplayer.cpp | 8 +++++++ pacman-c++/audioplayer.h | 2 ++ pacman-c++/client.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++- pacman-c++/client.h | 9 +++++++ pacman-c++/mainwidget.cpp | 46 ++---------------------------------- pacman-c++/mainwidget.h | 6 +---- 6 files changed, 79 insertions(+), 50 deletions(-) (limited to 'pacman-c++') diff --git a/pacman-c++/audioplayer.cpp b/pacman-c++/audioplayer.cpp index 5170183..238e05d 100644 --- a/pacman-c++/audioplayer.cpp +++ b/pacman-c++/audioplayer.cpp @@ -39,6 +39,8 @@ AudioPlayer::AudioPlayer() connect(m_player, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish_p())); m_output = new Phonon::AudioOutput(Phonon::MusicCategory, m_player); + connect(m_output, SIGNAL(mutedChanged(bool)), this, SLOT(mutedChanged_p(bool))); + Phonon::createPath(m_player, m_output); preload(); @@ -169,3 +171,9 @@ void AudioPlayer::testFinished() qDebug() << "There's no sound for you :("; disconnect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State))); } + + +void AudioPlayer::mutedChanged_p(bool muted) +{ + emit mutedChanged(muted); +} diff --git a/pacman-c++/audioplayer.h b/pacman-c++/audioplayer.h index 567ccb1..474b6ca 100644 --- a/pacman-c++/audioplayer.h +++ b/pacman-c++/audioplayer.h @@ -41,6 +41,7 @@ public: signals: void finished(); void aboutToFinish(); + void mutedChanged(bool muted); private: void test(); @@ -50,6 +51,7 @@ private slots: void finished_p(); void aboutToFinish_p(); void stateChanged_p(Phonon::State newstate, Phonon::State oldstate); + void mutedChanged_p(bool muted); void testFinished(); private: diff --git a/pacman-c++/client.cpp b/pacman-c++/client.cpp index 2135636..2142278 100644 --- a/pacman-c++/client.cpp +++ b/pacman-c++/client.cpp @@ -1,14 +1,70 @@ #include "client.h" +#include "clicklabel.h" +#include "audioplayer.h" Client::Client() { - m_mainWidget = new MainWidget(this, this); + m_settings = new QSettings(qApp->organizationName(), qApp->applicationName(), this); + createMenu(); + m_mainWidget = new MainWidget(this); 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); + + ClickLabel *toggleSound = new ClickLabel("Toggle Sound", this); + toggleSound->setFixedWidth(20); + toggleSound->setFixedHeight(16); + toggleSound->setAlignment(Qt::AlignBottom); + + bool sound = AudioPlayer::self()->isWorking(); + bool muted = sound && m_settings->value("muted", false).toBool(); + AudioPlayer::self()->setMuted(muted); + + QImage img(muted ? ":/soundoff" : ":/soundon"); + img.setColor(1, menuBar()->palette().color( + muted ? QPalette::Disabled : QPalette::Active, + dQPalette::ButtonText).rgba()); + toggleSound->setPixmap(QPixmap::fromImage(img)); + + if (sound) + { + connect(toggleSound, SIGNAL(clicked()), this, SLOT(toggleSound())); + connect(AudioPlayer::self(), SIGNAL(mutedChanged(bool)), this, SLOT(mutedChanged(bool))); + } + + menuBar()->setCornerWidget(toggleSound); +} + +void Client::toggleSound() const +{ + if (!AudioPlayer::self()->isWorking()) + return; + AudioPlayer::self()->setMuted(!AudioPlayer::self()->isMuted()); +} + +void Client::mutedChanged(bool muted) const +{ + 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)); + + m_settings->setValue("muted", muted); +} + int main(int argc, char ** argv) { QApplication app(argc, argv); + app.setOrganizationName("TU Wien FOOP"); app.setApplicationName("Pacman Client"); app.setWindowIcon(QIcon(":/appicon")); diff --git a/pacman-c++/client.h b/pacman-c++/client.h index 476b972..2d507e2 100644 --- a/pacman-c++/client.h +++ b/pacman-c++/client.h @@ -10,9 +10,18 @@ class Client Q_OBJECT public: Client(); + QSettings *settings(); + +public slots: + void toggleSound() const; + void mutedChanged(bool) const; + +private: + void createMenu(); private: MainWidget *m_mainWidget; + QSettings *m_settings; }; #endif // CLIENT_H diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 02ece0f..06a0b9c 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -1,15 +1,12 @@ #include "mainwidget.h" #include "actor.h" #include "block.h" -#include "bonuspoint.h" -#include "point.h" #include "constants.h" #include "audioplayer.h" -#include "clicklabel.h" #include "util.h" -MainWidget::MainWidget(QMainWindow *window, QWidget *parent) - : SceneHolder(parent), m_mainwindow(window), m_currentKey(0), m_running(false) +MainWidget::MainWidget(QWidget *parent) + : SceneHolder(parent), m_currentKey(0), m_running(false) { createGui(); updateMap(Util::createDummyMap()); @@ -26,7 +23,6 @@ MainWidget::MainWidget(QMainWindow *window, QWidget *parent) void MainWidget::createGui() { - createMenu(); setFocusPolicy(Qt::StrongFocus); QVBoxLayout *layout = new QVBoxLayout(this); @@ -68,29 +64,6 @@ void MainWidget::createGui() setLayout(layout); } -void MainWidget::createMenu() -{ - QAction *quitAction = new QAction("E&xit", this); - connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); - - QMenu *fileMenu = m_mainwindow->menuBar()->addMenu("File"); - fileMenu->addAction(quitAction); - - ClickLabel *toggleSound = new ClickLabel("Toggle Sound", this); - toggleSound->setFixedWidth(20); - toggleSound->setFixedHeight(16); - toggleSound->setAlignment(Qt::AlignBottom); - bool sound = AudioPlayer::self()->isWorking(); - QImage img(sound ? ":/soundon" : ":/soundoff"); - img.setColor(1, m_mainwindow->menuBar()->palette().color(sound ? QPalette::Active : QPalette::Disabled, - QPalette::ButtonText).rgba()); - toggleSound->setPixmap(QPixmap::fromImage(img)); - if (sound) - connect(toggleSound, SIGNAL(clicked()), this, SLOT(toggleSound())); - - m_mainwindow->menuBar()->setCornerWidget(toggleSound); -} - void MainWidget::updateScore() { QMapIterator i(m_actors); @@ -213,18 +186,3 @@ void MainWidget::playerScoreClicked() tmp->setChecked(true); return; } - -void MainWidget::toggleSound() const -{ - if (!AudioPlayer::self()->isWorking()) - return; - - bool muted = AudioPlayer::self()->isMuted(); - QImage img(muted ? ":/soundon" : ":/soundoff"); - img.setColor(1, m_mainwindow->menuBar()->palette().color( - muted ? QPalette::Active : QPalette::Disabled, - QPalette::ButtonText).rgba()); - ClickLabel *tmp = qobject_cast(m_mainwindow->menuBar()->cornerWidget()); - tmp->setPixmap(QPixmap::fromImage(img)); - AudioPlayer::self()->setMuted(!muted); -} diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h index 650a472..6adc876 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h @@ -2,7 +2,6 @@ #define MAINWIDGET_H #include "sceneholder.h" - #include "constants.h" #include "pixmapitem.h" #include @@ -16,7 +15,7 @@ class MainWidget Q_OBJECT public: - MainWidget(QMainWindow *window = 0, QWidget *parent = 0); + MainWidget(QWidget *parent = 0); protected: // handling of current key @@ -35,11 +34,8 @@ private slots: void startGame(); void playerScoreClicked(); void tick(); - void toggleSound() const; private: - QMainWindow *m_mainwindow; - // GUI elements needed in the progress of the game QList m_playerScoreLayouts; -- cgit v1.2.3