chore: tidy code and hello 2025 :)

This commit is contained in:
Gary Wang 2025-02-09 13:12:37 +08:00
parent f17b722600
commit b6a64a2495
No known key found for this signature in database
GPG Key ID: 5D30A4F15EA78760
4 changed files with 363 additions and 365 deletions

View File

@ -678,7 +678,7 @@ void MainWindow::on_actionHelp_triggered()
"\n" "\n"
"[Source Code](https://github.com/BLumia/pineapple-music)\n" "[Source Code](https://github.com/BLumia/pineapple-music)\n"
"\n" "\n"
"Copyright © 2024 [BLumia](https://github.com/BLumia/)" "Copyright © 2025 [BLumia](https://github.com/BLumia/)"
); );
infoBox.setTextFormat(Qt::MarkdownText); infoBox.setTextFormat(Qt::MarkdownText);
infoBox.exec(); infoBox.exec();

View File

@ -1,255 +1,253 @@
// SPDX-FileCopyrightText: 2024 Gary Wang <git@blumia.net> // SPDX-FileCopyrightText: 2025 Gary Wang <git@blumia.net>
// //
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
#include "playlistmanager.h" #include "playlistmanager.h"
#include <QCollator> #include <QCollator>
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include <QUrl> #include <QUrl>
PlaylistModel::PlaylistModel(QObject *parent) PlaylistModel::PlaylistModel(QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
{ {
} }
PlaylistModel::~PlaylistModel() PlaylistModel::~PlaylistModel()
{ = default;
} void PlaylistModel::setPlaylist(const QList<QUrl> &urls)
{
void PlaylistModel::setPlaylist(const QList<QUrl> &urls) beginResetModel();
{ m_playlist = urls;
beginResetModel(); endResetModel();
m_playlist = urls; }
endResetModel();
} QModelIndex PlaylistModel::loadPlaylist(const QList<QUrl> & urls)
{
QModelIndex PlaylistModel::loadPlaylist(const QList<QUrl> & urls) if (urls.isEmpty()) return {};
{ if (urls.count() == 1) {
if (urls.isEmpty()) return QModelIndex(); return loadPlaylist(urls.constFirst());
if (urls.count() == 1) { } else {
return loadPlaylist(urls.constFirst()); setPlaylist(urls);
} else { return index(0);
setPlaylist(urls); }
return index(0); }
}
} QModelIndex PlaylistModel::loadPlaylist(const QUrl &url)
{
QModelIndex PlaylistModel::loadPlaylist(const QUrl &url) QFileInfo info(url.toLocalFile());
{ QDir dir(info.path());
QFileInfo info(url.toLocalFile()); QString && currentFileName = info.fileName();
QDir dir(info.path());
QString && currentFileName = info.fileName(); if (dir.path() == m_currentDir) {
int idx = indexOf(url);
if (dir.path() == m_currentDir) { return idx == -1 ? appendToPlaylist(url) : index(idx);
int idx = indexOf(url); }
return idx == -1 ? appendToPlaylist(url) : index(idx);
} QStringList entryList = dir.entryList(
m_autoLoadSuffixes,
QStringList entryList = dir.entryList( QDir::Files | QDir::NoSymLinks, QDir::NoSort);
m_autoLoadSuffixes,
QDir::Files | QDir::NoSymLinks, QDir::NoSort); QCollator collator;
collator.setNumericMode(true);
QCollator collator;
collator.setNumericMode(true); std::sort(entryList.begin(), entryList.end(), collator);
std::sort(entryList.begin(), entryList.end(), collator); QList<QUrl> playlist;
QList<QUrl> playlist; int idx = -1;
for (int i = 0; i < entryList.count(); i++) {
int idx = -1; const QString & fileName = entryList.at(i);
for (int i = 0; i < entryList.count(); i++) { const QString & oneEntry = dir.absoluteFilePath(fileName);
const QString & fileName = entryList.at(i); const QUrl & url = QUrl::fromLocalFile(oneEntry);
const QString & oneEntry = dir.absoluteFilePath(fileName); playlist.append(url);
const QUrl & url = QUrl::fromLocalFile(oneEntry); if (fileName == currentFileName) {
playlist.append(url); idx = i;
if (fileName == currentFileName) { }
idx = i; }
} if (idx == -1) {
} idx = playlist.count();
if (idx == -1) { playlist.append(url);
idx = playlist.count(); }
playlist.append(url); m_currentDir = dir.path();
}
m_currentDir = dir.path(); setPlaylist(playlist);
setPlaylist(playlist); return index(idx);
}
return index(idx);
} QModelIndex PlaylistModel::appendToPlaylist(const QUrl &url)
{
QModelIndex PlaylistModel::appendToPlaylist(const QUrl &url) const int lastIndex = rowCount();
{ beginInsertRows(QModelIndex(), lastIndex, lastIndex);
const int lastIndex = rowCount(); m_playlist.append(url);
beginInsertRows(QModelIndex(), lastIndex, lastIndex); endInsertRows();
m_playlist.append(url); return index(lastIndex);
endInsertRows(); }
return index(lastIndex);
} bool PlaylistModel::removeAt(int index)
{
bool PlaylistModel::removeAt(int index) if (index < 0 || index >= rowCount()) return false;
{ beginRemoveRows(QModelIndex(), index, index);
if (index < 0 || index >= rowCount()) return false; m_playlist.removeAt(index);
beginRemoveRows(QModelIndex(), index, index); endRemoveRows();
m_playlist.removeAt(index); return true;
endRemoveRows(); }
return true;
} int PlaylistModel::indexOf(const QUrl &url) const
{
int PlaylistModel::indexOf(const QUrl &url) const return m_playlist.indexOf(url);
{ }
return m_playlist.indexOf(url);
} QUrl PlaylistModel::urlByIndex(int index) const
{
QUrl PlaylistModel::urlByIndex(int index) const return m_playlist.value(index);
{ }
return m_playlist.value(index);
} QStringList PlaylistModel::autoLoadFilterSuffixes() const
{
QStringList PlaylistModel::autoLoadFilterSuffixes() const return m_autoLoadSuffixes;
{ }
return m_autoLoadSuffixes;
} QHash<int, QByteArray> PlaylistModel::roleNames() const
{
QHash<int, QByteArray> PlaylistModel::roleNames() const QHash<int, QByteArray> result = QAbstractListModel::roleNames();
{ result.insert(UrlRole, "url");
QHash<int, QByteArray> result = QAbstractListModel::roleNames(); return result;
result.insert(UrlRole, "url"); }
return result;
} int PlaylistModel::rowCount(const QModelIndex &parent) const
{
int PlaylistModel::rowCount(const QModelIndex &parent) const return m_playlist.count();
{ }
return m_playlist.count();
} QVariant PlaylistModel::data(const QModelIndex &index, int role) const
{
QVariant PlaylistModel::data(const QModelIndex &index, int role) const if (!index.isValid()) return {};
{
if (!index.isValid()) return QVariant(); switch (role) {
case Qt::DisplayRole:
switch (role) { return m_playlist.at(index.row()).fileName();
case Qt::DisplayRole: case UrlRole:
return m_playlist.at(index.row()).fileName(); return m_playlist.at(index.row());
case UrlRole: }
return m_playlist.at(index.row());
} return {};
}
return QVariant();
} PlaylistManager::PlaylistManager(QObject *parent)
: QObject(parent)
PlaylistManager::PlaylistManager(QObject *parent) {
: QObject(parent) connect(&m_model, &PlaylistModel::rowsRemoved, this,
{ [this](const QModelIndex &, int, int) {
connect(&m_model, &PlaylistModel::rowsRemoved, this, if (m_model.rowCount() <= m_currentIndex) {
[this](const QModelIndex &, int, int) { setProperty("currentIndex", m_currentIndex - 1);
if (m_model.rowCount() <= m_currentIndex) { }
setProperty("currentIndex", m_currentIndex - 1); });
}
}); auto onRowCountChanged = [this](){
emit totalCountChanged(m_model.rowCount());
auto onRowCountChanged = [this](){ };
emit totalCountChanged(m_model.rowCount());
}; connect(&m_model, &PlaylistModel::rowsInserted, this, onRowCountChanged);
connect(&m_model, &PlaylistModel::rowsRemoved, this, onRowCountChanged);
connect(&m_model, &PlaylistModel::rowsInserted, this, onRowCountChanged); connect(&m_model, &PlaylistModel::modelReset, this, onRowCountChanged);
connect(&m_model, &PlaylistModel::rowsRemoved, this, onRowCountChanged); }
connect(&m_model, &PlaylistModel::modelReset, this, onRowCountChanged);
} PlaylistManager::~PlaylistManager()
{
PlaylistManager::~PlaylistManager()
{ }
} PlaylistModel *PlaylistManager::model()
{
PlaylistModel *PlaylistManager::model() return &m_model;
{ }
return &m_model;
} void PlaylistManager::setPlaylist(const QList<QUrl> &urls)
{
void PlaylistManager::setPlaylist(const QList<QUrl> &urls) m_model.setPlaylist(urls);
{ }
m_model.setPlaylist(urls);
} QModelIndex PlaylistManager::loadPlaylist(const QList<QUrl> &urls)
{
QModelIndex PlaylistManager::loadPlaylist(const QList<QUrl> &urls) QModelIndex idx = m_model.loadPlaylist(urls);
{ setProperty("currentIndex", idx.row());
QModelIndex idx = m_model.loadPlaylist(urls); return idx;
setProperty("currentIndex", idx.row()); }
return idx;
} QModelIndex PlaylistManager::loadPlaylist(const QUrl &url)
{
QModelIndex PlaylistManager::loadPlaylist(const QUrl &url) QModelIndex idx = m_model.loadPlaylist(url);
{ setProperty("currentIndex", idx.row());
QModelIndex idx = m_model.loadPlaylist(url); return idx;
setProperty("currentIndex", idx.row()); }
return idx;
} int PlaylistManager::totalCount() const
{
int PlaylistManager::totalCount() const return m_model.rowCount();
{ }
return m_model.rowCount();
} QModelIndex PlaylistManager::previousIndex() const
{
QModelIndex PlaylistManager::previousIndex() const int count = totalCount();
{ if (count == 0) return {};
int count = totalCount();
if (count == 0) return QModelIndex(); return m_model.index(m_currentIndex - 1 < 0 ? count - 1 : m_currentIndex - 1);
}
return m_model.index(m_currentIndex - 1 < 0 ? count - 1 : m_currentIndex - 1);
} QModelIndex PlaylistManager::nextIndex() const
{
QModelIndex PlaylistManager::nextIndex() const int count = totalCount();
{ if (count == 0) return {};
int count = totalCount();
if (count == 0) return QModelIndex(); return m_model.index(m_currentIndex + 1 == count ? 0 : m_currentIndex + 1);
}
return m_model.index(m_currentIndex + 1 == count ? 0 : m_currentIndex + 1);
} QModelIndex PlaylistManager::curIndex() const
{
QModelIndex PlaylistManager::curIndex() const return m_model.index(m_currentIndex);
{ }
return m_model.index(m_currentIndex);
} void PlaylistManager::setCurrentIndex(const QModelIndex &index)
{
void PlaylistManager::setCurrentIndex(const QModelIndex &index) if (index.isValid() && index.row() >= 0 && index.row() < totalCount()) {
{ setProperty("currentIndex", index.row());
if (index.isValid() && index.row() >= 0 && index.row() < totalCount()) { }
setProperty("currentIndex", index.row()); }
}
} QUrl PlaylistManager::urlByIndex(const QModelIndex &index)
{
QUrl PlaylistManager::urlByIndex(const QModelIndex &index) return m_model.urlByIndex(index.row());
{ }
return m_model.urlByIndex(index.row());
} QString PlaylistManager::localFileByIndex(const QModelIndex &index)
{
QString PlaylistManager::localFileByIndex(const QModelIndex &index) return urlByIndex(index).toLocalFile();
{ }
return urlByIndex(index).toLocalFile();
} bool PlaylistManager::removeAt(const QModelIndex &index)
{
bool PlaylistManager::removeAt(const QModelIndex &index) return m_model.removeAt(index.row());
{ }
return m_model.removeAt(index.row());
} void PlaylistManager::setAutoLoadFilterSuffixes(const QStringList &nameFilters)
{
void PlaylistManager::setAutoLoadFilterSuffixes(const QStringList &nameFilters) m_model.setProperty("autoLoadFilterSuffixes", nameFilters);
{ }
m_model.setProperty("autoLoadFilterSuffixes", nameFilters);
} QList<QUrl> PlaylistManager::convertToUrlList(const QStringList &files)
{
QList<QUrl> PlaylistManager::convertToUrlList(const QStringList &files) QList<QUrl> urlList;
{ for (const QString & str : std::as_const(files)) {
QList<QUrl> urlList; QUrl url = QUrl::fromLocalFile(str);
for (const QString & str : std::as_const(files)) { if (url.isValid()) {
QUrl url = QUrl::fromLocalFile(str); urlList.append(url);
if (url.isValid()) { }
urlList.append(url); }
}
} return urlList;
}
return urlList;
}

View File

@ -1,85 +1,85 @@
// SPDX-FileCopyrightText: 2024 Gary Wang <git@blumia.net> // SPDX-FileCopyrightText: 2025 Gary Wang <git@blumia.net>
// //
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
#pragma once #pragma once
#include <QUrl> #include <QUrl>
#include <QAbstractListModel> #include <QAbstractListModel>
class PlaylistModel : public QAbstractListModel class PlaylistModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
public: public:
enum PlaylistRole { enum PlaylistRole {
UrlRole = Qt::UserRole UrlRole = Qt::UserRole
}; };
Q_ENUM(PlaylistRole) Q_ENUM(PlaylistRole)
Q_PROPERTY(QStringList autoLoadFilterSuffixes MEMBER m_autoLoadSuffixes NOTIFY autoLoadFilterSuffixesChanged) Q_PROPERTY(QStringList autoLoadFilterSuffixes MEMBER m_autoLoadSuffixes NOTIFY autoLoadFilterSuffixesChanged)
explicit PlaylistModel(QObject *parent = nullptr); explicit PlaylistModel(QObject *parent = nullptr);
~PlaylistModel(); ~PlaylistModel() override;
void setPlaylist(const QList<QUrl> & urls); void setPlaylist(const QList<QUrl> & urls);
QModelIndex loadPlaylist(const QList<QUrl> & urls); QModelIndex loadPlaylist(const QList<QUrl> & urls);
QModelIndex loadPlaylist(const QUrl & url); QModelIndex loadPlaylist(const QUrl & url);
QModelIndex appendToPlaylist(const QUrl & url); QModelIndex appendToPlaylist(const QUrl & url);
bool removeAt(int index); bool removeAt(int index);
int indexOf(const QUrl & url) const; int indexOf(const QUrl & url) const;
QUrl urlByIndex(int index) const; QUrl urlByIndex(int index) const;
QStringList autoLoadFilterSuffixes() const; QStringList autoLoadFilterSuffixes() const;
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
signals: signals:
void autoLoadFilterSuffixesChanged(QStringList suffixes); void autoLoadFilterSuffixesChanged(QStringList suffixes);
private: private:
// model data // model data
QList<QUrl> m_playlist; QList<QUrl> m_playlist;
// properties // properties
QStringList m_autoLoadSuffixes = {}; QStringList m_autoLoadSuffixes = {};
// internal // internal
QString m_currentDir; QString m_currentDir;
}; };
class PlaylistManager : public QObject class PlaylistManager : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
Q_PROPERTY(int currentIndex MEMBER m_currentIndex NOTIFY currentIndexChanged) Q_PROPERTY(int currentIndex MEMBER m_currentIndex NOTIFY currentIndexChanged)
Q_PROPERTY(QStringList autoLoadFilterSuffixes WRITE setAutoLoadFilterSuffixes) Q_PROPERTY(QStringList autoLoadFilterSuffixes WRITE setAutoLoadFilterSuffixes)
Q_PROPERTY(PlaylistModel * model READ model CONSTANT) Q_PROPERTY(PlaylistModel * model READ model CONSTANT)
explicit PlaylistManager(QObject *parent = nullptr); explicit PlaylistManager(QObject *parent = nullptr);
~PlaylistManager(); ~PlaylistManager();
PlaylistModel * model(); PlaylistModel * model();
void setPlaylist(const QList<QUrl> & url); void setPlaylist(const QList<QUrl> & url);
Q_INVOKABLE QModelIndex loadPlaylist(const QList<QUrl> & urls); Q_INVOKABLE QModelIndex loadPlaylist(const QList<QUrl> & urls);
Q_INVOKABLE QModelIndex loadPlaylist(const QUrl & url); Q_INVOKABLE QModelIndex loadPlaylist(const QUrl & url);
int totalCount() const; int totalCount() const;
QModelIndex previousIndex() const; QModelIndex previousIndex() const;
QModelIndex nextIndex() const; QModelIndex nextIndex() const;
QModelIndex curIndex() const; QModelIndex curIndex() const;
void setCurrentIndex(const QModelIndex & index); void setCurrentIndex(const QModelIndex & index);
QUrl urlByIndex(const QModelIndex & index); QUrl urlByIndex(const QModelIndex & index);
QString localFileByIndex(const QModelIndex & index); QString localFileByIndex(const QModelIndex & index);
bool removeAt(const QModelIndex & index); bool removeAt(const QModelIndex & index);
void setAutoLoadFilterSuffixes(const QStringList &nameFilters); void setAutoLoadFilterSuffixes(const QStringList &nameFilters);
static QList<QUrl> convertToUrlList(const QStringList & files); static QList<QUrl> convertToUrlList(const QStringList & files);
signals: signals:
void currentIndexChanged(int index); void currentIndexChanged(int index);
void totalCountChanged(int count); void totalCountChanged(int count);
private: private:
int m_currentIndex = -1; int m_currentIndex = -1;
PlaylistModel m_model; PlaylistModel m_model;
}; };

View File

@ -1,24 +1,24 @@
// SPDX-FileCopyrightText: 2024 Gary Wang <git@blumia.net> // SPDX-FileCopyrightText: 2025 Gary Wang <git@blumia.net>
// //
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
#pragma once #pragma once
#include <QSlider> #include <QSlider>
#include <QMouseEvent> #include <QMouseEvent>
class SeekableSlider : public QSlider class SeekableSlider : public QSlider
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit SeekableSlider(QWidget *parent = nullptr); explicit SeekableSlider(QWidget *parent = nullptr);
~SeekableSlider() = default; ~SeekableSlider() override = default;
signals: signals:
public slots: public slots:
protected: protected:
void mouseReleaseEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override;
}; };