2020-04-06 02:47:57 +02:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2022-06-05 04:39:45 +02:00
|
|
|
* Copyright 2020-2022, Jonas Kvinge <jonas@jkvinge.net>
|
2020-04-06 02:47:57 +02:00
|
|
|
*
|
|
|
|
* 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 "config.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QPalette>
|
|
|
|
#include <QBrush>
|
|
|
|
#include <QMovie>
|
|
|
|
#include <QTimeLine>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QSizePolicy>
|
2020-04-25 14:48:43 +02:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QContextMenuEvent>
|
2020-04-06 02:47:57 +02:00
|
|
|
#include <QPaintEvent>
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
#include "core/shared_ptr.h"
|
2022-12-28 03:12:00 +01:00
|
|
|
#include "utilities/imageutils.h"
|
2020-04-06 02:47:57 +02:00
|
|
|
#include "covermanager/albumcoverchoicecontroller.h"
|
|
|
|
|
2020-04-25 14:48:43 +02:00
|
|
|
#include "contextview.h"
|
2020-04-06 02:47:57 +02:00
|
|
|
#include "contextalbum.h"
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
using std::make_unique;
|
|
|
|
using std::make_shared;
|
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
const int ContextAlbum::kFadeTimeLineMs = 1000;
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2021-07-11 07:40:57 +02:00
|
|
|
ContextAlbum::ContextAlbum(QWidget *parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
menu_(new QMenu(this)),
|
|
|
|
context_view_(nullptr),
|
|
|
|
album_cover_choice_controller_(nullptr),
|
|
|
|
downloading_covers_(false),
|
2022-06-05 18:20:44 +02:00
|
|
|
timeline_fade_(new QTimeLine(kFadeTimeLineMs, this)),
|
2021-07-11 07:40:57 +02:00
|
|
|
image_strawberry_(":/pictures/strawberry.png"),
|
|
|
|
image_original_(image_strawberry_),
|
2023-05-14 11:34:55 +02:00
|
|
|
pixmap_current_opacity_(1.0),
|
|
|
|
desired_height_(width()) {
|
2020-04-06 02:47:57 +02:00
|
|
|
|
|
|
|
setObjectName("context-widget-album");
|
|
|
|
|
2022-06-09 00:46:39 +02:00
|
|
|
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
2022-06-06 18:24:00 +02:00
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
QImage image = ImageUtils::ScaleImage(image_strawberry_, QSize(desired_height_, desired_height_), devicePixelRatioF(), true);
|
2022-06-05 18:20:44 +02:00
|
|
|
if (!image.isNull()) {
|
|
|
|
pixmap_current_ = QPixmap::fromImage(image);
|
|
|
|
}
|
2022-06-05 04:39:45 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
timeline_fade_->setDirection(QTimeLine::Forward);
|
|
|
|
QObject::connect(timeline_fade_, &QTimeLine::valueChanged, this, &ContextAlbum::FadeCurrentCover);
|
|
|
|
QObject::connect(timeline_fade_, &QTimeLine::finished, this, &ContextAlbum::FadeCurrentCoverFinished);
|
2020-04-06 02:47:57 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-25 14:48:43 +02:00
|
|
|
void ContextAlbum::Init(ContextView *context_view, AlbumCoverChoiceController *album_cover_choice_controller) {
|
|
|
|
|
|
|
|
context_view_ = context_view;
|
2020-04-06 02:47:57 +02:00
|
|
|
|
|
|
|
album_cover_choice_controller_ = album_cover_choice_controller;
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(album_cover_choice_controller_, &AlbumCoverChoiceController::AutomaticCoverSearchDone, this, &ContextAlbum::AutomaticCoverSearchDone);
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2020-04-25 14:48:43 +02:00
|
|
|
QList<QAction*> cover_actions = album_cover_choice_controller_->GetAllActions();
|
|
|
|
menu_->addActions(cover_actions);
|
|
|
|
menu_->addSeparator();
|
2021-02-26 21:03:51 +01:00
|
|
|
menu_->addAction(album_cover_choice_controller_->search_cover_auto_action());
|
|
|
|
menu_->addSeparator();
|
2020-04-25 14:48:43 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-05 04:39:45 +02:00
|
|
|
QSize ContextAlbum::sizeHint() const {
|
|
|
|
|
2023-06-06 23:18:49 +02:00
|
|
|
return QSize(static_cast<int>(pixmap_current_.width() / devicePixelRatioF()), static_cast<int>(pixmap_current_.height() / devicePixelRatioF()));
|
2022-06-05 04:39:45 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-06 20:54:15 +02:00
|
|
|
void ContextAlbum::paintEvent(QPaintEvent*) {
|
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
QPainter p(this);
|
|
|
|
p.setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
DrawPreviousCovers(&p);
|
|
|
|
DrawImage(&p, pixmap_current_, pixmap_current_opacity_);
|
|
|
|
DrawSpinner(&p);
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContextAlbum::mouseDoubleClickEvent(QMouseEvent *e) {
|
|
|
|
|
|
|
|
// Same behaviour as right-click > Show Fullsize
|
|
|
|
if (image_original_ != image_strawberry_ && e->button() == Qt::LeftButton && context_view_->song_playing().is_valid()) {
|
|
|
|
album_cover_choice_controller_->ShowCover(context_view_->song_playing(), image_original_);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-25 14:48:43 +02:00
|
|
|
void ContextAlbum::contextMenuEvent(QContextMenuEvent *e) {
|
2022-06-05 04:39:45 +02:00
|
|
|
|
|
|
|
if (menu_ && image_original_ != image_strawberry_) {
|
|
|
|
menu_->popup(mapToGlobal(e->pos()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QWidget::contextMenuEvent(e);
|
|
|
|
}
|
|
|
|
|
2020-04-25 14:48:43 +02:00
|
|
|
}
|
|
|
|
|
2022-06-09 00:46:39 +02:00
|
|
|
void ContextAlbum::UpdateWidth(const int new_width) {
|
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
if (new_width != desired_height_) {
|
|
|
|
desired_height_ = new_width;
|
2022-06-09 00:46:39 +02:00
|
|
|
ScaleCover();
|
|
|
|
ScalePreviousCovers();
|
|
|
|
updateGeometry();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
void ContextAlbum::SetImage(QImage image) {
|
2020-04-25 14:48:43 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
if (image.isNull()) {
|
|
|
|
image = image_strawberry_;
|
2020-04-25 14:48:43 +02:00
|
|
|
}
|
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
if (downloading_covers_) {
|
|
|
|
downloading_covers_ = false;
|
|
|
|
spinner_animation_.reset();
|
|
|
|
}
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
QImage image_previous = image_original_;
|
|
|
|
QPixmap pixmap_previous = pixmap_current_;
|
|
|
|
qreal opacity_previous = pixmap_current_opacity_;
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
image_original_ = image;
|
|
|
|
pixmap_current_opacity_ = 0.0;
|
|
|
|
ScaleCover();
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
if (!pixmap_previous.isNull()) {
|
2023-07-21 05:55:24 +02:00
|
|
|
SharedPtr<PreviousCover> previous_cover = make_shared<PreviousCover>();
|
2022-06-05 18:20:44 +02:00
|
|
|
previous_cover->image = image_previous;
|
|
|
|
previous_cover->pixmap = pixmap_previous;
|
|
|
|
previous_cover->opacity = opacity_previous;
|
|
|
|
previous_cover->timeline.reset(new QTimeLine(kFadeTimeLineMs), [](QTimeLine *timeline) { timeline->deleteLater(); });
|
|
|
|
previous_cover->timeline->setDirection(QTimeLine::Backward);
|
|
|
|
previous_cover->timeline->setCurrentTime(timeline_fade_->state() == QTimeLine::Running ? timeline_fade_->currentTime() : kFadeTimeLineMs);
|
2023-07-21 05:55:24 +02:00
|
|
|
QObject::connect(&*previous_cover->timeline, &QTimeLine::valueChanged, this, [this, previous_cover]() { FadePreviousCover(previous_cover); });
|
|
|
|
QObject::connect(&*previous_cover->timeline, &QTimeLine::finished, this, [this, previous_cover]() { FadePreviousCoverFinished(previous_cover); });
|
2022-06-05 18:20:44 +02:00
|
|
|
previous_covers_ << previous_cover;
|
|
|
|
previous_cover->timeline->start();
|
|
|
|
}
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
if (timeline_fade_->state() == QTimeLine::Running) {
|
|
|
|
timeline_fade_->stop();
|
2020-04-06 02:47:57 +02:00
|
|
|
}
|
2022-06-05 18:20:44 +02:00
|
|
|
timeline_fade_->start();
|
2020-04-06 02:47:57 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
void ContextAlbum::DrawImage(QPainter *p, const QPixmap &pixmap, const qreal opacity) {
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2022-06-13 00:23:42 +02:00
|
|
|
if (qFuzzyCompare(opacity, static_cast<qreal>(0.0))) return;
|
2022-06-05 04:39:45 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
p->setOpacity(opacity);
|
2023-06-06 23:18:49 +02:00
|
|
|
p->drawPixmap(0, 0, static_cast<int>(pixmap.width() / pixmap.devicePixelRatioF()), static_cast<int>(pixmap.height() / pixmap.devicePixelRatioF()), pixmap);
|
2022-06-05 18:20:44 +02:00
|
|
|
|
|
|
|
}
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
void ContextAlbum::DrawSpinner(QPainter *p) {
|
|
|
|
|
|
|
|
if (downloading_covers_) {
|
2020-04-06 02:47:57 +02:00
|
|
|
p->drawPixmap(50, 50, 16, 16, spinner_animation_->currentPixmap());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
void ContextAlbum::DrawPreviousCovers(QPainter *p) {
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
for (SharedPtr<PreviousCover> previous_cover : previous_covers_) {
|
2022-06-05 18:20:44 +02:00
|
|
|
DrawImage(p, previous_cover->pixmap, previous_cover->opacity);
|
2020-04-06 02:47:57 +02:00
|
|
|
}
|
2022-06-05 18:20:44 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContextAlbum::FadeCurrentCover(const qreal value) {
|
|
|
|
|
|
|
|
if (value <= pixmap_current_opacity_) return;
|
|
|
|
|
|
|
|
pixmap_current_opacity_ = value;
|
2020-04-06 02:47:57 +02:00
|
|
|
update();
|
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContextAlbum::FadeCurrentCoverFinished() {
|
|
|
|
|
|
|
|
if (image_original_ == image_strawberry_) {
|
2020-04-06 02:47:57 +02:00
|
|
|
emit FadeStopFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
void ContextAlbum::FadePreviousCover(SharedPtr<PreviousCover> previous_cover) {
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
if (previous_cover->timeline->currentValue() >= previous_cover->opacity) return;
|
|
|
|
|
|
|
|
previous_cover->opacity = previous_cover->timeline->currentValue();
|
2020-04-06 02:47:57 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
void ContextAlbum::FadePreviousCoverFinished(SharedPtr<PreviousCover> previous_cover) {
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
previous_covers_.removeAll(previous_cover);
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
}
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
void ContextAlbum::ScaleCover() {
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
const QImage image = ImageUtils::ScaleImage(image_original_, QSize(desired_height_, desired_height_), devicePixelRatioF(), true);
|
2022-06-05 18:20:44 +02:00
|
|
|
if (image.isNull()) {
|
|
|
|
pixmap_current_ = QPixmap();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pixmap_current_ = QPixmap::fromImage(image);
|
|
|
|
}
|
2020-04-06 02:47:57 +02:00
|
|
|
|
2022-06-05 18:20:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContextAlbum::ScalePreviousCovers() {
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
for (SharedPtr<PreviousCover> previous_cover : previous_covers_) {
|
2023-05-14 11:34:55 +02:00
|
|
|
QImage image = ImageUtils::ScaleImage(previous_cover->image, QSize(desired_height_, desired_height_), devicePixelRatioF(), true);
|
2022-06-05 18:20:44 +02:00
|
|
|
if (image.isNull()) {
|
|
|
|
previous_cover->pixmap = QPixmap();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
previous_cover->pixmap = QPixmap::fromImage(image);
|
|
|
|
}
|
2020-04-06 02:47:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContextAlbum::SearchCoverInProgress() {
|
|
|
|
|
|
|
|
downloading_covers_ = true;
|
|
|
|
|
|
|
|
// Show a spinner animation
|
2023-07-21 05:55:24 +02:00
|
|
|
spinner_animation_ = make_unique<QMovie>(":/pictures/spinner.gif", QByteArray(), this);
|
|
|
|
QObject::connect(&*spinner_animation_, &QMovie::updated, this, &ContextAlbum::Update);
|
2020-04-06 02:47:57 +02:00
|
|
|
spinner_animation_->start();
|
|
|
|
update();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContextAlbum::AutomaticCoverSearchDone() {
|
|
|
|
|
|
|
|
downloading_covers_ = false;
|
|
|
|
spinner_animation_.reset();
|
|
|
|
update();
|
|
|
|
|
|
|
|
}
|