Skip to content

Commit

Permalink
chore: sync playlistmanager changes
Browse files Browse the repository at this point in the history
  • Loading branch information
BLumia committed Jul 28, 2024
1 parent f32cb99 commit cd01a05
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
30 changes: 16 additions & 14 deletions app/playlistmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ QModelIndex PlaylistModel::loadPlaylist(const QList<QUrl> & urls)
return loadPlaylist(urls.constFirst());
} else {
setPlaylist(urls);
return createIndex(0);
return index(0);
}
}

Expand All @@ -45,8 +45,8 @@ QModelIndex PlaylistModel::loadPlaylist(const QUrl &url)
QString && currentFileName = info.fileName();

if (dir.path() == m_currentDir) {
int index = indexOf(url);
return index == -1 ? appendToPlaylist(url) : createIndex(index);
int idx = indexOf(url);
return idx == -1 ? appendToPlaylist(url) : index(idx);
}

QStringList entryList = dir.entryList(
Expand All @@ -60,25 +60,25 @@ QModelIndex PlaylistModel::loadPlaylist(const QUrl &url)

QList<QUrl> playlist;

int index = -1;
int idx = -1;
for (int i = 0; i < entryList.count(); i++) {
const QString & fileName = entryList.at(i);
const QString & oneEntry = dir.absoluteFilePath(fileName);
const QUrl & url = QUrl::fromLocalFile(oneEntry);
playlist.append(url);
if (fileName == currentFileName) {
index = i;
idx = i;
}
}
if (index == -1) {
index = playlist.count();
if (idx == -1) {
idx = playlist.count();
playlist.append(url);
}
m_currentDir = dir.path();

setPlaylist(playlist);

return createIndex(index);
return index(idx);
}

QModelIndex PlaylistModel::appendToPlaylist(const QUrl &url)
Expand All @@ -87,7 +87,7 @@ QModelIndex PlaylistModel::appendToPlaylist(const QUrl &url)
beginInsertRows(QModelIndex(), lastIndex, lastIndex);
m_playlist.append(url);
endInsertRows();
return createIndex(lastIndex);
return index(lastIndex);
}

bool PlaylistModel::removeAt(int index)
Expand All @@ -114,9 +114,11 @@ QStringList PlaylistModel::autoLoadFilterSuffixes() const
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
Expand Down Expand Up @@ -196,20 +198,20 @@ QModelIndex PlaylistManager::previousIndex() const
int count = totalCount();
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
{
int count = totalCount();
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
{
return m_model.createIndex(m_currentIndex);
return m_model.index(m_currentIndex);
}

void PlaylistManager::setCurrentIndex(const QModelIndex &index)
Expand Down
9 changes: 6 additions & 3 deletions app/playlistmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <QUrl>
#include <QAbstractListModel>

class PlaylistModel : public QAbstractListModel
Expand All @@ -28,7 +29,7 @@ class PlaylistModel : public QAbstractListModel
QUrl urlByIndex(int index) const;
QStringList autoLoadFilterSuffixes() const;

QModelIndex createIndex(int row) const;
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

Expand All @@ -49,15 +50,17 @@ class PlaylistManager : public QObject
Q_OBJECT
public:
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);
~PlaylistManager();

PlaylistModel * model();

void setPlaylist(const QList<QUrl> & url);
QModelIndex loadPlaylist(const QList<QUrl> & urls);
QModelIndex loadPlaylist(const QUrl & url);
Q_INVOKABLE QModelIndex loadPlaylist(const QList<QUrl> & urls);
Q_INVOKABLE QModelIndex loadPlaylist(const QUrl & url);

int totalCount() const;
QModelIndex previousIndex() const;
Expand Down

0 comments on commit cd01a05

Please sign in to comment.