1
0
Fork 0
mirror of https://github.com/HenkKalkwater/harbour-sailfin.git synced 2025-09-05 10:12:46 +00:00

Implement playlist model and UI

This commit is contained in:
Chris Josten 2021-08-21 22:01:13 +02:00
parent e04ec364c1
commit f09593c245
13 changed files with 309 additions and 36 deletions

View file

@ -34,6 +34,7 @@ void registerTypes(const char *uri) {
qmlRegisterUncreatableType<BaseModelLoader>(uri, 1, 0, "BaseModelLoader", "Please use one of its subclasses");
qmlRegisterType<ViewModel::ItemModel>(uri, 1, 0, "ItemModel");
qmlRegisterType<ViewModel::UserModel>(uri, 1, 0, "UserModel");
qmlRegisterUncreatableType<ViewModel::Playlist>(uri, 1, 0, "Playlist", "Available via PlaybackManager");
// Loaders
qmlRegisterUncreatableType<ViewModel::LoaderBase>(uri, 1, 0, "LoaderBase", "Use one of its subclasses");
@ -52,6 +53,7 @@ void registerTypes(const char *uri) {
qmlRegisterUncreatableType<Jellyfin::ViewModel::ModelStatusClass>(uri, 1, 0, "ModelStatus", "Is an enum");
qmlRegisterUncreatableType<Jellyfin::DTO::PlayMethodClass>(uri, 1, 0, "PlayMethod", "Is an enum");
qmlRegisterUncreatableType<Jellyfin::DTO::ItemFieldsClass>(uri, 1, 0, "ItemFields", "Is an enum");
qmlRegisterUncreatableType<Jellyfin::ViewModel::NowPlayingSection>(uri, 1, 0, "NowPlayingSection", "Is an enum");
qRegisterMetaType<Jellyfin::DTO::PlayMethodClass::Value>();
}

View file

@ -28,6 +28,7 @@ Playlist::Playlist(QObject *parent)
m_shuffler(new NoShuffle(this)){}
void Playlist::clearList() {
emit beforeListCleared();
m_currentItem.clear();
m_nextItem.clear();
m_list.clear();
@ -50,13 +51,16 @@ void Playlist::previous() {
}
m_nextItemFromQueue = !m_queue.isEmpty();
m_currentItemFromQueue = false;
emit currentItemChanged();
}
void Playlist::next() {
// Determine the new current item
if (!m_queue.isEmpty()) {
m_currentItem = m_queue.first();
emit beforeItemsRemovedFromQueue(0, 1);
m_queue.removeFirst();
emit itemsRemovedFromQueue();
m_currentItemFromQueue = true;
} else if (!m_list.isEmpty()) {
// The queue is empty
@ -89,16 +93,33 @@ void Playlist::next() {
} else {
m_nextItem.clear();
}
emit currentItemChanged();
}
QSharedPointer<const Item> Playlist::listAt(int index) const {
return m_list.at(index);
if (m_shuffler->canShuffleInAdvance()) {
return m_list.at(m_shuffler->itemAt(index));
} else {
return m_list.at(index);
}
}
QSharedPointer<const Item> Playlist::queueAt(int index) const {
return m_queue.at(index);
}
QSharedPointer<Item> Playlist::currentItem() {
return m_currentItem;
}
int Playlist::currentItemIndexInList() const {
if (m_currentItemFromQueue) {
return -1;
} else {
return m_shuffler->currentItem();
}
}
QSharedPointer<Item> Playlist::nextItem() {
return m_nextItem;
}
@ -107,10 +128,11 @@ void Playlist::appendToList(ViewModel::ItemModel &model) {
int start = m_list.size();
int count = model.size();
m_list.reserve(count);
emit beforeItemsAddedToList(start, count);
for (int i = 0; i < count; i++) {
m_list.append(QSharedPointer<Model::Item>(model.at(i)));
}
emit itemsAddedToList(start, count);
emit itemsAddedToList();
reshuffle();
}
@ -130,6 +152,7 @@ void Playlist::reshuffle() {
}
}
emit listReshuffled();
emit currentItemChanged();
}
void Playlist::play(int index) {
@ -142,6 +165,11 @@ void Playlist::play(int index) {
m_nextItem.clear();
}
}
emit currentItemChanged();
}
bool Playlist::playingFromQueue() const {
return m_currentItemFromQueue;
}
} // NS Model

View file

