From 0040bd02289a73fcb6a24a22490f058d555d98b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20M=C3=A9ndez?= Date: Sat, 25 Nov 2023 16:30:11 -0600 Subject: [PATCH] Implemented volume and balance messages --- audiosourcecoordinator.cpp | 37 +++++++++++++++++++++++++++++++++---- audiosourcefile.cpp | 24 +++++++++++++++--------- mainwindow.cpp | 4 ++-- playerview.cpp | 9 +++++++-- playerview.h | 10 ++++------ 5 files changed, 61 insertions(+), 23 deletions(-) diff --git a/audiosourcecoordinator.cpp b/audiosourcecoordinator.cpp index 72a9b75..8c4fa18 100644 --- a/audiosourcecoordinator.cpp +++ b/audiosourcecoordinator.cpp @@ -17,17 +17,36 @@ AudioSourceCoordinator::AudioSourceCoordinator(QObject *parent, PlayerView *play } -void AudioSourceCoordinator::setSource(int source) +void AudioSourceCoordinator::setSource(int newSource) { - // TODO if(currentSource >= 0) { // deactivate old source sources[currentSource]->deactivate(); - // disconnect slots + disconnect(view, &PlayerView::positionChanged, sources[currentSource], &AudioSource::handleSeek); + disconnect(view, &PlayerView::previousClicked, sources[currentSource], &AudioSource::handlePrevious); + disconnect(view, &PlayerView::playClicked, sources[currentSource], &AudioSource::handlePlay); + disconnect(view, &PlayerView::pauseClicked, sources[currentSource], &AudioSource::handlePause); + disconnect(view, &PlayerView::stopClicked, sources[currentSource], &AudioSource::handleStop); + disconnect(view, &PlayerView::nextClicked, sources[currentSource], &AudioSource::handleNext); + disconnect(view, &PlayerView::openClicked, sources[currentSource], &AudioSource::handleOpen); + disconnect(view, &PlayerView::shuffleClicked, sources[currentSource], &AudioSource::handleShuffle); + disconnect(view, &PlayerView::repeatClicked, sources[currentSource], &AudioSource::handleRepeat); + + disconnect(sources[currentSource], &AudioSource::playbackStateChanged, view, &PlayerView::setPlaybackState); + disconnect(sources[currentSource], &AudioSource::positionChanged, view, &PlayerView::setPosition); + disconnect(sources[currentSource], &AudioSource::dataEmitted, view, &PlayerView::setSpectrumData); + disconnect(sources[currentSource], &AudioSource::metadataChanged, view, &PlayerView::setMetadata); + disconnect(sources[currentSource], &AudioSource::durationChanged, view, &PlayerView::setDuration); + disconnect(sources[currentSource], &AudioSource::eqEnabledChanged, view, &PlayerView::setEqEnabled); + disconnect(sources[currentSource], &AudioSource::plEnabledChanged, view, &PlayerView::setPlEnabled); + disconnect(sources[currentSource], &AudioSource::shuffleEnabledChanged, view, &PlayerView::setShuffleEnabled); + disconnect(sources[currentSource], &AudioSource::repeatEnabledChanged, view, &PlayerView::setRepeatEnabled); + disconnect(sources[currentSource], &AudioSource::messageSet, view, &PlayerView::setMessage); + disconnect(sources[currentSource], &AudioSource::messageClear, view, &PlayerView::clearMessage); } - currentSource = source; + currentSource = newSource; // connect slots to new source connect(view, &PlayerView::positionChanged, sources[currentSource], &AudioSource::handleSeek); connect(view, &PlayerView::previousClicked, sources[currentSource], &AudioSource::handlePrevious); @@ -58,11 +77,21 @@ void AudioSourceCoordinator::setSource(int source) void AudioSourceCoordinator::setVolume(int volume) { system_audio->setVolume(volume); + view->setMessage(QString("VOLUME: %1%").arg(volume), 500); } void AudioSourceCoordinator::setBalance(int balance) { system_audio->setBalance(balance); + QString message; + if(balance == 0) { + message = "BALANCE: CENTER"; + } else { + message = QString("BALANCE: %1% %2") + .arg(abs(balance)) + .arg(balance < 0 ? "LEFT" : "RIGHT"); + } + view->setMessage(message, 500); } void AudioSourceCoordinator::addSource(AudioSource *source, bool activate) diff --git a/audiosourcefile.cpp b/audiosourcefile.cpp index 80af838..9220986 100644 --- a/audiosourcefile.cpp +++ b/audiosourcefile.cpp @@ -1,10 +1,11 @@ #include -#include #include #include "audiosourcefile.h" #include "util.h" +//#define DISPLAY_BUFFERING + AudioSourceFile::AudioSourceFile(QObject *parent, PlaylistModel *playlistModel) : AudioSource{parent} @@ -144,13 +145,14 @@ void AudioSourceFile::handleMediaStatusChanged(MediaPlayer::MediaStatus status) break; case MediaPlayer::BufferingMedia: case MediaPlayer::BufferedMedia: - setStatusInfo(tr("Buffering %1%").arg(qRound(m_player->bufferProgress() * 100.))); + #ifdef DISPLAY_BUFFERING + setStatusInfo(tr("Buffering %1%").arg(qRound(m_player->bufferProgress()))); + #endif break; case MediaPlayer::StalledMedia: - setStatusInfo(tr("Stalled %1%").arg(qRound(m_player->bufferProgress() * 100.))); + setStatusInfo(tr("Stalled %1%").arg(qRound(m_player->bufferProgress()))); break; case MediaPlayer::EndOfMedia: - //QApplication::alert(this); handleNext(); break; case MediaPlayer::InvalidMedia: @@ -162,10 +164,14 @@ void AudioSourceFile::handleMediaStatusChanged(MediaPlayer::MediaStatus status) void AudioSourceFile::handleBufferingProgress(float progress) { - if (m_player->mediaStatus() == MediaPlayer::StalledMedia) - setStatusInfo(tr("Stalled %1%").arg(qRound(progress * 100.))); - else - setStatusInfo(tr("Buffering %1%").arg(qRound(progress * 100.))); + if (m_player->mediaStatus() == MediaPlayer::StalledMedia) { + setStatusInfo(tr("Stalled %1%").arg(qRound(progress))); + } + else { + #ifdef DISPLAY_BUFFERING + setStatusInfo(tr("Buffering %1%").arg(qRound(progress))); + #endif + } } void AudioSourceFile::handleMediaError() @@ -181,7 +187,7 @@ void AudioSourceFile::setStatusInfo(const QString &info) m_statusInfo = info; if (!m_statusInfo.isEmpty()) - emit messageSet(QString("%1 | %2").arg(m_statusInfo), 5000); + emit messageSet(QString("%1").arg(m_statusInfo), 1000); else emit messageClear(); } diff --git a/mainwindow.cpp b/mainwindow.cpp index 478dc74..73c475a 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -55,8 +55,8 @@ MainWindow::MainWindow(QWidget *parent) connect(controlButtons, &ControlButtonsWidget::nextClicked, player, &PlayerView::nextClicked); connect(controlButtons, &ControlButtonsWidget::previousClicked, player, &PlayerView::previousClicked); connect(controlButtons, &ControlButtonsWidget::openClicked, player, &PlayerView::showPlaylistClicked); - //TOSO connect(controlButtons, &ControlButtonsWidget::repeatClicked, player, &PlayerView::repeatButtonClicked); - //TODO connect(controlButtons, &ControlButtonsWidget::shuffleClicked, player, &PlayerView::shuffleButtonClicked); + connect(controlButtons, &ControlButtonsWidget::repeatClicked, player, &PlayerView::repeatClicked); + connect(controlButtons, &ControlButtonsWidget::shuffleClicked, player, &PlayerView::shuffleClicked); connect(controlButtons, &ControlButtonsWidget::logoClicked, this, &MainWindow::showShutdownModal); // Prepare player main view diff --git a/playerview.cpp b/playerview.cpp index fe4f000..3ece218 100644 --- a/playerview.cpp +++ b/playerview.cpp @@ -59,6 +59,9 @@ PlayerView::PlayerView(QWidget *parent, PlaylistModel *playlistModel) : spectrumLayout->setSpacing(0); ui->spectrumContainer->setLayout(spectrumLayout); + // Setup message functionality + messageTimer = new QTimer(this); + connect(messageTimer, &QTimer::timeout, this, &PlayerView::clearMessage); } PlayerView::~PlayerView() @@ -265,13 +268,15 @@ void PlayerView::setRepeatEnabled(bool enabled) void PlayerView::setMessage(QString message, qint64 timeout) { - //ui->songInfoLabel->setText(message); - // TODO timeout + ui->songInfoLabel->setText(message); + messageTimer->setSingleShot(true); + messageTimer->start(timeout); } void PlayerView::clearMessage() { ui->songInfoLabel->setText(m_trackInfo); + messageTimer->stop(); } ///////////// diff --git a/playerview.h b/playerview.h index f8b7fa4..21d869c 100644 --- a/playerview.h +++ b/playerview.h @@ -8,6 +8,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE class QAbstractItemView; @@ -62,10 +63,13 @@ public slots: void repeatClicked(); void menuClicked(); + void showPlaylistClicked(); + private: Ui::PlayerView *ui; void scale(); SpectrumWidget *spectrum = nullptr; + QTimer *messageTimer = nullptr; QString m_trackInfo; QString m_statusInfo; @@ -82,12 +86,6 @@ public slots: private slots: void handleBalanceChanged(); - ////////// - - -signals: - void showPlaylistClicked(); - }; #endif // PLAYERVIEW_H