Move code that handles spinning album art to own class
This commit is contained in:
82
src/components/spinningAlbumArt/spinningalbumart.cpp
Normal file
82
src/components/spinningAlbumArt/spinningalbumart.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#include "spinningalbumart.h"
|
||||
#include <QPainter>
|
||||
#include <QPainterPath>
|
||||
|
||||
SpinningAlbumArt::SpinningAlbumArt(const QString& imagePath, int size, QWidget* parent)
|
||||
: QWidget(parent), size_(size)
|
||||
{
|
||||
artLabel_ = new QLabel(this);
|
||||
artLabel_->setAlignment(Qt::AlignCenter);
|
||||
|
||||
setFixedSize(size, size);
|
||||
|
||||
// Load and crop square
|
||||
QPixmap pix(imagePath);
|
||||
QPixmap scaled = pix.scaled(size, size,
|
||||
Qt::KeepAspectRatioByExpanding,
|
||||
Qt::SmoothTransformation);
|
||||
|
||||
int side = qMin(scaled.width(), scaled.height());
|
||||
QPixmap square = scaled.copy(
|
||||
(scaled.width() - side) / 2,
|
||||
(scaled.height() - side) / 2,
|
||||
side,
|
||||
side
|
||||
);
|
||||
|
||||
// Mask into circle
|
||||
discArt_ = createCircularDisc(square, size);
|
||||
artLabel_->setPixmap(discArt_);
|
||||
|
||||
// Setup spin timer
|
||||
timer_ = new QTimer(this);
|
||||
timer_->setInterval(16);
|
||||
connect(timer_, &QTimer::timeout, this, &SpinningAlbumArt::updateRotation);
|
||||
}
|
||||
|
||||
QPixmap SpinningAlbumArt::createCircularDisc(const QPixmap& src, int size)
|
||||
{
|
||||
QPixmap disc(size, size);
|
||||
disc.fill(Qt::transparent);
|
||||
|
||||
QPainter p(&disc);
|
||||
p.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
QPainterPath path;
|
||||
path.addEllipse(0, 0, size, size);
|
||||
p.setClipPath(path);
|
||||
|
||||
p.drawPixmap(0, 0, src);
|
||||
|
||||
return disc;
|
||||
}
|
||||
|
||||
void SpinningAlbumArt::start()
|
||||
{
|
||||
timer_->start();
|
||||
}
|
||||
|
||||
void SpinningAlbumArt::stop()
|
||||
{
|
||||
timer_->stop();
|
||||
}
|
||||
|
||||
void SpinningAlbumArt::updateRotation()
|
||||
{
|
||||
rotationAngle_ += 0.6;
|
||||
|
||||
QPixmap frame(size_, size_);
|
||||
frame.fill(Qt::transparent);
|
||||
|
||||
QPainter p(&frame);
|
||||
p.setRenderHint(QPainter::Antialiasing);
|
||||
p.setRenderHint(QPainter::SmoothPixmapTransform);
|
||||
|
||||
p.translate(size_ / 2, size_ / 2);
|
||||
p.rotate(rotationAngle_);
|
||||
p.translate(-size_ / 2, -size_ / 2);
|
||||
|
||||
p.drawPixmap(0, 0, discArt_);
|
||||
|
||||
artLabel_->setPixmap(frame);
|
||||
}
|
||||
30
src/components/spinningAlbumArt/spinningalbumart.h
Normal file
30
src/components/spinningAlbumArt/spinningalbumart.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
#include <QLabel>
|
||||
#include <QPixmap>
|
||||
#include <QTimer>
|
||||
|
||||
class SpinningAlbumArt : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SpinningAlbumArt(const QString& imagePath, int size = 250, QWidget* parent = nullptr);
|
||||
|
||||
public slots:
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
private slots:
|
||||
void updateRotation();
|
||||
|
||||
private:
|
||||
QPixmap discArt_; // circular-masked album image
|
||||
QLabel* artLabel_; // widget that displays the spinning frame
|
||||
QTimer* timer_; // animation timer
|
||||
qreal rotationAngle_ = 0;
|
||||
int size_; // final diameter of the disc
|
||||
|
||||
QPixmap createCircularDisc(const QPixmap& src, int size);
|
||||
};
|
||||
Reference in New Issue
Block a user