soundboard/src/soundbutton.cpp

114 lines
2.6 KiB
C++
Raw Normal View History

2021-12-13 14:48:43 +01:00
#include "soundbutton.h"
#include <QTextItem>
2021-12-13 14:48:43 +01:00
#include "sound.h"
2021-12-14 10:32:23 +01:00
#include <Log.h>
2021-12-13 14:48:43 +01:00
uint64_t SoundButton::nextid = 0;
2021-12-15 14:39:39 +01:00
SoundButton::SoundButton(const std::string& filename, const std::string& name_, const std::string& keycombo, QWidget* parent) : QPushButton(parent), id(nextid++), file(filename), keycombo(keycombo) {
2021-12-13 14:48:43 +01:00
name = (name_.empty() ? filename : name_);
2021-12-15 14:39:39 +01:00
QString info = getInfo();
setText(info);
setObjectName(QString::fromStdString("soundButton" + std::to_string(id)));
setMinimumSize(QSize(80, 20));
2021-12-15 14:39:39 +01:00
setToolTip(info);
2021-12-15 00:04:35 +01:00
setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
2021-12-13 14:48:43 +01:00
2021-12-14 10:12:24 +01:00
QKeySequence seq(QString::fromStdString(keycombo));
globalShortcut = new QxtGlobalShortcut(seq);
2021-12-13 14:48:43 +01:00
QObject::connect(globalShortcut, SIGNAL( activated() ), this, SLOT( play() ));
2021-12-15 14:39:39 +01:00
QObject::connect(this, SIGNAL( clicked() ), this, SLOT( play() ));
2021-12-13 14:48:43 +01:00
}
SoundButton::~SoundButton() {
delete globalShortcut;
}
const std::string& SoundButton::getName() const {
return name;
}
const std::string& SoundButton::getFile() const {
return file;
}
const std::string& SoundButton::getKeyCombo() const {
return keycombo;
}
2021-12-13 17:28:32 +01:00
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;
}
2021-12-15 14:39:39 +01:00
void SoundButton::setHighlighted(bool highlighted) {
if(highlighted) {
2021-12-17 01:26:28 +01:00
setStyleSheet("SoundButton { color: red; }");
//setText("-> " + getInfo());
2021-12-15 14:39:39 +01:00
} else {
2021-12-17 01:26:28 +01:00
setStyleSheet("SoundButton {}");
//setText(getInfo());
2021-12-15 14:39:39 +01:00
}
2021-12-13 14:48:43 +01:00
}
void SoundButton::paintEvent(QPaintEvent* event) {
QPushButton::paintEvent(event);
QString text = "";
// cancel if nothing to print
if(text.isEmpty())
return;
QPainter painter(this);
painter.setPen(QPen(Qt::white));
QPoint textPos(0, painter.fontMetrics().height());
textPos.rx() = width() - painter.fontMetrics().horizontalAdvance(text) - 6;
painter.drawText(textPos, text);
}
2021-12-13 14:48:43 +01:00
void SoundButton::play() {
2021-12-14 10:32:23 +01:00
if(disabled) return;
2021-12-13 17:28:32 +01:00
uint64_t endms = (lengthms == 0 ? 0 : startms + lengthms);
2021-12-14 10:32:23 +01:00
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;
2021-12-15 14:39:39 +01:00
setEnabled(false);
}
QString SoundButton::getInfo() const {
return QString::fromStdString(name + (keycombo.empty() ? "" : "\n" + keycombo));
2021-12-13 14:48:43 +01:00
}