diff options
author | aqua <aqua@iserlohn-fortress.net> | 2023-01-08 13:22:41 +0200 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2023-01-08 13:22:41 +0200 |
commit | 6d25c63f29103f53fc151de0f2dfa91fdb852093 (patch) | |
tree | f5fb9e8d4fbc854377d82e69a8c88d0814149b13 /playlist | |
download | nyamp-master.tar.xz |
Diffstat (limited to 'playlist')
-rw-r--r-- | playlist/CMakeLists.txt | 6 | ||||
-rw-r--r-- | playlist/playlist.cpp | 36 | ||||
-rw-r--r-- | playlist/playlist.hpp | 39 | ||||
-rw-r--r-- | playlist/playlistmodel.cpp | 46 | ||||
-rw-r--r-- | playlist/playlistmodel.hpp | 28 |
5 files changed, 155 insertions, 0 deletions
diff --git a/playlist/CMakeLists.txt b/playlist/CMakeLists.txt new file mode 100644 index 0000000..54b397b --- /dev/null +++ b/playlist/CMakeLists.txt @@ -0,0 +1,6 @@ +add_library(playlist STATIC + playlist.cpp playlist.hpp + playlistmodel.cpp playlistmodel.hpp +) +target_link_libraries(playlist PUBLIC Qt6::Core) +target_include_directories(playlist INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/playlist/playlist.cpp b/playlist/playlist.cpp new file mode 100644 index 0000000..778fae1 --- /dev/null +++ b/playlist/playlist.cpp @@ -0,0 +1,36 @@ +/* ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2023 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#include "playlist.hpp" +#include "playlistmodel.hpp" +#include <QDir> +#include <QFileInfo> + +Playlist::Playlist(QObject *parent) : QObject{parent}, m_model(new PlaylistModel(this)) {} + +void Playlist::appendUrl(const QUrl &url) +{ + const QFileInfo info(url.toLocalFile()); + if (!info.exists()) return; // if file doesn't exist, return + + if (info.isFile()) { + emit mediaAboutToBeInserted(m_urls.count(), m_urls.count() + 1); + m_urls.append(url.toLocalFile()); + emit mediaInserted(); + } + else if (info.isFile()) { + QDir dir(info.absoluteFilePath()); + const auto list = dir.entryInfoList(QDir::Files); + emit mediaAboutToBeInserted(m_urls.count(), m_urls.count() + list.count()); + for (const auto &i : list) { m_urls.append(i.absoluteFilePath()); } + emit mediaInserted(); + } +} + +QUrl Playlist::operator[](const int index) const +{ + if (index < m_urls.count()) return m_urls.at(index); + return {}; +} diff --git a/playlist/playlist.hpp b/playlist/playlist.hpp new file mode 100644 index 0000000..4c9e385 --- /dev/null +++ b/playlist/playlist.hpp @@ -0,0 +1,39 @@ +/* ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2023 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#pragma once + +#include <QObject> +#include <QUrl> + +class PlaylistModel; +class Playlist : public QObject { + Q_OBJECT +public: + explicit Playlist(QObject *parent = nullptr); + +public slots: + void appendUrl(const QUrl &url); + +public: + void appendList(const QList<QUrl> &urllist) + { + for (const auto &url : urllist) appendUrl(url); + } + + [[nodiscard]] QUrl operator[](const int index) const; + [[nodiscard]] auto count() const { return m_urls.count(); } + [[nodiscard]] auto model() { return m_model; } + +signals: + void mediaAboutToBeInserted(int start, int end); + void mediaInserted(); + void mediaAboutToBeRemoved(int start, int end); + void mediaRemoved(); + +private: + QVector<QUrl> m_urls; + PlaylistModel *m_model; +}; diff --git a/playlist/playlistmodel.cpp b/playlist/playlistmodel.cpp new file mode 100644 index 0000000..9eb2629 --- /dev/null +++ b/playlist/playlistmodel.cpp @@ -0,0 +1,46 @@ +/* ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2023 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#include "playlistmodel.hpp" +#include "playlist.hpp" + +PlaylistModel::PlaylistModel(Playlist *parent) : QAbstractItemModel{parent}, playlist(parent) +{ + Q_CHECK_PTR(parent); + connect(parent, &Playlist::mediaAboutToBeInserted, this, &PlaylistModel::beginInsertItems); + connect(parent, &Playlist::mediaInserted, this, &PlaylistModel::endInsertItems); +} + +QModelIndex PlaylistModel::index(int row, int column, const QModelIndex &parent) const +{ + if (parent.isValid()) return {}; + return createIndex(row, column); +} + +QModelIndex PlaylistModel::parent(const QModelIndex &child) const { return {}; } + +int PlaylistModel::rowCount(const QModelIndex &parent) const +{ + if (parent.isValid()) return 0; + return playlist->count(); +} + +int PlaylistModel::columnCount(const QModelIndex &parent) const +{ + if (parent.isValid()) return 0; + return 1; +} + +QVariant PlaylistModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) return {}; + + switch (role) { + case Qt::DisplayRole: + return (*playlist)[index.row()]; + default: + return {}; + } +} diff --git a/playlist/playlistmodel.hpp b/playlist/playlistmodel.hpp new file mode 100644 index 0000000..e5cedfe --- /dev/null +++ b/playlist/playlistmodel.hpp @@ -0,0 +1,28 @@ +/* ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2023 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#pragma once + +#include <QAbstractItemModel> + +class Playlist; +class PlaylistModel : public QAbstractItemModel { +public: + explicit PlaylistModel(Playlist *parent); + ~PlaylistModel() = default; + + QModelIndex index(int row, int column, const QModelIndex &parent) const; + QModelIndex parent(const QModelIndex &child) const; + int rowCount(const QModelIndex &parent) const; + int columnCount(const QModelIndex &parent) const; + QVariant data(const QModelIndex &index, int role) const; + +private slots: + void beginInsertItems(int start, int end) { beginInsertRows(QModelIndex(), start, end); } + void endInsertItems() { endInsertRows(); } + +private: + Playlist *playlist; +}; |