From 090fd0e5f5de4ccdf59eb3e520112f69bfd2ea0c Mon Sep 17 00:00:00 2001 From: hjk Date: Tue, 13 Feb 2024 17:15:28 +0100 Subject: [PATCH] Utils: Merge NamesValueModel and EnvironmentModel (1/2) The abstraction is nowhere used (anymore?) In order to keep the diff small, this here merges environmentmodel.{h,cpp} into namevaluemodel.{h,cpp} which will be renamed back to env* in a second step. Change-Id: I1e7c14012ec3d3f54d8557f4b737a59ede2283e7 Reviewed-by: Christian Kandeler --- src/libs/utils/CMakeLists.txt | 1 - src/libs/utils/environmentfwd.h | 3 - src/libs/utils/environmentmodel.cpp | 20 ------- src/libs/utils/environmentmodel.h | 19 ------- src/libs/utils/namevaluemodel.cpp | 56 ++++++++++--------- src/libs/utils/namevaluemodel.h | 17 +++--- src/libs/utils/namevaluevalidator.cpp | 2 +- src/libs/utils/namevaluevalidator.h | 6 +- src/libs/utils/utils.qbs | 2 - .../projectexplorer/environmentwidget.cpp | 2 +- 10 files changed, 42 insertions(+), 86 deletions(-) delete mode 100644 src/libs/utils/environmentmodel.cpp delete mode 100644 src/libs/utils/environmentmodel.h diff --git a/src/libs/utils/CMakeLists.txt b/src/libs/utils/CMakeLists.txt index 9f11f3fc83c..dc3322456e5 100644 --- a/src/libs/utils/CMakeLists.txt +++ b/src/libs/utils/CMakeLists.txt @@ -44,7 +44,6 @@ add_qtc_library(Utils environment.cpp environment.h environmentdialog.cpp environmentdialog.h environmentfwd.h - environmentmodel.cpp environmentmodel.h execmenu.cpp execmenu.h expected.h externalterminalprocessimpl.cpp externalterminalprocessimpl.h diff --git a/src/libs/utils/environmentfwd.h b/src/libs/utils/environmentfwd.h index 8a2c444cc42..deb78279b5c 100644 --- a/src/libs/utils/environmentfwd.h +++ b/src/libs/utils/environmentfwd.h @@ -19,7 +19,4 @@ class PreprocessorMacroDictionary; using PreprocessorMacroItem = NameValueItem; using PreprocessorMacroItems = NameValueItems; -class NameValueModel; -class EnvironmentModel; - } // namespace Utils diff --git a/src/libs/utils/environmentmodel.cpp b/src/libs/utils/environmentmodel.cpp deleted file mode 100644 index 13329597846..00000000000 --- a/src/libs/utils/environmentmodel.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 - -#include "environmentmodel.h" - -#include "environment.h" - -namespace Utils { - -Environment EnvironmentModel::baseEnvironment() const -{ - return Environment(baseNameValueDictionary()); -} - -void EnvironmentModel::setBaseEnvironment(const Environment &env) -{ - setBaseNameValueDictionary(env.toDictionary()); -} - -} // namespace Utils diff --git a/src/libs/utils/environmentmodel.h b/src/libs/utils/environmentmodel.h deleted file mode 100644 index 82bf98d43f5..00000000000 --- a/src/libs/utils/environmentmodel.h +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 - -#pragma once - -#include "utils_global.h" - -#include "namevaluemodel.h" - -namespace Utils { - -class QTCREATOR_UTILS_EXPORT EnvironmentModel : public NameValueModel -{ -public: - Environment baseEnvironment() const; - void setBaseEnvironment(const Environment &env); -}; - -} // namespace Utils diff --git a/src/libs/utils/namevaluemodel.cpp b/src/libs/utils/namevaluemodel.cpp index 57327c4ebed..400824763f8 100644 --- a/src/libs/utils/namevaluemodel.cpp +++ b/src/libs/utils/namevaluemodel.cpp @@ -4,6 +4,7 @@ #include "namevaluemodel.h" #include "algorithm.h" +#include "environment.h" #include "hostosinfo.h" #include "namevaluedictionary.h" #include "namevalueitem.h" @@ -20,7 +21,7 @@ namespace Utils { namespace Internal { -class NameValueModelPrivate +class EnvironmentModelPrivate { public: void updateResultNameValueDictionary() @@ -78,21 +79,22 @@ public: } // namespace Internal -NameValueModel::NameValueModel(QObject *parent) +EnvironmentModel::EnvironmentModel(QObject *parent) : QAbstractTableModel(parent) - , d(std::make_unique()) + , d(std::make_unique()) {} -NameValueModel::~NameValueModel() = default; +EnvironmentModel::~EnvironmentModel() = default; -QString NameValueModel::indexToVariable(const QModelIndex &index) const +QString EnvironmentModel::indexToVariable(const QModelIndex &index) const { const auto it = std::next(d->m_resultNameValueDictionary.constBegin(), index.row()); return d->m_resultNameValueDictionary.key(it); } -void NameValueModel::setBaseNameValueDictionary(const NameValueDictionary &dictionary) +void EnvironmentModel::setBaseEnvironment(const Environment &env) { + const NameValueDictionary dictionary = env.toDictionary(); if (d->m_baseNameValueDictionary == dictionary) return; beginResetModel(); @@ -101,14 +103,14 @@ void NameValueModel::setBaseNameValueDictionary(const NameValueDictionary &dicti endResetModel(); } -int NameValueModel::rowCount(const QModelIndex &parent) const +int EnvironmentModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return d->m_resultNameValueDictionary.size(); } -int NameValueModel::columnCount(const QModelIndex &parent) const +int EnvironmentModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; @@ -116,17 +118,17 @@ int NameValueModel::columnCount(const QModelIndex &parent) const return 2; } -bool NameValueModel::changes(const QString &name) const +bool EnvironmentModel::changes(const QString &name) const { return d->findInChanges(name) >= 0; } -const NameValueDictionary &NameValueModel::baseNameValueDictionary() const +Environment EnvironmentModel::baseEnvironment() const { - return d->m_baseNameValueDictionary; + return Environment(d->m_baseNameValueDictionary); } -QVariant NameValueModel::data(const QModelIndex &index, int role) const +QVariant EnvironmentModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); @@ -176,13 +178,13 @@ QVariant NameValueModel::data(const QModelIndex &index, int role) const return QVariant(); } -Qt::ItemFlags NameValueModel::flags(const QModelIndex &index) const +Qt::ItemFlags EnvironmentModel::flags(const QModelIndex &index) const { Q_UNUSED(index) return Qt::ItemIsSelectable | Qt::ItemIsEnabled; } -QVariant NameValueModel::headerData(int section, Qt::Orientation orientation, int role) const +QVariant EnvironmentModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Vertical || role != Qt::DisplayRole) return QVariant(); @@ -192,7 +194,7 @@ QVariant NameValueModel::headerData(int section, Qt::Orientation orientation, in /// ***************** /// Utility functions /// ***************** -QModelIndex NameValueModel::variableToIndex(const QString &name) const +QModelIndex EnvironmentModel::variableToIndex(const QString &name) const { int row = d->findInResult(name); if (row == -1) @@ -200,7 +202,7 @@ QModelIndex NameValueModel::variableToIndex(const QString &name) const return index(row, 0); } -bool NameValueModel::setData(const QModelIndex &index, const QVariant &value, int role) +bool EnvironmentModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid() || role != Qt::EditRole) return false; @@ -262,12 +264,12 @@ bool NameValueModel::setData(const QModelIndex &index, const QVariant &value, in return false; } -QModelIndex NameValueModel::addVariable() +QModelIndex EnvironmentModel::addVariable() { return addVariable(NameValueItem("NEWVAR", "VALUE")); } -QModelIndex NameValueModel::addVariable(const NameValueItem &item) +QModelIndex EnvironmentModel::addVariable(const NameValueItem &item) { // Return existing index if the name is already in the result set: int pos = d->findInResult(item.name); @@ -298,7 +300,7 @@ QModelIndex NameValueModel::addVariable(const NameValueItem &item) return index(insertPos, 0, QModelIndex()); } -void NameValueModel::resetVariable(const QString &name) +void EnvironmentModel::resetVariable(const QString &name) { int rowInChanges = d->findInChanges(name); if (rowInChanges < 0) @@ -323,7 +325,7 @@ void NameValueModel::resetVariable(const QString &name) } } -void NameValueModel::unsetVariable(const QString &name) +void EnvironmentModel::unsetVariable(const QString &name) { // This does not change the number of rows as we will display a // in place of the original variable! @@ -347,7 +349,7 @@ void NameValueModel::unsetVariable(const QString &name) emit userChangesChanged(); } -void NameValueModel::toggleVariable(const QModelIndex &idx) +void EnvironmentModel::toggleVariable(const QModelIndex &idx) { const QString name = indexToVariable(idx); const auto newIt = d->m_resultNameValueDictionary.constFind(name); @@ -371,28 +373,28 @@ void NameValueModel::toggleVariable(const QModelIndex &idx) emit userChangesChanged(); } -bool NameValueModel::isUnset(const QString &name) +bool EnvironmentModel::isUnset(const QString &name) { const int pos = d->findInChanges(name); return pos == -1 ? false : d->m_items.at(pos).operation == NameValueItem::Unset; } -bool NameValueModel::isEnabled(const QString &name) const +bool EnvironmentModel::isEnabled(const QString &name) const { return d->m_resultNameValueDictionary.isEnabled(d->m_resultNameValueDictionary.constFind(name)); } -bool NameValueModel::canReset(const QString &name) +bool EnvironmentModel::canReset(const QString &name) { return d->m_baseNameValueDictionary.hasKey(name); } -NameValueItems NameValueModel::userChanges() const +NameValueItems EnvironmentModel::userChanges() const { return d->m_items; } -void NameValueModel::setUserChanges(const NameValueItems &items) +void EnvironmentModel::setUserChanges(const NameValueItems &items) { NameValueItems filtered = Utils::filtered(items, [](const NameValueItem &i) { return i.name != "export " && !i.name.contains('='); @@ -421,7 +423,7 @@ void NameValueModel::setUserChanges(const NameValueItems &items) emit userChangesChanged(); } -bool NameValueModel::currentEntryIsPathList(const QModelIndex ¤t) const +bool EnvironmentModel::currentEntryIsPathList(const QModelIndex ¤t) const { if (!current.isValid()) return false; diff --git a/src/libs/utils/namevaluemodel.h b/src/libs/utils/namevaluemodel.h index 84c1f2cece9..55f53ed59e6 100644 --- a/src/libs/utils/namevaluemodel.h +++ b/src/libs/utils/namevaluemodel.h @@ -13,17 +13,15 @@ namespace Utils { -namespace Internal { -class NameValueModelPrivate; -} +namespace Internal { class EnvironmentModelPrivate; } -class QTCREATOR_UTILS_EXPORT NameValueModel : public QAbstractTableModel +class QTCREATOR_UTILS_EXPORT EnvironmentModel : public QAbstractTableModel { Q_OBJECT public: - explicit NameValueModel(QObject *parent = nullptr); - ~NameValueModel() override; + explicit EnvironmentModel(QObject *parent = nullptr); + ~EnvironmentModel() override; int rowCount(const QModelIndex &parent) const override; int columnCount(const QModelIndex &parent) const override; @@ -34,6 +32,9 @@ public: Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + Environment baseEnvironment() const; + void setBaseEnvironment(const Environment &env); + QModelIndex addVariable(); QModelIndex addVariable(const NameValueItem &item); void resetVariable(const QString &name); @@ -45,8 +46,6 @@ public: QString indexToVariable(const QModelIndex &index) const; QModelIndex variableToIndex(const QString &name) const; bool changes(const QString &key) const; - const NameValueDictionary &baseNameValueDictionary() const; - void setBaseNameValueDictionary(const NameValueDictionary &dictionary); NameValueItems userChanges() const; void setUserChanges(const NameValueItems &items); bool currentEntryIsPathList(const QModelIndex ¤t) const; @@ -59,7 +58,7 @@ signals: void focusIndex(const QModelIndex &index); private: - std::unique_ptr d; + std::unique_ptr d; }; } // namespace Utils diff --git a/src/libs/utils/namevaluevalidator.cpp b/src/libs/utils/namevaluevalidator.cpp index ef2ccbc9a08..e64c1528939 100644 --- a/src/libs/utils/namevaluevalidator.cpp +++ b/src/libs/utils/namevaluevalidator.cpp @@ -10,7 +10,7 @@ namespace Utils { NameValueValidator::NameValueValidator(QWidget *parent, - NameValueModel *model, + EnvironmentModel *model, QTreeView *view, const QModelIndex &index, const QString &toolTipText) diff --git a/src/libs/utils/namevaluevalidator.h b/src/libs/utils/namevaluevalidator.h index bf0f1b27f74..e06560f1896 100644 --- a/src/libs/utils/namevaluevalidator.h +++ b/src/libs/utils/namevaluevalidator.h @@ -16,13 +16,13 @@ QT_END_NAMESPACE namespace Utils { -class NameValueModel; +class EnvironmentModel; class QTCREATOR_UTILS_EXPORT NameValueValidator : public QValidator { public: NameValueValidator(QWidget *parent, - NameValueModel *model, + EnvironmentModel *model, QTreeView *view, const QModelIndex &index, const QString &toolTipText); @@ -33,7 +33,7 @@ public: private: const QString m_toolTipText; - NameValueModel *m_model; + EnvironmentModel *m_model; QTreeView *m_view; QPersistentModelIndex m_index; mutable QTimer m_hideTipTimer; diff --git a/src/libs/utils/utils.qbs b/src/libs/utils/utils.qbs index d7ccd1d6761..d062400ee1c 100644 --- a/src/libs/utils/utils.qbs +++ b/src/libs/utils/utils.qbs @@ -101,8 +101,6 @@ QtcLibrary { "environment.h", "environmentdialog.cpp", "environmentdialog.h", - "environmentmodel.cpp", - "environmentmodel.h", "execmenu.cpp", "execmenu.h", "externalterminalprocessimpl.cpp", diff --git a/src/plugins/projectexplorer/environmentwidget.cpp b/src/plugins/projectexplorer/environmentwidget.cpp index 9a63d789ece..4331dbb06f1 100644 --- a/src/plugins/projectexplorer/environmentwidget.cpp +++ b/src/plugins/projectexplorer/environmentwidget.cpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include