edit button shortcuts with the ButtonManager

This commit is contained in:
mrbesen 2022-04-02 02:26:43 +02:00
parent 83eaed56c1
commit 129140331a
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
6 changed files with 262 additions and 0 deletions

View File

@ -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;

40
include/editbutton.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef EDITBUTTON_H
#define EDITBUTTON_H
#include <QDialog>
#include <QLineEdit>
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

View File

@ -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

View File

@ -3,6 +3,7 @@
#include <algorithm>
#include <Log.h>
#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<RowItem>()->getConfig().size()-1 || getParent<RowItem>()->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<RowItem>()->getConfig();
rowconf.erase(rowconf.begin() + pos);

78
src/editbutton.cpp Normal file
View File

@ -0,0 +1,78 @@
#include "editbutton.h"
#include "ui_editbutton.h"
#include <QRegExpValidator>
#include <Log.h>
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;
}

113
ui/editbutton.ui Normal file
View File

@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>EditButton</class>
<widget class="QDialog" name="EditButton">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item>
<layout class="QHBoxLayout" name="buttonNamerRow">
<item>
<widget class="QLabel" name="buttonNameLabel">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="buttonName">
<property name="placeholderText">
<string>Button Name</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="ButtonKeyRow">
<item>
<widget class="QLabel" name="buttonKeyLabel">
<property name="text">
<string>Shortcut:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="buttonKey">
<property name="inputMethodHints">
<set>Qt::ImhPreferLatin</set>
</property>
<property name="placeholderText">
<string>Ctrl+Num+1</string>
</property>
<property name="clearButtonEnabled">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>EditButton</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>EditButton</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>