forked from qt-creator/qt-creator
CMake: Implement cmake true/false semantics
Implement proper support for cmakes true/false value semantics. Change-Id: I127f73f62d1b7b21b2fee032f40c9cc448b876b8 Reviewed-by: Alessandro Portale <alessandro.portale@qt.io> Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
@@ -148,6 +148,25 @@ CMakeConfigItem::Type CMakeConfigItem::typeStringToType(const QByteArray &type)
|
|||||||
return CMakeConfigItem::INTERNAL;
|
return CMakeConfigItem::INTERNAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Utils::optional<bool> CMakeConfigItem::toBool(const QByteArray &value)
|
||||||
|
{
|
||||||
|
// Taken from CMakes if(<constant>) documentation:
|
||||||
|
// "Named boolean constants are case-insensitive."
|
||||||
|
const QString v = QString::fromUtf8(value).toUpper();
|
||||||
|
|
||||||
|
bool isInt = false;
|
||||||
|
v.toInt(&isInt);
|
||||||
|
|
||||||
|
// "False if the constant is 0, OFF, NO, FALSE, N, IGNORE, NOTFOUND, the empty string, or ends in the suffix -NOTFOUND."
|
||||||
|
if (v == "0" || v == "OFF" || v == "NO" || v == "FALSE" || v == "N" || v == "IGNORE" || v == "NOTFOUND" || v == "" || v.endsWith("-NOTFOUND"))
|
||||||
|
return false;
|
||||||
|
// "True if the constant is 1, ON, YES, TRUE, Y, or a non-zero number."
|
||||||
|
if (v == "1" || v == "ON" || v == "YES" || v == "TRUE" || v == "Y" || isInt)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
QString CMakeConfigItem::expandedValue(const ProjectExplorer::Kit *k) const
|
QString CMakeConfigItem::expandedValue(const ProjectExplorer::Kit *k) const
|
||||||
{
|
{
|
||||||
return expandedValue(k->macroExpander());
|
return expandedValue(k->macroExpander());
|
||||||
|
@@ -25,6 +25,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <utils/optional.h>
|
||||||
|
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
|
|
||||||
@@ -50,6 +52,7 @@ public:
|
|||||||
const QList<CMakeConfigItem> &input);
|
const QList<CMakeConfigItem> &input);
|
||||||
static QStringList cmakeSplitValue(const QString &in, bool keepEmpty = false);
|
static QStringList cmakeSplitValue(const QString &in, bool keepEmpty = false);
|
||||||
static Type typeStringToType(const QByteArray &typeString);
|
static Type typeStringToType(const QByteArray &typeString);
|
||||||
|
static Utils::optional<bool> toBool(const QByteArray &value);
|
||||||
bool isNull() const { return key.isEmpty(); }
|
bool isNull() const { return key.isEmpty(); }
|
||||||
|
|
||||||
QString expandedValue(const ProjectExplorer::Kit *k) const;
|
QString expandedValue(const ProjectExplorer::Kit *k) const;
|
||||||
|
@@ -36,13 +36,6 @@
|
|||||||
|
|
||||||
namespace CMakeProjectManager {
|
namespace CMakeProjectManager {
|
||||||
|
|
||||||
static bool isTrue(const QString &value)
|
|
||||||
{
|
|
||||||
const QString lower = value.toLower();
|
|
||||||
return lower == QStringLiteral("true") || lower == QStringLiteral("on")
|
|
||||||
|| lower == QStringLiteral("1") || lower == QStringLiteral("yes");
|
|
||||||
}
|
|
||||||
|
|
||||||
ConfigModel::ConfigModel(QObject *parent) : Utils::TreeModel<>(parent)
|
ConfigModel::ConfigModel(QObject *parent) : Utils::TreeModel<>(parent)
|
||||||
{
|
{
|
||||||
setHeader({tr("Key"), tr("Value")});
|
setHeader({tr("Key"), tr("Value")});
|
||||||
@@ -431,15 +424,17 @@ QVariant ConfigModelTreeItem::data(int column, int role) const
|
|||||||
}
|
}
|
||||||
case 1: {
|
case 1: {
|
||||||
const QString value = currentValue();
|
const QString value = currentValue();
|
||||||
|
const auto boolValue = CMakeConfigItem::toBool(value.toUtf8());
|
||||||
|
const bool isTrue = boolValue.has_value() && boolValue.value();
|
||||||
|
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::CheckStateRole:
|
case Qt::CheckStateRole:
|
||||||
return (dataItem->type == ConfigModel::DataItem::BOOLEAN)
|
return (dataItem->type == ConfigModel::DataItem::BOOLEAN)
|
||||||
? QVariant(isTrue(value) ? Qt::Checked : Qt::Unchecked) : QVariant();
|
? QVariant(isTrue ? Qt::Checked : Qt::Unchecked) : QVariant();
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
return value;
|
return value;
|
||||||
case Qt::EditRole:
|
case Qt::EditRole:
|
||||||
return (dataItem->type == ConfigModel::DataItem::BOOLEAN) ? QVariant(isTrue(value)) : QVariant(value);
|
return (dataItem->type == ConfigModel::DataItem::BOOLEAN) ? QVariant(isTrue) : QVariant(value);
|
||||||
case Qt::FontRole: {
|
case Qt::FontRole: {
|
||||||
QFont font;
|
QFont font;
|
||||||
font.setBold((dataItem->isUserChanged || dataItem->isUserNew) && !dataItem->isUnset);
|
font.setBold((dataItem->isUserChanged || dataItem->isUserNew) && !dataItem->isUnset);
|
||||||
|
Reference in New Issue
Block a user