Keep the state of the rain and hypnotoad menu actions in sync with whether they're actually playing. Fixes issue 1986

This commit is contained in:
David Sansome 2011-06-19 14:42:00 +00:00
parent 87a5ec5cc7
commit 36165ea9bf
3 changed files with 71 additions and 19 deletions

View File

@ -1,8 +1,10 @@
#include "backgroundstreams.h"
#include <QAction>
#include <QSettings>
#include <QtDebug>
#include "core/logging.h"
#include "engines/enginebase.h"
const char* BackgroundStreams::kSettingsGroup = "BackgroundStreams";
@ -74,6 +76,11 @@ void BackgroundStreams::AddStream(const QString& name,
}
void BackgroundStreams::EnableStream(const QString& name, bool enable) {
if (!streams_.contains(name)) {
qLog(Warning) << "Tried to toggle a stream" << name << "which didn't exist";
return;
}
Stream* stream = streams_[name];
if (enable == (stream->id != -1)) {
return;
@ -97,32 +104,70 @@ void BackgroundStreams::PlayStream(Stream* stream) {
stream->id = engine_->AddBackgroundStream(stream->url);
engine_->SetBackgroundStreamVolume(stream->id, stream->volume);
emit StreamStarted(stream->name);
if (stream->action) {
stream->action->setChecked(true);
}
}
void BackgroundStreams::StopStream(Stream* stream) {
engine_->StopBackgroundStream(stream->id);
stream->id = -1;
emit StreamStopped(stream->name);
if (stream->action) {
stream->action->setChecked(false);
}
}
int BackgroundStreams::GetStreamVolume(const QString& name) {
int BackgroundStreams::GetStreamVolume(const QString& name) const {
return streams_[name]->volume;
}
bool BackgroundStreams::IsPlaying(const QString& name) {
bool BackgroundStreams::IsPlaying(const QString& name) const {
return streams_[name]->id != -1;
}
void BackgroundStreams::MakeItRain(bool enable) {
if (!streams_.contains("Rain")) {
AddStream("Rain", QUrl(kRainUrl));
void BackgroundStreams::AddAction(const QString& name, QAction* action) {
if (!streams_.contains(name)) {
qLog(Error) << "Tried to add action for stream" << name << "which doesn't exist";
return;
}
EnableStream("Rain", enable);
Stream* stream = streams_[name];
if (stream->action) {
qLog(Error) << "Tried to add multiple actions for stream" << name;
return;
}
stream->action = action;
action->setChecked(IsPlaying(name));
connect(action, SIGNAL(toggled(bool)), SLOT(StreamActionToggled(bool)));
connect(action, SIGNAL(destroyed()), SLOT(StreamActionDestroyed()));
}
void BackgroundStreams::AllGloryToTheHypnotoad(bool enable) {
if (!streams_.contains("Hypnotoad")) {
AddStream("Hypnotoad", QUrl(kHypnotoadUrl));
void BackgroundStreams::StreamActionDestroyed() {
QAction* action = static_cast<QAction*>(sender());
if (!action) {
return;
}
foreach (Stream* stream, streams_.values()) {
if (stream->action == action) {
stream->action = NULL;
}
}
}
void BackgroundStreams::StreamActionToggled(bool checked) {
QAction* action = static_cast<QAction*>(sender());
if (!action) {
return;
}
foreach (Stream* stream, streams_.values()) {
if (stream->action == action) {
EnableStream(stream->name, checked);
}
}
EnableStream("Hypnotoad", enable);
}

View File

@ -8,6 +8,8 @@
#include "engines/engine_fwd.h"
class QAction;
class BackgroundStreams : public QObject {
Q_OBJECT
public:
@ -22,23 +24,29 @@ class BackgroundStreams : public QObject {
void EnableStream(const QString& name, bool enable);
void SetStreamVolume(const QString& name, int volume);
int GetStreamVolume(const QString& name);
bool IsPlaying(const QString& name);
int GetStreamVolume(const QString& name) const;
bool IsPlaying(const QString& name) const;
public slots:
void MakeItRain(bool enable);
void AllGloryToTheHypnotoad(bool enable);
void AddAction(const QString& name, QAction* action);
signals:
void StreamStarted(const QString& name);
void StreamStopped(const QString& name);
private slots:
void StreamActionToggled(bool checked);
void StreamActionDestroyed();
private:
struct Stream {
Stream() : volume(0), id(0), action(NULL) {}
QString name;
QUrl url;
int volume;
int id;
QAction* action;
};
void AddStream(

View File

@ -349,12 +349,11 @@ MainWindow::MainWindow(
connect(ui_->action_jump, SIGNAL(triggered()), ui_->playlist->view(), SLOT(JumpToCurrentlyPlayingTrack()));
connect(ui_->action_update_library, SIGNAL(triggered()), library_, SLOT(IncrementalScan()));
connect(ui_->action_full_library_scan, SIGNAL(triggered()), library_, SLOT(FullScan()));
connect(ui_->action_rain, SIGNAL(toggled(bool)),
background_streams_, SLOT(MakeItRain(bool)));
connect(ui_->action_hypnotoad, SIGNAL(toggled(bool)),
background_streams_, SLOT(AllGloryToTheHypnotoad(bool)));
connect(ui_->action_queue_manager, SIGNAL(triggered()), SLOT(ShowQueueManager()));
background_streams_->AddAction("Rain", ui_->action_rain);
background_streams_->AddAction("Hypnotoad", ui_->action_hypnotoad);
// Playlist view actions
ui_->action_next_playlist->setShortcuts(QList<QKeySequence>()
<< QKeySequence::fromString("Ctrl+Tab")