|
| 1 | +#include "adaptersettings.h" |
| 2 | + |
| 3 | +#include "customwidgets/addabletabwidget.h" |
| 4 | +#include "customwidgets/schemaformwidget.h" |
| 5 | +#include "models/adapterdata.h" |
| 6 | +#include "models/settingsmodel.h" |
| 7 | + |
| 8 | +#include <QJsonArray> |
| 9 | +#include <QVBoxLayout> |
| 10 | + |
| 11 | +AdapterSettings::AdapterSettings(SettingsModel* pSettingsModel, |
| 12 | + const QString& adapterId, |
| 13 | + const QString& propertyKey, |
| 14 | + QWidget* parent) |
| 15 | + : QWidget(parent), _pSettingsModel(pSettingsModel), _adapterId(adapterId), _propertyKey(propertyKey) |
| 16 | +{ |
| 17 | + auto* layout = new QVBoxLayout(this); |
| 18 | + setLayout(layout); |
| 19 | + |
| 20 | + const AdapterData* pAdapter = pSettingsModel->adapterData(adapterId); |
| 21 | + const QJsonObject propSchema = pAdapter->schema().value("properties").toObject().value(propertyKey).toObject(); |
| 22 | + const QJsonValue configValue = pAdapter->effectiveConfig().value(propertyKey); |
| 23 | + |
| 24 | + buildSection(propSchema, configValue, layout); |
| 25 | +} |
| 26 | + |
| 27 | +bool AdapterSettings::isRenderableProperty(const QJsonObject& propSchema) |
| 28 | +{ |
| 29 | + const QString type = propSchema.value("type").toString(); |
| 30 | + if (type == "array") |
| 31 | + { |
| 32 | + return !propSchema.value("items").toObject().isEmpty(); |
| 33 | + } |
| 34 | + if (type == "object") |
| 35 | + { |
| 36 | + return !propSchema.value("properties").toObject().isEmpty(); |
| 37 | + } |
| 38 | + return false; |
| 39 | +} |
| 40 | + |
| 41 | +void AdapterSettings::buildSection(const QJsonObject& propSchema, const QJsonValue& configValue, QVBoxLayout* layout) |
| 42 | +{ |
| 43 | + const QString type = propSchema.value("type").toString(); |
| 44 | + |
| 45 | + if (type == "array") |
| 46 | + { |
| 47 | + _itemSchema = propSchema.value("items").toObject(); |
| 48 | + _pItemTabs = new AddableTabWidget(this); |
| 49 | + |
| 50 | + const QJsonArray itemsArray = configValue.toArray(); |
| 51 | + QList<QWidget*> pages; |
| 52 | + QStringList names; |
| 53 | + for (int i = 0; i < itemsArray.size(); ++i) |
| 54 | + { |
| 55 | + auto* form = new SchemaFormWidget(_pItemTabs); |
| 56 | + form->setSchema(_itemSchema, itemsArray.at(i).toObject()); |
| 57 | + pages.append(form); |
| 58 | + names.append(QString("%1 %2").arg(_propertyKey[0].toUpper() + _propertyKey.mid(1)).arg(i + 1)); |
| 59 | + } |
| 60 | + |
| 61 | + if (!pages.isEmpty()) |
| 62 | + { |
| 63 | + _pItemTabs->setTabs(pages, names); |
| 64 | + } |
| 65 | + |
| 66 | + connect(_pItemTabs, &AddableTabWidget::addTabRequested, this, [this]() { |
| 67 | + auto* form = new SchemaFormWidget(_pItemTabs); |
| 68 | + QJsonObject defaultValues; |
| 69 | + const QJsonArray defaultItems = |
| 70 | + _pSettingsModel->adapterData(_adapterId)->defaults().value(_propertyKey).toArray(); |
| 71 | + if (!defaultItems.isEmpty()) |
| 72 | + { |
| 73 | + defaultValues = defaultItems.first().toObject(); |
| 74 | + } |
| 75 | + form->setSchema(_itemSchema, defaultValues); |
| 76 | + const int newIndex = _pItemTabs->count() + 1; |
| 77 | + const QString name = QString("%1 %2").arg(_propertyKey[0].toUpper() + _propertyKey.mid(1)).arg(newIndex); |
| 78 | + _pItemTabs->addNewTab(name, form); |
| 79 | + }); |
| 80 | + |
| 81 | + layout->addWidget(_pItemTabs, 1); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + _pForm = new SchemaFormWidget(this); |
| 86 | + _pForm->setSchema(propSchema, configValue.toObject()); |
| 87 | + layout->addWidget(_pForm); |
| 88 | + layout->addStretch(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +void AdapterSettings::acceptValues() |
| 93 | +{ |
| 94 | + QJsonObject config = _pSettingsModel->adapterData(_adapterId)->effectiveConfig(); |
| 95 | + |
| 96 | + if (_pItemTabs) |
| 97 | + { |
| 98 | + QJsonArray itemsArray; |
| 99 | + for (int i = 0; i < _pItemTabs->count(); ++i) |
| 100 | + { |
| 101 | + auto* form = qobject_cast<SchemaFormWidget*>(_pItemTabs->tabContent(i)); |
| 102 | + if (form) |
| 103 | + { |
| 104 | + itemsArray.append(form->values()); |
| 105 | + } |
| 106 | + } |
| 107 | + config[_propertyKey] = itemsArray; |
| 108 | + } |
| 109 | + else if (_pForm) |
| 110 | + { |
| 111 | + config[_propertyKey] = _pForm->values(); |
| 112 | + } |
| 113 | + |
| 114 | + _pSettingsModel->setAdapterCurrentConfig(_adapterId, config); |
| 115 | +} |
0 commit comments