Christmas cleaning
This commit is contained in:
118
src/ui/MainWindow.cpp
Normal file
118
src/ui/MainWindow.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
|
||||
#include "MainWindow.h"
|
||||
|
||||
#include <QTimer>
|
||||
#include "ui_MainWindow.h"
|
||||
|
||||
#include "../ParameterStore.h"
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
ui_(new Ui::MainWindow),
|
||||
audio_(new AudioEngine()),
|
||||
keyboard_(audio_->noteQueue()) {
|
||||
|
||||
ui_->setupUi(this);
|
||||
setFocusPolicy(Qt::StrongFocus);
|
||||
|
||||
// Initialize UI
|
||||
updateCounterLabel();
|
||||
|
||||
// Connect buttons to slots
|
||||
connect(ui_->buttonIncrement, &QPushButton::clicked, this, &MainWindow::onIncrementClicked);
|
||||
connect(ui_->buttonReset, &QPushButton::clicked, this, &MainWindow::onResetClicked);
|
||||
|
||||
// slider business
|
||||
// TODO: smart slider widget
|
||||
connect(ui_->slider, &QSlider::valueChanged, this, &MainWindow::onSliderValueChanged);
|
||||
connect(ui_->inputMin, &QLineEdit::editingFinished, this, &MainWindow::onMinChanged);
|
||||
connect(ui_->inputMax, &QLineEdit::editingFinished, this, &MainWindow::onMaxChanged);
|
||||
connect(ui_->inputStep, &QLineEdit::editingFinished, this, &MainWindow::onStepChanged);
|
||||
connect(ui_->inputValue, &QLineEdit::editingFinished, this, &MainWindow::onValueChanged);
|
||||
|
||||
// synth business
|
||||
audio_->start();
|
||||
|
||||
// init defaults
|
||||
// TODO:: there's gotta be a better way
|
||||
ui_->slider->setValue(PARAM_DEFS[static_cast<size_t>(ParamId::Osc1Frequency)].def);
|
||||
ui_->slider->setMinimum(PARAM_DEFS[static_cast<size_t>(ParamId::Osc1Frequency)].min);
|
||||
ui_->slider->setMaximum(PARAM_DEFS[static_cast<size_t>(ParamId::Osc1Frequency)].max);
|
||||
ui_->inputValue->setText(QString::number(PARAM_DEFS[static_cast<size_t>(ParamId::Osc1Frequency)].def));
|
||||
ui_->inputMin->setText(QString::number(PARAM_DEFS[static_cast<size_t>(ParamId::Osc1Frequency)].min));
|
||||
ui_->inputMax->setText(QString::number(PARAM_DEFS[static_cast<size_t>(ParamId::Osc1Frequency)].max));
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow() {
|
||||
delete ui_;
|
||||
}
|
||||
|
||||
void MainWindow::keyPressEvent(QKeyEvent* event) {
|
||||
keyboard_.handleKeyPress(event);
|
||||
}
|
||||
|
||||
void MainWindow::keyReleaseEvent(QKeyEvent* event) {
|
||||
keyboard_.handleKeyRelease(event);
|
||||
}
|
||||
|
||||
void MainWindow::onIncrementClicked() {
|
||||
counter_++;
|
||||
updateCounterLabel();
|
||||
}
|
||||
|
||||
void MainWindow::onResetClicked() {
|
||||
counter_ = 0;
|
||||
updateCounterLabel();
|
||||
}
|
||||
|
||||
void MainWindow::updateCounterLabel() {
|
||||
ui_->labelCounter->setText(QString::number(counter_));
|
||||
}
|
||||
|
||||
// allows only numbers to be set
|
||||
void MainWindow::onSliderValueChanged(int value) {
|
||||
QSignalBlocker blocker(ui_->inputValue);
|
||||
ui_->inputValue->setText(QString::number(value));
|
||||
|
||||
// forward value so synthesizer can read
|
||||
audio_->parameters()->set(ParamId::Osc1Frequency, static_cast<float>(value));
|
||||
}
|
||||
|
||||
// allows only values within the min, max to be set by the text field
|
||||
void MainWindow::onValueChanged() {
|
||||
bool ok = false;
|
||||
int value = ui_->inputValue->text().toInt(&ok);
|
||||
if(!ok) return;
|
||||
|
||||
value = qBound(ui_->slider->minimum(), value, ui_->slider->maximum());
|
||||
ui_->slider->setValue(value);
|
||||
}
|
||||
|
||||
void MainWindow::applySliderRange() {
|
||||
bool minOk, maxOk;
|
||||
int min = ui_->inputMin->text().toInt(&minOk);
|
||||
int max = ui_->inputMax->text().toInt(&maxOk);
|
||||
if(!minOk || !maxOk || min > max) return;
|
||||
|
||||
ui_->slider->setRange(min, max);
|
||||
|
||||
syncValueToUi(ui_->slider->value());
|
||||
}
|
||||
|
||||
void MainWindow::applySliderStep() {
|
||||
bool ok;
|
||||
int step = ui_->inputStep->text().toInt(&ok);
|
||||
if(!ok || step <= 0) return;
|
||||
|
||||
ui_->slider->setSingleStep(step);
|
||||
ui_->slider->setPageStep(step);
|
||||
}
|
||||
|
||||
void MainWindow::syncValueToUi(int value) {
|
||||
QSignalBlocker block1(ui_->slider);
|
||||
QSignalBlocker block2(ui_->inputValue);
|
||||
|
||||
value = qBound(ui_->slider->minimum(), value, ui_->slider->maximum());
|
||||
ui_->slider->setValue(value);
|
||||
ui_->inputValue->setText(QString::number(value));
|
||||
}
|
||||
52
src/ui/MainWindow.h
Normal file
52
src/ui/MainWindow.h
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QKeyEvent>
|
||||
|
||||
#include "../synth/AudioEngine.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
}
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit MainWindow(QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent* event) override;
|
||||
void keyReleaseEvent(QKeyEvent* event) override;
|
||||
|
||||
private slots:
|
||||
void onIncrementClicked();
|
||||
void onResetClicked();
|
||||
|
||||
void onSliderValueChanged(int value);
|
||||
void onMinChanged() { applySliderRange(); }
|
||||
void onMaxChanged() { applySliderRange(); }
|
||||
void onStepChanged() { applySliderStep(); }
|
||||
void onValueChanged();
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui_;
|
||||
int counter_ = 0;
|
||||
|
||||
int value = 0;
|
||||
|
||||
void updateCounterLabel();
|
||||
|
||||
void applySliderRange();
|
||||
void applySliderStep();
|
||||
void syncValueToUi(int value);
|
||||
|
||||
AudioEngine* audio_ = nullptr;
|
||||
KeyboardController keyboard_;
|
||||
|
||||
};
|
||||
194
src/ui/MainWindow.ui
Normal file
194
src/ui/MainWindow.ui
Normal file
@@ -0,0 +1,194 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MainWindow</class>
|
||||
<widget class="QMainWindow" name="MainWindow">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<widget class="QPushButton" name="buttonIncrement">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>50</x>
|
||||
<y>70</y>
|
||||
<width>101</width>
|
||||
<height>31</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>15</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Increment</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QPushButton" name="buttonReset">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>50</x>
|
||||
<y>100</y>
|
||||
<width>101</width>
|
||||
<height>31</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>15</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Reset</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="labelCounter">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>30</x>
|
||||
<y>20</y>
|
||||
<width>151</width>
|
||||
<height>41</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>30</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Counter</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignmentFlag::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QSlider" name="slider">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>390</x>
|
||||
<y>210</y>
|
||||
<width>16</width>
|
||||
<height>160</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="value">
|
||||
<number>50</number>
|
||||
</property>
|
||||
<property name="orientation">
|
||||
<enum>Qt::Orientation::Vertical</enum>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLineEdit" name="inputMax">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>370</x>
|
||||
<y>190</y>
|
||||
<width>61</width>
|
||||
<height>21</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>12</pointsize>
|
||||
<stylestrategy>PreferDefault</stylestrategy>
|
||||
</font>
|
||||
</property>
|
||||
<property name="layoutDirection">
|
||||
<enum>Qt::LayoutDirection::LeftToRight</enum>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>100</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignmentFlag::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLineEdit" name="inputStep">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>370</x>
|
||||
<y>170</y>
|
||||
<width>61</width>
|
||||
<height>21</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>12</pointsize>
|
||||
<stylestrategy>PreferDefault</stylestrategy>
|
||||
</font>
|
||||
</property>
|
||||
<property name="layoutDirection">
|
||||
<enum>Qt::LayoutDirection::LeftToRight</enum>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>100</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignmentFlag::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLineEdit" name="inputValue">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>360</x>
|
||||
<y>140</y>
|
||||
<width>81</width>
|
||||
<height>31</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>16</pointsize>
|
||||
<stylestrategy>PreferDefault</stylestrategy>
|
||||
</font>
|
||||
</property>
|
||||
<property name="layoutDirection">
|
||||
<enum>Qt::LayoutDirection::LeftToRight</enum>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>100</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignmentFlag::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLineEdit" name="inputMin">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>370</x>
|
||||
<y>370</y>
|
||||
<width>61</width>
|
||||
<height>21</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>12</pointsize>
|
||||
<stylestrategy>PreferDefault</stylestrategy>
|
||||
</font>
|
||||
</property>
|
||||
<property name="layoutDirection">
|
||||
<enum>Qt::LayoutDirection::LeftToRight</enum>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>100</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignmentFlag::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
0
src/ui/SmartSlider.ui
Normal file
0
src/ui/SmartSlider.ui
Normal file
Reference in New Issue
Block a user