Add song title to current playing screen
This commit is contained in:
30
src/data/cleanerScripts.h
Normal file
30
src/data/cleanerScripts.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QRegularExpression>
|
||||
#include <QFileInfo>
|
||||
|
||||
// Cleans track filenames like:
|
||||
// "01 - Speak_To_Me.flac" → "Speak To Me"
|
||||
// "03.Breathe.mp3" → "Breathe"
|
||||
// "7 Time.m4a" → "Time"
|
||||
static inline QString cleanTrackTitle(const QString& fileName)
|
||||
{
|
||||
QString title = fileName;
|
||||
|
||||
// Strip extension
|
||||
int dotIndex = title.lastIndexOf('.');
|
||||
if (dotIndex > 0)
|
||||
title = title.left(dotIndex);
|
||||
|
||||
// Remove leading digits + junk like "-", "_", ".", spaces
|
||||
title.remove(QRegularExpression(R"(^\s*\d+\s*[-_\.]*\s*)"));
|
||||
|
||||
// Replace underscores with spaces
|
||||
title.replace("_", " ");
|
||||
|
||||
// Trim whitespace
|
||||
title = title.trimmed();
|
||||
|
||||
return title;
|
||||
}
|
||||
@@ -2,46 +2,66 @@
|
||||
#include <QVBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QPixmap>
|
||||
#include <QFileInfo>
|
||||
|
||||
PlayerWindow::PlayerWindow(const AlbumData& item, QWidget* parent)
|
||||
: QWidget(parent), item_(item)
|
||||
{
|
||||
this->setWindowTitle("Now Playing");
|
||||
this->resize(300, 300);
|
||||
this->setWindowTitle("Now Playing");
|
||||
this->resize(300, 350);
|
||||
|
||||
QVBoxLayout* layout = new QVBoxLayout(this);
|
||||
QVBoxLayout* layout = new QVBoxLayout(this);
|
||||
|
||||
QLabel* art = new QLabel;
|
||||
QPixmap pix(item.imagePath);
|
||||
art->setPixmap(pix.scaled(250, 250, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
||||
art->setAlignment(Qt::AlignCenter);
|
||||
// Album Art
|
||||
QLabel* art = new QLabel;
|
||||
QPixmap pix(item.imagePath);
|
||||
art->setPixmap(pix.scaled(250, 250, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
||||
art->setAlignment(Qt::AlignCenter);
|
||||
layout->addWidget(art);
|
||||
|
||||
layout->addWidget(art);
|
||||
// Track Label (new)
|
||||
trackLabel_ = new QLabel("Track 1");
|
||||
trackLabel_->setAlignment(Qt::AlignCenter);
|
||||
trackLabel_->setStyleSheet("font-size: 14px; font-weight: 500;");
|
||||
layout->addWidget(trackLabel_);
|
||||
|
||||
player_ = new QMediaPlayer(this);
|
||||
audio_ = new QAudioOutput(this);
|
||||
// Player setup
|
||||
player_ = new QMediaPlayer(this);
|
||||
audio_ = new QAudioOutput(this);
|
||||
player_->setAudioOutput(audio_);
|
||||
|
||||
player_->setAudioOutput(audio_);
|
||||
connect(player_, &QMediaPlayer::mediaStatusChanged, this,
|
||||
[this](QMediaPlayer::MediaStatus s){
|
||||
if (s == QMediaPlayer::EndOfMedia) {
|
||||
playNext();
|
||||
}
|
||||
});
|
||||
|
||||
connect(player_, &QMediaPlayer::mediaStatusChanged, this, [this](QMediaPlayer::MediaStatus s){
|
||||
if (s == QMediaPlayer::EndOfMedia) {
|
||||
playNext();
|
||||
}
|
||||
});
|
||||
|
||||
playNext();
|
||||
playNext();
|
||||
}
|
||||
|
||||
void PlayerWindow::playNext()
|
||||
{
|
||||
if (index_ >= item_.audioFiles.size()) {
|
||||
this->close();
|
||||
return;
|
||||
}
|
||||
if (index_ >= item_.audioFiles.size()) {
|
||||
this->close();
|
||||
return;
|
||||
}
|
||||
|
||||
player_->setSource(QUrl::fromLocalFile(item_.audioFiles[index_]));
|
||||
player_->play();
|
||||
index_++;
|
||||
QString filePath = item_.audioFiles[index_];
|
||||
QString fileName = QFileInfo(filePath).fileName();
|
||||
|
||||
// Remove extension for cleaner title
|
||||
QString title = cleanTrackTitle(fileName);
|
||||
|
||||
// Track number is index + 1
|
||||
int trackNumber = index_ + 1;
|
||||
|
||||
trackLabel_->setText(QString("Track %1: %2").arg(trackNumber).arg(title));
|
||||
|
||||
player_->setSource(QUrl::fromLocalFile(filePath));
|
||||
player_->play();
|
||||
|
||||
index_++;
|
||||
}
|
||||
|
||||
// Stop the music when the window is closed
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
#pragma once
|
||||
#include <QWidget>
|
||||
#include <QLabel>
|
||||
#include <QMediaPlayer>
|
||||
#include <QAudioOutput>
|
||||
#include "src/data/albumInformation.h"
|
||||
#include "src/data/cleanerScripts.h"
|
||||
|
||||
class PlayerWindow : public QWidget {
|
||||
Q_OBJECT
|
||||
QLabel* trackLabel_;
|
||||
public:
|
||||
explicit PlayerWindow(const AlbumData& item, QWidget* parent = nullptr);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user