Implemented SettingsDialog

This commit is contained in:
2022-12-27 23:43:09 +01:00
parent 56d56947c9
commit e2a2980c4f
17 changed files with 382 additions and 36 deletions

View File

@ -40,6 +40,7 @@ SOURCES += \
widgets/samplewidget.cpp \
widgets/scratchwidget.cpp \
widgets/sequencerwidget.cpp \
widgets/settingsdialog.cpp \
widgets/synthisizerwidget.cpp \
widgets/trackdeck.cpp
@ -72,6 +73,7 @@ HEADERS += \
widgets/samplewidget.h \
widgets/scratchwidget.h \
widgets/sequencerwidget.h \
widgets/settingsdialog.h \
widgets/synthisizerwidget.h \
widgets/trackdeck.h
@ -84,5 +86,6 @@ FORMS += \
widgets/sampleswidget.ui \
widgets/samplewidget.ui \
widgets/sequencerwidget.ui \
widgets/settingsdialog.ui \
widgets/synthisizerwidget.ui \
widgets/trackdeck.ui

View File

@ -1,5 +1,38 @@
#include "drummachinesettings.h"
#include <QStandardPaths>
#include <QDebug>
QString DrumMachineSettings::defaultCacheDir() const
{
return QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
}
QString DrumMachineSettings::cacheDir() const
{
return value("cacheDir", defaultCacheDir()).toString();
}
void DrumMachineSettings::setCacheDir(const QString &cacheDir)
{
setValue("cacheDir", cacheDir);
}
qint64 DrumMachineSettings::defaultMaximumCacheSize() const
{
return 2ull * 1024 * 1024 * 1024;
}
qint64 DrumMachineSettings::maximumCacheSize() const
{
return value("maximumCacheSize", defaultMaximumCacheSize()).value<qint64>();
}
void DrumMachineSettings::setMaximumCacheSize(qint64 maximumCacheSize)
{
setValue("maximumCacheSize", maximumCacheSize);
}
QString DrumMachineSettings::lastAudioDevice() const
{
return value("lastAudioDevice").toString();

View File

@ -7,6 +7,14 @@ class DrumMachineSettings : public QSettings
public:
using QSettings::QSettings;
QString defaultCacheDir() const;
QString cacheDir() const;
void setCacheDir(const QString &cacheDir);
qint64 defaultMaximumCacheSize() const;
qint64 maximumCacheSize() const;
void setMaximumCacheSize(qint64 maximumCacheSize);
QString lastAudioDevice() const;
void setLastAudioDevice(const QString &lastAudioDevice);

View File

@ -90,7 +90,7 @@ const drumpad_presets::Preset &DrumPadPresetsModel::getPreset(const QModelIndex
const drumpad_presets::Preset &DrumPadPresetsModel::getPreset(int row) const
{
Q_ASSERT(row >= 0 && row < std::size(m_drumpad_presets));
Q_ASSERT(row >= 0 && row < int(std::size(m_drumpad_presets)));
return m_drumpad_presets.at(row);
}
@ -132,7 +132,7 @@ QVariant DrumPadPresetsModel::data(const QModelIndex &index, int role) const
return {};
if (index.row() < 0)
return {};
if (index.row() >= std::size(m_drumpad_presets))
if (index.row() >= int(std::size(m_drumpad_presets)))
return {};
const auto &preset = getPreset(index);

View File

@ -48,7 +48,7 @@ int main(int argc, char *argv[])
QApplication app{argc, argv};
QCoreApplication::setOrganizationDomain("brunner.ninja");
QCoreApplication::setOrganizationName("brunner.ninja");
QCoreApplication::setApplicationName("miditest");
QCoreApplication::setApplicationName("DrumMachine");
QCoreApplication::setApplicationVersion("1.0");
qDebug() << "supportsSsl:" << QSslSocket::supportsSsl()

View File

@ -307,20 +307,36 @@ void TreeToTableProxyModel::sourceColumnsRemoved(const QModelIndex &parent, int
void TreeToTableProxyModel::sourceRowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
{
Q_UNUSED(sourceParent)
Q_UNUSED(sourceStart)
Q_UNUSED(sourceEnd)
Q_UNUSED(destinationParent)
Q_UNUSED(destinationRow)
}
void TreeToTableProxyModel::sourceRowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row)
{
Q_UNUSED(parent)
Q_UNUSED(start)
Q_UNUSED(end)
Q_UNUSED(destination)
Q_UNUSED(row)
}
void TreeToTableProxyModel::sourceColumnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn)
{
Q_UNUSED(sourceParent)
Q_UNUSED(sourceStart)
Q_UNUSED(sourceEnd)
Q_UNUSED(destinationParent)
Q_UNUSED(destinationColumn)
}
void TreeToTableProxyModel::sourceColumnsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column)
{
Q_UNUSED(parent)
Q_UNUSED(start)
Q_UNUSED(end)
Q_UNUSED(destination)
Q_UNUSED(column)
}

