-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "volumebutton.hpp" | ||
|
||
VolumeButton::VolumeButton(const Settings &settings, spt::Spotify &spotify, QWidget *parent) | ||
: settings(settings), spotify(spotify), volume(new QSlider(this)), QToolButton(parent) | ||
{ | ||
// Volume slider | ||
volume->setOrientation(Qt::Orientation::Vertical); | ||
volume->setFixedHeight(100); | ||
volume->setMinimum(0); | ||
volume->setMaximum(20); | ||
volume->setValue(spt::ClientHandler::getVolume() * 20); | ||
|
||
// Layout for volume slider | ||
auto volumeMenu = new QMenu(this); | ||
volumeMenu->setContentsMargins(2, 6, 2, 6); | ||
auto volumeLayout = new QVBoxLayout(); | ||
volumeLayout->addWidget(volume); | ||
volumeMenu->setLayout(volumeLayout); | ||
|
||
setText("Volume"); | ||
updateIcon(); | ||
setPopupMode(QToolButton::InstantPopup); | ||
setMenu(volumeMenu); | ||
|
||
QAbstractSlider::connect(volume, &QAbstractSlider::valueChanged, [this](int value) { | ||
updateIcon(); | ||
}); | ||
|
||
if (settings.general.pulseVolume) | ||
{ | ||
// If using PulseAudio for volume control, update on every tick | ||
QSlider::connect(volume, &QAbstractSlider::valueChanged, [](int value) { | ||
spt::ClientHandler::setVolume(value * 0.05); | ||
}); | ||
} | ||
else | ||
{ | ||
// If using Spotify for volume control, only update on release | ||
QSlider::connect(volume, &QAbstractSlider::sliderReleased, [this]() { | ||
auto status = this->spotify.setVolume(volume->value() * 5); | ||
if (!status.isEmpty()) | ||
{ | ||
auto window = dynamic_cast<MainWindow*>(this->parent()); | ||
if (window != nullptr) | ||
window->setStatus(QString("Failed to set volume: %1").arg(status), true); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
void VolumeButton::wheelEvent(QWheelEvent *event) | ||
{ | ||
volume->setValue(volume->value() + (event->angleDelta().y() < 0 ? -1 : 1)); | ||
event->accept(); | ||
} | ||
|
||
void VolumeButton::updateIcon() | ||
{ | ||
auto vol = volume->value() * 5; | ||
setIcon(Icon::get(QString("audio-volume-%1") | ||
.arg(vol < 33 ? "low" : vol > 66 ? "high" : "medium"))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
class VolumeButton; | ||
|
||
#include "icon.hpp" | ||
#include "spotify/clienthandler.hpp" | ||
|
||
#include <QMenu> | ||
#include <QToolButton> | ||
#include <QWheelEvent> | ||
#include <QDebug> | ||
#include <QVBoxLayout> | ||
#include <QSlider> | ||
|
||
class VolumeButton : public QToolButton | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
VolumeButton(const Settings &settings, spt::Spotify &spotify, QWidget *parent); | ||
void updateIcon(); | ||
|
||
protected: | ||
void wheelEvent(QWheelEvent *event) override; | ||
|
||
|
||
private: | ||
QSlider *volume; | ||
const Settings &settings; | ||
spt::Spotify &spotify; | ||
}; |