strawberry-audio-player-win.../src/widgets/playingwidget.cpp

552 lines
15 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2013-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01: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/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
#include <QtGlobal>
#include <QWidget>
#include <QList>
#include <QByteArray>
#include <QVariant>
#include <QString>
2020-02-09 02:29:35 +01:00
#include <QUrl>
#include <QImage>
#include <QPixmap>
#include <QSize>
2018-02-27 18:06:05 +01:00
#include <QMenu>
#include <QMovie>
#include <QPainter>
#include <QPalette>
#include <QBrush>
2018-02-27 18:06:05 +01:00
#include <QTextDocument>
#include <QTimeLine>
#include <QAction>
#include <QActionGroup>
#include <QSettings>
#include <QtEvents>
2018-02-27 18:06:05 +01:00
#include "core/application.h"
#include "core/imageutils.h"
#include "covermanager/albumcoverchoicecontroller.h"
#include "playingwidget.h"
2018-02-27 18:06:05 +01:00
const char *PlayingWidget::kSettingsGroup = "PlayingWidget";
// Space between the cover and the details in small mode
const int PlayingWidget::kPadding = 2;
2018-03-10 13:02:56 +01:00
// Width of the transparent to black gradient above and below the text in large mode
2018-02-27 18:06:05 +01:00
const int PlayingWidget::kGradientHead = 40;
const int PlayingWidget::kGradientTail = 20;
// Maximum height of the cover in large mode, and offset between the bottom of the cover and bottom of the widget
2018-02-27 18:06:05 +01:00
const int PlayingWidget::kMaxCoverSize = 260;
const int PlayingWidget::kBottomOffset = 0;
// Border for large mode
const int PlayingWidget::kTopBorder = 4;
PlayingWidget::PlayingWidget(QWidget *parent)
: QWidget(parent),
app_(nullptr),
album_cover_choice_controller_(nullptr),
mode_(LargeSongDetails),
2018-02-27 18:06:05 +01:00
menu_(new QMenu(this)),
above_statusbar_action_(nullptr),
2018-02-27 18:06:05 +01:00
fit_cover_width_action_(nullptr),
enabled_(false),
visible_(false),
playing_(false),
2018-02-27 18:06:05 +01:00
active_(false),
small_ideal_height_(0),
2019-07-21 21:28:45 +02:00
total_height_(0),
2018-02-27 18:06:05 +01:00
fit_width_(false),
timeline_show_hide_(new QTimeLine(500, this)),
timeline_fade_(new QTimeLine(1000, this)),
2018-02-27 18:06:05 +01:00
details_(new QTextDocument(this)),
pixmap_previous_track_opacity_(0.0),
2018-02-27 18:06:05 +01:00
downloading_covers_(false) {
SetHeight(0);
2018-02-27 18:06:05 +01:00
// Load settings
QSettings s;
s.beginGroup(kSettingsGroup);
mode_ = Mode(s.value("mode", LargeSongDetails).toInt());
fit_width_ = s.value("fit_cover_width", false).toBool();
s.endGroup();
2018-02-27 18:06:05 +01:00
// Accept drops for setting album art
setAcceptDrops(true);
// Context menu
QActionGroup *mode_group = new QActionGroup(this);
CreateModeAction(SmallSongDetails, tr("Small album cover"), mode_group);
CreateModeAction(LargeSongDetails, tr("Large album cover"), mode_group);
2018-02-27 18:06:05 +01:00
menu_->addActions(mode_group->actions());
fit_cover_width_action_ = menu_->addAction(tr("Fit cover to width"));
fit_cover_width_action_->setCheckable(true);
fit_cover_width_action_->setEnabled(true);
2021-01-26 16:48:04 +01:00
QObject::connect(fit_cover_width_action_, &QAction::toggled, this, &PlayingWidget::FitCoverWidth);
2018-02-27 18:06:05 +01:00
fit_cover_width_action_->setChecked(fit_width_);
menu_->addSeparator();
// Animations
2021-01-26 16:48:04 +01:00
QObject::connect(timeline_show_hide_, &QTimeLine::frameChanged, this, &PlayingWidget::SetHeight);
QObject::connect(timeline_fade_, &QTimeLine::valueChanged, this, &PlayingWidget::FadePreviousTrack);
timeline_fade_->setDirection(QTimeLine::Backward); // 1.0 -> 0.0
2018-02-27 18:06:05 +01:00
details_->setUndoRedoEnabled(false);
2018-02-27 18:06:05 +01:00
// add placeholder text to get the correct height
if (mode_ == LargeSongDetails) {
details_->setDefaultStyleSheet("p { font-size: small; font-weight: bold; }");
2018-02-27 18:06:05 +01:00
details_->setHtml(QString("<p align=center><i></i><br/><br/></p>"));
}
2018-02-27 18:06:05 +01:00
UpdateHeight();
}
void PlayingWidget::Init(Application *app, AlbumCoverChoiceController *album_cover_choice_controller) {
2018-02-27 18:06:05 +01:00
app_ = app;
album_cover_choice_controller_ = album_cover_choice_controller;
album_cover_choice_controller_->Init(app_);
QList<QAction*> cover_actions = album_cover_choice_controller_->GetAllActions();
menu_->addActions(cover_actions);
menu_->addSeparator();
menu_->addAction(album_cover_choice_controller_->search_cover_auto_action());
menu_->addSeparator();
above_statusbar_action_ = menu_->addAction(tr("Show above status bar"));
above_statusbar_action_->setCheckable(true);
2018-09-06 17:37:03 +02:00
QSettings s;
s.beginGroup(kSettingsGroup);
above_statusbar_action_->setChecked(s.value("above_status_bar", false).toBool());
s.endGroup();
2021-01-26 16:48:04 +01:00
QObject::connect(above_statusbar_action_, &QAction::toggled, this, &PlayingWidget::ShowAboveStatusBar);
2021-01-26 16:48:04 +01:00
QObject::connect(album_cover_choice_controller_, &AlbumCoverChoiceController::AutomaticCoverSearchDone, this, &PlayingWidget::AutomaticCoverSearchDone);
2018-02-27 18:06:05 +01:00
}
2021-01-26 16:48:04 +01:00
void PlayingWidget::SetEnabled(const bool enabled) {
if (enabled == enabled_) return;
2019-03-09 17:20:07 +01:00
if (enabled) SetEnabled();
else SetDisabled();
2019-03-09 17:20:07 +01:00
}
void PlayingWidget::SetEnabled() {
2020-05-14 19:29:34 +02:00
if (enabled_) return;
enabled_ = true;
if (active_) {
SetVisible(true);
}
2020-05-14 19:29:34 +02:00
}
2018-02-27 18:06:05 +01:00
void PlayingWidget::SetDisabled() {
2020-05-14 19:29:34 +02:00
if (!enabled_) return;
enabled_ = false;
SetVisible(false);
2020-05-14 19:29:34 +02:00
}
2021-01-26 16:48:04 +01:00
void PlayingWidget::SetVisible(const bool visible) {
if (timeline_show_hide_->state() == QTimeLine::Running) {
2020-09-29 01:07:04 +02:00
if (timeline_show_hide_->direction() == QTimeLine::Backward && enabled_ && active_) {
timeline_show_hide_->toggleDirection();
}
else if (timeline_show_hide_->direction() == QTimeLine::Forward && (!enabled_ || !active_)) {
timeline_show_hide_->toggleDirection();
}
2020-09-30 00:44:40 +02:00
return;
}
if (visible != visible_) {
timeline_show_hide_->setFrameRange(0, total_height_);
timeline_show_hide_->setDirection(visible ? QTimeLine::Forward : QTimeLine::Backward);
timeline_show_hide_->start();
}
2018-02-27 18:06:05 +01:00
}
2021-01-26 16:48:04 +01:00
void PlayingWidget::set_ideal_height(const int height) {
2018-02-27 18:06:05 +01:00
small_ideal_height_ = height;
UpdateHeight();
2018-02-27 18:06:05 +01:00
}
QSize PlayingWidget::sizeHint() const {
return QSize(cover_loader_options_.desired_height_, total_height_);
}
2021-01-26 16:48:04 +01:00
void PlayingWidget::CreateModeAction(const Mode mode, const QString &text, QActionGroup *group) {
2018-02-27 18:06:05 +01:00
QAction *action = new QAction(text, group);
action->setCheckable(true);
2021-01-26 16:48:04 +01:00
QObject::connect(action, &QAction::triggered, [this, mode]() { SetMode(mode); } );
2018-02-27 18:06:05 +01:00
if (mode == mode_) action->setChecked(true);
2018-02-27 18:06:05 +01:00
}
2021-01-26 16:48:04 +01:00
void PlayingWidget::SetMode(const int mode) {
2018-02-27 18:06:05 +01:00
mode_ = Mode(mode);
2018-02-27 18:06:05 +01:00
2019-04-08 18:46:11 +02:00
fit_cover_width_action_->setEnabled(mode_ != SmallSongDetails);
2018-02-27 18:06:05 +01:00
UpdateHeight();
UpdateDetailsText();
update();
2018-02-27 18:06:05 +01:00
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("mode", mode_);
s.endGroup();
2018-10-02 00:38:52 +02:00
}
2018-02-27 18:06:05 +01:00
2021-01-26 16:48:04 +01:00
void PlayingWidget::FitCoverWidth(const bool fit) {
2018-10-02 00:38:52 +02:00
fit_width_ = fit;
UpdateHeight();
update();
2018-02-27 18:06:05 +01:00
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("fit_cover_width", fit_width_);
2020-05-14 19:29:34 +02:00
s.endGroup();
2018-02-27 18:06:05 +01:00
}
2021-01-26 16:48:04 +01:00
void PlayingWidget::ShowAboveStatusBar(const bool above) {
2020-05-14 19:29:34 +02:00
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("above_status_bar", above);
s.endGroup();
2020-05-14 19:29:34 +02:00
emit ShowAboveStatusBarChanged(above);
}
2018-02-27 18:06:05 +01:00
2020-05-14 19:29:34 +02:00
void PlayingWidget::Playing() {}
void PlayingWidget::Stopped() {
2020-05-14 19:29:34 +02:00
playing_ = false;
active_ = false;
2020-05-14 19:29:34 +02:00
song_playing_ = Song();
song_ = Song();
image_current_ = QImage();
SetVisible(false);
2020-05-14 19:29:34 +02:00
}
2018-02-27 18:06:05 +01:00
void PlayingWidget::Error() {
active_ = false;
2018-02-27 18:06:05 +01:00
}
void PlayingWidget::SongChanged(const Song &song) {
2020-05-14 19:29:34 +02:00
bool changed = (song.artist() != song_playing_.artist() || song.album() != song_playing_.album() || song.title() != song_playing_.title());
playing_ = true;
song_playing_ = song;
song_ = song;
2020-05-14 19:29:34 +02:00
if (changed) UpdateDetailsText();
}
2018-02-27 18:06:05 +01:00
void PlayingWidget::AlbumCoverLoaded(const Song &song, const QImage &image) {
2019-09-15 20:27:32 +02:00
if (!playing_ || song != song_playing_ || image == image_current_) return;
active_ = true;
downloading_covers_ = false;
song_ = song;
image_current_ = image;
2018-02-27 18:06:05 +01:00
SetImage(image);
}
void PlayingWidget::SetImage(const QImage &image) {
if (enabled_ && visible_ && active_) {
2018-02-27 18:06:05 +01:00
// Cache the current pixmap so we can fade between them
QSize psize(size());
if (size().height() <= 0) psize.setHeight(total_height_);
pixmap_previous_track_ = QPixmap(psize);
pixmap_previous_track_.fill(palette().window().color());
pixmap_previous_track_opacity_ = 1.0;
QPainter p(&pixmap_previous_track_);
2018-02-27 18:06:05 +01:00
DrawContents(&p);
p.end();
}
else { pixmap_previous_track_ = QPixmap(); }
2018-02-27 18:06:05 +01:00
image_original_ = image;
2018-02-27 18:06:05 +01:00
UpdateDetailsText();
ScaleCover();
if (enabled_ && active_) {
SetVisible(true);
// Were we waiting for this cover to load before we started fading?
if (!pixmap_previous_track_.isNull()) {
timeline_fade_->stop();
timeline_fade_->start();
}
2018-02-27 18:06:05 +01:00
}
}
void PlayingWidget::ScaleCover() {
2021-03-24 22:30:37 +01:00
QImage image = ImageUtils::ScaleAndPad(image_original_, cover_loader_options_.scale_output_image_, cover_loader_options_.pad_output_image_, cover_loader_options_.desired_height_);
if (image.isNull()) pixmap_cover_ = QPixmap();
else pixmap_cover_ = QPixmap::fromImage(image);
update();
2021-03-24 22:30:37 +01:00
2018-02-27 18:06:05 +01:00
}
void PlayingWidget::SetHeight(int height) {
setMaximumHeight(height);
update();
if (height >= total_height_ - 5) visible_ = true;
if (height <= 5) visible_ = false;
if (timeline_show_hide_->state() == QTimeLine::Running) {
if (timeline_show_hide_->direction() == QTimeLine::Backward && enabled_ && active_) {
timeline_show_hide_->toggleDirection();
}
if (timeline_show_hide_->direction() == QTimeLine::Forward && (!enabled_ || !active_)) {
timeline_show_hide_->toggleDirection();
}
}
2018-10-02 00:38:52 +02:00
2018-02-27 18:06:05 +01:00
}
void PlayingWidget::UpdateHeight() {
2018-02-27 18:06:05 +01:00
switch (mode_) {
case SmallSongDetails:
cover_loader_options_.desired_height_ = small_ideal_height_;
total_height_ = small_ideal_height_;
break;
case LargeSongDetails:
if (fit_width_) cover_loader_options_.desired_height_ = width();
else cover_loader_options_.desired_height_ = qMin(kMaxCoverSize, width());
2021-03-21 18:53:02 +01:00
total_height_ = kTopBorder + cover_loader_options_.desired_height_ + kBottomOffset + static_cast<int>(details_->size().height());
break;
}
// Update the animation settings and resize the widget now if we're visible
timeline_show_hide_->setFrameRange(0, total_height_);
if (visible_ && active_ && timeline_show_hide_->state() != QTimeLine::Running) setMaximumHeight(total_height_);
2018-02-27 18:06:05 +01:00
// Re-scale the current image
if (song_.is_valid()) {
ScaleCover();
}
// Tell Qt we've changed size
updateGeometry();
}
void PlayingWidget::UpdateDetailsText() {
2020-05-14 19:29:34 +02:00
QString html;
details_->setDefaultStyleSheet("p { font-size: small; font-weight: bold; }");
switch (mode_) {
case SmallSongDetails:
details_->setTextWidth(-1);
html += "<p>";
break;
case LargeSongDetails:
details_->setTextWidth(cover_loader_options_.desired_height_);
html += "<p align=center>";
break;
}
html += QString("%1<br/>%2<br/>%3").arg(song_.PrettyTitle().toHtmlEscaped(), song_.artist().toHtmlEscaped(), song_.album().toHtmlEscaped());
html += "</p>";
2020-05-14 19:29:34 +02:00
details_->setHtml(html);
// if something spans multiple lines the height needs to change
if (mode_ == LargeSongDetails) UpdateHeight();
2018-02-27 18:06:05 +01:00
update();
2018-02-27 18:06:05 +01:00
}
void PlayingWidget::paintEvent(QPaintEvent *e) {
2019-09-15 20:27:32 +02:00
Q_UNUSED(e);
2018-02-27 18:06:05 +01:00
QPainter p(this);
DrawContents(&p);
// Draw the previous track's image if we're fading
if (!pixmap_previous_track_.isNull()) {
p.setOpacity(pixmap_previous_track_opacity_);
p.drawPixmap(0, 0, pixmap_previous_track_);
2018-02-27 18:06:05 +01:00
}
2019-09-15 20:27:32 +02:00
2018-02-27 18:06:05 +01:00
}
void PlayingWidget::DrawContents(QPainter *p) {
switch (mode_) {
case SmallSongDetails:
// Draw the cover
p->drawPixmap(0, 0, small_ideal_height_, small_ideal_height_, pixmap_cover_);
2018-02-27 18:06:05 +01:00
if (downloading_covers_) {
p->drawPixmap(small_ideal_height_ - 18, 6, 16, 16, spinner_animation_->currentPixmap());
}
// Draw the details
p->translate(small_ideal_height_ + kPadding, 0);
details_->drawContents(p);
p->translate(-small_ideal_height_ - kPadding, 0);
break;
case LargeSongDetails:
// Work out how high the text is going to be
const int text_height = details_->size().height();
const int cover_size = fit_width_ ? width() : qMin(kMaxCoverSize, width());
const int x_offset = (width() - cover_loader_options_.desired_height_) / 2;
// Draw the cover
p->drawPixmap(x_offset, kTopBorder, cover_size, cover_size, pixmap_cover_);
2018-02-27 18:06:05 +01:00
if (downloading_covers_) {
p->drawPixmap(x_offset + 45, 35, 16, 16, spinner_animation_->currentPixmap());
}
// Draw the text below
if (timeline_show_hide_->state() != QTimeLine::Running) {
p->translate(x_offset, height() - text_height);
details_->drawContents(p);
p->translate(-x_offset, -height() + text_height);
}
2018-02-27 18:06:05 +01:00
break;
}
2018-10-02 00:38:52 +02:00
2018-02-27 18:06:05 +01:00
}
2021-01-26 16:48:04 +01:00
void PlayingWidget::FadePreviousTrack(const qreal value) {
2018-02-27 18:06:05 +01:00
if (!visible_) return;
2018-02-27 18:06:05 +01:00
pixmap_previous_track_opacity_ = value;
if (qFuzzyCompare(pixmap_previous_track_opacity_, qreal(0.0))) {
pixmap_previous_track_ = QPixmap();
2018-02-27 18:06:05 +01:00
}
update();
}
void PlayingWidget::resizeEvent(QResizeEvent* e) {
2018-10-02 00:38:52 +02:00
//if (visible_ && e->oldSize() != e->size()) {
if (e->oldSize() != e->size()) {
2018-02-27 18:06:05 +01:00
if (mode_ == LargeSongDetails) {
UpdateHeight();
UpdateDetailsText();
}
}
2018-10-02 00:38:52 +02:00
2018-02-27 18:06:05 +01:00
}
2021-01-26 16:48:04 +01:00
void PlayingWidget::contextMenuEvent(QContextMenuEvent *e) {
2018-02-27 18:06:05 +01:00
// show the menu
menu_->popup(mapToGlobal(e->pos()));
}
2021-01-26 16:48:04 +01:00
void PlayingWidget::mouseDoubleClickEvent(QMouseEvent *e) {
2019-11-19 21:20:36 +01:00
2018-02-27 18:06:05 +01:00
// Same behaviour as right-click > Show Fullsize
2019-11-19 21:20:36 +01:00
if (e->button() == Qt::LeftButton && song_.is_valid()) {
album_cover_choice_controller_->ShowCover(song_, image_original_);
}
2018-02-27 18:06:05 +01:00
}
void PlayingWidget::dragEnterEvent(QDragEnterEvent *e) {
2018-02-27 18:06:05 +01:00
if (AlbumCoverChoiceController::CanAcceptDrag(e)) {
e->acceptProposedAction();
}
QWidget::dragEnterEvent(e);
2018-02-27 18:06:05 +01:00
}
void PlayingWidget::dropEvent(QDropEvent *e) {
album_cover_choice_controller_->SaveCover(&song_, e);
2018-02-27 18:06:05 +01:00
QWidget::dropEvent(e);
2018-02-27 18:06:05 +01:00
}
void PlayingWidget::SearchCoverInProgress() {
downloading_covers_ = true;
2018-02-27 18:06:05 +01:00
// Show a spinner animation
spinner_animation_.reset(new QMovie(":/pictures/spinner.gif", QByteArray(), this));
2021-01-26 16:48:04 +01:00
QObject::connect(spinner_animation_.get(), &QMovie::updated, this, &PlayingWidget::Update);
spinner_animation_->start();
update();
2018-02-27 18:06:05 +01:00
}
void PlayingWidget::AutomaticCoverSearchDone() {
downloading_covers_ = false;
spinner_animation_.reset();
update();
}