| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "settingstab.h" |
| 5 | |
| 6 | #include <QVBoxLayout> |
| 7 | #include <QtQuick3DAssetImport/private/qssgassetimportmanager_p.h> |
| 8 | #include <QtWidgets> |
| 9 | |
| 10 | SettingsTab::SettingsTab(QWidget *parent) : QScrollArea(parent) |
| 11 | { |
| 12 | QWidget *central = new QWidget; |
| 13 | QVBoxLayout *mainLayout = new QVBoxLayout(central); |
| 14 | |
| 15 | const auto allDescs = QSSGAssetImportManager().getImporterPluginInfos(); |
| 16 | |
| 17 | for (const auto &plugin : allDescs) { |
| 18 | auto options = plugin.importOptions; |
| 19 | // Skip plugins without options |
| 20 | if (options.isEmpty()) |
| 21 | continue; |
| 22 | |
| 23 | if (auto optionsIt = options.constFind(key: QLatin1String("options" )); optionsIt != options.constEnd()) |
| 24 | options = optionsIt->toObject(); |
| 25 | |
| 26 | QGroupBox *pluginGroup = new QGroupBox(plugin.typeDescription + " (" + plugin.name + ")" ); |
| 27 | QFormLayout *extensionsLayout = new QFormLayout; |
| 28 | extensionsLayout->addRow(label: new QLabel(tr(s: "Supported extensions:" )), field: new QLabel(plugin.inputExtensions.join(sep: ", " ))); |
| 29 | |
| 30 | for (auto kv_it = options.constBegin(); kv_it != options.constEnd(); ++kv_it) { |
| 31 | if (kv_it->isObject()) { |
| 32 | auto map = kv_it->toObject(); |
| 33 | auto settingKeyName = kv_it.key(); |
| 34 | auto description = map.value(key: "description" ); |
| 35 | auto name = map.value(key: QLatin1String("name" )); |
| 36 | auto type = map.value(key: "type" ); |
| 37 | auto value = map.value(key: "value" ); |
| 38 | |
| 39 | auto label = new QLabel(name.toString() + ":" ); |
| 40 | label->setToolTip(description.toString()); |
| 41 | |
| 42 | if (type == "Boolean" ) { |
| 43 | QCheckBox *checkBox = new QCheckBox(); |
| 44 | checkBox->setChecked(value.toBool()); |
| 45 | extensionsLayout->addRow(label, field: checkBox); |
| 46 | settings.push_back(t: Setting { .uiELement: checkBox, .name: settingKeyName, .defaultBool: value.toBool(), .defaultReal: 0.0f }); |
| 47 | } else if (type == "Real" ) { |
| 48 | QSpinBox *spinBox = new QSpinBox(); |
| 49 | spinBox->setMinimum(-9999); |
| 50 | spinBox->setMaximum(9999); |
| 51 | spinBox->setSingleStep(1); |
| 52 | spinBox->setValue(value.toDouble()); |
| 53 | extensionsLayout->addRow(label, field: spinBox); |
| 54 | settings.push_back(t: Setting { .uiELement: spinBox, .name: settingKeyName, .defaultBool: false, .defaultReal: value.toDouble() }); |
| 55 | } else { |
| 56 | qWarning() << "Unsupported setting " << name; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | pluginGroup->setLayout(extensionsLayout); |
| 62 | mainLayout->addWidget(pluginGroup); |
| 63 | } |
| 64 | |
| 65 | mainLayout->addStretch(stretch: 1); |
| 66 | |
| 67 | setWidget(central); |
| 68 | setWidgetResizable(true); |
| 69 | } |
| 70 | |
| 71 | QJsonObject SettingsTab::getOptions() const |
| 72 | { |
| 73 | QJsonObject options; |
| 74 | |
| 75 | for (const Setting &setting : settings) { |
| 76 | auto checkBox = dynamic_cast<QCheckBox *>(setting.uiELement); |
| 77 | auto spinBox = dynamic_cast<QSpinBox *>(setting.uiELement); |
| 78 | |
| 79 | if (checkBox != nullptr && setting.defaultBool != checkBox->isChecked()) { |
| 80 | options[setting.name] = QJsonValue(checkBox->isChecked()); |
| 81 | } else if (spinBox != nullptr && setting.defaultReal != spinBox->value()) { |
| 82 | options[setting.name] = QJsonValue(double(spinBox->value())); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return options; |
| 87 | } |
| 88 | |