strawberry-audio-player-win.../src/collection/collectionmodel.h

331 lines
12 KiB
C
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2023-05-14 11:34:55 +02:00
* Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#ifndef COLLECTIONMODEL_H
#define COLLECTIONMODEL_H
#include "config.h"
#include <optional>
#include <QtGlobal>
#include <QObject>
2018-02-27 18:06:05 +01:00
#include <QAbstractItemModel>
2020-07-19 19:07:12 +02:00
#include <QFuture>
#include <QDataStream>
#include <QMetaType>
#include <QPair>
#include <QSet>
2020-02-08 03:40:30 +01:00
#include <QList>
#include <QMap>
#include <QVariant>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QImage>
2018-02-27 18:06:05 +01:00
#include <QIcon>
#include <QPixmap>
#include <QNetworkDiskCache>
2018-02-27 18:06:05 +01:00
#include "core/shared_ptr.h"
2018-02-27 18:06:05 +01:00
#include "core/simpletreemodel.h"
#include "core/song.h"
2022-05-13 18:15:04 +02:00
#include "core/sqlrow.h"
2023-05-14 11:34:55 +02:00
#include "covermanager/albumcoverloaderoptions.h"
2023-07-21 05:25:57 +02:00
#include "covermanager/albumcoverloaderresult.h"
#include "collectionfilteroptions.h"
#include "collectionqueryoptions.h"
#include "collectionitem.h"
2018-02-27 18:06:05 +01:00
2020-02-08 03:40:30 +01:00
class QSettings;
2018-02-27 18:06:05 +01:00
class Application;
class CollectionBackend;
class CollectionDirectoryModel;
2018-02-27 18:06:05 +01:00
class CollectionModel : public SimpleTreeModel<CollectionItem> {
Q_OBJECT
2019-09-15 20:27:32 +02:00
2018-02-27 18:06:05 +01:00
public:
explicit CollectionModel(SharedPtr<CollectionBackend> backend, Application *app, QObject *parent = nullptr);
2020-06-15 21:55:05 +02:00
~CollectionModel() override;
2018-02-27 18:06:05 +01:00
static const int kPrettyCoverSize;
static const char *kPixmapDiskCacheDir;
2018-02-27 18:06:05 +01:00
enum Role {
Role_Type = Qt::UserRole + 1,
Role_ContainerType,
Role_SortText,
Role_Key,
Role_Artist,
Role_IsDivider,
Role_Editable,
LastRole
};
// These values get saved in QSettings - don't change them
2023-02-18 14:09:27 +01:00
enum class GroupBy {
None = 0,
AlbumArtist = 1,
Artist = 2,
Album = 3,
AlbumDisc = 4,
YearAlbum = 5,
YearAlbumDisc = 6,
OriginalYearAlbum = 7,
OriginalYearAlbumDisc = 8,
Disc = 9,
Year = 10,
OriginalYear = 11,
Genre = 12,
Composer = 13,
Performer = 14,
Grouping = 15,
FileType = 16,
Format = 17,
Samplerate = 18,
Bitdepth = 19,
Bitrate = 20,
GroupByCount = 21,
2018-02-27 18:06:05 +01:00
};
2021-06-25 18:06:30 +02:00
Q_ENUM(GroupBy)
2018-02-27 18:06:05 +01:00
struct Grouping {
2023-02-18 14:09:27 +01:00
explicit Grouping(GroupBy f = GroupBy::None, GroupBy s = GroupBy::None, GroupBy t = GroupBy::None)
2018-02-27 18:06:05 +01:00
: first(f), second(s), third(t) {}
GroupBy first;
GroupBy second;
GroupBy third;
2020-05-06 22:26:29 +02:00
const GroupBy &operator[](const int i) const;
GroupBy &operator[](const int i);
2021-06-20 19:04:08 +02:00
bool operator==(const Grouping other) const {
2018-02-27 18:06:05 +01:00
return first == other.first && second == other.second && third == other.third;
}
2021-06-20 19:04:08 +02:00
bool operator!=(const Grouping other) const { return !(*this == other); }
2018-02-27 18:06:05 +01:00
};
struct QueryResult {
QueryResult() : create_va(false) {}
SqlRowList rows;
bool create_va;
};
SharedPtr<CollectionBackend> backend() const { return backend_; }
2018-02-27 18:06:05 +01:00
CollectionDirectoryModel *directory_model() const { return dir_model_; }
// Call before Init()
2020-05-06 22:26:29 +02:00
void set_show_various_artists(const bool show_various_artists) { show_various_artists_ = show_various_artists; }
2018-02-27 18:06:05 +01:00
// Get information about the collection
void GetChildSongs(CollectionItem *item, QList<QUrl> *urls, SongList *songs, QSet<int> *song_ids) const;
SongList GetChildSongs(const QModelIndex &idx) const;
2018-02-27 18:06:05 +01:00
SongList GetChildSongs(const QModelIndexList &indexes) const;
// Might be accurate
int total_song_count() const { return total_song_count_; }
int total_artist_count() const { return total_artist_count_; }
int total_album_count() const { return total_album_count_; }
// QAbstractItemModel
2020-06-15 21:55:05 +02:00
QVariant data(const QModelIndex &idx, const int role = Qt::DisplayRole) const override;
Qt::ItemFlags flags(const QModelIndex &idx) const override;
QStringList mimeTypes() const override;
QMimeData *mimeData(const QModelIndexList &indexes) const override;
bool canFetchMore(const QModelIndex &parent) const override;
2018-02-27 18:06:05 +01:00
// Whether or not to use album cover art, if it exists, in the collection view
2020-05-06 22:26:29 +02:00
void set_pretty_covers(const bool use_pretty_covers);
2018-02-27 18:06:05 +01:00
bool use_pretty_covers() const { return use_pretty_covers_; }
// Whether or not to show letters heading in the collection view
2020-05-06 22:26:29 +02:00
void set_show_dividers(const bool show_dividers);
2018-02-27 18:06:05 +01:00
// Whether to skip articles such as “The” when sorting artist names
void set_sort_skips_articles(const bool sort_skips_articles);
// Reload settings.
void ReloadSettings();
2018-02-27 18:06:05 +01:00
// Utility functions for manipulating text
static QString TextOrUnknown(const QString &text);
2019-08-05 19:17:31 +02:00
static QString PrettyYearAlbum(const int year, const QString &album);
static QString PrettyAlbumDisc(const QString &album, const int disc);
static QString PrettyYearAlbumDisc(const int year, const QString &album, const int disc);
static QString PrettyDisc(const int disc);
2018-02-27 18:06:05 +01:00
static QString SortText(QString text);
2020-06-14 18:58:24 +02:00
static QString SortTextForNumber(const int number);
static QString SortTextForArtist(QString artist, const bool skip_articles);
2018-02-27 18:06:05 +01:00
static QString SortTextForSong(const Song &song);
2020-05-06 22:26:29 +02:00
static QString SortTextForYear(const int year);
static QString SortTextForBitrate(const int bitrate);
2018-02-27 18:06:05 +01:00
quint64 icon_cache_disk_size() { return sIconCache->cacheSize(); }
static bool IsArtistGroupBy(const GroupBy group_by) {
2023-02-18 14:09:27 +01:00
return group_by == CollectionModel::GroupBy::Artist || group_by == CollectionModel::GroupBy::AlbumArtist;
}
2023-02-18 14:09:27 +01:00
static bool IsAlbumGroupBy(const GroupBy group_by) { return group_by == GroupBy::Album || group_by == GroupBy::YearAlbum || group_by == GroupBy::AlbumDisc || group_by == GroupBy::YearAlbumDisc || group_by == GroupBy::OriginalYearAlbum || group_by == GroupBy::OriginalYearAlbumDisc; }
void set_use_lazy_loading(const bool value) { use_lazy_loading_ = value; }
QMap<QString, CollectionItem*> container_nodes(const int i) { return container_nodes_[i]; }
QList<CollectionItem*> song_nodes() const { return song_nodes_.values(); }
int divider_nodes_count() const { return divider_nodes_.count(); }
void ExpandAll(CollectionItem *item = nullptr) const;
2020-05-06 22:35:55 +02:00
2021-06-20 19:04:08 +02:00
const CollectionModel::Grouping GetGroupBy() const { return group_by_; }
void SetGroupBy(const CollectionModel::Grouping g, const std::optional<bool> separate_albums_by_grouping = std::optional<bool>());
2021-03-21 04:38:47 +01:00
static QString ContainerKey(const GroupBy group_by, const bool separate_albums_by_grouping, const Song &song);
2021-07-04 17:34:42 +02:00
signals:
void TotalSongCountUpdated(const int count);
void TotalArtistCountUpdated(const int count);
void TotalAlbumCountUpdated(const int count);
void GroupingChanged(const CollectionModel::Grouping g, const bool separate_albums_by_grouping);
2018-02-27 18:06:05 +01:00
public slots:
void SetFilterMode(CollectionFilterOptions::FilterMode filter_mode);
void SetFilterAge(const int filter_age);
void SetFilterText(const QString &filter_text);
2018-02-27 18:06:05 +01:00
2020-05-06 22:26:29 +02:00
void Init(const bool async = true);
2018-02-27 18:06:05 +01:00
void Reset();
void ResetAsync();
2021-01-26 16:48:04 +01:00
void SongsDiscovered(const SongList &songs);
2018-02-27 18:06:05 +01:00
protected:
2020-06-15 21:55:05 +02:00
void LazyPopulate(CollectionItem *item) override { LazyPopulate(item, true); }
2020-06-14 18:58:24 +02:00
void LazyPopulate(CollectionItem *parent, const bool signal);
2018-02-27 18:06:05 +01:00
private slots:
// From CollectionBackend
void SongsDeleted(const SongList &songs);
void SongsSlightlyChanged(const SongList &songs);
2020-05-06 22:26:29 +02:00
void TotalSongCountUpdatedSlot(const int count);
void TotalArtistCountUpdatedSlot(const int count);
void TotalAlbumCountUpdatedSlot(const int count);
2021-06-22 13:41:38 +02:00
static void ClearDiskCache();
2018-02-27 18:06:05 +01:00
// Called after ResetAsync
2021-01-30 21:53:53 +01:00
void ResetAsyncQueryFinished();
2018-02-27 18:06:05 +01:00
void AlbumCoverLoaded(const quint64 id, const AlbumCoverLoaderResult &result);
2018-02-27 18:06:05 +01:00
private:
// Provides some optimizations for loading the list of items in the root.
// This gets called a lot when filtering the playlist, so it's nice to be able to do it in a background thread.
CollectionQueryOptions PrepareQuery(CollectionItem *parent);
QueryResult RunQuery(const CollectionFilterOptions &filter_options = CollectionFilterOptions(), const CollectionQueryOptions &query_options = CollectionQueryOptions());
2020-05-06 22:26:29 +02:00
void PostQuery(CollectionItem *parent, const QueryResult &result, const bool signal);
2018-02-27 18:06:05 +01:00
bool HasCompilations(const QSqlDatabase &db, const CollectionFilterOptions &filter_options, const CollectionQueryOptions &query_options);
2018-02-27 18:06:05 +01:00
void Clear();
2018-02-27 18:06:05 +01:00
void BeginReset();
// Functions for working with queries and creating items.
// When the model is reset or when a node is lazy-loaded the Collection constructs a database query to populate the items.
// Filters are added for each parent item, restricting the songs returned to a particular album or artist for example.
static void SetQueryColumnSpec(const GroupBy group_by, const bool separate_albums_by_grouping, CollectionQueryOptions *query_options);
static void AddQueryWhere(const GroupBy group_by, const bool separate_albums_by_grouping, CollectionItem *item, CollectionQueryOptions *query_options);
2018-02-27 18:06:05 +01:00
// Items can be created either from a query that's been run to populate a node, or by a spontaneous SongsDiscovered emission from the backend.
CollectionItem *ItemFromQuery(const GroupBy group_by, const bool separate_albums_by_grouping, const bool signal, const bool create_divider, CollectionItem *parent, const SqlRow &row, const int container_level);
CollectionItem *ItemFromSong(const GroupBy group_by, const bool separate_albums_by_grouping, const bool signal, const bool create_divider, CollectionItem *parent, const Song &s, const int container_level);
2018-02-27 18:06:05 +01:00
// The "Various Artists" node is an annoying special case.
2020-05-06 22:26:29 +02:00
CollectionItem *CreateCompilationArtistNode(const bool signal, CollectionItem *parent);
2018-02-27 18:06:05 +01:00
// Helpers for ItemFromQuery and ItemFromSong
CollectionItem *InitItem(const GroupBy group_by, const bool signal, CollectionItem *parent, const int container_level);
void FinishItem(const GroupBy group_by, const bool signal, const bool create_divider, CollectionItem *parent, CollectionItem *item);
2018-02-27 18:06:05 +01:00
static QString DividerKey(const GroupBy group_by, CollectionItem *item);
static QString DividerDisplayText(const GroupBy group_by, const QString &key);
2018-02-27 18:06:05 +01:00
// Helpers
static bool IsCompilationArtistNode(const CollectionItem *node) { return node == node->parent->compilation_artist_node_; }
QString AlbumIconPixmapCacheKey(const QModelIndex &idx) const;
QUrl AlbumIconPixmapDiskCacheKey(const QString &cache_key) const;
QVariant AlbumIcon(const QModelIndex &idx);
2020-05-06 22:26:29 +02:00
QVariant data(const CollectionItem *item, const int role) const;
2018-02-27 18:06:05 +01:00
bool CompareItems(const CollectionItem *a, const CollectionItem *b) const;
2021-07-11 09:49:38 +02:00
static qint64 MaximumCacheSize(QSettings *s, const char *size_id, const char *size_unit_id, const qint64 cache_size_default);
2018-02-27 18:06:05 +01:00
private:
SharedPtr<CollectionBackend> backend_;
2018-02-27 18:06:05 +01:00
Application *app_;
CollectionDirectoryModel *dir_model_;
bool show_various_artists_;
bool sort_skips_articles_;
2018-02-27 18:06:05 +01:00
int total_song_count_;
int total_artist_count_;
int total_album_count_;
CollectionFilterOptions filter_options_;
2018-02-27 18:06:05 +01:00
Grouping group_by_;
bool separate_albums_by_grouping_;
2018-02-27 18:06:05 +01:00
// Keyed on database ID
QMap<int, CollectionItem*> song_nodes_;
// Keyed on whatever the key is for that level - artist, album, year, etc.
QMap<QString, CollectionItem*> container_nodes_[3];
// Keyed on a letter, a year, a century, etc.
QMap<QString, CollectionItem*> divider_nodes_;
QIcon artist_icon_;
QIcon album_icon_;
// Used as a generic icon to show when no cover art is found, fixed to the same size as the artwork (32x32)
2018-02-27 18:06:05 +01:00
QPixmap no_cover_icon_;
static QNetworkDiskCache *sIconCache;
2018-02-27 18:06:05 +01:00
int init_task_id_;
bool use_pretty_covers_;
bool show_dividers_;
bool use_disk_cache_;
bool use_lazy_loading_;
2018-02-27 18:06:05 +01:00
2023-05-14 11:34:55 +02:00
AlbumCoverLoaderOptions::Types cover_types_;
2018-02-27 18:06:05 +01:00
2022-10-13 22:39:31 +02:00
using ItemAndCacheKey = QPair<CollectionItem*, QString>;
2018-02-27 18:06:05 +01:00
QMap<quint64, ItemAndCacheKey> pending_art_;
QSet<QString> pending_cache_keys_;
};
2019-09-15 20:27:32 +02:00
Q_DECLARE_METATYPE(CollectionModel::Grouping)
2018-02-27 18:06:05 +01:00
2021-06-20 19:04:08 +02:00
QDataStream &operator<<(QDataStream &s, const CollectionModel::Grouping g);
2018-02-27 18:06:05 +01:00
QDataStream &operator>>(QDataStream &s, CollectionModel::Grouping &g);
#endif // COLLECTIONMODEL_H