78 lines
2.1 KiB
C++
78 lines
2.1 KiB
C++
#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;
|
|
} |