Skip to content

Commit

Permalink
quick: same-folder playlist support
Browse files Browse the repository at this point in the history
  • Loading branch information
BLumia committed Jul 23, 2024
1 parent 03cff16 commit c4cb5ea
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 18 deletions.
30 changes: 16 additions & 14 deletions player/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 player/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
3 changes: 3 additions & 0 deletions quick/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "player.h"
#include "playlistmanager.h"
#include "util.h"

#include <QQmlContext>
Expand Down Expand Up @@ -28,6 +29,8 @@ int main(int argc, char *argv[])
QStringList urlStrList = parser.positionalArguments();
QList<QUrl> urlsToLoad = Util::convertToUrlList(urlStrList);

qmlRegisterUncreatableType<PlaylistModel>("Pineapple.TrackerPlayer", 1, 0, "PlaylistModel", "managed by PlaylistManager");
qmlRegisterType<PlaylistManager>("Pineapple.TrackerPlayer", 1, 0, "PlaylistManager");
qmlRegisterType<Player>("Pineapple.TrackerPlayer", 1, 0, "TrackerPlayer");

QQmlApplicationEngine engine;
Expand Down
73 changes: 72 additions & 1 deletion quick/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import QtQuick 2.4
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Dialogs
//import Qt.labs.platform 1 as Platform
import Pineapple.TrackerPlayer 1

ApplicationWindow {
Expand Down Expand Up @@ -44,6 +43,17 @@ ApplicationWindow {
}
}
}

Menu {
title: qsTr("&Help")

Action {
text: qsTr("About")
onTriggered: () => {
aboutDialog.open()
}
}
}
}

DropArea {
Expand All @@ -54,6 +64,7 @@ ApplicationWindow {
}
onDropped: (drop) => {
if (drop.urls.length <= 0) return;
playlistManager.loadPlaylist(drop.urls)
player.load(drop.urls[0]);
player.play();
}
Expand Down Expand Up @@ -117,6 +128,15 @@ ApplicationWindow {
Item {
Layout.fillWidth: true
}
Label {
text: "Playlist"
MouseArea {
anchors.fill: parent
onClicked: {
stackLayout.currentIndex = 3
}
}
}
Label {
text: "Message"
MouseArea {
Expand Down Expand Up @@ -174,6 +194,20 @@ ApplicationWindow {
id: instrumentsContainer
instruments: player.qml_instrumentNames
}
ListView {
id: playlistView
model: playlistManager.model
clip: true
delegate: ItemDelegate {
width: parent.width
text: model.display
onClicked: function() {
playlistManager.currentIndex = index
player.load(model.url);
player.play();
}
}
}
}
}
}
Expand Down Expand Up @@ -212,8 +246,14 @@ ApplicationWindow {
}
}

PlaylistManager {
id: playlistManager
autoLoadFilterSuffixes: ["*.xm", "*.it", "*.mod", "*.s3m", "*.mptm"]
}

Component.onCompleted: {
if (fileList.length > 0) {
playlistManager.loadPlaylist(fileList)
player.load(fileList[0]);
player.play();
}
Expand All @@ -226,6 +266,7 @@ ApplicationWindow {
"Module Files (*.xm *.it *.mod *.s3m *.mptm)"
]
onAccepted: {
playlistManager.loadPlaylist(fileDialog.currentFile)
player.load(fileDialog.currentFile);
player.play();
}
Expand All @@ -236,4 +277,34 @@ ApplicationWindow {
selectedFont.family: monoFontFamily
options: FontDialog.MonospacedFonts
}

Dialog {
id: aboutDialog
title: qsTr("About")
anchors.centerIn: parent
ColumnLayout {
Label {
textFormat: Text.MarkdownText
text: `Pineapple Tracker Player
Based on the following free software libraries:
- [Qt](https://www.qt.io/)
- [PortAudio](https://www.portaudio.com/)
- [libopenmpt](https://lib.openmpt.org/libopenmpt/)
[Source Code](https://github.com/BLumia/pineapple-tracker-player)
Copyright &copy; 2024 [BLumia](https://github.com/BLumia/)
`
onLinkActivated: function(link) {
Qt.openUrlExternally(link)
}
}
DialogButtonBox {
standardButtons: DialogButtonBox.Ok
onAccepted: aboutDialog.close()
}
}
}
}

0 comments on commit c4cb5ea

Please sign in to comment.