Commit b1157e4e authored by Tobias Hunger's avatar Tobias Hunger

CMake: Allow to unset configuration values

Change-Id: I649323e3c2cc51ea69dd7e216f30eeb653f3873b
Reviewed-by: default avatarhjk <hjk@qt.io>
parent aeb45f5c
......@@ -207,6 +207,7 @@ void CMakeBuildConfiguration::setConfigurationForCMake(const QList<ConfigModel::
ni.value = i.value.toUtf8();
ni.documentation = i.description.toUtf8();
ni.isAdvanced = i.isAdvanced;
ni.isUnset = i.isUnset;
ni.inCMakeCache = i.inCMakeCache;
ni.values = i.values;
switch (i.type) {
......@@ -274,7 +275,7 @@ void CMakeBuildConfiguration::setConfigurationForCMake(const CMakeConfig &config
bool hasKitOverride = false;
foreach (const CMakeConfigItem &i, m_configurationForCMake) {
const QString b = CMakeConfigItem::expandedValueOf(k, i.key, kitConfig);
if (!b.isNull() && i.expandedValue(k) != b) {
if (!b.isNull() && (i.expandedValue(k) != b || i.isUnset)) {
hasKitOverride = true;
break;
}
......
......@@ -61,6 +61,20 @@
namespace CMakeProjectManager {
namespace Internal {
static QModelIndex mapToSource(const QAbstractItemView *view, const QModelIndex &idx)
{
if (!idx.isValid())
return idx;
QAbstractItemModel *model = view->model();
QModelIndex result = idx;
while (QSortFilterProxyModel *proxy = qobject_cast<QSortFilterProxyModel *>(model)) {
result = proxy->mapToSource(result);
model = proxy->sourceModel();
}
return result;
}
// --------------------------------------------------------------------
// CMakeBuildSettingsWidget:
// --------------------------------------------------------------------
......@@ -88,7 +102,7 @@ CMakeBuildSettingsWidget::CMakeBuildSettingsWidget(CMakeBuildConfiguration *bc)
mainLayout->setMargin(0);
mainLayout->setColumnStretch(1, 10);
auto project = static_cast<CMakeProject *>(bc->target()->project());
auto project = static_cast<CMakeProject *>(bc->project());
auto buildDirChooser = new Utils::PathChooser;
buildDirChooser->setBaseFileName(project->projectDirectory());
......@@ -182,6 +196,7 @@ CMakeBuildSettingsWidget::CMakeBuildSettingsWidget(CMakeBuildConfiguration *bc)
auto buttonLayout = new QVBoxLayout;
m_addButton = new QPushButton(tr("&Add"));
m_addButton->setToolTip(tr("Add a new configuration value."));
buttonLayout->addWidget(m_addButton);
{
m_addButtonMenu = new QMenu;
......@@ -196,8 +211,13 @@ CMakeBuildSettingsWidget::CMakeBuildSettingsWidget(CMakeBuildConfiguration *bc)
m_addButton->setMenu(m_addButtonMenu);
}
m_editButton = new QPushButton(tr("&Edit"));
m_editButton->setToolTip(tr("Edit the current CMake configuration value."));
buttonLayout->addWidget(m_editButton);
m_unsetButton = new QPushButton(tr("&Unset"));
m_unsetButton->setToolTip(tr("Unset a value in the CMake configuration."));
buttonLayout->addWidget(m_unsetButton);
m_resetButton = new QPushButton(tr("&Reset"));
m_resetButton->setToolTip(tr("Reset all unapplied changes."));
m_resetButton->setEnabled(false);
buttonLayout->addWidget(m_resetButton);
buttonLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Fixed, QSizePolicy::Fixed));
......@@ -266,6 +286,9 @@ CMakeBuildSettingsWidget::CMakeBuildSettingsWidget(CMakeBuildConfiguration *bc)
connect(m_reconfigureButton, &QPushButton::clicked, this, [this]() {
m_buildConfiguration->setConfigurationForCMake(m_configModel->configurationChanges());
});
connect(m_unsetButton, &QPushButton::clicked, this, [this]() {
m_configModel->toggleUnsetFlag(mapToSource(m_configView, m_configView->currentIndex()));
});
connect(m_editButton, &QPushButton::clicked, this, [this]() {
QModelIndex idx = m_configView->currentIndex();
if (idx.column() != 1)
......@@ -296,6 +319,8 @@ CMakeBuildSettingsWidget::CMakeBuildSettingsWidget(CMakeBuildConfiguration *bc)
this, &CMakeBuildSettingsWidget::updateFromKit);
connect(m_buildConfiguration, &CMakeBuildConfiguration::enabledChanged,
this, [this]() { setError(m_buildConfiguration->disabledReason()); });
updateSelection(QModelIndex(), QModelIndex());
}
void CMakeBuildSettingsWidget::setError(const QString &message)
......@@ -308,6 +333,7 @@ void CMakeBuildSettingsWidget::setError(const QString &message)
m_errorMessageLabel->setToolTip(message);
m_editButton->setEnabled(!showError);
m_unsetButton->setEnabled(!showError);
m_resetButton->setEnabled(!showError);
m_showAdvancedCheckBox->setEnabled(!showError);
m_filterEdit->setEnabled(!showError);
......@@ -356,26 +382,12 @@ void CMakeBuildSettingsWidget::updateFromKit()
m_configModel->setKitConfiguration(configHash);
}
static QModelIndex mapToSource(const QAbstractItemView *view, const QModelIndex &idx)
{
if (!idx.isValid())
return idx;
QAbstractItemModel *model = view->model();
QModelIndex result = idx;
while (QSortFilterProxyModel *proxy = qobject_cast<QSortFilterProxyModel *>(model)) {
result = proxy->mapToSource(result);
model = proxy->sourceModel();
}
return result;
}
void CMakeBuildSettingsWidget::updateSelection(const QModelIndex &current, const QModelIndex &previous)
{
Q_UNUSED(previous);
const QModelIndex currentModelIndex = mapToSource(m_configView, current);
if (currentModelIndex.isValid())
m_editButton->setEnabled(currentModelIndex.flags().testFlag(Qt::ItemIsEditable));
m_editButton->setEnabled(current.isValid() && current.flags().testFlag(Qt::ItemIsEditable));
m_unsetButton->setEnabled(current.isValid() && current.flags().testFlag(Qt::ItemIsSelectable));
}
QAction *CMakeBuildSettingsWidget::createForceAction(int type, const QModelIndex &idx)
......
......@@ -78,6 +78,7 @@ private:
QPushButton *m_addButton;
QMenu *m_addButtonMenu;
QPushButton *m_editButton;
QPushButton *m_unsetButton;
QPushButton *m_resetButton;
QCheckBox *m_showAdvancedCheckBox;
QPushButton *m_reconfigureButton;
......
......@@ -44,7 +44,8 @@ CMakeConfigItem::CMakeConfigItem() = default;
CMakeConfigItem::CMakeConfigItem(const CMakeConfigItem &other) :
key(other.key), type(other.type), isAdvanced(other.isAdvanced),
value(other.value), documentation(other.documentation), values(other.values)
inCMakeCache(false), isUnset(other.isUnset), value(other.value),
documentation(other.documentation), values(other.values)
{}
CMakeConfigItem::CMakeConfigItem(const QByteArray &k, Type t,
......@@ -316,6 +317,9 @@ QString CMakeConfigItem::toString(const Utils::MacroExpander *expander) const
if (key.isEmpty() || type == CMakeProjectManager::CMakeConfigItem::STATIC)
return QString();
if (isUnset)
return "unset " + QString::fromUtf8(key);
QString typeStr;
switch (type)
{
......@@ -344,13 +348,15 @@ QString CMakeConfigItem::toString(const Utils::MacroExpander *expander) const
QString CMakeConfigItem::toArgument(const Utils::MacroExpander *expander) const
{
if (isUnset)
return "-U" + QString::fromUtf8(key);
return "-D" + toString(expander);
}
bool CMakeConfigItem::operator==(const CMakeConfigItem &o) const
{
// type, isAdvanced and documentation do not matter for a match!
return o.key == key && o.value == value;
return o.key == key && o.value == value && o.isUnset == isUnset;
}
#if WITH_TESTS
......
......@@ -68,6 +68,7 @@ public:
Type type = STRING;
bool isAdvanced = false;
bool inCMakeCache = false;
bool isUnset = false;
QByteArray value; // converted to string as needed
QByteArray documentation;
QStringList values;
......
......@@ -121,13 +121,16 @@ void ConfigModel::resetAllChanges()
InternalDataItem ni(i);
ni.newValue.clear();
ni.isUserChanged = false;
ni.isUnset = false;
return ni;
}));
}
bool ConfigModel::hasChanges() const
{
return Utils::contains(m_configuration, [](const InternalDataItem &i) { return i.isUserChanged || i.isUserNew; });
return Utils::contains(m_configuration, [](const InternalDataItem &i) {
return i.isUserChanged || i.isUserNew || i.isUnset;
});
}
bool ConfigModel::hasCMakeChanges() const
......@@ -155,6 +158,19 @@ void ConfigModel::forceTo(const QModelIndex &idx, const ConfigModel::DataItem::T
emit dataChanged(valueIdx, valueIdx);
}
void ConfigModel::toggleUnsetFlag(const QModelIndex &idx)
{
Utils::TreeItem *item = itemForIndex(idx);
auto cmti = dynamic_cast<Internal::ConfigModelTreeItem *>(item);
QTC_ASSERT(cmti, return);
cmti->dataItem->isUnset = !cmti->dataItem->isUnset;
const QModelIndex valueIdx = idx.sibling(idx.row(), 1);
const QModelIndex keyIdx = idx.sibling(idx.row(), 0);
emit dataChanged(keyIdx, valueIdx);
}
ConfigModel::DataItem ConfigModel::dataItemFromIndex(const QModelIndex &idx)
{
const QAbstractItemModel *m = idx.model();
......@@ -190,7 +206,7 @@ QList<ConfigModel::DataItem> ConfigModel::configurationChanges() const
{
const QList<InternalDataItem> tmp
= Utils::filtered(m_configuration, [](const InternalDataItem &i) {
return i.isUserChanged || i.isUserNew || !i.inCMakeCache;
return i.isUserChanged || i.isUserNew || !i.inCMakeCache || i.isUnset;
});
return Utils::transform(tmp, [](const InternalDataItem &item) {
DataItem newItem(item);
......@@ -246,7 +262,9 @@ void ConfigModel::setConfiguration(const QList<ConfigModel::InternalDataItem> &c
QList<InternalDataItem> result;
while (newIt != newEndIt && oldIt != oldEndIt) {
if (newIt->isHidden) {
if (oldIt->isUnset) {
++oldIt;
} else if (newIt->isHidden || newIt->isUnset) {
++newIt;
} else if (newIt->key < oldIt->key) {
// Add new entry:
......@@ -350,6 +368,8 @@ QString ConfigModel::InternalDataItem::toolTip() const
QString ConfigModel::InternalDataItem::currentValue() const
{
if (isUnset)
return value;
return isUserChanged ? newValue : value;
}
......@@ -387,7 +407,7 @@ QVariant ConfigModelTreeItem::data(int column, int role) const
QFont font;
font.setItalic(dataItem->isCMakeChanged);
font.setBold(dataItem->isUserNew);
font.setStrikeOut(!dataItem->inCMakeCache && !dataItem->isUserNew);
font.setStrikeOut((!dataItem->inCMakeCache && !dataItem->isUserNew) || dataItem->isUnset);
return font;
}
default:
......@@ -406,8 +426,9 @@ QVariant ConfigModelTreeItem::data(int column, int role) const
return (dataItem->type == ConfigModel::DataItem::BOOLEAN) ? QVariant(isTrue(value)) : QVariant(value);
case Qt::FontRole: {
QFont font;
font.setBold(dataItem->isUserChanged || dataItem->isUserNew);
font.setBold((dataItem->isUserChanged || dataItem->isUserNew) && !dataItem->isUnset);
font.setItalic(dataItem->isCMakeChanged);
font.setStrikeOut((!dataItem->inCMakeCache && !dataItem->isUserNew) || dataItem->isUnset);
return font;
}
case Qt::ForegroundRole:
......@@ -429,6 +450,8 @@ bool ConfigModelTreeItem::setData(int column, const QVariant &value, int role)
{
QTC_ASSERT(column >= 0 && column < 2, return false);
QTC_ASSERT(dataItem, return false);
if (dataItem->isUnset)
return false;
QString newValue = value.toString();
if (role == Qt::CheckStateRole) {
......@@ -467,6 +490,9 @@ Qt::ItemFlags ConfigModelTreeItem::flags(int column) const
QTC_ASSERT(dataItem, return Qt::NoItemFlags);
if (dataItem->isUnset)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (column == 1) {
if (dataItem->type == ConfigModel::DataItem::BOOLEAN)
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
......
......@@ -52,6 +52,7 @@ public:
bool isHidden = false;
bool isAdvanced = false;
bool inCMakeCache = false;
bool isUnset = false;
QString value;
QString description;
QStringList values;
......@@ -79,6 +80,8 @@ public:
bool canForceTo(const QModelIndex &idx, const DataItem::Type type) const;
void forceTo(const QModelIndex &idx, const DataItem::Type type);
void toggleUnsetFlag(const QModelIndex &idx);
static DataItem dataItemFromIndex(const QModelIndex &idx);
QList<DataItem> configurationChanges() const;
......
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