Soundbutton as QPushButton

This commit is contained in:
mrbesen 2021-12-15 14:39:39 +01:00
parent eff245ce6e
commit 2ba6aef1de
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
3 changed files with 35 additions and 30 deletions

View File

@ -1,17 +1,18 @@
#pragma once
#include <string>
#include <QtCore/QObject>
#include <QPushButton>
#include <cstdint>
#include <QPushButton>
#include "qxtglobalshortcut.h"
class SoundButton : public QObject {
class SoundButton : public QPushButton
{
Q_OBJECT
public:
SoundButton(const std::string& filename, const std::string& name_ = "", const std::string& keycombo = "");
explicit SoundButton(const std::string& filename, const std::string& name_ = "", const std::string& keycombo = "", QWidget* parent = nullptr);
~SoundButton();
const std::string& getName() const;
@ -26,13 +27,14 @@ public:
void setLengthMS(uint64_t lengthms);
void setVolume(float v);
QPushButton* getButton();
void setHighlighted(bool highlighted);
public slots:
void play();
private:
void setDisabled();
QString getInfo() const;
static uint64_t nextid;
@ -47,5 +49,4 @@ private:
bool disabled = false;
QxtGlobalShortcut* globalShortcut = nullptr;
QPushButton* button = nullptr;
};

View File

@ -145,7 +145,7 @@ void MainWindow::loadSoundFromConfig() {
void MainWindow::removeAllButtons() {
//remove old buttons
for (SoundButton* sb : soundbuttons) {
ui->gridLayout->removeWidget(sb->getButton());
ui->gridLayout->removeWidget(sb);
delete sb;
}
soundbuttons.clear();
@ -168,8 +168,8 @@ void MainWindow::loadButtonsFromConfig() {
soundbuttons.push_back(sb);
ui->gridLayout->addWidget(sb->getButton(), y, x, 1, bc.width);
sb->getButton()->show();
ui->gridLayout->addWidget(sb, y, x, 1, bc.width);
sb->show();
x += bc.width;
}
@ -188,8 +188,8 @@ void MainWindow::selectCurrentButton() {
QLayoutItem* item = findCurrentButton();
if(item) {
if(item->widget()) {
QPushButton* button = (QPushButton*) item->widget();
button->setText(">");
SoundButton* button = (SoundButton*) item->widget();
button->setHighlighted(true);
}
}
}
@ -198,8 +198,8 @@ void MainWindow::unselectCurrentButton() {
QLayoutItem* item = findCurrentButton();
if(item) {
if(item->widget()) {
QPushButton* button = (QPushButton*) item->widget();
button->setText(" ");
SoundButton* button = (SoundButton*) item->widget();
button->setHighlighted(false);
}
}
}
@ -208,7 +208,7 @@ 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;
Log::info << "check: " << (int) ypos << " " << (int) step;
QLayoutItem* item = ui->gridLayout->itemAtPosition(ypos, step);
if(lastitem == item) continue;

View File

@ -6,19 +6,14 @@
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) {
SoundButton::SoundButton(const std::string& filename, const std::string& name_, const std::string& keycombo, QWidget* parent) : QPushButton(parent), id(nextid++), file(filename), keycombo(keycombo) {
name = (name_.empty() ? filename : name_);
std::string keycomb;
if(!keycombo.empty()) {
keycomb = "\n" + keycombo;
}
QString info = QString::fromStdString(name + keycomb);
button = new QPushButton(info);
button->setObjectName(QString::fromStdString("soundButton" + std::to_string(id)));
button->setMinimumHeight(50);
button->setToolTip(info);
QString info = getInfo();
setText(info);
setObjectName(QString::fromStdString("soundButton" + std::to_string(id)));
setMinimumHeight(50);
setToolTip(info);
// button->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum));
@ -26,12 +21,11 @@ SoundButton::SoundButton(const std::string& filename, const std::string& name_,
globalShortcut = new QxtGlobalShortcut(seq);
QObject::connect(globalShortcut, SIGNAL( activated() ), this, SLOT( play() ));
QObject::connect(button, SIGNAL( clicked() ), this, SLOT( play() ));
QObject::connect(this, SIGNAL( clicked() ), this, SLOT( play() ));
}
SoundButton::~SoundButton() {
delete globalShortcut;
delete button;
}
const std::string& SoundButton::getName() const {
@ -70,8 +64,14 @@ void SoundButton::setVolume(float v) {
volume = v;
}
QPushButton* SoundButton::getButton() {
return button;
void SoundButton::setHighlighted(bool highlighted) {
if(highlighted) {
setStyleSheet("QPushButton { background: red; }");
setText(">" + getInfo());
} else {
setStyleSheet("QPushButton {}");
setText(getInfo());
}
}
void SoundButton::play() {
@ -88,5 +88,9 @@ void SoundButton::play() {
void SoundButton::setDisabled() {
disabled = true;
button->setEnabled(false);
setEnabled(false);
}
QString SoundButton::getInfo() const {
return QString::fromStdString(name + (keycombo.empty() ? "" : "\n" + keycombo));
}