Improved controls with menus
This commit is contained in:
1 parent
8076271edc
commit
b99a695159
10 files changed
+271
-165
No files matched your search
+125
-49
@@ -6,88 +6,164 @@
|
||||
#include <QMessageBox>
|
||||
#include <QStringBuilder>
|
||||
#include <QRadioButton>
|
||||
#include <QLabel>
|
||||
#include <QDebug>
|
||||
|
||||
// local includes
|
||||
#include "audiodevice.h"
|
||||
#include "fakedevice.h"
|
||||
|
||||
namespace {
|
||||
constexpr int samplerates[] = { 44100, 48000, 96000, 192000 };
|
||||
|
||||
constexpr int refreshrates[] = { 15, 30, 50, 60 };
|
||||
|
||||
constexpr int zoomlevels[] = { 50, 75, 100, 200, 400, 800 };
|
||||
|
||||
template<typename T>
|
||||
void setActionsEnabled(const T &actions, bool enabled)
|
||||
{
|
||||
for(auto action : actions)
|
||||
action->setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow{parent},
|
||||
m_ui{std::make_unique<Ui::MainWindow>()},
|
||||
m_audioDevices{QAudioDeviceInfo::availableDevices(QAudio::AudioInput)},
|
||||
m_input{std::make_unique<AudioDevice>()}
|
||||
//m_input{std::make_unique<FakeDevice>()}
|
||||
m_statusLabel{*new QLabel}
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
|
||||
connect(m_input.get(), &BaseDevice::samplesReceived, m_ui->widget, &OsciWidget::renderSamples);
|
||||
m_ui->statusbar->addWidget(&m_statusLabel);
|
||||
|
||||
connect(m_ui->widget, &OsciWidget::statusUpdate, &m_statusLabel, &QLabel::setText);
|
||||
|
||||
// setting up menu File
|
||||
connect(m_ui->actionStart, &QAction::triggered, this, &MainWindow::start);
|
||||
connect(m_ui->actionStop, &QAction::triggered, this, &MainWindow::stop);
|
||||
m_ui->action_Quit->setShortcut(QKeySequence::Quit);
|
||||
|
||||
// setting up menu Devices
|
||||
for (const auto &device : m_audioDevices)
|
||||
{
|
||||
auto name = device.deviceName();
|
||||
m_ui->comboBoxDevices->addItem(name);
|
||||
const auto action = m_ui->menu_Device->addAction(name);
|
||||
action->setCheckable(true);
|
||||
m_deviceGroup.addAction(action);
|
||||
|
||||
// Select last element containing monitor if available
|
||||
if(name.contains("monitor"))
|
||||
{
|
||||
m_ui->comboBoxDevices->setCurrentIndex(m_ui->comboBoxDevices->count()-1);
|
||||
}
|
||||
action->setChecked(true);
|
||||
}
|
||||
|
||||
for (const auto samplerate : { 44100, 48000, 96000, 192000 })
|
||||
m_ui->comboBoxSamplerate->addItem(tr("%0").arg(samplerate), samplerate);
|
||||
|
||||
connect(m_ui->pushButtonToggle, &QAbstractButton::pressed, this, &MainWindow::toggle);
|
||||
|
||||
for (const auto framerate : {15, 30, 50, 60})
|
||||
m_ui->comboBoxFps->addItem(tr("%0 FPS").arg(framerate), framerate);
|
||||
|
||||
connect(m_ui->comboBoxFps, qOverload<int>(&QComboBox::currentIndexChanged), m_ui->widget, [this](){
|
||||
m_ui->widget->setFps(m_ui->comboBoxFps->currentData().toInt());
|
||||
});
|
||||
|
||||
auto buttonGroup = new QButtonGroup;
|
||||
buttonGroup->setExclusive(true);
|
||||
for (auto factor : { .5f, 1.f, 2.f, 4.f, 8.f })
|
||||
// setting up menu Samplerates
|
||||
for (const auto samplerate : samplerates)
|
||||
{
|
||||
auto radioButton = new QRadioButton(QString::number(factor));
|
||||
connect(radioButton, &QRadioButton::pressed, this, [factor,&widget=*m_ui->widget](){
|
||||
widget.setFactor(factor);
|
||||
});
|
||||
m_ui->horizontalLayout->addWidget(radioButton);
|
||||
auto action = m_ui->menu_Samplerate->addAction(tr("%0").arg(samplerate));
|
||||
action->setCheckable(true);
|
||||
m_samplerateGroup.addAction(action);
|
||||
}
|
||||
|
||||
if (m_ui->comboBoxDevices->count())
|
||||
toggle();
|
||||
}
|
||||
m_samplerateGroup.actions().first()->setChecked(true);
|
||||
|
||||
void MainWindow::toggle()
|
||||
{
|
||||
if (!m_ui->comboBoxDevices->count())
|
||||
// setting up menu Refreshrates
|
||||
for (const auto refreshrate : refreshrates)
|
||||
{
|
||||
QMessageBox::warning(this, tr("Failed to start!"), tr("Failed to start!") % "\n\n" % tr("No audio devices available!"));
|
||||
return;
|
||||
auto action = m_ui->menu_Refreshrate->addAction(tr("%0FPS").arg(refreshrate));
|
||||
action->setCheckable(true);
|
||||
m_refreshrateGroup.addAction(action);
|
||||
}
|
||||
|
||||
if (m_input->running())
|
||||
{
|
||||
m_input->stop();
|
||||
m_ui->comboBoxDevices->setEnabled(true);
|
||||
m_ui->comboBoxSamplerate->setEnabled(true);
|
||||
m_ui->pushButtonToggle->setText("▶");
|
||||
const auto index = std::find(std::begin(refreshrates), std::end(refreshrates), m_ui->widget->fps());
|
||||
if (index != std::end(refreshrates))
|
||||
m_refreshrateGroup.actions().at(std::distance(std::begin(refreshrates), index))->setChecked(true);
|
||||
}
|
||||
|
||||
connect(&m_refreshrateGroup, &QActionGroup::triggered, this, &MainWindow::refreshRateChanged);
|
||||
|
||||
// setting up menu Zoom
|
||||
for (const auto zoomlevel : zoomlevels)
|
||||
{
|
||||
auto action = m_ui->menu_Zoom->addAction(tr("%0%").arg(zoomlevel));
|
||||
action->setCheckable(true);
|
||||
m_zoomlevelsGroup.addAction(action);
|
||||
}
|
||||
|
||||
{
|
||||
const auto index = std::find(std::begin(zoomlevels), std::end(zoomlevels), m_ui->widget->factor()*100);
|
||||
if (index != std::end(zoomlevels))
|
||||
m_zoomlevelsGroup.actions().at(std::distance(std::begin(zoomlevels), index))->setChecked(true);
|
||||
}
|
||||
|
||||
connect(&m_zoomlevelsGroup, &QActionGroup::triggered, this, &MainWindow::zoomChanged);
|
||||
|
||||
// autostart
|
||||
if (m_audioDevices.isEmpty())
|
||||
{
|
||||
m_ui->actionStart->setEnabled(false);
|
||||
m_ui->actionStop->setEnabled(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_input->setSamplerate(m_ui->comboBoxSamplerate->currentData().toInt());
|
||||
m_input->setFramerate(60);
|
||||
if (auto audioDevice = dynamic_cast<AudioDevice*>(m_input.get()))
|
||||
audioDevice->setDevice(m_audioDevices.at(m_ui->comboBoxDevices->currentIndex()));
|
||||
m_input->start();
|
||||
start();
|
||||
}
|
||||
|
||||
m_ui->comboBoxDevices->setEnabled(false);
|
||||
m_ui->comboBoxSamplerate->setEnabled(false);
|
||||
m_ui->pushButtonToggle->setText("▮▮");
|
||||
void MainWindow::start()
|
||||
{
|
||||
m_input = std::make_unique<AudioDevice>();
|
||||
//m_input = std::make_unique<FakeDevice>();
|
||||
|
||||
{
|
||||
auto *checked = m_samplerateGroup.checkedAction();
|
||||
const auto index = m_samplerateGroup.actions().indexOf(checked);
|
||||
const auto samplerate = samplerates[index];
|
||||
qDebug() << "samplerate: checked =" << checked << "index =" << index << "value =" << samplerate;
|
||||
m_input->setSamplerate(samplerate);
|
||||
}
|
||||
|
||||
connect(m_input.get(), &BaseDevice::samplesReceived, m_ui->widget, &OsciWidget::renderSamples);
|
||||
|
||||
if (auto audioDevice = dynamic_cast<AudioDevice*>(m_input.get()))
|
||||
{
|
||||
const auto device = m_audioDevices.at(m_deviceGroup.actions().indexOf(m_deviceGroup.checkedAction()));
|
||||
qDebug() << "setDevice" << device.deviceName();
|
||||
audioDevice->setDevice(device);
|
||||
}
|
||||
m_input->start();
|
||||
|
||||
setActionsEnabled(m_deviceGroup.actions(), false);
|
||||
setActionsEnabled(m_samplerateGroup.actions(), false);
|
||||
m_ui->actionStart->setEnabled(false);
|
||||
m_ui->actionStop->setEnabled(true);
|
||||
}
|
||||
|
||||
void MainWindow::stop()
|
||||
{
|
||||
m_input = nullptr;
|
||||
setActionsEnabled(m_deviceGroup.actions(), true);
|
||||
setActionsEnabled(m_samplerateGroup.actions(), true);
|
||||
m_ui->actionStart->setEnabled(true);
|
||||
m_ui->actionStop->setEnabled(false);
|
||||
}
|
||||
|
||||
void MainWindow::refreshRateChanged()
|
||||
{
|
||||
auto *checked = m_refreshrateGroup.checkedAction();
|
||||
const auto index = m_refreshrateGroup.actions().indexOf(checked);
|
||||
const auto refreshrate = refreshrates[index];
|
||||
qDebug() << "refreshrate: checked =" << checked << "index =" << index << "value =" << refreshrate;
|
||||
m_ui->widget->setFps(refreshrate);
|
||||
}
|
||||
|
||||
void MainWindow::zoomChanged()
|
||||
{
|
||||
auto *checked = m_zoomlevelsGroup.checkedAction();
|
||||
const auto index = m_zoomlevelsGroup.actions().indexOf(checked);
|
||||
const auto zoomlevel = zoomlevels[index];
|
||||
qDebug() << "zoomlevel: checked =" << checked << "index =" << index << "value =" << zoomlevel;
|
||||
m_ui->widget->setFactor(zoomlevel/100.f);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow() = default;
|
||||
Reference in new issue
Block a user