diff options
author | Eike Ziller <eike.ziller@qt.io> | 2019-04-16 16:46:36 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2019-05-09 11:19:43 +0000 |
commit | e0d38ae4140d6101b483fde25158fc94b34a3a64 (patch) | |
tree | 9137897e6cc85c195ff76ebedc9de0d9a447a168 /src/plugins/projectexplorer/jsonwizard | |
parent | 0c5837a1114ef526727748a0db956f0758597350 (diff) |
Export Wizard values to JavaScript macro
Registers a new function "value('name')", available to the wizard json
files, which returns the value of the variable "name" as a JavaScript
object. So, variables with a string value are actual JavaScript strings,
booleans are booleans, lists are lists, and dictionaries are
dictionaries.
The patch also makes it actually possible to assign JSON lists and
dictionaries to values.
This removes some hacks involving creating complex JavaScript objects
through string substitution.
Change-Id: I4ac6da22bc5bccc9fadee97694c2fa14d44c9307
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'src/plugins/projectexplorer/jsonwizard')
6 files changed, 104 insertions, 14 deletions
diff --git a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp index 8048d4cc6d..b04ec95efc 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp @@ -810,7 +810,7 @@ std::unique_ptr<QStandardItem> createStandardItemFromListItem(const QVariant &it if (item.type() == QVariant::Map) { QVariantMap tmp = item.toMap(); const QString key = JsonWizardFactory::localizedString(consumeValue(tmp, "trKey", QString()).toString()); - const QString value = consumeValue(tmp, "value", key).toString(); + const QVariant value = consumeValue(tmp, "value", key); if (key.isNull() || key.isEmpty()) { *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage", @@ -920,7 +920,7 @@ void ListField::initializeData(MacroExpander *expander) if (item.get() == currentItem) currentItem = expandedValuesItem; expandedValuesItem->setText(expander->expand(item->text())); - expandedValuesItem->setData(expander->expand(item->data(ValueRole).toString()), ValueRole); + expandedValuesItem->setData(expander->expandVariant(item->data(ValueRole)), ValueRole); expandedValuesItem->setData(expander->expand(item->data(IconStringRole).toString()), IconStringRole); expandedValuesItem->setData(condition, ConditionRole); @@ -1020,8 +1020,8 @@ void ComboBoxField::setup(JsonFieldPage *page, const QString &name) page->registerObjectAsFieldWithName<QItemSelectionModel>(name, selectionModel(), &QItemSelectionModel::selectionChanged, [this]() { const QModelIndex i = selectionModel()->currentIndex(); if (i.isValid()) - return i.data(ValueRole).toString(); - return QString(); + return i.data(ValueRole); + return QVariant(); }); QObject::connect(selectionModel(), &QItemSelectionModel::selectionChanged, page, [page]() { emit page->completeChanged(); @@ -1058,8 +1058,8 @@ void IconListField::setup(JsonFieldPage *page, const QString &name) page->registerObjectAsFieldWithName<QItemSelectionModel>(name, selectionModel(), &QItemSelectionModel::selectionChanged, [this]() { const QModelIndex i = selectionModel()->currentIndex(); if (i.isValid()) - return i.data(ValueRole).toString(); - return QString(); + return i.data(ValueRole); + return QVariant(); }); QObject::connect(selectionModel(), &QItemSelectionModel::selectionChanged, page, [page]() { emit page->completeChanged(); diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp index 5db2186c46..6939d0863e 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp @@ -45,6 +45,7 @@ #include <QDialogButtonBox> #include <QDir> #include <QFileInfo> +#include <QJSEngine> #include <QLabel> #include <QMessageBox> #include <QPushButton> @@ -143,7 +144,8 @@ private: } // namespace Internal -JsonWizard::JsonWizard(QWidget *parent) : Utils::Wizard(parent) +JsonWizard::JsonWizard(QWidget *parent) + : Utils::Wizard(parent) { setMinimumSize(800, 500); m_expander.registerExtraResolver([this](const QString &name, QString *ret) -> bool { @@ -157,7 +159,10 @@ JsonWizard::JsonWizard(QWidget *parent) : Utils::Wizard(parent) const QString key = QString::fromLatin1("%{") + value + QLatin1Char('}'); return m_expander.expand(key) == key ? QString() : QLatin1String("true"); }); - + // override default JS macro by custom one that adds Wizard specific features + m_jsExpander.registerObject("Wizard", new Internal::JsonWizardJsExtension(this)); + m_jsExpander.engine().evaluate("var value = Wizard.value"); + m_jsExpander.registerForExpander(&m_expander); } JsonWizard::~JsonWizard() @@ -520,4 +525,17 @@ bool JsonWizard::OptionDefinition::condition(Utils::MacroExpander &expander) con return JsonWizard::boolFromVariant(m_condition, &expander); } +namespace Internal { + +JsonWizardJsExtension::JsonWizardJsExtension(JsonWizard *wizard) + : m_wizard(wizard) +{} + +QVariant JsonWizardJsExtension::value(const QString &name) const +{ + const QVariant value = m_wizard->value(name); + return m_wizard->expander()->expandVariant(m_wizard->value(name)); +} + +} // namespace Internal } // namespace ProjectExplorer diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizard.h b/src/plugins/projectexplorer/jsonwizard/jsonwizard.h index 1f8aca7741..99a9ce4952 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizard.h +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizard.h @@ -29,6 +29,7 @@ #include <projectexplorer/projectnodes.h> #include <coreplugin/generatedfile.h> +#include <coreplugin/jsexpander.h> #include <utils/wizard.h> #include <utils/macroexpander.h> @@ -37,8 +38,25 @@ namespace ProjectExplorer { +class JsonWizard; class JsonWizardGenerator; +namespace Internal { + +class JsonWizardJsExtension : public QObject +{ + Q_OBJECT +public: + JsonWizardJsExtension(JsonWizard *wizard); + + Q_INVOKABLE QVariant value(const QString &name) const; + +private: + JsonWizard *m_wizard; +}; + +} // namespace Internal + // Documentation inside. class PROJECTEXPLORER_EXPORT JsonWizard : public Utils::Wizard { @@ -126,6 +144,7 @@ private: GeneratorFiles m_files; Utils::MacroExpander m_expander; + Core::JsExpander m_jsExpander; }; } // namespace ProjectExplorer diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp index 3a01d9e045..6ef34d8ddc 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp @@ -34,6 +34,7 @@ #include <coreplugin/coreconstants.h> #include <coreplugin/icontext.h> #include <coreplugin/icore.h> +#include <coreplugin/jsexpander.h> #include <coreplugin/messagemanager.h> #include <extensionsystem/pluginmanager.h> @@ -46,10 +47,11 @@ #include <QDebug> #include <QDir> -#include <QMap> +#include <QJSEngine> #include <QJsonDocument> #include <QJsonObject> #include <QJsonParseError> +#include <QMap> #include <QUuid> namespace ProjectExplorer { @@ -513,9 +515,17 @@ bool JsonWizardFactory::isAvailable(Core::Id platformId) const [platformId]() { return platformId.toString(); }); expander.registerVariable("Features", tr("The features available to this wizard."), [this, e, platformId]() { return JsonWizard::stringListToArrayString(Core::Id::toStringList(availableFeatures(platformId)), e); }); - expander.registerVariable("Plugins", tr("The plugins loaded."), - [this, e]() { return JsonWizard::stringListToArrayString(Core::Id::toStringList(pluginFeatures()), e); }); - + expander.registerVariable("Plugins", tr("The plugins loaded."), [this, e]() { + return JsonWizard::stringListToArrayString(Core::Id::toStringList(pluginFeatures()), e); + }); + Core::JsExpander jsExpander; + jsExpander.registerObject("Wizard", + new Internal::JsonWizardFactoryJsExtension(platformId, + availableFeatures( + platformId), + pluginFeatures())); + jsExpander.engine().evaluate("var value = Wizard.value"); + jsExpander.registerForExpander(e); return JsonWizard::boolFromVariant(m_enabledExpression, &expander); } @@ -660,4 +670,26 @@ bool JsonWizardFactory::initialize(const QVariantMap &data, const QDir &baseDir, return errorMessage->isEmpty(); } +namespace Internal { + +JsonWizardFactoryJsExtension::JsonWizardFactoryJsExtension(Core::Id platformId, + const QSet<Core::Id> &availableFeatures, + const QSet<Core::Id> &pluginFeatures) + : m_platformId(platformId) + , m_availableFeatures(availableFeatures) + , m_pluginFeatures(pluginFeatures) +{} + +QVariant JsonWizardFactoryJsExtension::value(const QString &name) const +{ + if (name == "Platform") + return m_platformId.toString(); + if (name == "Features") + return Core::Id::toStringList(m_availableFeatures); + if (name == "Plugins") + return Core::Id::toStringList(m_pluginFeatures); + return QVariant(); +} + +} // namespace Internal } // namespace ProjectExplorer diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h index ead93f4841..06af1ebae8 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h @@ -119,4 +119,23 @@ private: friend class ProjectExplorerPluginPrivate; }; -} //namespace ProjectExplorer +namespace Internal { + +class JsonWizardFactoryJsExtension : public QObject +{ + Q_OBJECT +public: + JsonWizardFactoryJsExtension(Core::Id platformId, + const QSet<Core::Id> &availableFeatures, + const QSet<Core::Id> &pluginFeatures); + + Q_INVOKABLE QVariant value(const QString &name) const; + +private: + Core::Id m_platformId; + QSet<Core::Id> m_availableFeatures; + QSet<Core::Id> m_pluginFeatures; +}; + +} // namespace Internal +} // namespace ProjectExplorer diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp index ee7da5b419..a168fea084 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp @@ -127,7 +127,9 @@ Core::GeneratedFile JsonWizardFileGenerator::generateFile(const File &file, *ret = options.value(n); return true; }); - nested.registerExtraResolver([expander](QString n, QString *ret) { return expander->resolveMacro(n, ret); }); + nested.registerExtraResolver([expander](QString n, QString *ret) { + return expander->resolveMacro(n, ret); + }); gf.setContents(Utils::TemplateEngine::processText(&nested, QString::fromUtf8(reader.data()), errorMessage)); |