Commit fa223ab6 authored by Tobias Hunger's avatar Tobias Hunger

JsonWizard: Add separate method to get stringified values

Have value(...) return a QVariant that is never mangeled and add
a stringValue(...) method to retrieve stringified values. It is
way easier to see who needs what now.

Use this consistently in the JsonWizard and fix one place where
a QVariant was expected but a stringified version was returned.

Task-number: QTCREATORBUG-13486
Change-Id: I2c4e9188280940e529f0f60bcc18b9b7330865d1
Reviewed-by: default avatarDaniel Teske <daniel.teske@theqtcompany.com>
parent 78f1b24c
......@@ -48,9 +48,9 @@ void JsonFilePage::initializePage()
return;
if (fileName().isEmpty())
setFileName(wiz->value(QLatin1String("InitialFileName")).toString());
setFileName(wiz->stringValue(QLatin1String("InitialFileName")));
if (path().isEmpty())
setPath(wiz->value(QLatin1String("InitialPath")).toString());
setPath(wiz->stringValue(QLatin1String("InitialPath")));
}
bool JsonFilePage::validatePage()
......
......@@ -57,7 +57,7 @@ void JsonKitsPage::initializePage()
connect(wiz, &JsonWizard::filesReady, this, &JsonKitsPage::setupProjectFiles);
const QString platform = wiz->value(QLatin1String("Platform")).toString();
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());
......
......@@ -48,7 +48,7 @@ void JsonProjectPage::initializePage()
{
JsonWizard *wiz = qobject_cast<JsonWizard *>(wizard());
QTC_ASSERT(wiz, return);
setPath(wiz->value(QLatin1String("InitialPath")).toString());
setPath(wiz->stringValue(QLatin1String("InitialPath")));
setProjectName(uniqueProjectName(path()));
}
......
......@@ -65,7 +65,7 @@ static QString generatedProjectFilePath(const QList<JsonWizard::GeneratorFile> &
static IWizardFactory::WizardKind wizardKind(JsonWizard *wiz)
{
IWizardFactory::WizardKind kind = IWizardFactory::ProjectWizard;
const QString kindStr = wiz->value(QLatin1String("kind")).toString();
const QString kindStr = wiz->stringValue(QLatin1String("kind"));
if (kindStr == QLatin1String(Core::Constants::WIZARD_KIND_PROJECT))
kind = IWizardFactory::ProjectWizard;
else if (kindStr == QLatin1String(Core::Constants::WIZARD_KIND_CLASS))
......
......@@ -46,14 +46,8 @@ JsonWizard::JsonWizard(QWidget *parent) :
{
setMinimumSize(800, 500);
m_expander.registerExtraResolver([this](const QString &name, QString *ret) -> bool {
QVariant v = value(name);
if (v.isValid()) {
if (v.type() == QVariant::Bool)
*ret = v.toBool() ? QLatin1String("true") : QString();
else
*ret = v.toString();
}
return v.isValid();
*ret = stringValue(name);
return !ret->isNull();
});
m_expander.registerPrefix("Exists", tr("Check whether a variable exists. Returns \"true\" if it does and an empty string if not."),
[this](const QString &value) -> QString
......@@ -87,14 +81,14 @@ JsonWizard::GeneratorFiles JsonWizard::generateFileList()
QString errorMessage;
GeneratorFiles list;
QString targetPath = value(QLatin1String("TargetPath")).toString();
QString targetPath = stringValue(QLatin1String("TargetPath"));
if (targetPath.isEmpty())
errorMessage = tr("Could not determine target path. \"TargetPath\" was not set on any page.");
if (m_files.isEmpty() && errorMessage.isEmpty()) {
emit preGenerateFiles();
foreach (JsonWizardGenerator *gen, m_generators) {
Core::GeneratedFiles tmp = gen->fileList(&m_expander, value(QStringLiteral("WizardDir")).toString(),
Core::GeneratedFiles tmp = gen->fileList(&m_expander, stringValue(QStringLiteral("WizardDir")),
targetPath, &errorMessage);
if (!errorMessage.isEmpty())
break;
......@@ -120,24 +114,25 @@ void JsonWizard::commitToFileList(const JsonWizard::GeneratorFiles &list)
emit postGenerateFiles(m_files);
}
QVariant JsonWizard::value(const QString &n) const
QString JsonWizard::stringValue(const QString &n) const
{
QVariant v = property(n.toUtf8());
if (v.isValid()) {
if (v.type() == QVariant::String) {
return m_expander.expand(v.toString());
} if (v.type() == QVariant::StringList) {
QStringList tmp = Utils::transform(v.toStringList(), [this](const QString &i) -> QString {
return m_expander.expand(i).replace(QLatin1Char('\''), QLatin1String("\\'"));
});
return QString(QString(QLatin1Char('\'')) + tmp.join(QLatin1String("', '")) + QString(QLatin1Char('\'')));
} else {
return v;
}
QVariant v = value(n);
if (!v.isValid())
return QString();
if (v.type() == QVariant::Bool)
return v.toBool() ? QString::fromLatin1("true") : QString();
if (v.type() == QVariant::String)
return m_expander.expand(v.toString());
if (v.type() == QVariant::StringList) {
QStringList tmp = Utils::transform(v.toStringList(), [this](const QString &i) -> QString {
return m_expander.expand(i).replace(QLatin1Char('\''), QLatin1String("\\'"));
});
return QString(QString(QLatin1Char('\'')) + tmp.join(QLatin1String("', '")) + QString(QLatin1Char('\'')));
}
if (hasField(n))
return field(n); // Can not contain macros!
return QVariant();
return v.toString();
}
void JsonWizard::setValue(const QString &key, const QVariant &value)
......@@ -145,6 +140,16 @@ void JsonWizard::setValue(const QString &key, const QVariant &value)
setProperty(key.toUtf8(), value);
}
QVariant JsonWizard::value(const QString &n) const
{
QVariant v = property(n.toUtf8());
if (v.isValid())
return v;
if (hasField(n))
return field(n); // Can not contain macros!
return QVariant();
}
bool JsonWizard::boolFromVariant(const QVariant &v, Utils::MacroExpander *expander)
{
if (v.type() == QVariant::String)
......
......@@ -73,6 +73,8 @@ public:
GeneratorFiles generateFileList();
void commitToFileList(const GeneratorFiles &list);
QString stringValue(const QString &n) const;
QVariant value(const QString &n) const;
void setValue(const QString &key, const QVariant &value);
......
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