forked from MrBesen/soundboard
101 lines
2.6 KiB
C++
101 lines
2.6 KiB
C++
#include "soundbutton.h"
|
|
|
|
#include <QTextItem>
|
|
|
|
#include "sound.h"
|
|
|
|
#include <Log.h>
|
|
|
|
uint64_t SoundButton::nextid = 0;
|
|
|
|
SoundButton::SoundButton(const std::string& name_, const std::string& keycombo, QWidget* parent) : QPushButton(parent), id(nextid++), name(name_), keycombo(keycombo) {
|
|
QString info = getInfo();
|
|
setText(info);
|
|
setObjectName(QString::fromStdString("soundButton" + std::to_string(id)));
|
|
setMinimumSize(QSize(80, 20));
|
|
setToolTip(info);
|
|
|
|
setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
|
|
|
|
QKeySequence seq(QString::fromStdString(keycombo));
|
|
globalShortcut = new QxtGlobalShortcut(seq);
|
|
|
|
QObject::connect(globalShortcut, SIGNAL( activated() ), this, SLOT( play() ));
|
|
QObject::connect(this, SIGNAL( clicked() ), this, SLOT( play() ));
|
|
}
|
|
|
|
SoundButton::~SoundButton() {
|
|
delete globalShortcut;
|
|
}
|
|
|
|
const std::string& SoundButton::getName() const {
|
|
return name;
|
|
}
|
|
|
|
const std::string& SoundButton::getKeyCombo() const {
|
|
return keycombo;
|
|
}
|
|
|
|
void SoundButton::addSample(Sample& s) {
|
|
samples.push_back(s);
|
|
}
|
|
|
|
void SoundButton::setHighlighted(bool highlighted_) {
|
|
highlighted = highlighted_;
|
|
|
|
repaint();
|
|
}
|
|
|
|
void SoundButton::paintEvent(QPaintEvent* event) {
|
|
QPushButton::paintEvent(event);
|
|
|
|
QPainter painter(this);
|
|
if(highlighted) {
|
|
QSize s = size();
|
|
QRect rect(HIGHLIGHTBORDEROFFSET, HIGHLIGHTBORDEROFFSET, s.width() - HIGHLIGHTBORDEROFFSET*2, s.height() - HIGHLIGHTBORDEROFFSET*2);
|
|
painter.setPen(QPen(Qt::red));
|
|
painter.drawRect(rect);
|
|
}
|
|
|
|
if(samples.size() > 1) {
|
|
QString text = QString::fromStdString(std::to_string(currentSample+1) + "/" + std::to_string(samples.size()));
|
|
|
|
painter.setPen(QPen(Qt::white));
|
|
QPoint textPos(0, painter.fontMetrics().height());
|
|
textPos.rx() = width() - painter.fontMetrics().horizontalAdvance(text) - 6;
|
|
painter.drawText(textPos, text);
|
|
}
|
|
}
|
|
|
|
void SoundButton::play() {
|
|
if(disabled) return;
|
|
|
|
if(samples.size() == 0) {
|
|
Log::error << "Button " << name << " has no sample set";
|
|
setDisabled();
|
|
return;
|
|
}
|
|
|
|
const Sample& sample = samples.at(currentSample++);
|
|
if(currentSample >= samples.size())
|
|
currentSample = 0;
|
|
|
|
uint64_t endms = (sample.lengthms == 0 ? 0 : sample.startms + sample.lengthms);
|
|
try {
|
|
Sound::instance().addPlayback(sample.file, sample.volume, sample.startms, endms);
|
|
} catch(const std::exception& e) {
|
|
Log::error << "Catched Exception when plaing Audio File: " << sample.file;
|
|
setDisabled();
|
|
}
|
|
|
|
repaint();
|
|
}
|
|
|
|
void SoundButton::setDisabled() {
|
|
disabled = true;
|
|
setEnabled(false);
|
|
}
|
|
|
|
QString SoundButton::getInfo() const {
|
|
return QString::fromStdString(name + (keycombo.empty() ? "" : "\n" + keycombo));
|
|
} |