-
Notifications
You must be signed in to change notification settings - Fork 1
/
audiosettingswidget.cpp
71 lines (57 loc) · 2.18 KB
/
audiosettingswidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "audiosettingswidget.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QIcon>
AudioSettingsWidget::AudioSettingsWidget(QWidget *parent)
: QWidget{parent}
{
auto deviceSelectorLabel = new QLabel("Output Device");
deviceSelector = new QComboBox;
auto volumeSliderLabel = new QLabel;
volumeSliderLabel->setPixmap(QPixmap{":/icons/speaker1.png"}.scaled({24,24}));
volumeSlider = new QSlider;
volumeSlider->setRange(0, 100);
volumeSlider->setValue(100);
volumeSlider->setTickInterval(10);
auto outputDeviceLayout = new QVBoxLayout;
outputDeviceLayout->addWidget(deviceSelectorLabel);
outputDeviceLayout->addWidget(deviceSelector);
outputDeviceLayout->addStretch();
auto volumeLayout = new QVBoxLayout;
volumeLayout->addWidget(volumeSliderLabel);
volumeLayout->addWidget(volumeSlider);
auto mainLayout = new QHBoxLayout;
mainLayout->addLayout(outputDeviceLayout);
mainLayout->addLayout(volumeLayout);
setLayout(mainLayout);
setAvailableOutputDevices(QAudioDeviceInfo::availableDevices(QAudio::Mode::AudioOutput));
connect(deviceSelector, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this]{
const auto d = deviceSelector->currentData().value<QAudioDeviceInfo>();
emit outputDeviceSelected(d);
});
connect(volumeSlider, &QSlider::sliderMoved, this, [this](int position){
emit outputVolumeChanged(QAudio::convertVolume (position / 100.0, QAudio::LogarithmicVolumeScale, QAudio::LinearVolumeScale));
});
}
void AudioSettingsWidget::setAvailableOutputDevices(const QList<QAudioDeviceInfo> &deviceList)
{
deviceSelector->clear();
for(auto it = deviceList.constBegin(); it != deviceList.constEnd(); ++it)
{
QVariant v;
v.setValue(*it);
deviceSelector->addItem(it->deviceName(), v);
}
deviceSelector->setCurrentText(QAudioDeviceInfo::defaultOutputDevice().deviceName());
}
void AudioSettingsWidget::setVolume(qreal linearVol)
{
volumeSlider->setSliderPosition(100 * QAudio::convertVolume(linearVol,
QAudio::LinearVolumeScale,
QAudio::LogarithmicVolumeScale));
}
QAudioDeviceInfo AudioSettingsWidget::getSelectedAudioDevice() const
{
return deviceSelector->currentData().value<QAudioDeviceInfo>();
}