2025-11-19 20:50:15 -07:00
|
|
|
#include "playerWindow.h"
|
|
|
|
|
#include <QVBoxLayout>
|
2025-11-19 21:20:27 -07:00
|
|
|
#include <QFileInfo>
|
2025-11-19 21:39:36 -07:00
|
|
|
#include <QCloseEvent>
|
|
|
|
|
#include "src/data/cleanerScripts.h"
|
2025-11-19 20:50:15 -07:00
|
|
|
|
|
|
|
|
PlayerWindow::PlayerWindow(const AlbumData& item, QWidget* parent)
|
|
|
|
|
: QWidget(parent), item_(item)
|
|
|
|
|
{
|
2025-11-19 22:33:32 -07:00
|
|
|
setWindowTitle("Now Playing");
|
|
|
|
|
resize(350, 500);
|
2025-11-19 20:50:15 -07:00
|
|
|
|
2025-11-19 21:20:27 -07:00
|
|
|
QVBoxLayout* layout = new QVBoxLayout(this);
|
2025-11-19 20:50:15 -07:00
|
|
|
|
2025-11-19 22:33:32 -07:00
|
|
|
// Album artwork
|
2025-11-19 21:46:02 -07:00
|
|
|
spinningArt_ = new SpinningAlbumArt(item.imagePath, 250);
|
2025-11-19 22:33:32 -07:00
|
|
|
layout->addWidget(spinningArt_, 0, Qt::AlignCenter);
|
2025-11-19 21:39:36 -07:00
|
|
|
|
2025-11-19 22:33:32 -07:00
|
|
|
// Track label
|
|
|
|
|
trackLabel_ = new QLabel("Track");
|
2025-11-19 21:20:27 -07:00
|
|
|
trackLabel_->setAlignment(Qt::AlignCenter);
|
|
|
|
|
trackLabel_->setStyleSheet("font-size: 14px; font-weight: 500;");
|
|
|
|
|
layout->addWidget(trackLabel_);
|
2025-11-19 20:50:15 -07:00
|
|
|
|
2025-11-19 22:33:32 -07:00
|
|
|
// Track list widget
|
|
|
|
|
trackList_ = new TrackListWidget(item_.audioFiles, this);
|
|
|
|
|
layout->addWidget(trackList_);
|
|
|
|
|
|
|
|
|
|
// Audio backend
|
2025-11-19 21:20:27 -07:00
|
|
|
player_ = new QMediaPlayer(this);
|
2025-11-19 21:39:36 -07:00
|
|
|
audio_ = new QAudioOutput(this);
|
2025-11-19 21:20:27 -07:00
|
|
|
player_->setAudioOutput(audio_);
|
2025-11-19 20:50:15 -07:00
|
|
|
|
2025-11-19 22:33:32 -07:00
|
|
|
// Spin album art based on playback
|
2025-11-19 21:39:36 -07:00
|
|
|
connect(player_, &QMediaPlayer::playbackStateChanged, this,
|
2025-11-19 21:46:02 -07:00
|
|
|
[this](QMediaPlayer::PlaybackState st){
|
2025-11-19 21:39:36 -07:00
|
|
|
if (st == QMediaPlayer::PlayingState)
|
2025-11-19 21:46:02 -07:00
|
|
|
spinningArt_->start();
|
2025-11-19 21:39:36 -07:00
|
|
|
else
|
2025-11-19 21:46:02 -07:00
|
|
|
spinningArt_->stop();
|
2025-11-19 21:39:36 -07:00
|
|
|
});
|
|
|
|
|
|
2025-11-19 22:33:32 -07:00
|
|
|
// When a track finishes, auto-play next
|
|
|
|
|
connect(player_, &QMediaPlayer::mediaStatusChanged, this,
|
|
|
|
|
[this](QMediaPlayer::MediaStatus st){
|
|
|
|
|
if (st == QMediaPlayer::EndOfMedia)
|
|
|
|
|
handleTrackFinished();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// When a track is clicked
|
|
|
|
|
connect(trackList_, &TrackListWidget::trackSelected, this,
|
|
|
|
|
[this](int i){
|
|
|
|
|
index_ = i;
|
|
|
|
|
playTrack(index_);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Start with track 0
|
|
|
|
|
playTrack(index_);
|
2025-11-19 20:50:15 -07:00
|
|
|
}
|
|
|
|
|
|
2025-11-19 22:33:32 -07:00
|
|
|
void PlayerWindow::playTrack(int idx)
|
2025-11-19 20:50:15 -07:00
|
|
|
{
|
2025-11-19 22:33:32 -07:00
|
|
|
if (idx < 0 || idx >= item_.audioFiles.size())
|
2025-11-19 21:20:27 -07:00
|
|
|
return;
|
|
|
|
|
|
2025-11-19 22:33:32 -07:00
|
|
|
index_ = idx;
|
|
|
|
|
|
|
|
|
|
QString filePath = item_.audioFiles[idx];
|
2025-11-19 21:20:27 -07:00
|
|
|
QString fileName = QFileInfo(filePath).fileName();
|
|
|
|
|
QString title = cleanTrackTitle(fileName);
|
|
|
|
|
|
2025-11-19 22:33:32 -07:00
|
|
|
trackLabel_->setText(QString("Track %1: %2").arg(idx + 1).arg(title));
|
2025-11-19 21:20:27 -07:00
|
|
|
|
|
|
|
|
player_->setSource(QUrl::fromLocalFile(filePath));
|
|
|
|
|
player_->play();
|
|
|
|
|
|
2025-11-19 22:33:32 -07:00
|
|
|
// Highlight in the list
|
|
|
|
|
trackList_->setCurrentRow(idx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PlayerWindow::handleTrackFinished()
|
|
|
|
|
{
|
|
|
|
|
int nextIndex = index_ + 1;
|
|
|
|
|
|
|
|
|
|
if (nextIndex >= item_.audioFiles.size()) {
|
|
|
|
|
close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
playTrack(nextIndex);
|
2025-11-19 20:50:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PlayerWindow::closeEvent(QCloseEvent* event)
|
|
|
|
|
{
|
2025-11-19 22:33:32 -07:00
|
|
|
if (player_)
|
2025-11-19 20:50:15 -07:00
|
|
|
player_->stop();
|
2025-11-19 22:33:32 -07:00
|
|
|
|
2025-11-19 21:39:36 -07:00
|
|
|
QWidget::closeEvent(event);
|
2025-11-19 20:50:15 -07:00
|
|
|
}
|