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

445 lines
12 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/***************************************************************************
amarokslider.cpp - description
-------------------
begin : Dec 15 2003
copyright : (C) 2003 by Mark Kretschmann
email : markey@web.de
copyright : (C) 2005 by Gábor Lehel
email : illissius@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
2019-03-09 16:48:45 +01:00
#include "volumeslider.h"
2018-02-27 18:06:05 +01:00
#include <QApplication>
#include <QWidget>
2021-06-20 19:04:08 +02:00
#include <QHash>
#include <QString>
#include <QStringBuilder>
2018-02-27 18:06:05 +01:00
#include <QImage>
#include <QPainter>
#include <QPainterPath>
#include <QPalette>
#include <QFont>
#include <QBrush>
#include <QPen>
#include <QPoint>
#include <QPolygon>
#include <QRect>
#include <QVector>
2018-02-27 18:06:05 +01:00
#include <QMenu>
#include <QStyle>
#include <QStyleOption>
#include <QTimer>
#include <QAction>
#include <QSlider>
#include <QLinearGradient>
#include <QStyleOptionViewItem>
#include <QFlags>
#include <QtEvents>
2018-02-27 18:06:05 +01:00
2021-10-30 02:21:29 +02:00
SliderSlider::SliderSlider(const Qt::Orientation orientation, QWidget *parent, const int max)
2018-02-27 18:06:05 +01:00
: QSlider(orientation, parent),
2021-06-12 16:06:30 +02:00
sliding_(false),
outside_(false),
prev_value_(0) {
2021-07-11 07:40:57 +02:00
2018-02-27 18:06:05 +01:00
setRange(0, max);
2021-07-11 07:40:57 +02:00
2018-02-27 18:06:05 +01:00
}
2021-02-02 21:08:58 +01:00
void SliderSlider::wheelEvent(QWheelEvent *e) {
2018-02-27 18:06:05 +01:00
if (orientation() == Qt::Vertical) {
// Will be handled by the parent widget
e->ignore();
return;
}
// Position Slider (horizontal)
int step = e->angleDelta().y() * 1500 / 18;
2018-02-27 18:06:05 +01:00
int nval = qBound(minimum(), QSlider::value() + step, maximum());
QSlider::setValue(nval);
emit sliderReleased(value());
}
2019-03-09 16:48:45 +01:00
void SliderSlider::mouseMoveEvent(QMouseEvent *e) {
2018-02-27 18:06:05 +01:00
2021-06-12 16:06:30 +02:00
if (sliding_) {
2018-02-27 18:06:05 +01:00
// feels better, but using set value of 20 is bad of course
QRect rect(-20, -20, width() + 40, height() + 40);
if (orientation() == Qt::Horizontal && !rect.contains(e->pos())) {
2021-06-12 16:06:30 +02:00
if (!outside_) QSlider::setValue(prev_value_);
outside_ = true;
2018-02-27 18:06:05 +01:00
}
else {
2021-06-12 16:06:30 +02:00
outside_ = false;
2018-02-27 18:06:05 +01:00
slideEvent(e);
emit sliderMoved(value());
}
}
2021-06-12 16:06:30 +02:00
else {
2018-02-27 18:06:05 +01:00
QSlider::mouseMoveEvent(e);
2021-06-12 16:06:30 +02:00
}
2018-02-27 18:06:05 +01:00
}
2021-02-02 21:08:58 +01:00
void SliderSlider::slideEvent(QMouseEvent *e) {
2018-02-27 18:06:05 +01:00
QStyleOptionSlider option;
initStyleOption(&option);
QRect sliderRect(style()->subControlRect(QStyle::CC_Slider, &option, QStyle::SC_SliderHandle, this));
QSlider::setValue(
orientation() == Qt::Horizontal
? ((QApplication::layoutDirection() == Qt::RightToLeft)
? QStyle::sliderValueFromPosition(
minimum(), maximum(),
width() - (e->pos().x() - sliderRect.width() / 2),
width() + sliderRect.width(), true)
: QStyle::sliderValueFromPosition(
minimum(), maximum(),
e->pos().x() - sliderRect.width() / 2,
width() - sliderRect.width()))
: QStyle::sliderValueFromPosition(
minimum(), maximum(), e->pos().y() - sliderRect.height() / 2,
height() - sliderRect.height()));
}
2021-02-02 21:08:58 +01:00
void SliderSlider::mousePressEvent(QMouseEvent *e) {
2018-02-27 18:06:05 +01:00
QStyleOptionSlider option;
initStyleOption(&option);
QRect sliderRect(style()->subControlRect(QStyle::CC_Slider, &option, QStyle::SC_SliderHandle, this));
2021-06-12 16:06:30 +02:00
sliding_ = true;
prev_value_ = QSlider::value();
2018-02-27 18:06:05 +01:00
if (!sliderRect.contains(e->pos())) mouseMoveEvent(e);
}
2019-03-09 16:48:45 +01:00
void SliderSlider::mouseReleaseEvent(QMouseEvent*) {
2021-08-23 21:21:08 +02:00
if (!outside_ && QSlider::value() != prev_value_) {
2018-02-27 18:06:05 +01:00
emit sliderReleased(value());
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
2021-06-12 16:06:30 +02:00
sliding_ = false;
outside_ = false;
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
}
2019-03-09 16:48:45 +01:00
void SliderSlider::setValue(int newValue) {
2018-02-27 18:06:05 +01:00
// don't adjust the slider while the user is dragging it!
2021-06-12 16:06:30 +02:00
if (!sliding_ || outside_) {
2018-02-27 18:06:05 +01:00
QSlider::setValue(adjustValue(newValue));
2021-06-12 16:06:30 +02:00
}
else {
prev_value_ = newValue;
}
2018-02-27 18:06:05 +01:00
}
//////////////////////////////////////////////////////////////////////////////////////////
/// CLASS PrettySlider
//////////////////////////////////////////////////////////////////////////////////////////
2021-06-12 16:06:30 +02:00
PrettySlider::PrettySlider(const Qt::Orientation orientation, const SliderMode mode, QWidget *parent, const uint max)
2021-10-30 02:21:29 +02:00
: SliderSlider(orientation, parent, static_cast<int>(max)), m_mode(mode) {
2021-06-12 16:06:30 +02:00
2018-02-27 18:06:05 +01:00
if (m_mode == Pretty) {
setFocusPolicy(Qt::NoFocus);
}
2021-06-12 16:06:30 +02:00
2018-02-27 18:06:05 +01:00
}
2021-02-02 21:08:58 +01:00
void PrettySlider::mousePressEvent(QMouseEvent *e) {
2021-06-12 16:06:30 +02:00
2019-03-09 16:48:45 +01:00
SliderSlider::mousePressEvent(e);
2018-02-27 18:06:05 +01:00
slideEvent(e);
2021-06-12 16:06:30 +02:00
2018-02-27 18:06:05 +01:00
}
2021-02-02 21:08:58 +01:00
void PrettySlider::slideEvent(QMouseEvent *e) {
2021-03-21 04:47:11 +01:00
2021-06-12 16:06:30 +02:00
if (m_mode == Pretty) {
2021-06-21 20:21:40 +02:00
QSlider::setValue(orientation() == Qt::Horizontal ? QStyle::sliderValueFromPosition(minimum(), maximum(), e->pos().x(), width() - 2) : QStyle::sliderValueFromPosition(minimum(), maximum(), e->pos().y(), height() - 2)); // clazy:exclude=skipped-base-method
2021-06-12 16:06:30 +02:00
}
else {
2019-03-09 16:48:45 +01:00
SliderSlider::slideEvent(e);
2021-06-12 16:06:30 +02:00
}
2021-03-21 04:47:11 +01:00
2018-02-27 18:06:05 +01:00
}
namespace Amarok {
namespace ColorScheme {
extern QColor Background;
extern QColor Foreground;
2021-06-20 23:55:02 +02:00
} // namespace ColorScheme
} // namespace Amarok
2018-02-27 18:06:05 +01:00
#if 0
2019-03-09 16:48:45 +01:00
/** these functions aren't required in our fixed size world, but they may become useful one day **/
2018-02-27 18:06:05 +01:00
2019-03-09 16:48:45 +01:00
QSize PrettySlider::minimumSizeHint() const {
2018-02-27 18:06:05 +01:00
return sizeHint();
}
2019-03-09 16:48:45 +01:00
QSize PrettySlider::sizeHint() const {
2018-02-27 18:06:05 +01:00
constPolish();
return (orientation() == Horizontal
? QSize( maxValue(), THICKNESS + MARGIN )
: QSize( THICKNESS + MARGIN, maxValue() )).expandedTo( QApplit ication::globalStrut() );
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////
/// CLASS VolumeSlider
//////////////////////////////////////////////////////////////////////////////////////////
2021-06-12 16:06:30 +02:00
VolumeSlider::VolumeSlider(QWidget *parent, const uint max)
2021-10-30 02:21:29 +02:00
: SliderSlider(Qt::Horizontal, parent, static_cast<int>(max)),
2021-06-20 23:53:28 +02:00
anim_enter_(false),
2021-06-12 16:06:30 +02:00
anim_count_(0),
timer_anim_(new QTimer(this)),
2022-06-13 00:23:42 +02:00
pixmap_inset_(drawVolumePixmap()) {
2021-06-12 16:06:30 +02:00
2018-02-27 18:06:05 +01:00
setFocusPolicy(Qt::NoFocus);
// Store theme colors to check theme change at paintEvent
2021-06-12 16:06:30 +02:00
previous_theme_text_color_ = palette().color(QPalette::WindowText);
previous_theme_highlight_color_ = palette().color(QPalette::Highlight);
2018-02-27 18:06:05 +01:00
drawVolumeSliderHandle();
generateGradient();
2021-06-12 16:06:30 +02:00
setMinimumWidth(pixmap_inset_.width());
setMinimumHeight(pixmap_inset_.height());
2018-02-27 18:06:05 +01:00
2021-06-12 16:06:30 +02:00
QObject::connect(timer_anim_, &QTimer::timeout, this, &VolumeSlider::slotAnimTimer);
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
}
2019-03-09 16:48:45 +01:00
void VolumeSlider::SetEnabled(const bool enabled) {
QSlider::setEnabled(enabled);
QSlider::setVisible(enabled);
}
void VolumeSlider::generateGradient() {
2018-02-27 18:06:05 +01:00
const QImage mask(":/pictures/volumeslider-gradient.png");
QImage gradient_image(mask.size(), QImage::Format_ARGB32_Premultiplied);
QPainter p(&gradient_image);
QLinearGradient gradient(gradient_image.rect().topLeft(), gradient_image.rect().topRight());
gradient.setColorAt(0, palette().color(QPalette::Window));
2018-02-27 18:06:05 +01:00
gradient.setColorAt(1, palette().color(QPalette::Highlight));
p.fillRect(gradient_image.rect(), QBrush(gradient));
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
p.drawImage(0, 0, mask);
p.end();
2021-06-12 16:06:30 +02:00
pixmap_gradient_ = QPixmap::fromImage(gradient_image);
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
}
2019-03-09 16:48:45 +01:00
void VolumeSlider::slotAnimTimer() {
2021-06-12 16:06:30 +02:00
if (anim_enter_) {
++anim_count_;
2018-02-27 18:06:05 +01:00
update();
2021-06-12 16:06:30 +02:00
if (anim_count_ == ANIM_MAX - 1) timer_anim_->stop();
2018-02-27 18:06:05 +01:00
}
else {
2021-06-12 16:06:30 +02:00
--anim_count_;
2018-02-27 18:06:05 +01:00
update();
2021-06-12 16:06:30 +02:00
if (anim_count_ == 0) timer_anim_->stop();
2018-02-27 18:06:05 +01:00
}
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
}
2021-02-02 21:08:58 +01:00
void VolumeSlider::mousePressEvent(QMouseEvent *e) {
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
if (e->button() != Qt::RightButton) {
2019-03-09 16:48:45 +01:00
SliderSlider::mousePressEvent(e);
2018-02-27 18:06:05 +01:00
slideEvent(e);
}
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
}
2021-02-02 21:08:58 +01:00
void VolumeSlider::contextMenuEvent(QContextMenuEvent *e) {
2019-03-09 16:48:45 +01:00
2021-06-20 19:04:08 +02:00
QHash<QAction*, int> values;
2018-02-27 18:06:05 +01:00
QMenu menu;
menu.setTitle("Volume");
values[menu.addAction("100%")] = 100;
values[menu.addAction("80%")] = 80;
values[menu.addAction("60%")] = 60;
values[menu.addAction("40%")] = 40;
values[menu.addAction("20%")] = 20;
values[menu.addAction("0%")] = 0;
2021-02-02 21:08:58 +01:00
QAction *ret = menu.exec(mapToGlobal(e->pos()));
2018-02-27 18:06:05 +01:00
if (ret) {
2021-06-21 20:21:40 +02:00
QSlider::setValue(values[ret]); // clazy:exclude=skipped-base-method
2018-02-27 18:06:05 +01:00
emit sliderReleased(values[ret]);
}
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
}
2021-02-02 21:08:58 +01:00
void VolumeSlider::slideEvent(QMouseEvent *e) {
2021-06-21 20:21:40 +02:00
QSlider::setValue(QStyle::sliderValueFromPosition(minimum(), maximum(), e->pos().x(), width() - 2)); // clazy:exclude=skipped-base-method
2018-02-27 18:06:05 +01:00
}
2021-02-02 21:08:58 +01:00
void VolumeSlider::wheelEvent(QWheelEvent *e) {
2019-03-09 16:48:45 +01:00
2021-10-30 02:21:29 +02:00
const int step = e->angleDelta().y() / (e->angleDelta().x() == 0 ? 30 : -30);
2021-06-21 20:21:40 +02:00
QSlider::setValue(SliderSlider::value() + step); // clazy:exclude=skipped-base-method
2018-02-27 18:06:05 +01:00
emit sliderReleased(value());
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
}
2019-03-09 16:48:45 +01:00
void VolumeSlider::paintEvent(QPaintEvent*) {
2018-02-27 18:06:05 +01:00
QPainter p(this);
const int padding = 7;
2021-10-11 22:28:28 +02:00
const int offset = static_cast<int>(static_cast<double>((width() - 2 * padding) * value()) / maximum());
2018-02-27 18:06:05 +01:00
2018-10-02 00:46:54 +02:00
// If theme changed since last paintEvent, redraw the volume pixmap with new theme colors
2021-06-12 16:06:30 +02:00
if (previous_theme_text_color_ != palette().color(QPalette::WindowText)) {
pixmap_inset_ = drawVolumePixmap();
previous_theme_text_color_ = palette().color(QPalette::WindowText);
2018-02-27 18:06:05 +01:00
}
2021-06-12 16:06:30 +02:00
if (previous_theme_highlight_color_ != palette().color(QPalette::Highlight)) {
2018-02-27 18:06:05 +01:00
drawVolumeSliderHandle();
2021-06-12 16:06:30 +02:00
previous_theme_highlight_color_ = palette().color(QPalette::Highlight);
2018-02-27 18:06:05 +01:00
}
2021-06-12 16:06:30 +02:00
p.drawPixmap(0, 0, pixmap_gradient_, 0, 0, offset + padding, 0);
p.drawPixmap(0, 0, pixmap_inset_);
p.drawPixmap(offset - handle_pixmaps_[0].width() / 2 + padding, 0, handle_pixmaps_[anim_count_]);
2018-02-27 18:06:05 +01:00
// Draw percentage number
QStyleOptionViewItem opt;
p.setPen(opt.palette.color(QPalette::Normal, QPalette::Text));
QFont vol_font(opt.font);
vol_font.setPixelSize(9);
p.setFont(vol_font);
const QRect rect(0, 0, 34, 15);
p.drawText(rect, Qt::AlignRight | Qt::AlignVCenter, QString::number(value()) + '%');
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
}
2020-09-05 19:20:43 +02:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void VolumeSlider::enterEvent(QEnterEvent*) {
#else
2019-03-09 16:48:45 +01:00
void VolumeSlider::enterEvent(QEvent*) {
2020-09-05 19:20:43 +02:00
#endif
2019-03-09 16:48:45 +01:00
2021-06-12 16:06:30 +02:00
anim_enter_ = true;
anim_count_ = 0;
2018-02-27 18:06:05 +01:00
2021-06-12 16:06:30 +02:00
timer_anim_->start(ANIM_INTERVAL);
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
}
2019-03-09 16:48:45 +01:00
void VolumeSlider::leaveEvent(QEvent*) {
2018-02-27 18:06:05 +01:00
// This can happen if you enter and leave the widget quickly
2021-06-12 16:06:30 +02:00
if (anim_count_ == 0) anim_count_ = 1;
2018-02-27 18:06:05 +01:00
2021-06-12 16:06:30 +02:00
anim_enter_ = false;
timer_anim_->start(ANIM_INTERVAL);
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
}
2019-03-09 16:48:45 +01:00
void VolumeSlider::paletteChange(const QPalette&) {
2018-02-27 18:06:05 +01:00
generateGradient();
}
2022-03-22 21:09:05 +01:00
QPixmap VolumeSlider::drawVolumePixmap() const {
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
QPixmap pixmap(112, 36);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
QPen pen(palette().color(QPalette::WindowText), 0.3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
painter.setPen(pen);
2018-10-02 00:38:52 +02:00
2018-02-27 18:06:05 +01:00
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
// Draw volume control pixmap
QPolygon poly;
2019-03-09 16:48:45 +01:00
poly << QPoint(6, 21) << QPoint(104, 21) << QPoint(104, 7) << QPoint(6, 16) << QPoint(6, 21);
2018-02-27 18:06:05 +01:00
QPainterPath path;
path.addPolygon(poly);
painter.drawPolygon(poly);
painter.drawLine(6, 29, 104, 29);
2021-06-12 16:06:30 +02:00
2018-02-27 18:06:05 +01:00
// Return QPixmap
return pixmap;
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
}
2019-03-09 16:48:45 +01:00
void VolumeSlider::drawVolumeSliderHandle() {
2018-02-27 18:06:05 +01:00
QImage pixmapHandle(":/pictures/volumeslider-handle.png");
QImage pixmapHandleGlow(":/pictures/volumeslider-handle_glow.png");
QImage pixmapHandleGlow_image(pixmapHandleGlow.size(), QImage::Format_ARGB32_Premultiplied);
QPainter painter(&pixmapHandleGlow_image);
2018-10-02 00:38:52 +02:00
2018-02-27 18:06:05 +01:00
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
2021-06-12 16:06:30 +02:00
// Repaint volume slider handle glow image with theme highlight color
2018-02-27 18:06:05 +01:00
painter.fillRect(pixmapHandleGlow_image.rect(), QBrush(palette().color(QPalette::Highlight)));
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
painter.drawImage(0, 0, pixmapHandleGlow);
2018-10-02 00:38:52 +02:00
2018-10-02 00:46:54 +02:00
// Overlay the volume slider handle image
2018-02-27 18:06:05 +01:00
painter.setCompositionMode(QPainter::CompositionMode_SourceAtop);
painter.drawImage(0, 0, pixmapHandle);
// BEGIN Calculate handle animation pixmaps for mouse-over effect
2022-02-06 04:19:45 +01:00
float opacity = 0.0F;
const float step = 1.0F / ANIM_MAX;
2018-02-27 18:06:05 +01:00
QImage dst;
2021-06-12 16:06:30 +02:00
handle_pixmaps_.clear();
2018-02-27 18:06:05 +01:00
for (int i = 0; i < ANIM_MAX; ++i) {
dst = pixmapHandle.copy();
QPainter p(&dst);
p.setOpacity(opacity);
p.drawImage(0, 0, pixmapHandleGlow_image);
p.end();
2021-06-12 16:06:30 +02:00
handle_pixmaps_.append(QPixmap::fromImage(dst));
2018-02-27 18:06:05 +01:00
opacity += step;
}
// END
2019-03-09 16:48:45 +01:00
2018-02-27 18:06:05 +01:00
}