Commit 695b752b authored by Tobias Hunger's avatar Tobias Hunger

JsonWizard: Explicitly set required/preferred features on kits page

Do not use the required/preferred features from the wizard in the
kits page. The wizard's features are used to decide which wizards
will be shown (together with the enabled key). The kits page has
a separate set of features that are used to decide on the Kits
shown.

Keep these settings separate, as they are different things.

If nothing is set at the Kits page, then the wizard's values
will be copied. This is to stay compatible with existing
JSON wizards.

Change-Id: I2f8e3ccc46ce92f5be89aecaf52e5a81e4b29e83
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent 39253542
......@@ -45,8 +45,13 @@
#include <utils/mimetypes/mimedatabase.h>
#include <utils/qtcassert.h>
using namespace Core;
namespace ProjectExplorer {
const char KEY_FEATURE[] = "feature";
const char KEY_CONDITION[] = "condition";
JsonKitsPage::JsonKitsPage(QWidget *parent) : TargetSetupPage(parent)
{ }
......@@ -58,8 +63,10 @@ void JsonKitsPage::initializePage()
connect(wiz, &JsonWizard::filesPolished, this, &JsonKitsPage::setupProjectFiles);
const QString platform = wiz->stringValue(QLatin1String("Platform"));
const Core::FeatureSet preferred = Core::FeatureSet::fromStringList(wiz->value(QLatin1String("PreferredFeatures")).toStringList());
const Core::FeatureSet required = Core::FeatureSet::fromStringList(wiz->value(QLatin1String("RequiredFeatures")).toStringList());
const FeatureSet preferred
= evaluate(m_preferredFeatures, wiz->value(QLatin1String("PreferredFeatures")), wiz);
const FeatureSet required
= evaluate(m_requiredFeatures, wiz->value(QLatin1String("RequiredFeatures")), wiz);
setRequiredKitMatcher(KitMatcher([required](const Kit *k) { return k->hasFeatures(required); }));
setPreferredKitMatcher(KitMatcher([platform, preferred](const Kit *k) { return k->hasPlatform(platform) && k->hasFeatures(preferred); }));
......@@ -88,13 +95,23 @@ QString JsonKitsPage::unexpandedProjectPath() const
return m_unexpandedProjectPath;
}
void JsonKitsPage::setRequiredFeatures(const QVariant &data)
{
m_requiredFeatures = parseFeatures(data);
}
void JsonKitsPage::setPreferredFeatures(const QVariant &data)
{
m_preferredFeatures = parseFeatures(data);
}
void JsonKitsPage::setupProjectFiles(const JsonWizard::GeneratorFiles &files)
{
Project *project = 0;
QList<IProjectManager *> managerList = ExtensionSystem::PluginManager::getObjects<IProjectManager>();
foreach (const JsonWizard::GeneratorFile &f, files) {
if (f.file.attributes() & Core::GeneratedFile::OpenProjectAttribute) {
if (f.file.attributes() & GeneratedFile::OpenProjectAttribute) {
QString errorMessage;
QString path = f.file.path();
const QFileInfo fi(path);
......@@ -119,4 +136,56 @@ void JsonKitsPage::setupProjectFiles(const JsonWizard::GeneratorFiles &files)
}
}
FeatureSet JsonKitsPage::evaluate(const QVector<JsonKitsPage::ConditionalFeature> &list,
const QVariant &defaultSet, JsonWizard *wiz)
{
if (list.isEmpty())
return FeatureSet::fromStringList(defaultSet.toStringList());
FeatureSet features;
foreach (const ConditionalFeature &f, list) {
if (JsonWizard::boolFromVariant(f.condition, wiz->expander()))
features |= f.feature;
}
return features;
}
QVector<JsonKitsPage::ConditionalFeature> JsonKitsPage::parseFeatures(const QVariant &data,
QString *errorMessage)
{
QVector<ConditionalFeature> result;
if (errorMessage)
errorMessage->clear();
if (data.isNull())
return result;
if (data.type() != QVariant::List) {
if (errorMessage)
*errorMessage = tr("Feature list is set and not of type list.");
return result;
}
foreach (const QVariant &element, data.toList()) {
if (element.type() == QVariant::String) {
result.append({ Id::fromString(element.toString()), QVariant(true) });
} else if (element.type() == QVariant::Map) {
const QVariantMap obj = element.toMap();
const QString feature = obj.value(QLatin1String(KEY_FEATURE)).toString();
if (feature.isEmpty()) {
*errorMessage = tr("No \"%1\" key found in feature list object.")
.arg(QLatin1String(KEY_FEATURE));
return QVector<ConditionalFeature>();
}
result.append({ Id::fromString(feature), obj.value(QLatin1String(KEY_CONDITION), true) });
} else {
if (errorMessage)
*errorMessage = tr("Feature list element is not a string or object.");
return QVector<ConditionalFeature>();
}
}
return result;
}
} // namespace ProjectExplorer
......@@ -34,6 +34,8 @@
#include "jsonwizard.h"
#include "../targetsetuppage.h"
#include <QVector>
namespace ProjectExplorer {
// Documentation inside.
......@@ -50,11 +52,32 @@ public:
void setUnexpandedProjectPath(const QString &path);
QString unexpandedProjectPath() const;
void setRequiredFeatures(const QVariant &data);
void setPreferredFeatures(const QVariant &data);
class ConditionalFeature {
public:
ConditionalFeature() : feature(-1) { }
ConditionalFeature(const Core::Feature &f, const QVariant &c) : feature(f), condition(c)
{ }
Core::Feature feature;
QVariant condition;
};
static QVector<ConditionalFeature> parseFeatures(const QVariant &data,
QString *errorMessage = 0);
private slots:
void setupProjectFiles(const JsonWizard::GeneratorFiles &files);
private:
QString m_unexpandedProjectPath;
QVector<ConditionalFeature> m_requiredFeatures;
QVector<ConditionalFeature> m_preferredFeatures;
Core::FeatureSet evaluate(const QVector<ConditionalFeature> &list, const QVariant &defaultSet,
JsonWizard *wiz);
};
} // namespace ProjectExplorer
......
......@@ -129,6 +129,8 @@ bool FilePageFactory::validateData(Core::Id typeId, const QVariant &data, QStrin
// --------------------------------------------------------------------
static const char KEY_PROJECT_FILE[] = "projectFilePath";
static const char KEY_REQUIRED_FEATURES[] = "requiredFeatures";
static const char KEY_PREFERRED_FEATURES[] = "preferredFeatures";
KitsPageFactory::KitsPageFactory()
{
......@@ -141,11 +143,27 @@ Utils::WizardPage *KitsPageFactory::create(JsonWizard *wizard, Core::Id typeId,
QTC_ASSERT(canCreate(typeId), return 0);
JsonKitsPage *page = new JsonKitsPage;
page->setUnexpandedProjectPath(data.toMap().value(QLatin1String(KEY_PROJECT_FILE)).toString());
const QVariantMap dataMap = data.toMap();
page->setUnexpandedProjectPath(dataMap.value(QLatin1String(KEY_PROJECT_FILE)).toString());
page->setRequiredFeatures(dataMap.value(QLatin1String(KEY_REQUIRED_FEATURES)));
page->setPreferredFeatures(dataMap.value(QLatin1String(KEY_PREFERRED_FEATURES)));
return page;
}
static bool validateFeatureList(const QVariantMap &data, const QByteArray &key, QString *errorMessage)
{
QString message;
JsonKitsPage::parseFeatures(data.value(QLatin1String(key)), &message);
if (!message.isEmpty()) {
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard",
"Error parsing \"%1\" in \"Kits\" page: %2")
.arg(QLatin1String(key), message);
return false;
}
return true;
}
bool KitsPageFactory::validateData(Core::Id typeId, const QVariant &data, QString *errorMessage)
{
QTC_ASSERT(canCreate(typeId), return false);
......@@ -164,7 +182,8 @@ bool KitsPageFactory::validateData(Core::Id typeId, const QVariant &data, QStrin
return false;
}
return true;
return validateFeatureList(tmp, KEY_REQUIRED_FEATURES, errorMessage)
&& validateFeatureList(tmp, KEY_PREFERRED_FEATURES, errorMessage);
}
// --------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment