From 7749386affe77d3d5299af26e75159331b791a5f Mon Sep 17 00:00:00 2001 From: David Sansome Date: Wed, 30 Dec 2009 04:05:33 +0000 Subject: [PATCH] More columns in the playlist --- TODO | 2 +- src/playlist.cpp | 18 +++++++++++++ src/playlist.h | 9 +++++++ src/playlistview.cpp | 60 ++++++++++++++++++++++++++++++++++++++------ src/playlistview.h | 9 ++++++- 5 files changed, 88 insertions(+), 10 deletions(-) diff --git a/TODO b/TODO index dd624b818..2603b3f26 100644 --- a/TODO +++ b/TODO @@ -6,10 +6,10 @@ - Edit tags in the playlist - Global shortcut keys - Make QSortFilterProxyModel on the library obey hasChildren() -- More columns in playlist Long-term: - iPod +- OSD Windows: - Playlist delegates diff --git a/src/playlist.cpp b/src/playlist.cpp index 2f015c1bb..0b7409fb7 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -41,6 +41,15 @@ QVariant Playlist::headerData(int section, Qt::Orientation, int role) const { case Column_Album: return "Album"; case Column_Length: return "Length"; case Column_Track: return "Track"; + case Column_Disc: return "Disc"; + case Column_Year: return "Year"; + case Column_Genre: return "Genre"; + + case Column_BPM: return "BPM"; + case Column_Bitrate: return "Bit rate"; + case Column_Samplerate: return "Sample rate"; + case Column_Filename: return "File name"; + case Column_Filesize: return "File size"; } return QVariant(); @@ -67,6 +76,15 @@ QVariant Playlist::data(const QModelIndex& index, int role) const { case Column_Album: return song.album(); case Column_Length: return song.length(); case Column_Track: return song.track(); + case Column_Disc: return song.disc(); + case Column_Year: return song.year(); + case Column_Genre: return song.genre(); + + case Column_BPM: return song.bpm(); + case Column_Bitrate: return song.bitrate(); + case Column_Samplerate: return song.samplerate(); + case Column_Filename: return song.filename(); + case Column_Filesize: return song.filesize(); } } diff --git a/src/playlist.h b/src/playlist.h index f6e219fe1..0f51d9a5c 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -23,6 +23,15 @@ class Playlist : public QAbstractListModel { Column_Album, Column_Length, Column_Track, + Column_Disc, + Column_Year, + Column_Genre, + + Column_BPM, + Column_Bitrate, + Column_Samplerate, + Column_Filename, + Column_Filesize, ColumnCount }; diff --git a/src/playlistview.cpp b/src/playlistview.cpp index 66af8e61c..2d23ee23e 100644 --- a/src/playlistview.cpp +++ b/src/playlistview.cpp @@ -24,6 +24,27 @@ PlaylistDelegateBase::PlaylistDelegateBase(QTreeView* view) { } +QString PlaylistDelegateBase::displayText(const QVariant& value, const QLocale&) const { + switch (value.type()) { + case QVariant::Int: { + int v = value.toInt(); + if (v <= 0) + return QString::null; + return QString::number(v); + } + + case QVariant::Double: { + double v = value.toDouble(); + if (v <= 0) + return QString::null; + return QString::number(v); + } + + default: + return value.toString(); + } +} + void PlaylistDelegateBase::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QStyledItemDelegate::paint(painter, Adjusted(option, index), index); @@ -67,16 +88,10 @@ QStyleOptionViewItemV4 PlaylistDelegateBase::Adjusted(const QStyleOptionViewItem } - -LengthItemDelegate::LengthItemDelegate(QTreeView* view) - : PlaylistDelegateBase(view) -{ -} - QString LengthItemDelegate::displayText(const QVariant& value, const QLocale&) const { bool ok = false; int seconds = value.toInt(&ok); - QString ret = "-"; + QString ret; if (ok && seconds > 0) { int hours = seconds / (60*60); @@ -92,6 +107,25 @@ QString LengthItemDelegate::displayText(const QVariant& value, const QLocale&) c } +QString SizeItemDelegate::displayText(const QVariant& value, const QLocale&) const { + bool ok = false; + int bytes = value.toInt(&ok); + QString ret; + + if (ok && bytes > 0) { + if (bytes <= 1024) + ret.sprintf("%d bytes", bytes); + else if (bytes <= 1024*1024) + ret.sprintf("%.1f KB", float(bytes) / 1024); + else if (bytes <= 1024*1024*1024) + ret.sprintf("%.1f MB", float(bytes) / (1024*1024)); + else + ret.sprintf("%.1f GB", float(bytes) / (1024*1024*1024)); + } + return ret; +} + + PlaylistView::PlaylistView(QWidget *parent) : QTreeView(parent), glow_enabled_(false), @@ -105,6 +139,7 @@ PlaylistView::PlaylistView(QWidget *parent) { setItemDelegate(new PlaylistDelegateBase(this)); setItemDelegateForColumn(Playlist::Column_Length, new LengthItemDelegate(this)); + setItemDelegateForColumn(Playlist::Column_Filesize, new SizeItemDelegate(this)); setHeader(new PlaylistHeader(Qt::Horizontal, this)); header()->setMovable(true); @@ -130,7 +165,16 @@ void PlaylistView::LoadGeometry() { QSettings settings; settings.beginGroup(kSettingsGroup); - header()->restoreState(settings.value("state").toByteArray()); + if (!header()->restoreState(settings.value("state").toByteArray())) { + header()->hideSection(Playlist::Column_Disc); + header()->hideSection(Playlist::Column_Year); + header()->hideSection(Playlist::Column_Genre); + header()->hideSection(Playlist::Column_BPM); + header()->hideSection(Playlist::Column_Bitrate); + header()->hideSection(Playlist::Column_Samplerate); + header()->hideSection(Playlist::Column_Filename); + header()->hideSection(Playlist::Column_Filesize); + } } void PlaylistView::SaveGeometry() { diff --git a/src/playlistview.h b/src/playlistview.h index e6ce58b53..107166975 100644 --- a/src/playlistview.h +++ b/src/playlistview.h @@ -10,6 +10,7 @@ class PlaylistDelegateBase : public QStyledItemDelegate { public: PlaylistDelegateBase(QTreeView* view); void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; + QString displayText(const QVariant& value, const QLocale& locale) const; QStyleOptionViewItemV4 Adjusted(const QStyleOptionViewItem& option, const QModelIndex& index) const; @@ -19,7 +20,13 @@ class PlaylistDelegateBase : public QStyledItemDelegate { class LengthItemDelegate : public PlaylistDelegateBase { public: - LengthItemDelegate(QTreeView* view); + LengthItemDelegate(QTreeView* view) : PlaylistDelegateBase(view) {} + QString displayText(const QVariant& value, const QLocale& locale) const; +}; + +class SizeItemDelegate : public PlaylistDelegateBase { + public: + SizeItemDelegate(QTreeView* view) : PlaylistDelegateBase(view) {} QString displayText(const QVariant& value, const QLocale& locale) const; };