Use unique keys for all container nodes in collection model

Fixes #539 and probably many more issues with the model.
This commit is contained in:
Jonas Kvinge 2020-09-10 22:09:24 +02:00
parent e3587d369e
commit 10303cb9c0
7 changed files with 657 additions and 390 deletions

View File

@ -213,14 +213,26 @@ QActionGroup *CollectionFilterWidget::CreateGroupByActions(QObject *parent) {
// read saved groupings
QSettings s;
s.beginGroup(CollectionModel::kSavedGroupingsSettingsGroup);
QStringList saved = s.childKeys();
for (int i = 0; i < saved.size(); ++i) {
QByteArray bytes = s.value(saved.at(i)).toByteArray();
QDataStream ds(&bytes, QIODevice::ReadOnly);
CollectionModel::Grouping g;
ds >> g;
ret->addAction(CreateGroupByAction(saved.at(i), parent, g));
int version = s.value("version").toInt();
if (version == 1) {
QStringList saved = s.childKeys();
for (int i = 0; i < saved.size(); ++i) {
if (saved.at(i) == "version") continue;
QByteArray bytes = s.value(saved.at(i)).toByteArray();
QDataStream ds(&bytes, QIODevice::ReadOnly);
CollectionModel::Grouping g;
ds >> g;
ret->addAction(CreateGroupByAction(saved.at(i), parent, g));
}
}
else {
QStringList saved = s.childKeys();
for (int i = 0; i < saved.size(); ++i) {
if (saved.at(i) == "version") continue;
s.remove(saved.at(i));
}
}
s.endGroup();
QAction *sep2 = new QAction(parent);
sep2->setSeparator(true);
@ -301,10 +313,18 @@ void CollectionFilterWidget::SetCollectionModel(CollectionModel *model) {
if (!settings_group_.isEmpty()) {
QSettings s;
s.beginGroup(settings_group_);
model_->SetGroupBy(CollectionModel::Grouping(
CollectionModel::GroupBy(s.value(group_by(1), int(CollectionModel::GroupBy_AlbumArtist)).toInt()),
CollectionModel::GroupBy(s.value(group_by(2), int(CollectionModel::GroupBy_AlbumDisc)).toInt()),
CollectionModel::GroupBy(s.value(group_by(3), int(CollectionModel::GroupBy_None)).toInt())));
int version = 0;
if (s.contains("group_by_version")) version = s.value("group_by_version", 0).toInt();
if (version == 1) {
model_->SetGroupBy(CollectionModel::Grouping(
CollectionModel::GroupBy(s.value(group_by(1), int(CollectionModel::GroupBy_AlbumArtist)).toInt()),
CollectionModel::GroupBy(s.value(group_by(2), int(CollectionModel::GroupBy_AlbumDisc)).toInt()),
CollectionModel::GroupBy(s.value(group_by(3), int(CollectionModel::GroupBy_None)).toInt())));
}
else {
model_->SetGroupBy(CollectionModel::Grouping(CollectionModel::GroupBy_AlbumArtist, CollectionModel::GroupBy_AlbumDisc, CollectionModel::GroupBy_None));
}
s.endGroup();
}
}
@ -327,9 +347,11 @@ void CollectionFilterWidget::GroupingChanged(const CollectionModel::Grouping &g)
// Save the settings
QSettings s;
s.beginGroup(settings_group_);
s.setValue("group_by_version", 1);
s.setValue(group_by(1), int(g[0]));
s.setValue(group_by(2), int(g[1]));
s.setValue(group_by(3), int(g[2]));
s.endGroup();
}
// Now make sure the correct action is checked

File diff suppressed because it is too large Load Diff

View File

@ -85,25 +85,27 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
// These values get saved in QSettings - don't change them
enum GroupBy {
GroupBy_None = 0,
GroupBy_Artist = 1,
GroupBy_Album = 2,
GroupBy_YearAlbum = 3,
GroupBy_Year = 4,
GroupBy_Composer = 5,
GroupBy_Genre = 6,
GroupBy_AlbumArtist = 7,
GroupBy_FileType = 8,
GroupBy_Performer = 9,
GroupBy_Grouping = 10,
GroupBy_Bitrate = 11,
GroupBy_Disc = 12,
GroupBy_OriginalYearAlbum = 13,
GroupBy_OriginalYear = 14,
GroupBy_Samplerate = 15,
GroupBy_Bitdepth = 16,
GroupBy_AlbumArtist = 1,
GroupBy_Artist = 2,
GroupBy_Album = 3,
GroupBy_AlbumDisc = 4,
GroupBy_YearAlbum = 5,
GroupBy_YearAlbumDisc = 6,
GroupBy_OriginalYearAlbum = 7,
GroupBy_OriginalYearAlbumDisc = 8,
GroupBy_Disc = 9,
GroupBy_Year = 10,
GroupBy_OriginalYear = 11,
GroupBy_Genre = 12,
GroupBy_Composer = 13,
GroupBy_Performer = 14,
GroupBy_Grouping = 15,
GroupBy_FileType = 16,
GroupBy_Format = 17,
GroupBy_AlbumDisc = 18,
GroupBy_YearAlbumDisc = 19
GroupBy_Samplerate = 18,
GroupBy_Bitdepth = 19,
GroupBy_Bitrate = 20,
GroupByCount = 21,
};
struct Grouping {
@ -170,6 +172,7 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
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);
static QString SortText(QString text);
static QString SortTextForNumber(const int number);
static QString SortTextForArtist(QString artist);
@ -179,7 +182,18 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
quint64 icon_cache_disk_size() { return sIconCache->cacheSize(); }
static bool IsAlbumGrouping(const GroupBy group_by) { return group_by == GroupBy_Album || group_by == GroupBy_YearAlbum || group_by == GroupBy_OriginalYearAlbum || group_by == GroupBy_AlbumDisc || group_by == GroupBy_YearAlbumDisc; }
static bool IsArtistGroupBy(const GroupBy group_by) {
return group_by == CollectionModel::GroupBy_Artist || group_by == CollectionModel::GroupBy_AlbumArtist;
}
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;
signals:
void TotalSongCountUpdated(const int count);
@ -248,6 +262,7 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
QString DividerDisplayText(const GroupBy type, const QString &key) const;
// Helpers
static bool IsCompilationArtistNode(const CollectionItem *node) { return node == node->parent->compilation_artist_node_; }
QString AlbumIconPixmapCacheKey(const QModelIndex &idx) const;
QVariant AlbumIcon(const QModelIndex &idx);
QVariant data(const CollectionItem *item, const int role) const;
@ -288,6 +303,7 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
bool use_pretty_covers_;
bool show_dividers_;
bool use_disk_cache_;
bool use_lazy_loading_;
AlbumCoverLoaderOptions cover_loader_options_;

View File

@ -74,44 +74,45 @@ SavedGroupingManager::~SavedGroupingManager() {
QString SavedGroupingManager::GroupByToString(const CollectionModel::GroupBy &g) {
switch (g) {
case CollectionModel::GroupBy_None: {
case CollectionModel::GroupBy_None:
case CollectionModel::GroupByCount: {
return tr("None");
}
case CollectionModel::GroupBy_Artist: {
return tr("Artist");
}
case CollectionModel::GroupBy_AlbumArtist: {
return tr("Album artist");
}
case CollectionModel::GroupBy_Artist: {
return tr("Artist");
}
case CollectionModel::GroupBy_Album: {
return tr("Album");
}
case CollectionModel::GroupBy_AlbumDisc: {
return tr("Album - Disc");
}
case CollectionModel::GroupBy_Disc: {
return tr("Disc");
}
case CollectionModel::GroupBy_Format: {
return tr("Format");
}
case CollectionModel::GroupBy_Genre: {
return tr("Genre");
}
case CollectionModel::GroupBy_Year: {
return tr("Year");
}
case CollectionModel::GroupBy_YearAlbum: {
return tr("Year - Album");
}
case CollectionModel::GroupBy_YearAlbumDisc: {
return tr("Year - Album - Disc");
}
case CollectionModel::GroupBy_OriginalYearAlbum: {
return tr("Original year - Album");
}
case CollectionModel::GroupBy_OriginalYearAlbumDisc: {
return tr("Original year - Album - Disc");
}
case CollectionModel::GroupBy_Disc: {
return tr("Disc");
}
case CollectionModel::GroupBy_Year: {
return tr("Year");
}
case CollectionModel::GroupBy_OriginalYear: {
return tr("Original year");
}
case CollectionModel::GroupBy_OriginalYearAlbum: {
return tr("Original year - Album");
case CollectionModel::GroupBy_Genre: {
return tr("Genre");
}
case CollectionModel::GroupBy_Composer: {
return tr("Composer");
@ -125,6 +126,9 @@ QString SavedGroupingManager::GroupByToString(const CollectionModel::GroupBy &g)
case CollectionModel::GroupBy_FileType: {
return tr("File type");
}
case CollectionModel::GroupBy_Format: {
return tr("Format");
}
case CollectionModel::GroupBy_Samplerate: {
return tr("Sample rate");
}
@ -134,9 +138,10 @@ QString SavedGroupingManager::GroupByToString(const CollectionModel::GroupBy &g)
case CollectionModel::GroupBy_Bitrate: {
return tr("Bitrate");
}
default: { return tr("Unknown"); }
}
return tr("Unknown");
}
void SavedGroupingManager::UpdateModel() {
@ -144,21 +149,33 @@ void SavedGroupingManager::UpdateModel() {
model_->setRowCount(0); // don't use clear, it deletes headers
QSettings s;
s.beginGroup(CollectionModel::kSavedGroupingsSettingsGroup);
QStringList saved = s.childKeys();
for (int i = 0; i < saved.size(); ++i) {
QByteArray bytes = s.value(saved.at(i)).toByteArray();
QDataStream ds(&bytes, QIODevice::ReadOnly);
CollectionModel::Grouping g;
ds >> g;
int version = s.value("version").toInt();
if (version == 1) {
QStringList saved = s.childKeys();
for (int i = 0; i < saved.size(); ++i) {
if (saved.at(i) == "version") continue;
QByteArray bytes = s.value(saved.at(i)).toByteArray();
QDataStream ds(&bytes, QIODevice::ReadOnly);
CollectionModel::Grouping g;
ds >> g;
QList<QStandardItem*> list;
list << new QStandardItem(saved.at(i))
<< new QStandardItem(GroupByToString(g.first))
<< new QStandardItem(GroupByToString(g.second))
<< new QStandardItem(GroupByToString(g.third));
QList<QStandardItem*> list;
list << new QStandardItem(saved.at(i))
<< new QStandardItem(GroupByToString(g.first))
<< new QStandardItem(GroupByToString(g.second))
<< new QStandardItem(GroupByToString(g.third));
model_->appendRow(list);
model_->appendRow(list);
}
}
else {
QStringList saved = s.childKeys();
for (int i = 0; i < saved.size(); ++i) {
if (saved.at(i) == "version") continue;
s.remove(saved.at(i));
}
}
s.endGroup();
}
@ -173,6 +190,7 @@ void SavedGroupingManager::Remove() {
s.remove(model_->item(index.row(), 0)->text());
}
}
s.endGroup();
}
UpdateModel();
filter_->UpdateGroupByActions();

View File

@ -32,21 +32,21 @@
template <typename T>
class SimpleTreeItem {
public:
explicit SimpleTreeItem(int _type, SimpleTreeModel<T>* _model); // For the root item
explicit SimpleTreeItem(int _type, const QString& _key, T* _parent = nullptr);
explicit SimpleTreeItem(int _type, T* _parent = nullptr);
explicit SimpleTreeItem(int _type, SimpleTreeModel<T> *_model); // For the root item
explicit SimpleTreeItem(int _type, const QString &_key, T *_parent = nullptr);
explicit SimpleTreeItem(int _type, T *_parent = nullptr);
virtual ~SimpleTreeItem();
void InsertNotify(T* _parent);
void InsertNotify(T *_parent);
void DeleteNotify(int child_row);
void ClearNotify();
void ChangedNotify();
void Delete(int child_row);
T* ChildByKey(const QString& key) const;
T* ChildByKey(const QString &key) const;
QString DisplayText() const { return display_text.isNull() ? key : display_text; }
QString SortText() const { return sort_text.isNull() ? key : sort_text; }
QString DisplayText() const { return display_text; }
QString SortText() const { return sort_text; }
int type;
QString key;
@ -64,7 +64,7 @@ class SimpleTreeItem {
};
template <typename T>
SimpleTreeItem<T>::SimpleTreeItem(int _type, SimpleTreeModel<T>* _model)
SimpleTreeItem<T>::SimpleTreeItem(int _type, SimpleTreeModel<T> *_model)
: type(_type),
row(0),
lazy_loaded(true),
@ -73,7 +73,7 @@ SimpleTreeItem<T>::SimpleTreeItem(int _type, SimpleTreeModel<T>* _model)
model(_model) {}
template <typename T>
SimpleTreeItem<T>::SimpleTreeItem(int _type, const QString& _key, T* _parent)
SimpleTreeItem<T>::SimpleTreeItem(int _type, const QString &_key, T *_parent)
: type(_type),
key(_key),
lazy_loaded(false),
@ -87,7 +87,7 @@ SimpleTreeItem<T>::SimpleTreeItem(int _type, const QString& _key, T* _parent)
}
template <typename T>
SimpleTreeItem<T>::SimpleTreeItem(int _type, T* _parent)
SimpleTreeItem<T>::SimpleTreeItem(int _type, T *_parent)
: type(_type),
lazy_loaded(false),
parent(_parent),
@ -100,7 +100,7 @@ SimpleTreeItem<T>::SimpleTreeItem(int _type, T* _parent)
}
template <typename T>
void SimpleTreeItem<T>::InsertNotify(T* _parent) {
void SimpleTreeItem<T>::InsertNotify(T *_parent) {
parent = _parent;
model = parent->model;
row = parent->children.count();
@ -151,8 +151,8 @@ void SimpleTreeItem<T>::Delete(int child_row) {
}
template <typename T>
T* SimpleTreeItem<T>::ChildByKey(const QString &_key) const {
for (T* child : children) {
T *SimpleTreeItem<T>::ChildByKey(const QString &_key) const {
for (T *child : children) {
if (child->key == _key) return child;
}
return nullptr;

View File

@ -113,6 +113,22 @@ QStandardItem *InternetSearchModel::BuildContainers(const Song &s, QStandardItem
has_artist_icon = true;
break;
case CollectionModel::GroupBy_Album:
display_text = CollectionModel::TextOrUnknown(s.album());
sort_text = CollectionModel::SortTextForArtist(s.album());
unique_tag = s.album_id();
has_album_icon = true;
break;
case CollectionModel::GroupBy_AlbumDisc:{
int disc = qMax(0, s.disc());
display_text = CollectionModel::PrettyAlbumDisc(s.album(), disc);
sort_text = s.album() + CollectionModel::SortTextForNumber(disc);
unique_tag = s.album_id();
has_album_icon = true;
break;
}
case CollectionModel::GroupBy_YearAlbum:{
int year = qMax(0, s.year());
display_text = CollectionModel::PrettyYearAlbum(year, s.album());
@ -122,6 +138,16 @@ QStandardItem *InternetSearchModel::BuildContainers(const Song &s, QStandardItem
break;
}
case CollectionModel::GroupBy_YearAlbumDisc:{
int year = qMax(0, s.year());
int disc = qMax(0, s.disc());
display_text = CollectionModel::PrettyYearAlbumDisc(year, s.album(), disc);
sort_text = CollectionModel::SortTextForNumber(year) + s.album() + CollectionModel::SortTextForNumber(disc);
unique_tag = s.album_id();
has_album_icon = true;
break;
}
case CollectionModel::GroupBy_OriginalYearAlbum:{
int year = qMax(0, s.effective_originalyear());
display_text = CollectionModel::PrettyYearAlbum(year, s.album());
@ -131,20 +157,42 @@ QStandardItem *InternetSearchModel::BuildContainers(const Song &s, QStandardItem
break;
}
case CollectionModel::GroupBy_OriginalYearAlbumDisc:{
const int year = qMax(0, s.effective_originalyear());
const int disc = qMax(0, s.disc());
display_text = CollectionModel::PrettyYearAlbumDisc(year, s.album(), disc);
sort_text = CollectionModel::SortTextForNumber(year) + s.album() + CollectionModel::SortTextForNumber(disc);
unique_tag = s.album_id();
has_album_icon = true;
break;
}
case CollectionModel::GroupBy_Disc:
display_text = CollectionModel::PrettyDisc(s.disc());
sort_text = CollectionModel::SortTextForArtist(display_text);
has_album_icon = true;
break;
case CollectionModel::GroupBy_Year:{
int year = qMax(0, s.year());
const int year = qMax(0, s.year());
display_text = QString::number(year);
sort_text = CollectionModel::SortTextForNumber(year) + " ";
break;
}
case CollectionModel::GroupBy_OriginalYear:{
int year = qMax(0, s.effective_originalyear());
const int year = qMax(0, s.effective_originalyear());
display_text = QString::number(year);
sort_text = CollectionModel::SortTextForNumber(year) + " ";
break;
}
case CollectionModel::GroupBy_Genre:
display_text = CollectionModel::TextOrUnknown(s.genre());
sort_text = CollectionModel::SortTextForArtist(s.genre());
has_album_icon = true;
break;
case CollectionModel::GroupBy_Composer:
display_text = CollectionModel::TextOrUnknown(s.composer());
sort_text = CollectionModel::SortTextForArtist(s.composer());
@ -157,51 +205,17 @@ QStandardItem *InternetSearchModel::BuildContainers(const Song &s, QStandardItem
has_album_icon = true;
break;
case CollectionModel::GroupBy_Disc:
display_text = s.disc();
sort_text = CollectionModel::SortTextForArtist(display_text);
has_album_icon = true;
break;
case CollectionModel::GroupBy_Grouping:
display_text = CollectionModel::TextOrUnknown(s.grouping());
sort_text = CollectionModel::SortTextForArtist(s.grouping());
has_album_icon = true;
break;
case CollectionModel::GroupBy_Genre:
display_text = CollectionModel::TextOrUnknown(s.genre());
sort_text = CollectionModel::SortTextForArtist(s.genre());
has_album_icon = true;
break;
case CollectionModel::GroupBy_Album:
display_text = CollectionModel::TextOrUnknown(s.album());
sort_text = CollectionModel::SortTextForArtist(s.album());
unique_tag = s.album_id();
has_album_icon = true;
break;
case CollectionModel::GroupBy_FileType:
display_text = s.TextForFiletype();
sort_text = display_text;
break;
case CollectionModel::GroupBy_Bitrate:
display_text = QString(s.bitrate(), 1);
sort_text = display_text;
break;
case CollectionModel::GroupBy_Samplerate:
display_text = QString(s.samplerate(), 1);
sort_text = display_text;
break;
case CollectionModel::GroupBy_Bitdepth:
display_text = QString(s.bitdepth(), 1);
sort_text = display_text;
break;
case CollectionModel::GroupBy_Format:
if (s.samplerate() <= 0) {
display_text = s.TextForFiletype();
@ -217,24 +231,23 @@ QStandardItem *InternetSearchModel::BuildContainers(const Song &s, QStandardItem
sort_text = display_text;
break;
case CollectionModel::GroupBy_AlbumDisc:{
int disc = qMax(0, s.disc());
display_text = CollectionModel::PrettyAlbumDisc(s.album(), disc);
sort_text = s.album() + CollectionModel::SortTextForNumber(disc);
unique_tag = s.album_id();
has_album_icon = true;
case CollectionModel::GroupBy_Samplerate:
display_text = QString(s.samplerate(), 1);
sort_text = display_text;
break;
}
case CollectionModel::GroupBy_YearAlbumDisc:{
int year = qMax(0, s.year());
int disc = qMax(0, s.disc());
display_text = CollectionModel::PrettyYearAlbumDisc(year, s.album(), disc);
sort_text = CollectionModel::SortTextForNumber(year) + s.album() + CollectionModel::SortTextForNumber(disc);
unique_tag = s.album_id();
has_album_icon = true;
case CollectionModel::GroupBy_Bitdepth:
display_text = QString(s.bitdepth(), 1);
sort_text = display_text;
break;
}
case CollectionModel::GroupBy_Bitrate:
display_text = QString(s.bitrate(), 1);
sort_text = display_text;
break;
case CollectionModel::GroupBy_None:
case CollectionModel::GroupByCount:
return parent;
}

View File

@ -810,7 +810,7 @@ void InternetSearchView::LazyLoadAlbumCover(const QModelIndex &proxy_index) {
// Is this an album?
const CollectionModel::GroupBy container_type = CollectionModel::GroupBy(proxy_index.data(CollectionModel::Role_ContainerType).toInt());
if (!CollectionModel::IsAlbumGrouping(container_type)) return;
if (!CollectionModel::IsAlbumGroupBy(container_type)) return;
// Mark the item as loading art