schema_editor/comdel/display/attribute_dialog.h

199 lines
6.1 KiB
C
Raw Normal View History

2022-04-19 06:16:41 +00:00
#ifndef ATTRIBUTE_DIALOG_H
#define ATTRIBUTE_DIALOG_H
#include <QIntValidator>
2022-05-15 14:13:29 +00:00
#include <QPushButton>
#include <QComboBox>
2022-04-19 06:16:41 +00:00
#include <QGroupBox>
#include <QRadioButton>
#include <QDialog>
#include <QLineEdit>
#include <QLabel>
#include <QVBoxLayout>
#include <utility>
2022-04-19 06:16:41 +00:00
#include <comdel/domain/instanceattribute.h>
#include <comdel/domain/value.h>
#include "comdel/domain/comdelvalidator.h"
2022-04-19 06:16:41 +00:00
namespace display {
class AttributeDialog: public QDialog {
2022-05-15 14:13:29 +00:00
domain::Value value;
long long int parseInt(std::string expression) {
try {
if (expression.size() > 2) {
if (expression.substr(0, 2) == "0x") {
return std::stoll(expression, 0, 16);
} else if (expression.substr(0, 2) == "0b") {
return std::stoll(expression, 0, 2);
} else {
return std::stoll(expression, 0, 10);
}
} else {
return std::stoll(expression, 0, 10);
}
} catch (std::exception &e) {
return 0;
}
}
domain::InstanceAttribute* attributeValue;
2022-04-19 06:16:41 +00:00
public:
AttributeDialog(domain::InstanceAttribute *attribute) {
2022-05-15 14:13:29 +00:00
setAttribute(Qt::WA_DeleteOnClose);
attributeValue = attribute;
2022-04-19 06:16:41 +00:00
this->setWindowTitle(QString::fromStdString("Izmjeni " + attribute->attribute.getName()));
auto layout = new QVBoxLayout(this);
this->setLayout(layout);
auto popup = *attribute->attribute.getPopup();
layout->addWidget(new QLabel(popup.getTitle().c_str()));
layout->addWidget(new QLabel(popup.getText().c_str()));
auto type = attribute->attribute.getDefault().getType();
2022-05-15 14:13:29 +00:00
value = attribute->value;
2022-04-19 06:16:41 +00:00
if(attribute->attribute.getPopup()->isEnumerated()) {
2022-05-15 14:13:29 +00:00
auto* combo = new QComboBox(this);
auto enumeration = attribute->attribute.getPopup()->getEnumeration();
for(auto entry: enumeration) {
combo->addItem(QString::fromStdString(entry.getName()), QVariant::fromValue(NULL));
}
connect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &AttributeDialog::onEnumerationChanged);
layout->addWidget(combo);
for(int i=0; i<enumeration.size(); i++) {
if(attributeValue->value.equals(enumeration[i].getValue())) {
combo->setCurrentIndex(i);
break;
}
}
2022-04-19 06:16:41 +00:00
} else if(!(type == domain::Value::ValueType::WIRE_REFERENCE || type == domain::Value::ValueType::BOOL)) {
auto edit = new QLineEdit(this);
2022-05-15 14:13:29 +00:00
connect(edit, &QLineEdit::textChanged, this, &AttributeDialog::onTextChanged);
2022-04-19 06:16:41 +00:00
layout->addWidget(edit);
switch (attribute->attribute.getDefault().getType()) {
case domain::Value::ValueType::INT:
edit->setValidator(new QIntValidator(-10000000, 10000000, edit));
edit->insert(std::to_string(attribute->value.asInt()).c_str());
break;
case domain::Value::ValueType::STRING:
edit->insert(attribute->value.asString().c_str());
break;
}
} else if(type == domain::Value::ValueType::BOOL) {
auto *group = new QGroupBox(this);
2022-05-15 14:13:29 +00:00
auto *radioLayout = new QHBoxLayout(group);
group->setLayout(radioLayout);
2022-04-19 06:16:41 +00:00
auto isTrue = new QRadioButton("true", group);
2022-05-15 14:13:29 +00:00
connect(isTrue, &QRadioButton::clicked, [this]() {
this->value = domain::Value::fromBool(true);
});
2022-04-19 06:16:41 +00:00
auto isFalse = new QRadioButton("false", group);
2022-05-15 14:13:29 +00:00
connect(isFalse, &QRadioButton::clicked, [this]() {
this->value = domain::Value::fromBool(false);
});
2022-04-19 06:16:41 +00:00
if(attribute->value.asBool()) {
isTrue->setChecked(true);
} else {
isFalse->setChecked(true);
}
2022-05-15 14:13:29 +00:00
radioLayout->addWidget(isTrue);
radioLayout->addWidget(isFalse);
layout->addWidget(group);
}
auto button = new QPushButton("Ažuriraj");
connect(button, &QPushButton::clicked, this, &AttributeDialog::onUpdate);
2022-04-19 06:16:41 +00:00
2022-05-15 14:13:29 +00:00
layout->addWidget(button);
}
2022-04-19 06:16:41 +00:00
2022-05-15 14:13:29 +00:00
public slots:
void onTextChanged(const QString& string) {
switch (value.getType()) {
case domain::Value::STRING:
value.setString(string.toStdString());
break;
case domain::Value::INT:
value = domain::Value::fromInt(parseInt(string.toStdString()));
2022-04-19 06:16:41 +00:00
}
2022-05-15 14:13:29 +00:00
};
void onEnumerationChanged(int index) {
value = attributeValue->attribute.getPopup()->getEnumeration()[index].getValue();
}
void onUpdate();
2022-04-19 06:16:41 +00:00
};
class ErrorDialog: public QDialog {
public:
ErrorDialog(std::vector<domain::ValidationError> errors) {
setAttribute(Qt::WA_DeleteOnClose);
this->setWindowTitle("Greške");
auto layout = new QVBoxLayout(this);
this->setLayout(layout);
for(auto& err: errors) {
layout->addWidget(new QLabel(QString::fromStdString(err.message), this));
}
}
};
class WarningDialog: public QDialog {
public:
WarningDialog(domain::ValidationError error) {
setAttribute(Qt::WA_DeleteOnClose);
this->setWindowTitle("Upozorenje");
auto layout = new QVBoxLayout(this);
this->setLayout(layout);
layout->addWidget(new QLabel(QString::fromStdString(error.message), this));
auto buttonLayout = new QHBoxLayout(this);
auto okButton = new QPushButton("U redu", this);
auto cancelButton = new QPushButton("Odustani", this);
connect(okButton, &QPushButton::clicked, [this]() { accept(); });
connect(cancelButton, &QPushButton::clicked, [this]() { reject(); });
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
layout->addItem(buttonLayout);
}
};
2022-04-19 06:16:41 +00:00
}
#endif //ATTRIBUTE_DIALOG_H