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++/mainwidget.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) (limited to 'pacman-c++/mainwidget.cpp') 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); +} -- cgit v1.2.3