soundboard/src/mainwindow.cpp

109 lines
2.6 KiB
C++
Raw Normal View History

2021-12-13 00:28:04 +01:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
2021-12-13 01:23:03 +01:00
#include <string>
2021-12-13 00:28:04 +01:00
2021-12-13 14:48:43 +01:00
#include <Log.h>
2021-12-13 01:23:03 +01:00
2021-12-13 17:28:32 +01:00
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::Soundboard) {
2021-12-13 14:48:43 +01:00
ui->setupUi(this);
2021-12-13 00:28:04 +01:00
2021-12-13 14:48:43 +01:00
reloadConfig();
2021-12-13 01:23:03 +01:00
2021-12-13 17:28:32 +01:00
QObject::connect(ui->reloadButton, SIGNAL(clicked()), this, SLOT(reloadConfig()));
QObject::connect(this, SIGNAL(newStatusMessage(const QString&)), ui->statusbar, SLOT(showMessage(const QString&)));
2021-12-13 00:28:04 +01:00
}
MainWindow::~MainWindow() {
delete ui;
Sound::deinit();
}
2021-12-14 10:12:24 +01:00
// https://stackoverflow.com/a/4857631/4399859
static void clearLayout(QLayout* layout) {
if (layout == NULL)
return;
QLayoutItem* item;
while ((item = layout->takeAt(0))) {
if (item->layout()) {
clearLayout(item->layout());
delete item->layout();
}
if (item->widget()) {
delete item->widget();
}
delete item;
}
}
2021-12-13 14:48:43 +01:00
void MainWindow::reloadConfig() {
Log::info << "realodConfig()";
2021-12-13 00:28:04 +01:00
2021-12-13 14:48:43 +01:00
QString loaded = QString::fromStdString("loading config");
emit newStatusMessage(loaded);
2021-12-13 00:28:04 +01:00
2021-12-13 14:48:43 +01:00
config.load();
2021-12-13 00:28:04 +01:00
2021-12-13 14:48:43 +01:00
Sound& sound = Sound::instance(); // init sound
sound.reset();
const std::vector<std::string>& devices = config.rootConfig.audio.devices;
2021-12-13 17:28:32 +01:00
for (const std::string& device : devices) {
2021-12-13 14:48:43 +01:00
Log::note << "loadAudio device: \"" << device << '"';
2021-12-13 17:28:32 +01:00
if (!sound.addDeviceWithName(device)) {
2021-12-13 14:48:43 +01:00
Log::warn << "AudioDevice could not be loaded: \"" << device << '"';
QString done = QString::fromStdString("Sound Device: " + device + " not found!");
emit newStatusMessage(done);
return;
}
}
2021-12-13 00:28:04 +01:00
2021-12-13 17:28:32 +01:00
Log::debug << "Sound devices loaded";
//remove old buttons
for (SoundButton* sb : soundbuttons) {
ui->gridLayout->removeWidget(sb->getButton());
delete sb;
}
soundbuttons.clear();
2021-12-14 10:12:24 +01:00
// clearLayout(ui->gridLayout);
2021-12-13 17:28:32 +01:00
Sound::FOLDER = config.rootConfig.audioPath;
Log::debug << "Sound::FOLDER: " << Sound::FOLDER;
int x = 0;
int y = 0;
2021-12-14 10:12:24 +01:00
const std::vector<std::vector<Config::ButtonConfig>>& buttonConfigs = config.rootConfig.buttons;
for (const std::vector<Config::ButtonConfig>& line : buttonConfigs) {
for (const Config::ButtonConfig& bc : line) {
if (!bc.isValid()) continue;
2021-12-13 17:28:32 +01:00
SoundButton* sb = new SoundButton(bc.file, bc.name, bc.key);
sb->setStartMS(bc.offset);
sb->setLengthMS(bc.length);
sb->setVolume(bc.volume);
Log::info << "Button: " << bc.name << ": " << bc.file << " " << x << " " << y;
soundbuttons.push_back(sb);
2021-12-14 10:12:24 +01:00
ui->gridLayout->addWidget(sb->getButton(), y, x, 1, bc.width);
2021-12-13 17:28:32 +01:00
sb->getButton()->show();
2021-12-14 10:12:24 +01:00
x += bc.width;
2021-12-13 17:28:32 +01:00
}
2021-12-14 10:12:24 +01:00
ui->gridLayout->setRowMinimumHeight(y, 40);
x = 0;
y++;
2021-12-13 17:28:32 +01:00
}
2021-12-13 14:48:43 +01:00
QString done = QString::fromStdString("config loaded");
emit newStatusMessage(done);
2021-12-13 00:28:04 +01:00
2021-12-13 14:48:43 +01:00
Log::info << "realodConfig() done";
2021-12-13 00:28:04 +01:00
}