View File

@ -79,6 +79,7 @@ void DjWidget::writeSamples(frame_t *begin, frame_t *end)
void DjWidget::loadSettings(DrumMachineSettings &settings)
{
Q_UNUSED(settings)
}
void DjWidget::unsendColors()
@ -91,6 +92,7 @@ void DjWidget::sendColors()
void DjWidget::midiReceived(const midi::MidiMessage &message)
{
Q_UNUSED(message)
}
void DjWidget::directorySelected()

View File

@ -11,6 +11,7 @@
#include "audioformat.h"
#include "midicontainers.h"
#include "settingsdialog.h"
namespace {
void DummyDeleter(PaStream *stream);
@ -33,8 +34,9 @@ MainWindow::MainWindow(QWidget *parent) :
{
m_ui->setupUi(this);
m_cache.setCacheDirectory("cache");
m_cache.setMaximumCacheSize(2ull * 1024 * 1024 * 1024);
m_cache.setCacheDirectory(m_settings.cacheDir());
m_cache.setMaximumCacheSize(m_settings.maximumCacheSize());
m_networkAccessManager.setCache(&m_cache);
m_ui->drumPadWidget->injectNetworkAccessManager(m_networkAccessManager);
@ -53,6 +55,8 @@ MainWindow::MainWindow(QWidget *parent) :
m_ui->loopStationWidget->injectDecodingThread(m_decoderThread);
m_ui->djWidget->injectDecodingThread(m_decoderThread);
connect(m_ui->pushButtonSettings, &QAbstractButton::pressed, this, &MainWindow::showSettings);
updateAudioDevices();
connect(m_ui->pushButtonRefreshAudioDevices, &QAbstractButton::pressed, this, &MainWindow::updateAudioDevices);
if (const auto &lastAudioDevice = m_settings.lastAudioDevice(); !lastAudioDevice.isEmpty())
@ -126,6 +130,16 @@ int MainWindow::writeSamples(frame_t *begin, frame_t *end)
return paContinue;
}
void MainWindow::showSettings()
{
SettingsDialog dialog{m_settings, m_cache.cacheSize(), this};
if (dialog.exec() == QDialog::Accepted)
{
m_cache.setCacheDirectory(m_settings.cacheDir());
m_cache.setMaximumCacheSize(m_settings.maximumCacheSize());
}
}
void MainWindow::openAudioDevice()
{
if (m_paStream)

View File

@ -28,6 +28,7 @@ public:
int writeSamples(frame_t *begin, frame_t *end);
private slots:
void showSettings();
void openAudioDevice();
void openMidiInDevice();
void openMidiOutDevice();

View File

@ -36,6 +36,19 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonSettings">
<property name="maximumSize">
<size>
<width>32</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>⚙️</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">

View File

@ -82,6 +82,8 @@ void PreviewWidget::mouseMoveEvent(QMouseEvent *event)
void PreviewWidget::clicked(int x, int y)
{
Q_UNUSED(y)
if (!m_buffer.isValid())
return;

View File

@ -102,7 +102,7 @@ void SamplesWidget::sendColors()
void SamplesWidget::sequencerTriggerSample(int index)
{
const auto widgets = getWidgets();
if (index < 0 || index >= std::size(widgets))
if (index < 0 || index >= int(std::size(widgets)))
{
qDebug() << "index out of range" << index;
return;

View File

@ -19,6 +19,8 @@ ScratchWidget::ScratchWidget(QWidget *parent) :
void ScratchWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter;
painter.begin(this);

View File

@ -103,7 +103,7 @@
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<y>-272</y>
<width>878</width>
<height>828</height>
</rect>
@ -124,7 +124,7 @@
</widget>
</item>
<item row="19" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal10">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -141,7 +141,7 @@
</widget>
</item>
<item row="7" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal4">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -158,7 +158,7 @@
</widget>
</item>
<item row="5" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal2">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -168,7 +168,7 @@
</widget>
</item>
<item row="15" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal8">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -178,7 +178,7 @@
</widget>
</item>
<item row="41" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal21">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -195,7 +195,7 @@
</widget>
</item>
<item row="47" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal24">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -205,7 +205,7 @@
</widget>
</item>
<item row="29" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal15">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -229,7 +229,7 @@
</widget>
</item>
<item row="27" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal14">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -246,7 +246,7 @@
</widget>
</item>
<item row="11" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal6">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -277,7 +277,7 @@
</widget>
</item>
<item row="13" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal7">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -287,7 +287,7 @@
</widget>
</item>
<item row="25" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal13">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -325,7 +325,7 @@
</widget>
</item>
<item row="43" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal22">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -342,7 +342,7 @@
</widget>
</item>
<item row="3" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal1">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -352,7 +352,7 @@
</widget>
</item>
<item row="39" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal20">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -362,7 +362,7 @@
</widget>
</item>
<item row="21" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal11">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -372,7 +372,7 @@
</widget>
</item>
<item row="31" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal16">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -382,7 +382,7 @@
</widget>
</item>
<item row="23" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal12">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -392,7 +392,7 @@
</widget>
</item>
<item row="37" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal19">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -409,7 +409,7 @@
</widget>
</item>
<item row="35" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal18">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -419,7 +419,7 @@
</widget>
</item>
<item row="9" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal5">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -436,7 +436,7 @@
</widget>
</item>
<item row="17" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal9">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -453,7 +453,7 @@
</widget>
</item>
<item row="0" column="1" rowspan="49" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameVertical">
<property name="frameShape">
<enum>QFrame::VLine</enum>
</property>
@ -477,7 +477,7 @@
</widget>
</item>
<item row="33" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal17">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -494,7 +494,7 @@
</widget>
</item>
<item row="1" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal0">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>
@ -511,7 +511,7 @@
</widget>
</item>
<item row="45" column="0" colspan="3">
<widget class="QFrame" name="frame">
<widget class="QFrame" name="frameHorizontal23">
<property name="frameShape">
<enum>QFrame::HLine</enum>
</property>

View File

@ -0,0 +1,98 @@
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include <QMessageBox>
#include <QDebug>
#include <QDir>
#include <QFileDialog>
#include "drummachinesettings.h"
SettingsDialog::SettingsDialog(DrumMachineSettings &settings, qint64 currentCacheSize, QWidget *parent) :
QDialog{parent},
m_ui{std::make_unique<Ui::SettingsDialog>()},
m_settings{settings}
{
m_ui->setupUi(this);
connect(m_ui->toolButtonSelectCacheDir, &QAbstractButton::pressed, this, &SettingsDialog::selectCacheDir);
connect(m_ui->buttonBox, &QDialogButtonBox::clicked, this, &SettingsDialog::buttonClicked);
resetFields();
m_ui->labelCurrentCacheSize->setText(tr("(Current Cache Size: %0MB)").arg(currentCacheSize / 1024 / 1024));
}
SettingsDialog::~SettingsDialog() = default;
void SettingsDialog::accept()
{
{
const auto &cacheDir = m_ui->lineEditCacheDir->text();
try
{
if (cacheDir.isEmpty())
throw tr("Empty cacheDir!");
else if (QDir dir{cacheDir}; !dir.exists() && !dir.mkpath(dir.absolutePath()))
throw tr("Could not create cache directory!");
}
catch (const QString &e)
{
QMessageBox::warning(this, e, e);
return;
}
if (m_settings.cacheDir() != cacheDir)
m_settings.setCacheDir(cacheDir);
}
const auto maximumCacheSize = qint64{m_ui->spinBoxMaxCacheSize->value()} * 1024 * 1024;
if (m_settings.maximumCacheSize() != maximumCacheSize)
m_settings.setMaximumCacheSize(maximumCacheSize);
QDialog::accept();
}
void SettingsDialog::buttonClicked(QAbstractButton *button)
{
if (!button)
{
qWarning() << "invalid button";
return;
}
switch (m_ui->buttonBox->standardButton(button))
{
case QDialogButtonBox::Reset:
resetFields();
return;
case QDialogButtonBox::RestoreDefaults:
restoreDefaults();
return;
default:;
}
}
void SettingsDialog::resetFields()
{
m_ui->lineEditCacheDir->setText(m_settings.cacheDir());
m_ui->spinBoxMaxCacheSize->setValue(m_settings.maximumCacheSize() / 1024 / 1024);
}
void SettingsDialog::restoreDefaults()
{
m_ui->lineEditCacheDir->setText(m_settings.defaultCacheDir());
m_ui->spinBoxMaxCacheSize->setValue(m_settings.defaultMaximumCacheSize() / 1024 / 1024);
}
void SettingsDialog::selectCacheDir()
{
QDir dir{m_ui->lineEditCacheDir->text()};
if (!dir.exists())
dir = QDir{m_settings.cacheDir()};
const auto selected = QFileDialog::getExistingDirectory(this, tr("Select cache dir"), dir.exists() ? dir.absolutePath() : QString{});
if (selected.isEmpty())
return;
m_ui->lineEditCacheDir->setText(selected);
}

31
widgets/settingsdialog.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include <QDialog>
#include <memory>
namespace Ui { class SettingsDialog; }
class DrumMachineSettings;
class QAbstractButton;
class SettingsDialog : public QDialog
{
Q_OBJECT
public:
explicit SettingsDialog(DrumMachineSettings &settings, qint64 currentCacheSize, QWidget *parent = nullptr);
~SettingsDialog() override;
public slots:
void accept() override;
private slots:
void buttonClicked(QAbstractButton *button);
void resetFields();
void restoreDefaults();
void selectCacheDir();
private:
const std::unique_ptr<Ui::SettingsDialog> m_ui;
DrumMachineSettings &m_settings;
};

123
widgets/settingsdialog.ui Normal file
View File

@ -0,0 +1,123 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SettingsDialog</class>
<widget class="QDialog" name="SettingsDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>439</width>
<height>214</height>
</rect>
</property>
<property name="windowTitle">
<string>Settings</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="labelCacheDir">
<property name="text">
<string>Cache Dir:</string>
</property>
<property name="buddy">
<cstring>lineEditCacheDir</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineEditCacheDir"/>
</item>
<item>
<widget class="QToolButton" name="toolButtonSelectCacheDir">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="1,0">
<item>
<widget class="QSpinBox" name="spinBoxMaxCacheSize">
<property name="suffix">
<string>MB</string>
</property>
<property name="maximum">
<number>2147483647</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labelCurrentCacheSize">
<property name="text">
<string>(Current Cache Size: 0)</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelMaxCacheSize">
<property name="text">
<string>Max Cache Size:</string>
</property>
<property name="buddy">
<cstring>spinBoxMaxCacheSize</cstring>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset|QDialogButtonBox::RestoreDefaults</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>SettingsDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>SettingsDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>