Commit be376ae7 authored by Tobias Hunger's avatar Tobias Hunger

CMake: Enable type forcing for CMake configuation values

Allow to force the type for CMake configuration values, now that
this type changes how you can edit the values.

Change-Id: Id89e0ec8547b778fc0aff9a2e00c0d7406cbcac1
Reviewed-by: default avatarhjk <hjk@qt.io>
parent 9afe6fa1
......@@ -378,6 +378,33 @@ void CMakeBuildSettingsWidget::updateSelection(const QModelIndex &current, const
m_editButton->setEnabled(currentModelIndex.flags().testFlag(Qt::ItemIsEditable));
}
QAction *CMakeBuildSettingsWidget::createForceAction(int type, const QModelIndex &idx)
{
ConfigModel::DataItem::Type t = static_cast<ConfigModel::DataItem::Type>(type);
QString typeString;
switch (type) {
case ConfigModel::DataItem::BOOLEAN:
typeString = tr("bool", "display string for cmake type BOOLEAN");
break;
case ConfigModel::DataItem::FILE:
typeString = tr("file", "display string for cmake type FILE");
break;
case ConfigModel::DataItem::DIRECTORY:
typeString = tr("directory", "display string for cmake type DIRECTORY");
break;
case ConfigModel::DataItem::STRING:
typeString = tr("string", "display string for cmake type STRING");
break;
case ConfigModel::DataItem::UNKNOWN:
return nullptr;
}
QAction *forceAction = new QAction(tr("Force to %1").arg(typeString));
forceAction->setEnabled(m_configModel->canForceTo(idx, t));
connect(forceAction, &QAction::triggered,
this, [this, idx, t]() { m_configModel->forceTo(idx, t); });
return forceAction;
}
bool CMakeBuildSettingsWidget::eventFilter(QObject *target, QEvent *event)
{
// handle context menu events:
......@@ -392,10 +419,15 @@ bool CMakeBuildSettingsWidget::eventFilter(QObject *target, QEvent *event)
QMenu *menu = new QMenu(this);
connect(menu, &QMenu::triggered, menu, &QMenu::deleteLater);
QAction *forceToStringAction = new QAction(tr("Force to String"), nullptr);
forceToStringAction->setEnabled(m_configModel->canForceToString(idx));
menu->addAction(forceToStringAction);
connect(forceToStringAction, &QAction::triggered, this, [this, idx]() { m_configModel->forceToString(idx); });
QAction *action = nullptr;
if ((action = createForceAction(ConfigModel::DataItem::BOOLEAN, idx)))
menu->addAction(action);
if ((action = createForceAction(ConfigModel::DataItem::FILE, idx)))
menu->addAction(action);
if ((action = createForceAction(ConfigModel::DataItem::DIRECTORY, idx)))
menu->addAction(action);
if ((action = createForceAction(ConfigModel::DataItem::STRING, idx)))
menu->addAction(action);
menu->move(e->globalPos());
menu->show();
......
......@@ -65,6 +65,7 @@ private:
void updateFromKit();
void updateSelection(const QModelIndex &current, const QModelIndex &previous);
QAction *createForceAction(int type, const QModelIndex &idx);
bool eventFilter(QObject *target, QEvent *event);
......
......@@ -135,23 +135,23 @@ bool ConfigModel::hasCMakeChanges() const
return Utils::contains(m_configuration, [](const InternalDataItem &i) { return i.isCMakeChanged; });
}
bool ConfigModel::canForceToString(const QModelIndex &idx) const
bool ConfigModel::canForceTo(const QModelIndex &idx, const ConfigModel::DataItem::Type type) const
{
if (idx.model() != const_cast<ConfigModel *>(this) || idx.column() != 1)
return false;
Utils::TreeItem *item = itemForIndex(idx);
auto cmti = dynamic_cast<Internal::ConfigModelTreeItem *>(item);
return cmti && (cmti->dataItem->type != DataItem::STRING);
return cmti && (cmti->dataItem->type != type);
}
void ConfigModel::forceToString(const QModelIndex &idx)
void ConfigModel::forceTo(const QModelIndex &idx, const ConfigModel::DataItem::Type type)
{
QTC_ASSERT(canForceToString(idx), return);
QTC_ASSERT(canForceTo(idx, type), return);
Utils::TreeItem *item = itemForIndex(idx);
auto cmti = dynamic_cast<Internal::ConfigModelTreeItem *>(item);
cmti->dataItem->type = DataItem::STRING;
const QModelIndex valueIdx = idx.sibling(1, idx.column());
cmti->dataItem->type = type;
const QModelIndex valueIdx = idx.sibling(idx.row(), 1);
emit dataChanged(valueIdx, valueIdx);
}
......
......@@ -73,8 +73,8 @@ public:
bool hasChanges() const;
bool hasCMakeChanges() const;
bool canForceToString(const QModelIndex &idx) const;
void forceToString(const QModelIndex &idx);
bool canForceTo(const QModelIndex &idx, const DataItem::Type type) const;
void forceTo(const QModelIndex &idx, const DataItem::Type type);
static DataItem dataItemFromIndex(const QModelIndex &idx);
......
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