soundboard/src/mainwindow.cpp

226 lines
5.5 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
up = new QxtGlobalShortcut(QKeySequence("Shift+Up"));
down = new QxtGlobalShortcut(QKeySequence("Shift+Down"));
left = new QxtGlobalShortcut(QKeySequence("Shift+Left"));
right = new QxtGlobalShortcut(QKeySequence("Shift+Right"));
play = new QxtGlobalShortcut(QKeySequence("Shift+Num+Enter"));
QKeySequence seq(QString::fromStdString("Shift+Num+0"));
stopGlobal = new QxtGlobalShortcut(seq);
2021-12-13 01:23:03 +01:00
reloadConfig();
2021-12-13 17:28:32 +01:00
QObject::connect(ui->reloadButton, SIGNAL(clicked()), this, SLOT(reloadConfig()));
2021-12-14 12:46:57 +01:00
QObject::connect(ui->stopButton, SIGNAL( clicked() ), this, SLOT( stop() ));
QObject::connect(stopGlobal, SIGNAL( activated() ), this, SLOT( stop() ));
2021-12-13 17:28:32 +01:00
QObject::connect(this, SIGNAL(newStatusMessage(const QString&)), ui->statusbar, SLOT(showMessage(const QString&)));
QObject::connect(up, SIGNAL( activated() ), this, SLOT( moveUp() ));
QObject::connect(down, SIGNAL( activated() ), this, SLOT( moveDown() ));
QObject::connect(left, SIGNAL( activated() ), this, SLOT( moveLeft() ));
QObject::connect(right, SIGNAL( activated() ), this, SLOT( moveRight() ));
QObject::connect(play, SIGNAL( activated() ), this, SLOT( playCurrent() ));
2021-12-13 00:28:04 +01:00
}
MainWindow::~MainWindow() {
delete ui;
2021-12-14 12:46:57 +01:00
delete stopGlobal;
2021-12-13 00:28:04 +01:00
delete up;
delete down;
delete left;
delete right;
delete play;
2021-12-13 00:28:04 +01:00
Sound::deinit();
}
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-14 10:23:06 +01:00
loadSoundFromConfig();
removeAllButtons();
loadButtonsFromConfig();
QString done = QString::fromStdString("config loaded");
emit newStatusMessage(done);
Log::info << "realodConfig() done";
}
2021-12-14 12:46:57 +01:00
void MainWindow::stop() {
Sound::instance().stopAll();
}
void MainWindow::moveUp() {
Log::debug << "up";
2021-12-15 01:46:54 +01:00
unselectCurrentButton();
ypos--;
if(ypos >= ui->gridLayout->rowCount()) {
ypos = ui->gridLayout->rowCount()-1;
}
selectCurrentButton();
}
void MainWindow::moveDown() {
Log::debug << "down";
2021-12-15 01:46:54 +01:00
unselectCurrentButton();
ypos++;
if(ypos >= ui->gridLayout->rowCount()) {
ypos = ui->gridLayout->rowCount()-1;
}
selectCurrentButton();
}
void MainWindow::moveLeft() {
Log::debug << "left";
2021-12-15 01:46:54 +01:00
unselectCurrentButton();
xpos--;
if(xpos >= ui->gridLayout->columnCount()) {
xpos = ui->gridLayout->columnCount()-1;
}
selectCurrentButton();
}
void MainWindow::moveRight() {
Log::debug << "right";
2021-12-15 01:46:54 +01:00
unselectCurrentButton();
xpos++;
if(xpos >= ui->gridLayout->columnCount()) {
xpos = ui->gridLayout->columnCount()-1;
}
selectCurrentButton();
}
void MainWindow::playCurrent() {
Log::debug << "play";
2021-12-15 01:46:54 +01:00
QLayoutItem* item = findCurrentButton();
if(item) {
if(item->widget()) {
QPushButton* button = (QPushButton*) item->widget();
button->click();
}
}
}
2021-12-14 10:23:06 +01:00
void MainWindow::loadSoundFromConfig() {
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";
2021-12-14 10:23:06 +01:00
Sound::FOLDER = config.rootConfig.audioPath;
Log::debug << "Sound::FOLDER: " << Sound::FOLDER;
}
void MainWindow::removeAllButtons() {
2021-12-13 17:28:32 +01:00
//remove old buttons
for (SoundButton* sb : soundbuttons) {
ui->gridLayout->removeWidget(sb->getButton());
delete sb;
}
soundbuttons.clear();
2021-12-14 10:23:06 +01:00
}
2021-12-13 17:28:32 +01:00
2021-12-14 10:23:06 +01:00
void MainWindow::loadButtonsFromConfig() {
2021-12-13 17:28:32 +01:00
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-15 00:04:35 +01:00
ui->gridLayout->setRowMinimumHeight(y, 60);
2021-12-14 10:12:24 +01:00
x = 0;
y++;
2021-12-13 17:28:32 +01:00
}
2021-12-15 00:04:35 +01:00
2021-12-15 01:46:54 +01:00
xpos = 0;
ypos = 0;
2021-12-15 00:04:35 +01:00
// ui->gridLayoutWidget->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
2021-12-13 00:28:04 +01:00
}
2021-12-15 01:46:54 +01:00
void MainWindow::selectCurrentButton() {
QLayoutItem* item = findCurrentButton();
if(item) {
if(item->widget()) {
QPushButton* button = (QPushButton*) item->widget();
button->setText(">");
}
}
}
void MainWindow::unselectCurrentButton() {
QLayoutItem* item = findCurrentButton();
if(item) {
if(item->widget()) {
QPushButton* button = (QPushButton*) item->widget();
button->setText(" ");
}
}
}
QLayoutItem* MainWindow::findCurrentButton() const {
QLayoutItem* lastitem = nullptr;
int8_t rstep = -1;
for(uint8_t step = 0; step < ui->gridLayout->columnCount() && rstep < xpos; ++step) {
Log::info << "check: " << ypos << " " << step;
QLayoutItem* item = ui->gridLayout->itemAtPosition(ypos, step);
if(lastitem == item) continue;
rstep++;
lastitem = item;
}
if(lastitem != nullptr && rstep == xpos) {
return lastitem;
}
Log::debug << "current button is null";
return nullptr;
}