Skip to content

Commit

Permalink
Scrollable volume button
Browse files Browse the repository at this point in the history
  • Loading branch information
kraxarn committed Jun 14, 2020
1 parent b5fba39 commit 263227c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 47 deletions.
47 changes: 2 additions & 45 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,45 +552,9 @@ QToolBar *MainWindow::createToolBar()
if (!status.isEmpty())
setStatus(QString("Failed to toggle repeat: %1").arg(status), true);
});
// Volume slider
volume = new QSlider(this);
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);
// Volume action for slider
volumeButton = new QToolButton(this);
volumeButton->setText("Volume");
volumeButton->setIcon(volumeIcon());
volumeButton->setPopupMode(QToolButton::InstantPopup);
volumeButton->setMenu(volumeMenu);
// Volume
volumeButton = new VolumeButton(settings, *spotify, this);
toolBar->addWidget(volumeButton);
QSlider::connect(volume, &QAbstractSlider::valueChanged, [this](int value) {
volumeButton->setIcon(volumeIcon());
});
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 = spotify->setVolume(volume->value() * 5);
if (!status.isEmpty())
setStatus(QString("Failed to set volume: %1").arg(status), true);
});
}
// Return final tool bar
return toolBar;
}
Expand Down Expand Up @@ -923,13 +887,6 @@ QSet<QString> MainWindow::allArtists()
return artists;
}

QIcon MainWindow::volumeIcon()
{
auto vol = volume->value() * 5;
return Icon::get(QString("audio-volume-%1")
.arg(vol < 33 ? "low" : vol > 66 ? "high" : "medium"));
}

void MainWindow::setFixedWidthTime(bool value)
{
position->setFont(value ? QFont("monospace") : QFont());
Expand Down
4 changes: 2 additions & 2 deletions src/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "spotify/clienthandler.hpp"
#include "spotify/user.hpp"
#include "whatsnewdialog.hpp"
#include "volumebutton.hpp"

#include <QMainWindow>
#include <QListWidget>
Expand Down Expand Up @@ -104,7 +105,7 @@ class MainWindow : public QMainWindow
QLabel *nowPlaying, *position, *nowAlbum;
QSlider *progress, *volume;
QAction *playPause, *repeat, *shuffle;
QToolButton *volumeButton;
VolumeButton *volumeButton;
// Everything else
spt::Spotify *spotify;
QNetworkAccessManager *network;
Expand Down Expand Up @@ -139,5 +140,4 @@ class MainWindow : public QMainWindow
QStringList currentTracks();
void setPlayingTrackItem(QTreeWidgetItem *item);
QSet<QString> allArtists();
QIcon volumeIcon();
};
62 changes: 62 additions & 0 deletions src/volumebutton.cpp
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")));
}
31 changes: 31 additions & 0 deletions src/volumebutton.hpp
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;
};

0 comments on commit 263227c

Please sign in to comment.