From c2f60ab20223d156dd784513c0e095806b081493 Mon Sep 17 00:00:00 2001 From: Flavio Tordini Date: Thu, 13 Aug 2009 01:00:54 +0200 Subject: [PATCH] Volume keyboard shortcuts Automatically unmute when volume changes --- src/MainWindow.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++ src/MainWindow.h | 45 +++++++++++++++++++++++++++---------------- 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index cc967d9..d19c080 100755 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -171,12 +171,26 @@ void MainWindow::createActions() { actions->insert("about", aboutAct); connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + // Invisible actions + searchFocusAct = new QAction(tr("&Search"), this); searchFocusAct->setShortcut(QKeySequence::Find); actions->insert("search", searchFocusAct); connect(searchFocusAct, SIGNAL(triggered()), this, SLOT(searchFocus())); addAction(searchFocusAct); + volumeUpAct = new QAction(tr("&Volume up"), this); + volumeUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Plus)); + actions->insert("volume-up", volumeUpAct); + connect(volumeUpAct, SIGNAL(triggered()), this, SLOT(volumeUp())); + addAction(volumeUpAct); + + volumeDownAct = new QAction(tr("&Volume down"), this); + volumeDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Minus)); + actions->insert("volume-down", volumeDownAct); + connect(volumeDownAct, SIGNAL(triggered()), this, SLOT(volumeDown())); + addAction(volumeDownAct); + // common action properties foreach (QAction *action, actions->values()) { @@ -567,6 +581,8 @@ void MainWindow::initPhonon() { connect(mediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(totalTimeChanged(qint64))); seekSlider->setMediaObject(mediaObject); audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this); + connect(audioOutput, SIGNAL(volumeChanged(qreal)), this, SLOT(volumeChanged(qreal))); + connect(audioOutput, SIGNAL(mutedChanged(bool)), this, SLOT(volumeMutedChanged(bool))); volumeSlider->setAudioOutput(audioOutput); Phonon::createPath(mediaObject, audioOutput); } @@ -591,6 +607,38 @@ void MainWindow::totalTimeChanged(qint64 time) { // qDebug() << "totalTime" << time << displayTime.toString("mm:ss"); } +void MainWindow::volumeUp() { + qreal newVolume = volumeSlider->audioOutput()->volume() + .1; + if (newVolume > volumeSlider->maximumVolume()) + newVolume = volumeSlider->maximumVolume(); + volumeSlider->audioOutput()->setVolume(newVolume); +} + +void MainWindow::volumeDown() { + qreal newVolume = volumeSlider->audioOutput()->volume() - .1; + if (newVolume < 0) + newVolume = 0; + volumeSlider->audioOutput()->setVolume(newVolume); +} + +void MainWindow::volumeMute() { + volumeSlider->audioOutput()->setMuted(!volumeSlider->audioOutput()->isMuted()); +} + +void MainWindow::volumeChanged(qreal newVolume) { + // automatically unmute when volume changes + if (volumeSlider->audioOutput()->isMuted()) + volumeSlider->audioOutput()->setMuted(false); + statusBar()->showMessage(tr("Volume at %1%").arg(newVolume*100)); +} + +void MainWindow::volumeMutedChanged(bool muted) { + if (muted) + statusBar()->showMessage(tr("Volume is muted")); + else + statusBar()->showMessage(tr("Volume is unmuted")); +} + void MainWindow::abortDownload() { QProgressDialog* dlg = dynamic_cast(this->sender()); QMap::iterator cur; diff --git a/src/MainWindow.h b/src/MainWindow.h index 0a44bea..1f493f1 100755 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -27,11 +27,11 @@ public: protected: void closeEvent(QCloseEvent *); - struct DownloadResource - { - QProgressDialog* dialog; - QFile* file; - }; + struct DownloadResource + { + QProgressDialog* dialog; + QFile* file; + }; private slots: void fadeInWidget(QWidget *oldWidget, QWidget *newWidget); @@ -44,21 +44,29 @@ private slots: void about(); void quit(); void fullscreen(); - void compactView(bool enable); + void compactView(bool enable); void stop(); void stateChanged(Phonon::State newState, Phonon::State oldState); void searchFocus(); void tick(qint64 time); void totalTimeChanged(qint64 time); - // download related stuff - void abortDownload(); - void download(); - void download(const QUrl& url, const DownloadResource& res); - void replyReadyRead(); - void replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal); - void replyError(QNetworkReply::NetworkError code); - void replyFinished(); - void replyMetaDataChanged(); + + // volume shortcuts + void volumeUp(); + void volumeDown(); + void volumeMute(); + void volumeChanged(qreal newVolume); + void volumeMutedChanged(bool muted); + + // download related stuff + void abortDownload(); + void download(); + void download(const QUrl& url, const DownloadResource& res); + void replyReadyRead(); + void replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal); + void replyError(QNetworkReply::NetworkError code); + void replyFinished(); + void replyMetaDataChanged(); private: void initPhonon(); @@ -96,9 +104,12 @@ private: QAction *pauseAct; QAction *stopAct; QAction *fullscreenAct; - QAction *compactViewAct; + QAction *compactViewAct; QAction *webPageAct; QAction *downloadAct; + QAction *volumeUpAct; + QAction *volumeDownAct; + QAction *volumeMuteAct; // playlist actions QAction *removeAct; @@ -127,7 +138,7 @@ private: bool m_fullscreen; bool m_maximized; - QMap m_downloads; + QMap m_downloads; }; #endif -- 2.39.5