soundboard/src/soundbutton.cpp
2021-12-14 10:32:23 +01:00

82 lines
1.8 KiB
C++

#include "soundbutton.h"
#include "sound.h"
#include <Log.h>
uint64_t SoundButton::nextid = 0;
SoundButton::SoundButton(const std::string& filename, const std::string& name_, const std::string& keycombo) : id(nextid++), file(filename), keycombo(keycombo) {
name = (name_.empty() ? filename : name_);
button = new QPushButton(QString::fromStdString(name));
button->setObjectName(QString::fromStdString("soundButton" + std::to_string(id)));
QKeySequence seq(QString::fromStdString(keycombo));
globalShortcut = new QxtGlobalShortcut(seq);
QObject::connect(globalShortcut, SIGNAL( activated() ), this, SLOT( play() ));
QObject::connect(button, SIGNAL( clicked() ), this, SLOT( play() ));
}
SoundButton::~SoundButton() {
delete globalShortcut;
delete button;
}
const std::string& SoundButton::getName() const {
return name;
}
const std::string& SoundButton::getFile() const {
return file;
}
const std::string& SoundButton::getKeyCombo() const {
return keycombo;
}
uint64_t SoundButton::getStartMS() const {
return startms;
}
uint64_t SoundButton::getLengthMS() const {
return lengthms;
}
float SoundButton::getVolume() const {
return volume;
}
void SoundButton::setStartMS(uint64_t startms_) {
startms = startms_;
}
void SoundButton::setLengthMS(uint64_t lengthms_) {
lengthms = lengthms_;
}
void SoundButton::setVolume(float v) {
volume = v;
}
QPushButton* SoundButton::getButton() {
return button;
}
void SoundButton::play() {
if(disabled) return;
uint64_t endms = (lengthms == 0 ? 0 : startms + lengthms);
try {
Sound::instance().addPlayback(file, volume, startms, endms);
} catch(const std::exception& e) {
Log::error << "Catched Exception when plaing Audio File: " << file;
setDisabled();
}
}
void SoundButton::setDisabled() {
disabled = true;
button->setText(QString::fromStdString(name + " DISABLED"));
}