2019-04-18 15:03:01 +02:00
|
|
|
/* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QtConcurrentRun>
|
|
|
|
#include <QFuture>
|
2021-01-30 21:53:53 +01:00
|
|
|
#include <QFutureWatcher>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QAbstractItemModel>
|
2019-04-18 15:03:01 +02:00
|
|
|
#include <QSortFilterProxyModel>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QSettings>
|
2019-04-18 15:03:01 +02:00
|
|
|
#include <QItemDelegate>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QList>
|
|
|
|
#include <QVector>
|
|
|
|
#include <QVariant>
|
2019-04-18 15:03:01 +02:00
|
|
|
#include <QByteArray>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QPainter>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QPalette>
|
|
|
|
#include <QRect>
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
#include "core/application.h"
|
|
|
|
#include "playlist/playlist.h"
|
|
|
|
#include "playlist/playlistview.h"
|
|
|
|
|
|
|
|
#include "moodbaritemdelegate.h"
|
|
|
|
#include "moodbarloader.h"
|
|
|
|
#include "moodbarpipeline.h"
|
|
|
|
#include "moodbarrenderer.h"
|
|
|
|
|
|
|
|
#include "settings/moodbarsettingspage.h"
|
|
|
|
|
|
|
|
MoodbarItemDelegate::Data::Data() : state_(State_None) {}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
MoodbarItemDelegate::MoodbarItemDelegate(Application *app, PlaylistView *view, QObject *parent)
|
2019-04-18 15:03:01 +02:00
|
|
|
: QItemDelegate(parent),
|
|
|
|
app_(app),
|
|
|
|
view_(view),
|
2021-01-28 21:43:06 +01:00
|
|
|
enabled_(false),
|
2019-04-18 15:03:01 +02:00
|
|
|
style_(MoodbarRenderer::Style_Normal) {
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(app_, &Application::SettingsChanged, this, &MoodbarItemDelegate::ReloadSettings);
|
2019-04-18 15:03:01 +02:00
|
|
|
ReloadSettings();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoodbarItemDelegate::ReloadSettings() {
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(MoodbarSettingsPage::kSettingsGroup);
|
2021-01-28 21:43:06 +01:00
|
|
|
enabled_ = s.value("enabled", false).toBool();
|
2019-04-18 15:03:01 +02:00
|
|
|
MoodbarRenderer::MoodbarStyle new_style = static_cast<MoodbarRenderer::MoodbarStyle>(s.value("style", MoodbarRenderer::Style_Normal).toInt());
|
|
|
|
s.endGroup();
|
|
|
|
|
2021-01-28 21:43:06 +01:00
|
|
|
if (!enabled_) {
|
|
|
|
data_.clear();
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:03:01 +02:00
|
|
|
if (new_style != style_) {
|
|
|
|
style_ = new_style;
|
|
|
|
ReloadAllColors();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void MoodbarItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &idx) const {
|
2019-04-18 15:03:01 +02:00
|
|
|
|
2021-01-28 21:43:06 +01:00
|
|
|
QPixmap pixmap;
|
|
|
|
|
|
|
|
if (enabled_) {
|
|
|
|
pixmap = const_cast<MoodbarItemDelegate*>(this)->PixmapForIndex(idx, option.rect.size());
|
|
|
|
}
|
2019-04-18 15:03:01 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
drawBackground(painter, option, idx);
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
if (!pixmap.isNull()) {
|
|
|
|
// Make a little border for the moodbar
|
|
|
|
const QRect moodbar_rect(option.rect.adjusted(1, 1, -1, -1));
|
|
|
|
painter->drawPixmap(moodbar_rect, pixmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QPixmap MoodbarItemDelegate::PixmapForIndex(const QModelIndex &idx, const QSize &size) {
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
// Pixmaps are keyed off URL.
|
2021-01-26 16:48:04 +01:00
|
|
|
const QUrl url(idx.sibling(idx.row(), Playlist::Column_Filename).data().toUrl());
|
2019-04-18 15:03:01 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
Data *data = data_[url];
|
2019-04-18 15:03:01 +02:00
|
|
|
if (!data) {
|
|
|
|
data = new Data;
|
|
|
|
data_.insert(url, data);
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
data->indexes_.insert(idx);
|
2019-04-18 15:03:01 +02:00
|
|
|
data->desired_size_ = size;
|
|
|
|
|
|
|
|
switch (data->state_) {
|
|
|
|
case Data::State_CannotLoad:
|
|
|
|
case Data::State_LoadingData:
|
|
|
|
case Data::State_LoadingColors:
|
|
|
|
case Data::State_LoadingImage:
|
|
|
|
return data->pixmap_;
|
|
|
|
|
|
|
|
case Data::State_Loaded:
|
|
|
|
// Is the pixmap the right size?
|
|
|
|
if (data->pixmap_.size() != size) {
|
|
|
|
StartLoadingImage(url, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data->pixmap_;
|
|
|
|
|
|
|
|
case Data::State_None:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to start loading the data from scratch.
|
|
|
|
StartLoadingData(url, data);
|
|
|
|
|
|
|
|
return QPixmap();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void MoodbarItemDelegate::StartLoadingData(const QUrl &url, Data *data) {
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
data->state_ = Data::State_LoadingData;
|
|
|
|
|
|
|
|
// Load a mood file for this song and generate some colors from it
|
|
|
|
QByteArray bytes;
|
2021-01-26 16:48:04 +01:00
|
|
|
MoodbarPipeline *pipeline = nullptr;
|
2019-04-18 15:03:01 +02:00
|
|
|
switch (app_->moodbar_loader()->Load(url, &bytes, &pipeline)) {
|
|
|
|
case MoodbarLoader::CannotLoad:
|
|
|
|
data->state_ = Data::State_CannotLoad;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MoodbarLoader::Loaded:
|
|
|
|
// We got the data immediately.
|
|
|
|
StartLoadingColors(url, bytes, data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MoodbarLoader::WillLoadAsync:
|
|
|
|
// Maybe in a little while.
|
2021-01-29 18:47:50 +01:00
|
|
|
QObject::connect(pipeline, &MoodbarPipeline::Finished, this, [this, url, pipeline]() { DataLoaded(url, pipeline); });
|
2019-04-18 15:03:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
bool MoodbarItemDelegate::RemoveFromCacheIfIndexesInvalid(const QUrl &url, Data *data) {
|
2019-04-18 15:03:01 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
for (const QPersistentModelIndex &idx : data->indexes_) {
|
|
|
|
if (idx.isValid()) {
|
2019-04-18 15:03:01 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data_.remove(url);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoodbarItemDelegate::ReloadAllColors() {
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
for (const QUrl &url : data_.keys()) {
|
|
|
|
Data *data = data_[url];
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
if (data->state_ == Data::State_Loaded) {
|
|
|
|
StartLoadingData(url, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void MoodbarItemDelegate::DataLoaded(const QUrl &url, MoodbarPipeline *pipeline) {
|
2019-04-18 15:03:01 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
Data *data = data_[url];
|
2019-04-18 15:03:01 +02:00
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RemoveFromCacheIfIndexesInvalid(url, data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pipeline->success()) {
|
|
|
|
data->state_ = Data::State_CannotLoad;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the colors next.
|
|
|
|
StartLoadingColors(url, pipeline->data(), data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void MoodbarItemDelegate::StartLoadingColors(const QUrl &url, const QByteArray &bytes, Data *data) {
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
data->state_ = Data::State_LoadingColors;
|
|
|
|
|
|
|
|
QFuture<ColorVector> future = QtConcurrent::run(MoodbarRenderer::Colors, bytes, style_, qApp->palette());
|
2021-01-30 21:53:53 +01:00
|
|
|
QFutureWatcher<ColorVector> *watcher = new QFutureWatcher<ColorVector>();
|
|
|
|
watcher->setFuture(future);
|
2021-01-30 22:42:21 +01:00
|
|
|
QObject::connect(watcher, &QFutureWatcher<ColorVector>::finished, this, [this, watcher, url]() {
|
2021-01-30 21:53:53 +01:00
|
|
|
ColorsLoaded(url, watcher->result());
|
|
|
|
watcher->deleteLater();
|
|
|
|
});
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-30 21:53:53 +01:00
|
|
|
void MoodbarItemDelegate::ColorsLoaded(const QUrl &url, const ColorVector &colors) {
|
2019-04-18 15:03:01 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
Data *data = data_[url];
|
2019-04-18 15:03:01 +02:00
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RemoveFromCacheIfIndexesInvalid(url, data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-30 21:53:53 +01:00
|
|
|
data->colors_ = colors;
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
// Load the image next.
|
|
|
|
StartLoadingImage(url, data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void MoodbarItemDelegate::StartLoadingImage(const QUrl &url, Data *data) {
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
data->state_ = Data::State_LoadingImage;
|
|
|
|
|
|
|
|
QFuture<QImage> future = QtConcurrent::run(MoodbarRenderer::RenderToImage, data->colors_, data->desired_size_);
|
2021-01-30 21:53:53 +01:00
|
|
|
QFutureWatcher<QImage> *watcher = new QFutureWatcher<QImage>();
|
|
|
|
watcher->setFuture(future);
|
|
|
|
QObject::connect(watcher, &QFutureWatcher<QImage>::finished, this, [this, watcher, url]() {
|
|
|
|
ImageLoaded(url, watcher->result());
|
|
|
|
watcher->deleteLater();
|
|
|
|
});
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-30 21:53:53 +01:00
|
|
|
void MoodbarItemDelegate::ImageLoaded(const QUrl &url, const QImage &image) {
|
2019-04-18 15:03:01 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
Data *data = data_[url];
|
2019-04-18 15:03:01 +02:00
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RemoveFromCacheIfIndexesInvalid(url, data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the desired size changed then don't even bother converting the image
|
|
|
|
// to a pixmap, just reload it at the new size.
|
|
|
|
if (!image.isNull() && data->desired_size_ != image.size()) {
|
|
|
|
StartLoadingImage(url, data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->pixmap_ = QPixmap::fromImage(image);
|
|
|
|
data->state_ = Data::State_Loaded;
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
Playlist *playlist = view_->playlist();
|
|
|
|
const QSortFilterProxyModel *filter = playlist->proxy();
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
// Update all the indices with the new pixmap.
|
2021-01-26 16:48:04 +01:00
|
|
|
for (const QPersistentModelIndex &idx : data->indexes_) {
|
|
|
|
if (idx.isValid() && idx.sibling(idx.row(), Playlist::Column_Filename).data().toUrl() == url) {
|
|
|
|
QModelIndex source_index = idx;
|
|
|
|
if (idx.model() == filter) {
|
2019-04-18 15:03:01 +02:00
|
|
|
source_index = filter->mapToSource(source_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source_index.model() != playlist) {
|
|
|
|
// The pixmap was for an index in a different playlist, maybe the user
|
|
|
|
// switched to a different one.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
playlist->MoodbarUpdated(source_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|