2010-06-12 19:13:01 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-06-12 19:13:01 +02: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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "albumcoversearcher.h"
|
|
|
|
#include "ui_albumcoversearcher.h"
|
2012-02-13 21:44:04 +01:00
|
|
|
#include "core/application.h"
|
2011-06-20 01:15:29 +02:00
|
|
|
#include "core/logging.h"
|
2011-07-26 14:02:59 +02:00
|
|
|
#include "core/utilities.h"
|
2011-04-02 15:34:06 +02:00
|
|
|
#include "covers/albumcoverfetcher.h"
|
|
|
|
#include "covers/albumcoverloader.h"
|
2011-06-26 17:06:59 +02:00
|
|
|
#include "widgets/forcescrollperpixel.h"
|
2011-06-25 18:47:00 +02:00
|
|
|
#include "widgets/groupediconview.h"
|
2010-06-12 19:13:01 +02:00
|
|
|
|
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QListWidgetItem>
|
2011-06-20 01:15:29 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
|
2011-07-26 14:02:59 +02:00
|
|
|
const int SizeOverlayDelegate::kMargin = 4;
|
|
|
|
const int SizeOverlayDelegate::kPaddingX = 3;
|
|
|
|
const int SizeOverlayDelegate::kPaddingY = 1;
|
|
|
|
const qreal SizeOverlayDelegate::kBorder = 5.0;
|
|
|
|
const qreal SizeOverlayDelegate::kFontPointSize = 7.5;
|
|
|
|
const int SizeOverlayDelegate::kBorderAlpha = 200;
|
|
|
|
const int SizeOverlayDelegate::kBackgroundAlpha = 175;
|
|
|
|
|
|
|
|
SizeOverlayDelegate::SizeOverlayDelegate(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QStyledItemDelegate(parent) {}
|
2011-07-26 14:02:59 +02:00
|
|
|
|
|
|
|
void SizeOverlayDelegate::paint(QPainter* painter,
|
|
|
|
const QStyleOptionViewItem& option,
|
|
|
|
const QModelIndex& index) const {
|
|
|
|
QStyledItemDelegate::paint(painter, option, index);
|
|
|
|
|
|
|
|
if (!index.data(AlbumCoverSearcher::Role_ImageFetchFinished).toBool()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QSize size = index.data(AlbumCoverSearcher::Role_ImageSize).toSize();
|
|
|
|
const QString text = Utilities::PrettySize(size);
|
|
|
|
|
|
|
|
QFont font(option.font);
|
|
|
|
font.setPointSizeF(kFontPointSize);
|
|
|
|
font.setBold(true);
|
|
|
|
|
|
|
|
const QFontMetrics metrics(font);
|
|
|
|
|
|
|
|
const int text_width = metrics.width(text);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
const QRect icon_rect(option.rect.left(), option.rect.top(),
|
|
|
|
option.rect.width(), option.rect.width());
|
2011-07-26 14:02:59 +02:00
|
|
|
|
|
|
|
const QRect background_rect(
|
2014-02-07 16:34:20 +01:00
|
|
|
icon_rect.right() - kMargin - text_width - kPaddingX * 2,
|
|
|
|
icon_rect.bottom() - kMargin - metrics.height() - kPaddingY * 2,
|
|
|
|
text_width + kPaddingX * 2, metrics.height() + kPaddingY * 2);
|
|
|
|
const QRect text_rect(background_rect.left() + kPaddingX,
|
|
|
|
background_rect.top() + kPaddingY, text_width,
|
|
|
|
metrics.height());
|
2011-07-26 14:02:59 +02:00
|
|
|
|
|
|
|
painter->save();
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter->setPen(QColor(0, 0, 0, kBorderAlpha));
|
|
|
|
painter->setBrush(QColor(0, 0, 0, kBackgroundAlpha));
|
|
|
|
painter->drawRoundedRect(background_rect, kBorder, kBorder);
|
|
|
|
|
|
|
|
painter->setPen(Qt::white);
|
|
|
|
painter->setFont(font);
|
|
|
|
painter->drawText(text_rect, text);
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
AlbumCoverSearcher::AlbumCoverSearcher(const QIcon& no_cover_icon,
|
|
|
|
Application* app, QWidget* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QDialog(parent),
|
|
|
|
ui_(new Ui_AlbumCoverSearcher),
|
|
|
|
app_(app),
|
|
|
|
model_(new QStandardItemModel(this)),
|
|
|
|
no_cover_icon_(no_cover_icon),
|
|
|
|
fetcher_(nullptr),
|
|
|
|
id_(0) {
|
2011-08-27 15:50:11 +02:00
|
|
|
setWindowModality(Qt::WindowModal);
|
2010-06-12 19:13:01 +02:00
|
|
|
ui_->setupUi(this);
|
|
|
|
ui_->busy->hide();
|
|
|
|
|
2011-06-25 18:47:00 +02:00
|
|
|
ui_->covers->set_header_text(tr("Covers from %1"));
|
2011-07-26 13:17:28 +02:00
|
|
|
ui_->covers->AddSortSpec(Role_ImageDimensions, Qt::DescendingOrder);
|
2011-07-26 14:02:59 +02:00
|
|
|
ui_->covers->setItemDelegate(new SizeOverlayDelegate(this));
|
2011-06-25 18:47:00 +02:00
|
|
|
ui_->covers->setModel(model_);
|
2011-06-20 01:15:29 +02:00
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
options_.scale_output_image_ = false;
|
|
|
|
options_.pad_output_image_ = false;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(app_->album_cover_loader(), SIGNAL(ImageLoaded(quint64, QImage)),
|
|
|
|
SLOT(ImageLoaded(quint64, QImage)));
|
2010-12-26 21:19:12 +01:00
|
|
|
|
2010-06-12 19:13:01 +02:00
|
|
|
connect(ui_->search, SIGNAL(clicked()), SLOT(Search()));
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(ui_->covers, SIGNAL(doubleClicked(QModelIndex)),
|
|
|
|
SLOT(CoverDoubleClicked(QModelIndex)));
|
2011-06-26 17:06:59 +02:00
|
|
|
|
|
|
|
new ForceScrollPerPixel(ui_->covers, this);
|
2011-08-27 15:50:11 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
ui_->buttonBox->button(QDialogButtonBox::Cancel)
|
|
|
|
->setShortcut(QKeySequence::Close);
|
2010-06-12 19:13:01 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
AlbumCoverSearcher::~AlbumCoverSearcher() { delete ui_; }
|
2010-06-12 19:13:01 +02:00
|
|
|
|
2010-12-26 21:19:12 +01:00
|
|
|
void AlbumCoverSearcher::Init(AlbumCoverFetcher* fetcher) {
|
2010-06-12 19:13:01 +02:00
|
|
|
fetcher_ = fetcher;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(fetcher_, SIGNAL(SearchFinished(quint64, CoverSearchResults,
|
|
|
|
CoverSearchStatistics)),
|
|
|
|
SLOT(SearchFinished(quint64, CoverSearchResults)));
|
2010-06-12 19:13:01 +02:00
|
|
|
}
|
|
|
|
|
2011-06-22 21:07:15 +02:00
|
|
|
QImage AlbumCoverSearcher::Exec(const QString& artist, const QString& album) {
|
|
|
|
ui_->artist->setText(artist);
|
|
|
|
ui_->album->setText(album);
|
2012-06-28 11:22:31 +02:00
|
|
|
ui_->artist->setFocus();
|
2011-01-26 00:33:27 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!artist.isEmpty() || !album.isEmpty()) {
|
2011-01-26 00:33:27 +01:00
|
|
|
Search();
|
|
|
|
}
|
2010-06-12 19:13:01 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (exec() == QDialog::Rejected) return QImage();
|
2011-06-20 01:15:29 +02:00
|
|
|
|
|
|
|
QModelIndex selected = ui_->covers->currentIndex();
|
|
|
|
if (!selected.isValid() || !selected.data(Role_ImageFetchFinished).toBool())
|
2010-06-12 19:13:01 +02:00
|
|
|
return QImage();
|
|
|
|
|
2011-06-20 01:15:29 +02:00
|
|
|
QIcon icon = selected.data(Qt::DecorationRole).value<QIcon>();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (icon.cacheKey() == no_cover_icon_.cacheKey()) return QImage();
|
2010-06-12 19:13:01 +02:00
|
|
|
|
|
|
|
return icon.pixmap(icon.availableSizes()[0]).toImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverSearcher::Search() {
|
2011-06-20 01:15:29 +02:00
|
|
|
model_->clear();
|
2011-04-06 17:24:56 +02:00
|
|
|
cover_loading_tasks_.clear();
|
|
|
|
|
2013-03-03 11:11:10 +01:00
|
|
|
if (ui_->album->isEnabled()) {
|
|
|
|
id_ = fetcher_->SearchForCovers(ui_->artist->text(), ui_->album->text());
|
|
|
|
ui_->search->setText(tr("Abort"));
|
|
|
|
ui_->busy->show();
|
|
|
|
ui_->artist->setEnabled(false);
|
|
|
|
ui_->album->setEnabled(false);
|
|
|
|
ui_->covers->setEnabled(false);
|
|
|
|
} else {
|
|
|
|
fetcher_->Clear();
|
|
|
|
ui_->search->setText(tr("Search"));
|
|
|
|
ui_->busy->hide();
|
|
|
|
ui_->search->setEnabled(true);
|
|
|
|
ui_->artist->setEnabled(true);
|
|
|
|
ui_->album->setEnabled(true);
|
|
|
|
ui_->covers->setEnabled(true);
|
|
|
|
}
|
2010-06-12 19:13:01 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AlbumCoverSearcher::SearchFinished(quint64 id,
|
|
|
|
const CoverSearchResults& results) {
|
|
|
|
if (id != id_) return;
|
2010-06-12 19:13:01 +02:00
|
|
|
|
|
|
|
ui_->search->setEnabled(true);
|
2011-06-22 21:07:15 +02:00
|
|
|
ui_->artist->setEnabled(true);
|
|
|
|
ui_->album->setEnabled(true);
|
2010-06-12 19:13:01 +02:00
|
|
|
ui_->covers->setEnabled(true);
|
2013-03-03 11:11:10 +01:00
|
|
|
ui_->search->setText(tr("Search"));
|
2010-06-12 19:13:01 +02:00
|
|
|
id_ = 0;
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const CoverSearchResult& result : results) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (result.image_url.isEmpty()) continue;
|
2010-06-12 19:13:01 +02:00
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
quint64 id = app_->album_cover_loader()->LoadImageAsync(
|
2014-02-07 16:34:20 +01:00
|
|
|
options_, result.image_url.toString(), QString());
|
2010-06-12 19:13:01 +02:00
|
|
|
|
2011-06-20 01:15:29 +02:00
|
|
|
QStandardItem* item = new QStandardItem;
|
2010-12-20 16:46:38 +01:00
|
|
|
item->setIcon(no_cover_icon_);
|
2011-04-02 15:34:06 +02:00
|
|
|
item->setText(result.description);
|
2011-06-20 01:15:29 +02:00
|
|
|
item->setData(result.image_url, Role_ImageURL);
|
|
|
|
item->setData(id, Role_ImageRequestId);
|
|
|
|
item->setData(false, Role_ImageFetchFinished);
|
2014-02-07 16:34:20 +01:00
|
|
|
item->setData(QVariant(Qt::AlignTop | Qt::AlignHCenter),
|
|
|
|
Qt::TextAlignmentRole);
|
2011-06-26 17:07:48 +02:00
|
|
|
item->setData(result.provider, GroupedIconView::Role_Group);
|
2011-06-20 01:15:29 +02:00
|
|
|
|
|
|
|
model_->appendRow(item);
|
2010-06-12 19:13:01 +02:00
|
|
|
|
|
|
|
cover_loading_tasks_[id] = item;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (cover_loading_tasks_.isEmpty()) ui_->busy->hide();
|
2010-06-12 19:13:01 +02:00
|
|
|
}
|
|
|
|
|
2010-12-26 21:19:12 +01:00
|
|
|
void AlbumCoverSearcher::ImageLoaded(quint64 id, const QImage& image) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!cover_loading_tasks_.contains(id)) return;
|
2011-07-26 14:02:59 +02:00
|
|
|
QStandardItem* item = cover_loading_tasks_.take(id);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (cover_loading_tasks_.isEmpty()) ui_->busy->hide();
|
2011-07-26 14:02:59 +02:00
|
|
|
|
|
|
|
if (image.isNull()) {
|
|
|
|
model_->removeRow(item->row());
|
|
|
|
return;
|
|
|
|
}
|
2010-06-12 19:13:01 +02:00
|
|
|
|
2010-12-26 21:19:12 +01:00
|
|
|
QIcon icon(QPixmap::fromImage(image));
|
|
|
|
|
2011-06-25 18:47:00 +02:00
|
|
|
// Create a pixmap that's padded and exactly the right size for the icon.
|
2014-02-07 16:34:20 +01:00
|
|
|
QImage scaled_image(image.scaled(ui_->covers->iconSize(), Qt::KeepAspectRatio,
|
2011-06-25 18:47:00 +02:00
|
|
|
Qt::SmoothTransformation));
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QImage padded_image(ui_->covers->iconSize(),
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
2011-06-25 18:47:00 +02:00
|
|
|
padded_image.fill(0);
|
|
|
|
|
|
|
|
QPainter p(&padded_image);
|
|
|
|
p.drawImage((padded_image.width() - scaled_image.width()) / 2,
|
|
|
|
(padded_image.height() - scaled_image.height()) / 2,
|
|
|
|
scaled_image);
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
icon.addPixmap(QPixmap::fromImage(padded_image));
|
2010-12-26 21:19:12 +01:00
|
|
|
|
2011-06-20 01:15:29 +02:00
|
|
|
item->setData(true, Role_ImageFetchFinished);
|
2011-07-26 13:17:28 +02:00
|
|
|
item->setData(image.width() * image.height(), Role_ImageDimensions);
|
2011-07-26 14:02:59 +02:00
|
|
|
item->setData(image.size(), Role_ImageSize);
|
2010-12-26 21:19:12 +01:00
|
|
|
item->setIcon(icon);
|
2010-06-12 19:13:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverSearcher::keyPressEvent(QKeyEvent* e) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
|
2010-06-12 19:13:01 +02:00
|
|
|
e->ignore();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDialog::keyPressEvent(e);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AlbumCoverSearcher::CoverDoubleClicked(const QModelIndex& index) {
|
|
|
|
if (index.isValid()) accept();
|
2010-06-12 19:13:01 +02:00
|
|
|
}
|