aboutsummaryrefslogtreecommitdiffstats
path: root/tools/balsamui/settingstab.cpp
blob: e8acef1f2e67772bc8fe6695eaa5f51e14b37df3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "settingstab.h"

#include <QVBoxLayout>
#include <QtQuick3DAssetImport/private/qssgassetimportmanager_p.h>
#include <QtWidgets>

SettingsTab::SettingsTab(QWidget *parent) : QScrollArea(parent)
{
    QWidget *central = new QWidget;
    QVBoxLayout *mainLayout = new QVBoxLayout(central);

    const auto allDescs = QSSGAssetImportManager().getImporterPluginInfos();

    for (const auto &plugin : allDescs) {
        auto options = plugin.importOptions;
        // Skip plugins without options
        if (options.isEmpty())
            continue;

        if (auto optionsIt = options.constFind(QLatin1String("options")); optionsIt != options.constEnd())
            options = optionsIt->toObject();

        QGroupBox *pluginGroup = new QGroupBox(plugin.typeDescription + " (" + plugin.name + ")");
        QFormLayout *extensionsLayout = new QFormLayout;
        extensionsLayout->addRow(new QLabel(tr("Supported extensions:")), new QLabel(plugin.inputExtensions.join(", ")));

        for (auto kv_it = options.constBegin(); kv_it != options.constEnd(); ++kv_it) {
            if (kv_it->isObject()) {
                auto map = kv_it->toObject();
                auto settingKeyName = kv_it.key();
                auto description = map.value("description");
                auto name = map.value(QLatin1String("name"));
                auto type = map.value("type");
                auto value = map.value("value");

                auto label = new QLabel(name.toString() + ":");
                label->setToolTip(description.toString());

                if (type == "Boolean") {
                    QCheckBox *checkBox = new QCheckBox();
                    checkBox->setChecked(value.toBool());
                    extensionsLayout->addRow(label, checkBox);
                    settings.push_back(Setting { checkBox, settingKeyName, value.toBool(), 0.0f });
                } else if (type == "Real") {
                    QSpinBox *spinBox = new QSpinBox();
                    spinBox->setMinimum(-9999);
                    spinBox->setMaximum(9999);
                    spinBox->setSingleStep(1);
                    spinBox->setValue(value.toDouble());
                    extensionsLayout->addRow(label, spinBox);
                    settings.push_back(Setting { spinBox, settingKeyName, false, value.toDouble() });
                } else {
                    qWarning() << "Unsupported setting " << name;
                }
            }
        }

        pluginGroup->setLayout(extensionsLayout);
        mainLayout->addWidget(pluginGroup);
    }

    mainLayout->addStretch(1);

    setWidget(central);
    setWidgetResizable(true);
}

QJsonObject SettingsTab::getOptions() const
{
    QJsonObject options;

    for (const Setting &setting : settings) {
        auto checkBox = dynamic_cast<QCheckBox *>(setting.uiELement);
        auto spinBox = dynamic_cast<QSpinBox *>(setting.uiELement);

        if (checkBox != nullptr && setting.defaultBool != checkBox->isChecked()) {
            options[setting.name] = QJsonValue(checkBox->isChecked());
        } else if (spinBox != nullptr && setting.defaultReal != spinBox->value()) {
            options[setting.name] = QJsonValue(double(spinBox->value()));
        }
    }

    return options;
}