From 2ba6aef1dee31774fa5184ccdb3992990f11d8b6 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Wed, 15 Dec 2021 14:39:39 +0100 Subject: [PATCH] Soundbutton as QPushButton --- include/soundbutton.h | 13 +++++++------ src/mainwindow.cpp | 16 ++++++++-------- src/soundbutton.cpp | 36 ++++++++++++++++++++---------------- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/include/soundbutton.h b/include/soundbutton.h index d7856f6..3f5be3a 100644 --- a/include/soundbutton.h +++ b/include/soundbutton.h @@ -1,17 +1,18 @@ #pragma once #include -#include -#include #include +#include #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; }; \ No newline at end of file diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 45a9efb..99e7cc4 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -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; diff --git a/src/soundbutton.cpp b/src/soundbutton.cpp index 060dd8a..aad83a0 100644 --- a/src/soundbutton.cpp +++ b/src/soundbutton.cpp @@ -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)); } \ No newline at end of file