Updated attribute dialog

This commit is contained in:
Borna Rajković 2022-05-15 16:13:29 +02:00
parent 05fa2abbb0
commit 23764776f1
8 changed files with 151 additions and 23 deletions

View File

@ -2,6 +2,8 @@
#define ATTRIBUTE_DIALOG_H #define ATTRIBUTE_DIALOG_H
#include <QIntValidator> #include <QIntValidator>
#include <QPushButton>
#include <QComboBox>
#include <QGroupBox> #include <QGroupBox>
#include <QRadioButton> #include <QRadioButton>
#include <QDialog> #include <QDialog>
@ -15,9 +17,35 @@
namespace display { namespace display {
class AttributeDialog: public QDialog { 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: public:
AttributeDialog(domain::InstanceAttribute *attribute) { AttributeDialog(domain::InstanceAttribute *attribute) {
setAttribute(Qt::WA_DeleteOnClose);
attributeValue = attribute;
this->setWindowTitle(QString::fromStdString("Izmjeni " + attribute->attribute.getName())); this->setWindowTitle(QString::fromStdString("Izmjeni " + attribute->attribute.getName()));
auto layout = new QVBoxLayout(this); auto layout = new QVBoxLayout(this);
@ -28,12 +56,29 @@ public:
layout->addWidget(new QLabel(popup.getText().c_str())); layout->addWidget(new QLabel(popup.getText().c_str()));
auto type = attribute->attribute.getDefault().getType(); auto type = attribute->attribute.getDefault().getType();
value = attribute->value;
if(attribute->attribute.getPopup()->isEnumerated()) { 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<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;
}
}
} else if(!(type == domain::Value::ValueType::WIRE_REFERENCE || type == domain::Value::ValueType::BOOL)) { } else if(!(type == domain::Value::ValueType::WIRE_REFERENCE || type == domain::Value::ValueType::BOOL)) {
auto edit = new QLineEdit(this); auto edit = new QLineEdit(this);
connect(edit, &QLineEdit::textChanged, this, &AttributeDialog::onTextChanged);
layout->addWidget(edit); layout->addWidget(edit);
switch (attribute->attribute.getDefault().getType()) { switch (attribute->attribute.getDefault().getType()) {
@ -46,15 +91,20 @@ public:
break; break;
} }
} else if(type == domain::Value::ValueType::BOOL) { } else if(type == domain::Value::ValueType::BOOL) {
auto *group = new QGroupBox(this); auto *group = new QGroupBox(this);
group->setCheckable(true);
group->setChecked(true); auto *radioLayout = new QHBoxLayout(group);
group->setLayout(radioLayout);
auto isTrue = new QRadioButton("true", group); auto isTrue = new QRadioButton("true", group);
connect(isTrue, &QRadioButton::clicked, [this]() {
this->value = domain::Value::fromBool(true);
});
auto isFalse = new QRadioButton("false", group); auto isFalse = new QRadioButton("false", group);
connect(isFalse, &QRadioButton::clicked, [this]() {
this->value = domain::Value::fromBool(false);
});
if(attribute->value.asBool()) { if(attribute->value.asBool()) {
isTrue->setChecked(true); isTrue->setChecked(true);
@ -62,10 +112,35 @@ public:
isFalse->setChecked(true); isFalse->setChecked(true);
} }
radioLayout->addWidget(isTrue);
radioLayout->addWidget(isFalse);
layout->addWidget(group); 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();
} }
}; };

View File

@ -1,4 +1,6 @@
#include "component_display.h" #include "component_display.h"
#include "attribute_dialog.h"
#include "name_dialog.h"
#include <QMenu> #include <QMenu>
#include <QGraphicsSceneContextMenuEvent> #include <QGraphicsSceneContextMenuEvent>
@ -11,11 +13,15 @@ void Component::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu; QMenu menu;
menu.addAction("Izmjeni ime", [this](){}); menu.addAction("Izmjeni ime", [this](){});
menu.addSeparator(); menu.addSeparator();
for(auto attr: this->instance->attributes) { for(int i=0; i<this->instance->attributes.size(); i++) {
bool enabled = attr.attribute.getPopup().has_value(); auto* attr = &this->instance->attributes[i];
bool enabled = attr->attribute.getPopup().has_value();
auto action = menu.addAction(QString::fromStdString("Izmjeni " + attr.name), auto action = menu.addAction(QString::fromStdString("Izmjeni " + attr->name),
[&attr, this]() {}); [attr]() {
auto dialog = new AttributeDialog(attr);
dialog->exec();
});
action->setEnabled(enabled); action->setEnabled(enabled);
} }
menu.exec(event->screenPos()); menu.exec(event->screenPos());

