91 lines
2.4 KiB
C++
91 lines
2.4 KiB
C++
#include "editbutton.h"
|
|
#include "ui_editbutton.h"
|
|
|
|
#include <QRegExpValidator>
|
|
#include "keysequencevalidator.h"
|
|
|
|
#include <Log.h>
|
|
|
|
EditButton::EditButton(QWidget *parent) : QDialog(parent), ui(new Ui::EditButton) {
|
|
ui->setupUi(this);
|
|
|
|
ui->buttonName->setValidator(new QRegExpValidator(QRegExp("(\\w+ \\w+)|(\\w+)")));
|
|
ui->buttonKey->setValidator(new KeysequenceValidator());
|
|
|
|
ui->buttonWidth->addItem("1/6", 1);
|
|
ui->buttonWidth->addItem("1/3", 2);
|
|
ui->buttonWidth->addItem("1/2", 3);
|
|
ui->buttonWidth->addItem("2/3", 4);
|
|
ui->buttonWidth->addItem("Normal", 6);
|
|
ui->buttonWidth->addItem("1 + 1/3", 8);
|
|
ui->buttonWidth->addItem("1 + 2/3", 10);
|
|
ui->buttonWidth->addItem("Double", 12);
|
|
|
|
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;
|
|
}
|
|
|
|
uint8_t EditButton::getWidth() const {
|
|
QVariant var = ui->buttonWidth->currentData();
|
|
assert(var.type() == QVariant::Type::Int);
|
|
return var.toUInt();
|
|
}
|
|
|
|
|
|
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::setWidth(uint8_t w) {
|
|
int index = ui->buttonWidth->findData((int) w);
|
|
if(index == -1) index = 4; // default to "normal"
|
|
|
|
ui->buttonWidth->setCurrentIndex(index);
|
|
}
|
|
|
|
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;
|
|
}
|