2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2012, David Sansome <me@davidsansome.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QObject>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QAbstractItemModel>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QMap>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QSet>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QString>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QIcon>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
#include "covermanager/albumcoverloader.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "standarditemiconloader.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
StandardItemIconLoader::StandardItemIconLoader(AlbumCoverLoader *cover_loader, QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
cover_loader_(cover_loader),
|
2019-03-22 23:23:50 +01:00
|
|
|
model_(nullptr) {
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
cover_options_.desired_height_ = 16;
|
|
|
|
|
2019-07-07 21:14:24 +02:00
|
|
|
connect(cover_loader_, SIGNAL(ImageLoaded(quint64, QUrl, QImage)), SLOT(ImageLoaded(quint64, QUrl, QImage)));
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardItemIconLoader::SetModel(QAbstractItemModel *model) {
|
|
|
|
|
|
|
|
if (model_) {
|
2018-05-01 00:41:33 +02:00
|
|
|
disconnect(model_, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), this, SLOT(RowsAboutToBeRemoved(QModelIndex, int, int)));
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
model_ = model;
|
|
|
|
|
|
|
|
connect(model_, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(RowsAboutToBeRemoved(QModelIndex,int,int)));
|
|
|
|
connect(model_, SIGNAL(modelAboutToBeReset()), SLOT(ModelReset()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-07 21:14:24 +02:00
|
|
|
void StandardItemIconLoader::LoadIcon(const QUrl &art_automatic, const QUrl &art_manual, QStandardItem *for_item) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
const quint64 id = cover_loader_->LoadImageAsync(cover_options_, art_automatic, art_manual);
|
|
|
|
pending_covers_[id] = for_item;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
void StandardItemIconLoader::LoadIcon(const Song &song, QStandardItem *for_item) {
|
2018-02-27 18:06:05 +01:00
|
|
|
const quint64 id = cover_loader_->LoadImageAsync(cover_options_, song);
|
|
|
|
pending_covers_[id] = for_item;
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
void StandardItemIconLoader::RowsAboutToBeRemoved(const QModelIndex &parent, int begin, int end) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
for (QMap<quint64, QStandardItem*>::iterator it = pending_covers_.begin() ; it != pending_covers_.end() ; ) {
|
|
|
|
const QStandardItem *item = it.value();
|
|
|
|
const QStandardItem *item_parent = item->parent();
|
|
|
|
|
|
|
|
if (item_parent && item_parent->index() == parent && item->index().row() >= begin && item->index().row() <= end) {
|
|
|
|
cover_loader_->CancelTask(it.key());
|
|
|
|
it = pending_covers_.erase(it);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++ it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void StandardItemIconLoader::ModelReset() {
|
|
|
|
|
2019-12-21 18:22:18 +01:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
|
2019-12-30 02:28:54 +01:00
|
|
|
cover_loader_->CancelTasks(QSet<quint64>(pending_covers_.keyBegin(), pending_covers_.keyEnd()));
|
2019-12-21 18:22:18 +01:00
|
|
|
#else
|
2018-02-27 18:06:05 +01:00
|
|
|
cover_loader_->CancelTasks(QSet<quint64>::fromList(pending_covers_.keys()));
|
2019-12-21 18:22:18 +01:00
|
|
|
#endif
|
2018-02-27 18:06:05 +01:00
|
|
|
pending_covers_.clear();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-07 21:14:24 +02:00
|
|
|
void StandardItemIconLoader::ImageLoaded(const quint64 id, const QUrl &cover_url, const QImage &image) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-09-15 20:27:32 +02:00
|
|
|
Q_UNUSED(cover_url);
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
QStandardItem *item = pending_covers_.take(id);
|
|
|
|
if (!item) return;
|
|
|
|
|
|
|
|
if (!image.isNull()) {
|
|
|
|
item->setIcon(QIcon(QPixmap::fromImage(image)));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|