View File

@ -6,7 +6,7 @@ Enumeration::Enumeration(std::string name, Value value)
: name(name), value(value) : name(name), value(value)
{} {}
std::string Enumeration::getName() { std::string& Enumeration::getName() {
return name; return name;
} }
Value Enumeration::getValue() { Value Enumeration::getValue() {

View File

@ -15,7 +15,7 @@ class Enumeration {
public: public:
Enumeration(std::string name, Value value); Enumeration(std::string name, Value value);
std::string getName(); std::string& getName();
Value getValue(); Value getValue();
}; };

View File

@ -39,6 +39,29 @@ public:
this->type = UNDEFINED; this->type = UNDEFINED;
} }
bool equals(Value value) {
if(value.getType() == type) {
switch (type) {
case INT:
return value.asInt() == intValue;
case STRING:
return value.asString() == stringValue;
case NIL:
case UNDEFINED:
return true;
case WIRE_REFERENCE:
case ATTRIBUTE_REFERENCE:
case ADDRESS_SPACE_REFERENCE:
return value.asReference() == reference;
case BOOL:
return value.asBool() == boolValue;
default:
return false;
}
}
return false;
}
std::string string(); std::string string();
ValueType getType(); ValueType getType();

View File

@ -75,6 +75,27 @@
@tooltip "Memorijska komponenta, mora postojati barem jedna" @tooltip "Memorijska komponenta, mora postojati barem jedna"
@count (1,1000) @count (1,1000)
@source "memory.cdl" @source "memory.cdl"
@attribute sinkroniziran bool default false {
@popup automatic {
@title "Postavite sinkroniziranost"
@text "Zadajte sinkronizaciju"
}
}
@attribute brzina int default 1 {
@popup automatic {
@title "Postavite brzinu memorije"
@text "Zadajte brzinu memorije"
@enumerated {
"Spora" = 1,
"Srednja" = 5,
"Brza" = 25,
}
}
}
@attribute kapacitet int default 65536 { @attribute kapacitet int default 65536 {
@popup automatic { @popup automatic {
@title "Kapacitet memorije" @title "Kapacitet memorije"
@ -171,6 +192,7 @@
@tooltip "DMA-kontroler" @tooltip "DMA-kontroler"
@count (1,1000) @count (1,1000)
@source "dma.cdl" @source "dma.cdl"
@attribute pocetnaAdresa int default 0 { @attribute pocetnaAdresa int default 0 {
@popup automatic { @popup automatic {
@title "Početna adresa DMA-kontrolera" @title "Početna adresa DMA-kontrolera"

View File

@ -7,6 +7,8 @@
@instance mem Memorija { @instance mem Memorija {
@position (0, 250) @position (0, 250)
@attribute sinkroniziran false
@attribute brzina 1
@attribute kapacitet 1024 @attribute kapacitet 1024
@attribute size 8 @attribute size 8
@attribute pocetnaAdresa 1023 @attribute pocetnaAdresa 1023

View File

@ -7,9 +7,10 @@
@instance mem Memorija { @instance mem Memorija {
@position (0, 250) @position (0, 250)
@attribute kapacitet 1024 @attribute brzina 25
@attribute kapacitet 2048
@attribute size 8 @attribute size 8
@attribute pocetnaAdresa 1023 @attribute pocetnaAdresa 0
} }
@instance bus glavnaSabirnica { @instance bus glavnaSabirnica {
@ -17,18 +18,17 @@
@size 100 @size 100
} }
@wire wire_001 { @instance testBus Test {
@position (50, 116) @position (0, 200)
@display {} @size 0
}
@wire wire_002 {
@position (50, 220)
@display {}
} }
@connection (proc.glavniPin, bus) { @connection (proc.glavniPin, bus) {
@wire wire_001
} }
@connection (mem.glavniPin, bus) { @connection (mem.glavniPin, bus) {
@wire wire_002 }
@connection (proc.glavniPin, testBus, mem.glavniPin) {
@attribute dmaPoveznica "READY"
@attribute mmaPoveznica "PIO_DATA"
} }
} }