#ifndef ATTRIBUTE_DIALOG_H #define ATTRIBUTE_DIALOG_H #include #include #include #include #include #include #include #include #include #include #include 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() { attributeValue->value = value; accept(); } }; } #endif //ATTRIBUTE_DIALOG_H