Scale artist images in a background thread
This commit is contained in:
parent
47004d0ac3
commit
9d690c1b7e
|
@ -43,7 +43,7 @@ SongInfoBase::SongInfoBase(QWidget* parent)
|
|||
// Add a container widget to the scroll area
|
||||
QWidget* container_widget = new QWidget;
|
||||
container_widget->setLayout(container_);
|
||||
container_widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
|
||||
container_widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
|
||||
container_widget->setBackgroundRole(QPalette::Base);
|
||||
container_->setSizeConstraint(QLayout::SetMinAndMaxSize);
|
||||
container_->setContentsMargins(0, 0, 0, 0);
|
||||
|
@ -52,7 +52,7 @@ SongInfoBase::SongInfoBase(QWidget* parent)
|
|||
scroll_area_->setWidgetResizable(true);
|
||||
|
||||
// Add a spacer to the bottom of the container
|
||||
container_->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::Expanding));
|
||||
container_->addStretch();
|
||||
|
||||
// Set stylesheet
|
||||
QFile stylesheet(":/songinfo.css");
|
||||
|
|
|
@ -23,12 +23,15 @@
|
|||
#include <QDesktopWidget>
|
||||
#include <QDir>
|
||||
#include <QFileDialog>
|
||||
#include <QFuture>
|
||||
#include <QFutureWatcher>
|
||||
#include <QLabel>
|
||||
#include <QMenu>
|
||||
#include <QNetworkReply>
|
||||
#include <QPainter>
|
||||
#include <QScrollArea>
|
||||
#include <QSettings>
|
||||
#include <QtConcurrentRun>
|
||||
|
||||
const int PrettyImage::kTotalHeight = 200;
|
||||
const int PrettyImage::kReflectionHeight = 40;
|
||||
|
@ -58,7 +61,7 @@ void PrettyImage::LazyLoad() {
|
|||
// Start fetching the image
|
||||
QNetworkReply* reply = network_->get(QNetworkRequest(url_));
|
||||
connect(reply, SIGNAL(finished()), SLOT(ImageFetched()));
|
||||
state_ = State_Loading;
|
||||
state_ = State_Fetching;
|
||||
}
|
||||
|
||||
QSize PrettyImage::image_size() const {
|
||||
|
@ -82,17 +85,32 @@ void PrettyImage::ImageFetched() {
|
|||
if (image.isNull()) {
|
||||
deleteLater();
|
||||
} else {
|
||||
state_ = State_Finished;
|
||||
state_ = State_CreatingThumbnail;
|
||||
image_ = image;
|
||||
thumbnail_ = QPixmap::fromImage(image_.scaledToHeight(
|
||||
kImageHeight, Qt::SmoothTransformation));
|
||||
|
||||
updateGeometry();
|
||||
update();
|
||||
emit Loaded();
|
||||
QFuture<QImage> future = QtConcurrent::run(image_,
|
||||
&QImage::scaledToHeight, kImageHeight, Qt::SmoothTransformation);
|
||||
|
||||
QFutureWatcher<QImage>* watcher = new QFutureWatcher<QImage>(this);
|
||||
watcher->setFuture(future);
|
||||
connect(watcher, SIGNAL(finished()), SLOT(ImageScaled()));
|
||||
}
|
||||
}
|
||||
|
||||
void PrettyImage::ImageScaled() {
|
||||
QFutureWatcher<QImage>* watcher = reinterpret_cast<QFutureWatcher<QImage>*>(sender());
|
||||
if (!watcher)
|
||||
return;
|
||||
watcher->deleteLater();
|
||||
|
||||
thumbnail_ = QPixmap::fromImage(watcher->result());
|
||||
state_ = State_Finished;
|
||||
|
||||
updateGeometry();
|
||||
update();
|
||||
emit Loaded();
|
||||
}
|
||||
|
||||
void PrettyImage::paintEvent(QPaintEvent* ) {
|
||||
// Draw at the bottom of our area
|
||||
QRect image_rect(QPoint(0, 0), image_size());
|
||||
|
@ -142,7 +160,8 @@ void PrettyImage::paintEvent(QPaintEvent* ) {
|
|||
void PrettyImage::DrawThumbnail(QPainter* p, const QRect& rect) {
|
||||
switch (state_) {
|
||||
case State_WaitingForLazyLoad:
|
||||
case State_Loading:
|
||||
case State_Fetching:
|
||||
case State_CreatingThumbnail:
|
||||
p->setPen(palette().color(QPalette::Disabled, QPalette::Text));
|
||||
p->drawText(rect, Qt::AlignHCenter | Qt::AlignBottom, tr("Loading..."));
|
||||
break;
|
||||
|
|
|
@ -56,11 +56,13 @@ protected:
|
|||
|
||||
private slots:
|
||||
void ImageFetched();
|
||||
void ImageScaled();
|
||||
|
||||
private:
|
||||
enum State {
|
||||
State_WaitingForLazyLoad,
|
||||
State_Loading,
|
||||
State_Fetching,
|
||||
State_CreatingThumbnail,
|
||||
State_Finished,
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue