playlist handling

This commit is contained in:
Carl Philipp Klemm 2026-03-04 22:16:20 +01:00
parent c4d2fa3ffa
commit 134e827053
5 changed files with 418 additions and 67 deletions

View file

@ -1,10 +1,13 @@
#include "SongListModel.h"
#include <QApplication>
#include <QTime>
#include <QRandomGenerator>
#include <QDebug>
#include <QFont>
SongListModel::SongListModel(QObject *parent)
: QAbstractListModel(parent)
: QAbstractTableModel(parent),
m_playingIndex(-1)
{
}
@ -15,6 +18,12 @@ int SongListModel::rowCount(const QModelIndex &parent) const
return songList.size();
}
int SongListModel::columnCount(const QModelIndex &parent) const
{
// We have 2 columns: play indicator and song name
return 2;
}
QVariant SongListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= songList.size())
@ -24,13 +33,40 @@ QVariant SongListModel::data(const QModelIndex &index, int role) const
switch (role) {
case Qt::DisplayRole:
// Column 0: Play indicator column
if (index.column() == 0) {
return index.row() == m_playingIndex ? "" : "";
}
// Column 1: Song name
else if (index.column() == 1) {
return song.caption;
}
break;
case Qt::FontRole:
// Make play indicator bold and larger
if (index.column() == 0 && index.row() == m_playingIndex) {
QFont font = QApplication::font();
font.setBold(true);
return font;
}
break;
case Qt::TextAlignmentRole:
// Center align the play indicator
if (index.column() == 0) {
return Qt::AlignCenter;
}
break;
case CaptionRole:
return song.caption;
case LyricsRole:
return song.lyrics;
case IsPlayingRole:
return index.row() == m_playingIndex;
default:
return QVariant();
}
return QVariant();
}
bool SongListModel::setData(const QModelIndex &index, const QVariant &value, int role)
@ -88,6 +124,30 @@ SongItem SongListModel::getSong(int index) const
return SongItem();
}
QVariant SongListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
// Hide headers since we don't need column titles
return QVariant();
}
return QAbstractTableModel::headerData(section, orientation, role);
}
void SongListModel::setPlayingIndex(int index)
{
int oldPlayingIndex = m_playingIndex;
m_playingIndex = index;
// Update both the old and new playing indices to trigger UI updates
if (oldPlayingIndex >= 0 && oldPlayingIndex < songList.size()) {
emit dataChanged(this->index(oldPlayingIndex, 0), this->index(oldPlayingIndex, 0));
}
if (index >= 0 && index < songList.size()) {
emit dataChanged(this->index(index, 0), this->index(index, 0));
}
}
int SongListModel::findNextIndex(int currentIndex, bool shuffle) const
{
if (songList.isEmpty())