#ifndef ATTRIBUTE_DIALOG_H #define ATTRIBUTE_DIALOG_H #include #include #include #include #include #include #include #include #include #include #include #include #include "comdel/domain/comdelvalidator.h" namespace display { class AttributeDialog: public QDialog { 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; public: AttributeDialog(domain::InstanceAttribute *attribute) { setAttribute(Qt::WA_DeleteOnClose); attributeValue = attribute; 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(); value = attribute->value; if(attribute->attribute.getPopup()->isEnumerated()) { 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::of(&QComboBox::currentIndexChanged), this, &AttributeDialog::onEnumerationChanged); layout->addWidget(combo); for(int i=0; ivalue.equals(enumeration[i].getValue())) { combo->setCurrentIndex(i); break; } } } else if(!(type == domain::Value::ValueType::WIRE_REFERENCE || type == domain::Value::ValueType::BOOL)) { auto edit = new QLineEdit(this); connect(edit, &QLineEdit::textChanged, this, &AttributeDialog::onTextChanged); 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); auto *radioLayout = new QHBoxLayout(group); group->setLayout(radioLayout); auto isTrue = new QRadioButton("true", group); connect(isTrue, &QRadioButton::clicked, [this]() { this->value = domain::Value::fromBool(true); }); auto isFalse = new QRadioButton("false", group); connect(isFalse, &QRadioButton::clicked, [this]() { this->value = domain::Value::fromBool(false); }); if(attribute->value.asBool()) { isTrue->setChecked(true); } else { isFalse->setChecked(true); } radioLayout->addWidget(isTrue); radioLayout->addWidget(isFalse); layout->addWidget(group); } auto button = new QPushButton("Ažuriraj"); connect(button, &QPushButton::clicked, this, &AttributeDialog::onUpdate); layout->addWidget(button); } 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())); } }; void onEnumerationChanged(int index) { value = attributeValue->attribute.getPopup()->getEnumeration()[index].getValue(); } void onUpdate(); }; class ErrorDialog: public QDialog { public: ErrorDialog(std::vector 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); } }; } #endif //ATTRIBUTE_DIALOG_H