From b0d6e4fcf33d21b24d2e9bbf7c5abf2065f0a4b3 Mon Sep 17 00:00:00 2001 From: manuel Date: Sat, 9 Apr 2011 15:28:00 +0200 Subject: move sound toggle icon to mainwidget --- pacman-c++/client.cpp | 38 +------------------------------------- pacman-c++/client.h | 3 --- pacman-c++/mainwidget.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++--- pacman-c++/mainwidget.h | 6 ++++-- 4 files changed, 47 insertions(+), 45 deletions(-) (limited to 'pacman-c++') diff --git a/pacman-c++/client.cpp b/pacman-c++/client.cpp index 7827591..2135636 100644 --- a/pacman-c++/client.cpp +++ b/pacman-c++/client.cpp @@ -1,47 +1,11 @@ #include "client.h" -#include "audioplayer.h" -#include "clicklabel.h" Client::Client() { - 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(); - QImage img(sound ? ":/soundon" : ":/soundoff"); - img.setColor(1, 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())); - - menuBar()->setCornerWidget(toggleSound); - - m_mainWidget = new MainWidget(); + m_mainWidget = new MainWidget(this, this); setCentralWidget(m_mainWidget); } -void Client::toggleSound() const -{ - if (!AudioPlayer::self()->isWorking()) - return; - - bool muted = AudioPlayer::self()->isMuted(); - QImage img(muted ? ":/soundon" : ":/soundoff"); - img.setColor(1, menuBar()->palette().color(muted ? QPalette::Active : QPalette::Disabled, - QPalette::ButtonText).rgba()); - ClickLabel *tmp = qobject_cast(menuBar()->cornerWidget()); - tmp->setPixmap(QPixmap::fromImage(img)); - AudioPlayer::self()->setMuted(!muted); -} - int main(int argc, char ** argv) { QApplication app(argc, argv); diff --git a/pacman-c++/client.h b/pacman-c++/client.h index 56f3602..476b972 100644 --- a/pacman-c++/client.h +++ b/pacman-c++/client.h @@ -11,9 +11,6 @@ class Client public: Client(); -public slots: - void toggleSound() const; - private: MainWidget *m_mainWidget; }; diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 79648c5..02ece0f 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -5,11 +5,11 @@ #include "point.h" #include "constants.h" #include "audioplayer.h" - +#include "clicklabel.h" #include "util.h" -MainWidget::MainWidget(QWidget *parent) - : SceneHolder(parent), m_currentKey(0), m_running(false) +MainWidget::MainWidget(QMainWindow *window, QWidget *parent) + : SceneHolder(parent), m_mainwindow(window), m_currentKey(0), m_running(false) { createGui(); updateMap(Util::createDummyMap()); @@ -26,6 +26,7 @@ MainWidget::MainWidget(QWidget *parent) void MainWidget::createGui() { + createMenu(); setFocusPolicy(Qt::StrongFocus); QVBoxLayout *layout = new QVBoxLayout(this); @@ -67,6 +68,29 @@ 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); @@ -189,3 +213,18 @@ 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 27e901f..650a472 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h @@ -7,7 +7,6 @@ #include "pixmapitem.h" #include #include -#include class Actor; @@ -17,7 +16,7 @@ class MainWidget Q_OBJECT public: - MainWidget(QWidget *parent = 0); + MainWidget(QMainWindow *window = 0, QWidget *parent = 0); protected: // handling of current key @@ -28,6 +27,7 @@ protected: private: void createGui(); + void createMenu(); void updateScore(); bool isRunning(); @@ -35,8 +35,10 @@ 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