@ -63,6 +63,10 @@ int NoShuffle::previousIndex() const {
}
}
int NoShuffle::itemAt(int index) const {
return index;
}
int NoShuffle::nextIndex() const {
if (m_repeatAll) {
return (m_index + 1) % m_playlist->listSize();
@ -91,6 +95,10 @@ int ListShuffleBase::nextItem() const {
return m_map[nextIndex()];
}
int ListShuffleBase::itemAt(int index) const {
return m_map[index];
}
// SimpleListShuffle
SimpleListShuffle::SimpleListShuffle(const Playlist *parent)
: ListShuffleBase(parent) {}

View file

@ -40,6 +40,8 @@ PlaybackManager::PlaybackManager(QObject *parent)
m_mediaPlayer(new QMediaPlayer(this)),
m_urlFetcherThread(new ItemUrlFetcherThread(this)),
m_queue(new Model::Playlist(this)) {
m_displayQueue = new ViewModel::Playlist(m_queue, this);
// Set up connections.
m_updateTimer.setInterval(10000); // 10 seconds
m_updateTimer.setSingleShot(false);

View file

@ -21,15 +21,141 @@
namespace Jellyfin {
namespace ViewModel {
/*Playlist::Playlist(ApiClient *apiClient, QObject *parent)
: ItemModel(parent) {
Playlist::Playlist(Model::Playlist *data, QObject *parent)
: QAbstractListModel(parent),
m_data(data) {
connect(this, &QAbstractListModel::rowsInserted, this, &Playlist::onItemsAdded);
connect(this, &QAbstractListModel::rowsRemoved, this, &Playlist::onItemsRemoved);
connect(this, &QAbstractListModel::rowsMoved, this, &Playlist::onItemsMoved);
connect(this, &QAbstractListModel::modelReset, this, &Playlist::onItemsReset);
connect(data, &Model::Playlist::beforeListCleared, this, &Playlist::onBeforePlaylistCleared);
connect(data, &Model::Playlist::listCleared, this, &Playlist::onPlaylistCleared);
connect(data, &Model::Playlist::beforeItemsAddedToList, this, &Playlist::onBeforeItemsAddedToList);
connect(data, &Model::Playlist::beforeItemsAddedToQueue, this, &Playlist::onBeforeItemsAddedToQueue);
connect(data, &Model::Playlist::itemsAddedToList, this, &Playlist::onItemsAddedToList);
connect(data, &Model::Playlist::itemsAddedToQueue, this, &Playlist::onItemsAddedToQueue);
connect(data, &Model::Playlist::beforeItemsRemovedFromList, this, &Playlist::onBeforeItemsRemovedFromList);
connect(data, &Model::Playlist::beforeItemsRemovedFromQueue, this, &Playlist::onBeforeItemsRemovedFromQueue);
connect(data, &Model::Playlist::itemsRemovedFromList, this, &Playlist::onItemsRemovedFromList);
connect(data, &Model::Playlist::itemsRemovedFromQueue, this, &Playlist::onItemsRemovedFromQueue);
connect(data, &Model::Playlist::listReshuffled, this, &Playlist::onReshuffled);
connect(data, &Model::Playlist::currentItemChanged, this, &Playlist::onPlayingItemChanged);
}
int Playlist::rowCount(const QModelIndex &parent) const {
if (!parent.isValid()) return m_data->totalSize();
return 0;
}
QHash<int, QByteArray> Playlist::roleNames() const {
return {
{RoleNames::name, "name"},
{RoleNames::artists, "artists"},
{RoleNames::runTimeTicks, "runTimeTicks"},
{RoleNames::section, "section"},
{RoleNames::playing, "playing"},
};
};
QVariant Playlist::data(const QModelIndex &index, int role) const {
if (!index.isValid()) return QVariant();
if (index.row() >= m_data->totalSize()) return QVariant();
bool inQueue = index.row() < m_data->queueSize();
// Handle the special "section" role
if (role == RoleNames::section) {
if (inQueue) {
return QVariant(NowPlayingSection::Queue);
} else {
return QVariant(NowPlayingSection::NowPlaying);
}
} else if (role == RoleNames::playing) {
return isPlaying(index.row());
}
// Handle the other roles
QSharedPointer<const Model::Item> rowData;
if (inQueue) {
rowData = m_data->queueAt(index.row());
} else {
rowData = m_data->listAt((index.row() - m_data->queueSize()));
}
switch(role) {
case RoleNames::name:
return QVariant(rowData->name());
case RoleNames::artists:
return QVariant(rowData->artists());
case RoleNames::runTimeTicks:
return QVariant(rowData->runTimeTicks().value_or(-1));
default:
return QVariant();
}
return QVariant();
}
void Playlist::onBeforePlaylistCleared() {
onBeforeItemsRemovedFromList(0, m_data->listSize());
}
void Playlist::onPlaylistCleared() {
onItemsRemovedFromList();
}
void Playlist::onBeforeItemsAddedToList(int startIndex, int count) {
int start = startIndex + m_data->queueSize();
this->beginInsertRows(QModelIndex(), start, start + count - 1);
}
void Playlist::onItemsAddedToList() {
this->endInsertRows();
}
void Playlist::onBeforeItemsAddedToQueue(int startIndex, int count) {
this->beginInsertRows(QModelIndex(), startIndex, startIndex + count - 1);
}
void Playlist::onItemsAddedToQueue() {
this->endInsertRows();
}
void Playlist::onBeforeItemsRemovedFromList(int startIndex, int count) {
int start = startIndex + m_data->queueSize();
this->beginRemoveRows(QModelIndex(), start, start + count - 1);
}
void Playlist::onItemsRemovedFromList() {
this->endInsertRows();
}
void Playlist::onBeforeItemsRemovedFromQueue(int startIndex, int count) {
this->beginRemoveRows(QModelIndex(), startIndex, startIndex + count - 1);
}
void Playlist::onItemsRemovedFromQueue() {
this->endRemoveRows();
}
void Playlist::onReshuffled() {
this->beginResetModel();
this->endResetModel();
}
void Playlist::onPlayingItemChanged() {
if (m_lastPlayedRow >= 0 ) {
this->dataChanged(index(m_lastPlayedRow), index(m_lastPlayedRow), {RoleNames::playing});
}
int newIndex = 0;
if (!m_data->playingFromQueue()) {
newIndex = m_data->queueSize() + m_data->currentItemIndexInList();
}
m_lastPlayedRow = newIndex;
emit this->dataChanged(index(newIndex), index(newIndex), {RoleNames::playing});
}
bool Playlist::isPlaying(int index) const {
return (m_data->playingFromQueue() && index == 0)
|| (m_data->currentItemIndexInList() == index - m_data->queueSize());
}
}*/
} // NS ViewModel
} // NS Jellyfin