From 129140331a2240a4bc576f55410693ce37e81db2 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Sat, 2 Apr 2022 02:26:43 +0200 Subject: [PATCH] edit button shortcuts with the ButtonManager --- include/buttonmanageritems.h | 2 + include/editbutton.h | 40 +++++++++++++ soundboard.pro | 3 + src/buttonmanageritems.cpp | 26 ++++++++ src/editbutton.cpp | 78 ++++++++++++++++++++++++ ui/editbutton.ui | 113 +++++++++++++++++++++++++++++++++++ 6 files changed, 262 insertions(+) create mode 100644 include/editbutton.h create mode 100644 src/editbutton.cpp create mode 100644 ui/editbutton.ui diff --git a/include/buttonmanageritems.h b/include/buttonmanageritems.h index 813e004..5abe98a 100644 --- a/include/buttonmanageritems.h +++ b/include/buttonmanageritems.h @@ -78,10 +78,12 @@ public: const static int TYPE = 1001; + virtual bool hasEdit() const override; virtual bool hasRemove() const override; virtual bool hasMoveUp() const override; virtual bool hasMoveDown() const override; + virtual void edit() override; virtual void remove() override; virtual void moveUp() override; virtual void moveDown() override; diff --git a/include/editbutton.h b/include/editbutton.h new file mode 100644 index 0000000..6a83c9d --- /dev/null +++ b/include/editbutton.h @@ -0,0 +1,40 @@ +#ifndef EDITBUTTON_H +#define EDITBUTTON_H + +#include +#include + +namespace Ui { + class EditButton; +} + +class EditButton : public QDialog +{ + Q_OBJECT + +public: + explicit EditButton(QWidget *parent = nullptr); + ~EditButton(); + + const std::string& getName() const; + const std::string& getKey() const; + + void setName(const std::string& name); + void setKey(const std::string& key); + +private slots: + void editName(const QString& text); + void editShortcut(const QString& text); + +private: + Ui::EditButton *ui; + + std::string name; + std::string key; + + bool validShortcut = true; + + bool setTextValid(QLineEdit* box, QString& newtext) const; +}; + +#endif // EDITBUTTON_H diff --git a/soundboard.pro b/soundboard.pro index 52b7ecc..ffc5f4b 100644 --- a/soundboard.pro +++ b/soundboard.pro @@ -25,6 +25,7 @@ SOURCES += \ src/sounddevice.cpp \ src/soundbutton.cpp \ src/config.cpp \ + src/editbutton.cpp \ src/editsample.cpp \ src/soundview.cpp \ src/samplereader.cpp \ @@ -39,6 +40,7 @@ HEADERS += \ include/sounddevice.h \ include/soundbutton.h \ include/config.h \ + include/editbutton.h \ include/editsample.h \ include/soundview.h \ include/samplereader.h \ @@ -55,6 +57,7 @@ OBJECTS_DIR = obj/ FORMS += \ ui/addnewwhat.ui \ ui/buttonmanager.ui \ + ui/editbutton.ui \ ui/editsample.ui \ ui/mainwindow.ui diff --git a/src/buttonmanageritems.cpp b/src/buttonmanageritems.cpp index 068001b..4a04b2c 100644 --- a/src/buttonmanageritems.cpp +++ b/src/buttonmanageritems.cpp @@ -3,6 +3,7 @@ #include #include +#include "editbutton.h" #include "editsample.h" ButtonManagerItem::ButtonManagerItem(QTreeWidgetItem* parent, int type, uint8_t pos) : QTreeWidgetItem(parent, type), mparent(parent), pos(pos) { @@ -121,6 +122,8 @@ RowItem::RowItem(QTreeWidgetItem* parent, uint8_t rownr, Config::RootConfig& con // iterate buttons in a row for(uint8_t btnnr = 0; btnnr < btnrow.size(); btnnr++) { ButtonItem* btnItem = new ButtonItem(this, btnnr, conf); + // no need to use this var, they add themself to their parent (this) + (void) btnItem; } } @@ -199,9 +202,15 @@ ButtonItem::ButtonItem(RowItem* parent, uint8_t buttonnr, Config::RootConfig& co // iterate samples in a button for(uint8_t samplenr = 0; samplenr < btn.samples.size(); ++samplenr) { SampleItem* sample = new SampleItem(this, samplenr, conf); + // no need to use this var, they add themself to their parent (this) + (void) sample; } } +bool ButtonItem::hasEdit() const { + return true; +} + bool ButtonItem::hasRemove() const { return true; } @@ -214,6 +223,23 @@ bool ButtonItem::hasMoveDown() const { return pos < getParent()->getConfig().size()-1 || getParent()->hasMoveDown(); } +void ButtonItem::edit() { + Config::ButtonConfig& buttonConf = getConfig(); + + EditButton editor; + editor.setName(buttonConf.name); + editor.setKey(buttonConf.key); + editor.exec(); + + if(editor.result() == 0) { + Log::info << "Dialog failed"; + return; + } + + buttonConf.name = editor.getName(); + buttonConf.key = editor.getKey(); +} + void ButtonItem::remove() { auto& rowconf = getParent()->getConfig(); rowconf.erase(rowconf.begin() + pos); diff --git a/src/editbutton.cpp b/src/editbutton.cpp new file mode 100644 index 0000000..107bddd --- /dev/null +++ b/src/editbutton.cpp @@ -0,0 +1,78 @@ +#include "editbutton.h" +#include "ui_editbutton.h" + +#include + +#include + +class MyKeysequenceValidtor : public QValidator { +public: + explicit MyKeysequenceValidtor(QObject * parent = nullptr) : QValidator(parent) {} + ~MyKeysequenceValidtor() {} + + virtual State validate(QString& str, [[maybe_unused]] int& pos) const override { + QKeySequence seq(str); + return (seq.toString().isEmpty() && !str.isEmpty()) ? State::Intermediate : State::Acceptable; + } +}; + + +EditButton::EditButton(QWidget *parent) : QDialog(parent), ui(new Ui::EditButton) { + ui->setupUi(this); + + ui->buttonName->setValidator(new QRegExpValidator(QRegExp("\\w{1,64}"))); + ui->buttonKey->setValidator(new MyKeysequenceValidtor()); + + QObject::connect(ui->buttonName, SIGNAL( textEdited(const QString&) ), this, SLOT( editName(const QString&) )); + QObject::connect(ui->buttonKey, SIGNAL( textEdited(const QString&) ), this, SLOT( editShortcut(const QString&) )); +} + +EditButton::~EditButton() { + delete ui; +} + +const std::string& EditButton::getName() const { + return name; +} + +const std::string& EditButton::getKey() const { + return key; +} + + +void EditButton::setName(const std::string& name) { + this->name = name; + ui->buttonName->setText(QString::fromStdString(this->name)); +} +void EditButton::setKey(const std::string& key) { + this->key = key; + ui->buttonKey->setText(QString::fromStdString(this->key)); +} + +void EditButton::editName(const QString& text) { + // neded without const + QString copy(text); + if(setTextValid(ui->buttonName, copy)) { + name = text.toStdString(); + } +} + +void EditButton::editShortcut(const QString& text) { + // neded without const + QString copy(text); + if(setTextValid(ui->buttonKey, copy)) { + key = text.toStdString(); + } +} + +bool EditButton::setTextValid(QLineEdit* box, QString& newtext) const { + int pos = 0; + bool valid = box->validator()->validate(newtext, pos) == QValidator::State::Acceptable; + if(valid) { + box->setStyleSheet(QString("")); + } else { + // text-decoration-color: red; text-decoration-style: wavy; + box->setStyleSheet(QString("QLineEdit { border: 1px solid red; border-radius: 0.1em; text-decoration: underline; }")); + } + return valid; +} \ No newline at end of file diff --git a/ui/editbutton.ui b/ui/editbutton.ui new file mode 100644 index 0000000..2c3da89 --- /dev/null +++ b/ui/editbutton.ui @@ -0,0 +1,113 @@ + + + EditButton + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + QLayout::SetDefaultConstraint + + + + + + + Name: + + + + + + + Button Name + + + + + + + + + + + Shortcut: + + + + + + + Qt::ImhPreferLatin + + + Ctrl+Num+1 + + + false + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + EditButton + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + EditButton + reject() + + + 316 + 260 + + + 286 + 274 + + + + +