chore: sync playlistmanager changes

This commit is contained in:
Gary Wang 2024-07-28 16:14:14 +08:00
parent f32cb998ae
commit cd01a05f23
No known key found for this signature in database
GPG Key ID: 5D30A4F15EA78760
2 changed files with 22 additions and 17 deletions

View File

@ -34,7 +34,7 @@ QModelIndex PlaylistModel::loadPlaylist(const QList<QUrl> & urls)
return loadPlaylist(urls.constFirst()); return loadPlaylist(urls.constFirst());
} else { } else {
setPlaylist(urls); setPlaylist(urls);
return createIndex(0); return index(0);
} }
} }
@ -45,8 +45,8 @@ QModelIndex PlaylistModel::loadPlaylist(const QUrl &url)
QString && currentFileName = info.fileName(); QString && currentFileName = info.fileName();
if (dir.path() == m_currentDir) { if (dir.path() == m_currentDir) {
int index = indexOf(url); int idx = indexOf(url);
return index == -1 ? appendToPlaylist(url) : createIndex(index); return idx == -1 ? appendToPlaylist(url) : index(idx);
} }
QStringList entryList = dir.entryList( QStringList entryList = dir.entryList(
@ -60,25 +60,25 @@ QModelIndex PlaylistModel::loadPlaylist(const QUrl &url)
QList<QUrl> playlist; QList<QUrl> playlist;
int index = -1; int idx = -1;
for (int i = 0; i < entryList.count(); i++) { for (int i = 0; i < entryList.count(); i++) {
const QString & fileName = entryList.at(i); const QString & fileName = entryList.at(i);
const QString & oneEntry = dir.absoluteFilePath(fileName); const QString & oneEntry = dir.absoluteFilePath(fileName);
const QUrl & url = QUrl::fromLocalFile(oneEntry); const QUrl & url = QUrl::fromLocalFile(oneEntry);
playlist.append(url); playlist.append(url);
if (fileName == currentFileName) { if (fileName == currentFileName) {
index = i; idx = i;
} }
} }
if (index == -1) { if (idx == -1) {
index = playlist.count(); idx = playlist.count();
playlist.append(url); playlist.append(url);
} }
m_currentDir = dir.path(); m_currentDir = dir.path();
setPlaylist(playlist); setPlaylist(playlist);
return createIndex(index); return index(idx);
} }
QModelIndex PlaylistModel::appendToPlaylist(const QUrl &url) QModelIndex PlaylistModel::appendToPlaylist(const QUrl &url)
@ -87,7 +87,7 @@ QModelIndex PlaylistModel::appendToPlaylist(const QUrl &url)
beginInsertRows(QModelIndex(), lastIndex, lastIndex); beginInsertRows(QModelIndex(), lastIndex, lastIndex);
m_playlist.append(url); m_playlist.append(url);
endInsertRows(); endInsertRows();
return createIndex(lastIndex); return index(lastIndex);
} }
bool PlaylistModel::removeAt(int index) bool PlaylistModel::removeAt(int index)
@ -114,9 +114,11 @@ QStringList PlaylistModel::autoLoadFilterSuffixes() const
return m_autoLoadSuffixes; return m_autoLoadSuffixes;
} }
QModelIndex PlaylistModel::createIndex(int row) const QHash<int, QByteArray> PlaylistModel::roleNames() const
{ {
return QAbstractItemModel::createIndex(row, 0, nullptr); QHash<int, QByteArray> result = QAbstractListModel::roleNames();
result.insert(UrlRole, "url");
return result;
} }
int PlaylistModel::rowCount(const QModelIndex &parent) const int PlaylistModel::rowCount(const QModelIndex &parent) const
@ -196,7 +198,7 @@ QModelIndex PlaylistManager::previousIndex() const
int count = totalCount(); int count = totalCount();
if (count == 0) return QModelIndex(); if (count == 0) return QModelIndex();
return m_model.createIndex(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
@ -204,12 +206,12 @@ QModelIndex PlaylistManager::nextIndex() const
int count = totalCount(); int count = totalCount();
if (count == 0) return QModelIndex(); if (count == 0) return QModelIndex();
return m_model.createIndex(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.createIndex(m_currentIndex); return m_model.index(m_currentIndex);
} }
void PlaylistManager::setCurrentIndex(const QModelIndex &index) void PlaylistManager::setCurrentIndex(const QModelIndex &index)

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <QUrl>
#include <QAbstractListModel> #include <QAbstractListModel>
class PlaylistModel : public QAbstractListModel class PlaylistModel : public QAbstractListModel
@ -28,7 +29,7 @@ public:
QUrl urlByIndex(int index) const; QUrl urlByIndex(int index) const;
QStringList autoLoadFilterSuffixes() const; QStringList autoLoadFilterSuffixes() const;
QModelIndex createIndex(int row) const; 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;
@ -49,6 +50,8 @@ 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(PlaylistModel * model READ model CONSTANT)
explicit PlaylistManager(QObject *parent = nullptr); explicit PlaylistManager(QObject *parent = nullptr);
~PlaylistManager(); ~PlaylistManager();
@ -56,8 +59,8 @@ public:
PlaylistModel * model(); PlaylistModel * model();
void setPlaylist(const QList<QUrl> & url); void setPlaylist(const QList<QUrl> & url);
QModelIndex loadPlaylist(const QList<QUrl> & urls); Q_INVOKABLE QModelIndex loadPlaylist(const QList<QUrl> & urls);
QModelIndex loadPlaylist(const QUrl & url); Q_INVOKABLE QModelIndex loadPlaylist(const QUrl & url);
int totalCount() const; int totalCount() const;
QModelIndex previousIndex() const; QModelIndex previousIndex() const;