2012-03-06 22:24:41 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-03-06 22:24:41 +01:00
|
|
|
Clementine 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.
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-03-06 22:24:41 +01:00
|
|
|
Clementine 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.
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-03-06 22:24:41 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "standarditemiconloader.h"
|
|
|
|
#include "covers/albumcoverloader.h"
|
|
|
|
|
|
|
|
#include <QAbstractItemModel>
|
|
|
|
#include <QSet>
|
|
|
|
#include <QStandardItem>
|
|
|
|
|
|
|
|
StandardItemIconLoader::StandardItemIconLoader(AlbumCoverLoader* cover_loader,
|
|
|
|
QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(parent), cover_loader_(cover_loader), model_(nullptr) {
|
2012-03-06 22:24:41 +01:00
|
|
|
cover_options_.desired_height_ = 16;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(cover_loader_, SIGNAL(ImageLoaded(quint64, QImage)),
|
|
|
|
SLOT(ImageLoaded(quint64, QImage)));
|
2012-03-06 22:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardItemIconLoader::SetModel(QAbstractItemModel* model) {
|
|
|
|
if (model_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
disconnect(model_, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
|
|
|
|
this, SLOT(RowsAboutToBeRemoved(QModelIndex, int, int)));
|
2012-03-06 22:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
model_ = model;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(model_, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
|
|
|
|
SLOT(RowsAboutToBeRemoved(QModelIndex, int, int)));
|
|
|
|
connect(model_, SIGNAL(modelAboutToBeReset()), SLOT(ModelReset()));
|
2012-03-06 22:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardItemIconLoader::LoadIcon(const QString& art_automatic,
|
|
|
|
const QString& art_manual,
|
|
|
|
QStandardItem* for_item) {
|
2014-02-07 16:34:20 +01:00
|
|
|
const quint64 id =
|
|
|
|
cover_loader_->LoadImageAsync(cover_options_, art_automatic, art_manual);
|
2012-03-06 22:24:41 +01:00
|
|
|
pending_covers_[id] = for_item;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void StandardItemIconLoader::LoadIcon(const Song& song,
|
|
|
|
QStandardItem* for_item) {
|
2012-03-06 22:24:41 +01:00
|
|
|
const quint64 id = cover_loader_->LoadImageAsync(cover_options_, song);
|
|
|
|
pending_covers_[id] = for_item;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void StandardItemIconLoader::RowsAboutToBeRemoved(const QModelIndex& parent,
|
|
|
|
int begin, int end) {
|
|
|
|
for (QMap<quint64, QStandardItem*>::iterator it = pending_covers_.begin();
|
|
|
|
it != pending_covers_.end();) {
|
2012-03-06 22:24:41 +01:00
|
|
|
const QStandardItem* item = it.value();
|
|
|
|
const QStandardItem* item_parent = item->parent();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (item_parent && item_parent->index() == parent &&
|
|
|
|
item->index().row() >= begin && item->index().row() <= end) {
|
2012-03-06 22:24:41 +01:00
|
|
|
cover_loader_->CancelTask(it.key());
|
|
|
|
it = pending_covers_.erase(it);
|
|
|
|
} else {
|
2014-02-07 16:34:20 +01:00
|
|
|
++it;
|
2012-03-06 22:24:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StandardItemIconLoader::ModelReset() {
|
|
|
|
cover_loader_->CancelTasks(QSet<quint64>::fromList(pending_covers_.keys()));
|
|
|
|
pending_covers_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StandardItemIconLoader::ImageLoaded(quint64 id, const QImage& image) {
|
|
|
|
QStandardItem* item = pending_covers_.take(id);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!item) return;
|
2012-03-06 22:24:41 +01:00
|
|
|
|
|
|
|
if (!image.isNull()) {
|
|
|
|
item->setIcon(QIcon(QPixmap::fromImage(image)));
|
|
|
|
}
|
|
|
|
}
|