Utils: Rename NameValueItem to EnvironmentItem (1/2)

Only used in Environments nowadays.

Change-Id: I64a645ebebd5cb57c50d5d8f72a5e4dba40e122f
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2024-02-13 18:01:32 +01:00
parent 91c1c244a1
commit 0a1783d656
20 changed files with 126 additions and 132 deletions

View File

@@ -50,7 +50,7 @@ Environment::Environment(const NameValueDictionary &dict)
m_changeItems.append(dict); m_changeItems.append(dict);
} }
NameValueItems Environment::diff(const Environment &other, bool checkAppendPrepend) const EnvironmentItems Environment::diff(const Environment &other, bool checkAppendPrepend) const
{ {
const NameValueDictionary &dict = resolved(); const NameValueDictionary &dict = resolved();
const NameValueDictionary &otherDict = other.resolved(); const NameValueDictionary &otherDict = other.resolved();
@@ -393,7 +393,7 @@ void Environment::unset(const QString &key)
addItem(Item{std::in_place_index_t<UnsetValue>(), key}); addItem(Item{std::in_place_index_t<UnsetValue>(), key});
} }
void Environment::modify(const NameValueItems &items) void Environment::modify(const EnvironmentItems &items)
{ {
addItem(Item{std::in_place_index_t<Modify>(), items}); addItem(Item{std::in_place_index_t<Modify>(), items});
} }
@@ -485,7 +485,7 @@ const NameValueDictionary &Environment::resolved() const
break; break;
} }
case Modify: { case Modify: {
NameValueItems items = std::get<Modify>(item); EnvironmentItems items = std::get<Modify>(item);
m_dict.modify(items); m_dict.modify(items);
break; break;
} }

View File

@@ -37,7 +37,7 @@ public:
void set(const QString &key, const QString &value, bool enabled = true); void set(const QString &key, const QString &value, bool enabled = true);
void setFallback(const QString &key, const QString &value); void setFallback(const QString &key, const QString &value);
void unset(const QString &key); void unset(const QString &key);
void modify(const NameValueItems &items); void modify(const EnvironmentItems &items);
bool hasChanges() const; bool hasChanges() const;
@@ -79,7 +79,7 @@ public:
QStringList expandVariables(const QStringList &input) const; QStringList expandVariables(const QStringList &input) const;
NameValueDictionary toDictionary() const; // FIXME: avoid NameValueDictionary toDictionary() const; // FIXME: avoid
NameValueItems diff(const Environment &other, bool checkAppendPrepend = false) const; // FIXME: avoid EnvironmentItems diff(const Environment &other, bool checkAppendPrepend = false) const; // FIXME: avoid
struct Entry { QString key; QString value; bool enabled; }; struct Entry { QString key; QString value; bool enabled; };
using FindResult = std::optional<Entry>; using FindResult = std::optional<Entry>;
@@ -117,7 +117,7 @@ public:
QString, // UnsetValue (key) QString, // UnsetValue (key)
std::tuple<QString, QString, PathSeparator>, // PrependOrSet (key, value, separator) std::tuple<QString, QString, PathSeparator>, // PrependOrSet (key, value, separator)
std::tuple<QString, QString, PathSeparator>, // AppendOrSet (key, value, separator) std::tuple<QString, QString, PathSeparator>, // AppendOrSet (key, value, separator)
NameValueItems, // Modify EnvironmentItems, // Modify
std::monostate, // SetupEnglishOutput std::monostate, // SetupEnglishOutput
FilePath // SetupSudoAskPass (file path of qtc-askpass or ssh-askpass) FilePath // SetupSudoAskPass (file path of qtc-askpass or ssh-askpass)
>; >;

View File

@@ -7,16 +7,10 @@
namespace Utils { namespace Utils {
class NameValueDictionary;
class NameValueItem;
using NameValueItems = QList<NameValueItem>;
class Environment; class Environment;
using EnvironmentItem = NameValueItem; class EnvironmentItem;
using EnvironmentItems = NameValueItems; using EnvironmentItems = QList<EnvironmentItem>;
class PreprocessorMacroDictionary; class NameValueDictionary;
using PreprocessorMacroItem = NameValueItem;
using PreprocessorMacroItems = NameValueItems;
} // namespace Utils } // namespace Utils

View File

@@ -30,8 +30,8 @@ public:
m_resultNameValueDictionary.modify(m_items); m_resultNameValueDictionary.modify(m_items);
// Add removed variables again and mark them as "<UNSET>" so // Add removed variables again and mark them as "<UNSET>" so
// that the user can actually see those removals: // that the user can actually see those removals:
for (const NameValueItem &item : std::as_const(m_items)) { for (const EnvironmentItem &item : std::as_const(m_items)) {
if (item.operation == NameValueItem::Unset) if (item.operation == EnvironmentItem::Unset)
m_resultNameValueDictionary.set(item.name, Tr::tr("<UNSET>")); m_resultNameValueDictionary.set(item.name, Tr::tr("<UNSET>"));
} }
} }
@@ -74,7 +74,7 @@ public:
NameValueDictionary m_baseNameValueDictionary; NameValueDictionary m_baseNameValueDictionary;
NameValueDictionary m_resultNameValueDictionary; NameValueDictionary m_resultNameValueDictionary;
NameValueItems m_items; EnvironmentItems m_items;
}; };
} // namespace Internal } // namespace Internal
@@ -145,7 +145,7 @@ QVariant EnvironmentModel::data(const QModelIndex &index, int role) const
// Do not return "<UNSET>" when editing a previously unset variable: // Do not return "<UNSET>" when editing a previously unset variable:
if (role == Qt::EditRole) { if (role == Qt::EditRole) {
int pos = d->findInChanges(indexToVariable(index)); int pos = d->findInChanges(indexToVariable(index));
if (pos != -1 && d->m_items.at(pos).operation == NameValueItem::Unset) if (pos != -1 && d->m_items.at(pos).operation == EnvironmentItem::Unset)
return QString(); return QString();
} }
QString value = d->m_resultNameValueDictionary.value(resultIterator); QString value = d->m_resultNameValueDictionary.value(resultIterator);
@@ -225,7 +225,7 @@ bool EnvironmentModel::setData(const QModelIndex &index, const QVariant &value,
if (d->m_resultNameValueDictionary.hasKey(newName) || newName.isEmpty()) if (d->m_resultNameValueDictionary.hasKey(newName) || newName.isEmpty())
return false; return false;
NameValueItem newVariable(newName, oldValue); EnvironmentItem newVariable(newName, oldValue);
if (changesPos != -1) if (changesPos != -1)
resetVariable(oldName); // restore the original base variable again resetVariable(oldName); // restore the original base variable again
@@ -249,12 +249,12 @@ bool EnvironmentModel::setData(const QModelIndex &index, const QVariant &value,
} else { } else {
// ... and changed it again // ... and changed it again
d->m_items[changesPos].value = stringValue; d->m_items[changesPos].value = stringValue;
if (d->m_items[changesPos].operation == NameValueItem::Unset) if (d->m_items[changesPos].operation == EnvironmentItem::Unset)
d->m_items[changesPos].operation = NameValueItem::SetEnabled; d->m_items[changesPos].operation = EnvironmentItem::SetEnabled;
} }
} else { } else {
// Add a new change item: // Add a new change item:
d->m_items.append(NameValueItem(oldName, stringValue)); d->m_items.append(EnvironmentItem(oldName, stringValue));
} }
d->updateResultNameValueDictionary(); d->updateResultNameValueDictionary();
emit dataChanged(index, index); emit dataChanged(index, index);
@@ -266,10 +266,10 @@ bool EnvironmentModel::setData(const QModelIndex &index, const QVariant &value,
QModelIndex EnvironmentModel::addVariable() QModelIndex EnvironmentModel::addVariable()
{ {
return addVariable(NameValueItem("NEWVAR", "VALUE")); return addVariable(EnvironmentItem("NEWVAR", "VALUE"));
} }
QModelIndex EnvironmentModel::addVariable(const NameValueItem &item) QModelIndex EnvironmentModel::addVariable(const EnvironmentItem &item)
{ {
// Return existing index if the name is already in the result set: // Return existing index if the name is already in the result set:
int pos = d->findInResult(item.name); int pos = d->findInResult(item.name);
@@ -283,7 +283,7 @@ QModelIndex EnvironmentModel::addVariable(const NameValueItem &item)
Q_ASSERT(changePos >= 0); Q_ASSERT(changePos >= 0);
// Do not insert a line here as we listed the variable as <UNSET> before! // Do not insert a line here as we listed the variable as <UNSET> before!
Q_ASSERT(d->m_items.at(changePos).name == item.name); Q_ASSERT(d->m_items.at(changePos).name == item.name);
Q_ASSERT(d->m_items.at(changePos).operation == NameValueItem::Unset); Q_ASSERT(d->m_items.at(changePos).operation == EnvironmentItem::Unset);
Q_ASSERT(d->m_items.at(changePos).value.isEmpty()); Q_ASSERT(d->m_items.at(changePos).value.isEmpty());
d->m_items[changePos] = item; d->m_items[changePos] = item;
emit dataChanged(index(insertPos, 0, QModelIndex()), index(insertPos, 1, QModelIndex())); emit dataChanged(index(insertPos, 0, QModelIndex()), index(insertPos, 1, QModelIndex()));
@@ -336,14 +336,14 @@ void EnvironmentModel::unsetVariable(const QString &name)
// look in d->m_items for the variable // look in d->m_items for the variable
int pos = d->findInChanges(name); int pos = d->findInChanges(name);
if (pos != -1) { if (pos != -1) {
d->m_items[pos].operation = NameValueItem::Unset; d->m_items[pos].operation = EnvironmentItem::Unset;
d->m_items[pos].value.clear(); d->m_items[pos].value.clear();
d->updateResultNameValueDictionary(); d->updateResultNameValueDictionary();
emit dataChanged(index(row, 0, QModelIndex()), index(row, 1, QModelIndex())); emit dataChanged(index(row, 0, QModelIndex()), index(row, 1, QModelIndex()));
emit userChangesChanged(); emit userChangesChanged();
return; return;
} }
d->m_items.append(NameValueItem(name, QString(), NameValueItem::Unset)); d->m_items.append(EnvironmentItem(name, QString(), EnvironmentItem::Unset));
d->updateResultNameValueDictionary(); d->updateResultNameValueDictionary();
emit dataChanged(index(row, 0, QModelIndex()), index(row, 1, QModelIndex())); emit dataChanged(index(row, 0, QModelIndex()), index(row, 1, QModelIndex()));
emit userChangesChanged(); emit userChangesChanged();
@@ -355,7 +355,7 @@ void EnvironmentModel::toggleVariable(const QModelIndex &idx)
const auto newIt = d->m_resultNameValueDictionary.constFind(name); const auto newIt = d->m_resultNameValueDictionary.constFind(name);
QTC_ASSERT(newIt != d->m_resultNameValueDictionary.constEnd(), return); QTC_ASSERT(newIt != d->m_resultNameValueDictionary.constEnd(), return);
const auto op = d->m_resultNameValueDictionary.isEnabled(newIt) const auto op = d->m_resultNameValueDictionary.isEnabled(newIt)
? NameValueItem::SetDisabled : NameValueItem::SetEnabled; ? EnvironmentItem::SetDisabled : EnvironmentItem::SetEnabled;
const int changesPos = d->findInChanges(name); const int changesPos = d->findInChanges(name);
if (changesPos != -1) { if (changesPos != -1) {
const auto oldIt = d->m_baseNameValueDictionary.constFind(name); const auto oldIt = d->m_baseNameValueDictionary.constFind(name);
@@ -376,7 +376,7 @@ void EnvironmentModel::toggleVariable(const QModelIndex &idx)
bool EnvironmentModel::isUnset(const QString &name) bool EnvironmentModel::isUnset(const QString &name)
{ {
const int pos = d->findInChanges(name); const int pos = d->findInChanges(name);
return pos == -1 ? false : d->m_items.at(pos).operation == NameValueItem::Unset; return pos == -1 ? false : d->m_items.at(pos).operation == EnvironmentItem::Unset;
} }
bool EnvironmentModel::isEnabled(const QString &name) const bool EnvironmentModel::isEnabled(const QString &name) const
@@ -389,14 +389,14 @@ bool EnvironmentModel::canReset(const QString &name)
return d->m_baseNameValueDictionary.hasKey(name); return d->m_baseNameValueDictionary.hasKey(name);
} }
NameValueItems EnvironmentModel::userChanges() const EnvironmentItems EnvironmentModel::userChanges() const
{ {
return d->m_items; return d->m_items;
} }
void EnvironmentModel::setUserChanges(const NameValueItems &items) void EnvironmentModel::setUserChanges(const EnvironmentItems &items)
{ {
NameValueItems filtered = Utils::filtered(items, [](const NameValueItem &i) { EnvironmentItems filtered = Utils::filtered(items, [](const EnvironmentItem &i) {
return i.name != "export " && !i.name.contains('='); return i.name != "export " && !i.name.contains('=');
}); });
// We assume nobody is reordering the items here. // We assume nobody is reordering the items here.
@@ -404,7 +404,7 @@ void EnvironmentModel::setUserChanges(const NameValueItems &items)
return; return;
beginResetModel(); beginResetModel();
d->m_items = filtered; d->m_items = filtered;
for (NameValueItem &item : d->m_items) { for (EnvironmentItem &item : d->m_items) {
QString &name = item.name; QString &name = item.name;
name = name.trimmed(); name = name.trimmed();
if (name.startsWith("export ")) if (name.startsWith("export "))

View File

@@ -36,7 +36,7 @@ public:
void setBaseEnvironment(const Environment &env); void setBaseEnvironment(const Environment &env);
QModelIndex addVariable(); QModelIndex addVariable();
QModelIndex addVariable(const NameValueItem &item); QModelIndex addVariable(const EnvironmentItem &item);
void resetVariable(const QString &name); void resetVariable(const QString &name);
void unsetVariable(const QString &name); void unsetVariable(const QString &name);
void toggleVariable(const QModelIndex &index); void toggleVariable(const QModelIndex &index);
@@ -46,8 +46,8 @@ public:
QString indexToVariable(const QModelIndex &index) const; QString indexToVariable(const QModelIndex &index) const;
QModelIndex variableToIndex(const QString &name) const; QModelIndex variableToIndex(const QString &name) const;
bool changes(const QString &key) const; bool changes(const QString &key) const;
NameValueItems userChanges() const; EnvironmentItems userChanges() const;
void setUserChanges(const NameValueItems &items); void setUserChanges(const EnvironmentItems &items);
bool currentEntryIsPathList(const QModelIndex &current) const; bool currentEntryIsPathList(const QModelIndex &current) const;
signals: signals:

View File

@@ -97,34 +97,34 @@ int NameValueDictionary::size() const
return m_values.size(); return m_values.size();
} }
void NameValueDictionary::modify(const NameValueItems &items) void NameValueDictionary::modify(const EnvironmentItems &items)
{ {
NameValueDictionary resultKeyValueDictionary = *this; NameValueDictionary resultKeyValueDictionary = *this;
for (const NameValueItem &item : items) for (const EnvironmentItem &item : items)
item.apply(&resultKeyValueDictionary); item.apply(&resultKeyValueDictionary);
*this = resultKeyValueDictionary; *this = resultKeyValueDictionary;
} }
NameValueItems NameValueDictionary::diff(const NameValueDictionary &other, bool checkAppendPrepend) const EnvironmentItems NameValueDictionary::diff(const NameValueDictionary &other, bool checkAppendPrepend) const
{ {
NameValueMap::const_iterator thisIt = constBegin(); NameValueMap::const_iterator thisIt = constBegin();
NameValueMap::const_iterator otherIt = other.constBegin(); NameValueMap::const_iterator otherIt = other.constBegin();
NameValueItems result; EnvironmentItems result;
while (thisIt != constEnd() || otherIt != other.constEnd()) { while (thisIt != constEnd() || otherIt != other.constEnd()) {
if (thisIt == constEnd()) { if (thisIt == constEnd()) {
result.append({other.key(otherIt), other.value(otherIt), result.append({other.key(otherIt), other.value(otherIt),
otherIt.value().second ? NameValueItem::SetEnabled : NameValueItem::SetDisabled}); otherIt.value().second ? EnvironmentItem::SetEnabled : EnvironmentItem::SetDisabled});
++otherIt; ++otherIt;
} else if (otherIt == other.constEnd()) { } else if (otherIt == other.constEnd()) {
result.append(NameValueItem(key(thisIt), QString(), NameValueItem::Unset)); result.append(EnvironmentItem(key(thisIt), QString(), EnvironmentItem::Unset));
++thisIt; ++thisIt;
} else if (thisIt.key() < otherIt.key()) { } else if (thisIt.key() < otherIt.key()) {
result.append(NameValueItem(key(thisIt), QString(), NameValueItem::Unset)); result.append(EnvironmentItem(key(thisIt), QString(), EnvironmentItem::Unset));
++thisIt; ++thisIt;
} else if (thisIt.key() > otherIt.key()) { } else if (thisIt.key() > otherIt.key()) {
result.append({other.key(otherIt), otherIt.value().first, result.append({other.key(otherIt), otherIt.value().first,
otherIt.value().second ? NameValueItem::SetEnabled : NameValueItem::SetDisabled}); otherIt.value().second ? EnvironmentItem::SetEnabled : EnvironmentItem::SetDisabled});
++otherIt; ++otherIt;
} else { } else {
const QString &oldValue = thisIt.value().first; const QString &oldValue = thisIt.value().first;
@@ -137,16 +137,16 @@ NameValueItems NameValueDictionary::diff(const NameValueDictionary &other, bool
QString appended = newValue.right(newValue.size() - oldValue.size()); QString appended = newValue.right(newValue.size() - oldValue.size());
if (appended.startsWith(OsSpecificAspects::pathListSeparator(osType()))) if (appended.startsWith(OsSpecificAspects::pathListSeparator(osType())))
appended.remove(0, 1); appended.remove(0, 1);
result.append(NameValueItem(other.key(otherIt), appended, NameValueItem::Append)); result.append(EnvironmentItem(other.key(otherIt), appended, EnvironmentItem::Append));
} else if (checkAppendPrepend && newValue.endsWith(oldValue) } else if (checkAppendPrepend && newValue.endsWith(oldValue)
&& oldEnabled == newEnabled) { && oldEnabled == newEnabled) {
QString prepended = newValue.left(newValue.size() - oldValue.size()); QString prepended = newValue.left(newValue.size() - oldValue.size());
if (prepended.endsWith(OsSpecificAspects::pathListSeparator(osType()))) if (prepended.endsWith(OsSpecificAspects::pathListSeparator(osType())))
prepended.chop(1); prepended.chop(1);
result.append(NameValueItem(other.key(otherIt), prepended, NameValueItem::Prepend)); result.append(EnvironmentItem(other.key(otherIt), prepended, EnvironmentItem::Prepend));
} else { } else {
result.append({other.key(otherIt), newValue, newEnabled result.append({other.key(otherIt), newValue, newEnabled
? NameValueItem::SetEnabled : NameValueItem::SetDisabled}); ? EnvironmentItem::SetEnabled : EnvironmentItem::SetDisabled});
} }
} }
++otherIt; ++otherIt;

View File

@@ -48,9 +48,9 @@ public:
QString value(const QString &key) const; QString value(const QString &key) const;
void set(const QString &key, const QString &value, bool enabled = true); void set(const QString &key, const QString &value, bool enabled = true);
void unset(const QString &key); void unset(const QString &key);
void modify(const NameValueItems &items); void modify(const EnvironmentItems &items);
/// Return the KeyValueDictionary changes necessary to modify this into the other environment. /// Return the KeyValueDictionary changes necessary to modify this into the other environment.
NameValueItems diff(const NameValueDictionary &other, bool checkAppendPrepend = false) const; EnvironmentItems diff(const NameValueDictionary &other, bool checkAppendPrepend = false) const;
bool hasKey(const QString &key) const; bool hasKey(const QString &key) const;
OsType osType() const; OsType osType() const;
Qt::CaseSensitivity nameCaseSensitivity() const; Qt::CaseSensitivity nameCaseSensitivity() const;

View File

@@ -10,34 +10,34 @@
namespace Utils { namespace Utils {
void NameValueItem::sort(NameValueItems *list) void EnvironmentItem::sort(EnvironmentItems *list)
{ {
Utils::sort(*list, &NameValueItem::name); Utils::sort(*list, &EnvironmentItem::name);
} }
NameValueItems NameValueItem::fromStringList(const QStringList &list) EnvironmentItems EnvironmentItem::fromStringList(const QStringList &list)
{ {
NameValueItems result; EnvironmentItems result;
for (const QString &string : list) { for (const QString &string : list) {
int pos = string.indexOf("+="); int pos = string.indexOf("+=");
if (pos != -1) { if (pos != -1) {
result.append({string.left(pos), string.mid(pos + 2), NameValueItem::Append}); result.append({string.left(pos), string.mid(pos + 2), EnvironmentItem::Append});
continue; continue;
} }
pos = string.indexOf("=+"); pos = string.indexOf("=+");
if (pos != -1) { if (pos != -1) {
result.append({string.left(pos), string.mid(pos + 2), NameValueItem::Prepend}); result.append({string.left(pos), string.mid(pos + 2), EnvironmentItem::Prepend});
continue; continue;
} }
pos = string.indexOf('=', 1); pos = string.indexOf('=', 1);
if (pos == -1) { if (pos == -1) {
result.append(NameValueItem(string, QString(), NameValueItem::Unset)); result.append(EnvironmentItem(string, QString(), EnvironmentItem::Unset));
continue; continue;
} }
const int hashPos = string.indexOf('#'); const int hashPos = string.indexOf('#');
if (hashPos != -1 && hashPos < pos) { if (hashPos != -1 && hashPos < pos) {
result.append({string.mid(hashPos + 1, pos - hashPos - 1), string.mid(pos + 1), result.append({string.mid(hashPos + 1, pos - hashPos - 1), string.mid(pos + 1),
NameValueItem::SetDisabled}); EnvironmentItem::SetDisabled});
} else { } else {
result.append({string.left(pos), string.mid(pos + 1)}); result.append({string.left(pos), string.mid(pos + 1)});
} }
@@ -45,49 +45,49 @@ NameValueItems NameValueItem::fromStringList(const QStringList &list)
return result; return result;
} }
QStringList NameValueItem::toStringList(const NameValueItems &list) QStringList EnvironmentItem::toStringList(const EnvironmentItems &list)
{ {
return Utils::transform<QStringList>(list, [](const NameValueItem &item) { return Utils::transform<QStringList>(list, [](const EnvironmentItem &item) {
switch (item.operation) { switch (item.operation) {
case NameValueItem::Unset: case EnvironmentItem::Unset:
return item.name; return item.name;
case NameValueItem::Append: case EnvironmentItem::Append:
return QString(item.name + "+=" + item.value); return QString(item.name + "+=" + item.value);
case NameValueItem::Prepend: case EnvironmentItem::Prepend:
return QString(item.name + "=+" + item.value); return QString(item.name + "=+" + item.value);
case NameValueItem::SetDisabled: case EnvironmentItem::SetDisabled:
return QString('#' + item.name + '=' + item.value); return QString('#' + item.name + '=' + item.value);
case NameValueItem::SetEnabled: case EnvironmentItem::SetEnabled:
return QString(item.name + '=' + item.value); return QString(item.name + '=' + item.value);
} }
return QString(); return QString();
}); });
} }
NameValueItems NameValueItem::itemsFromVariantList(const QVariantList &list) EnvironmentItems EnvironmentItem::itemsFromVariantList(const QVariantList &list)
{ {
return Utils::transform<NameValueItems>(list, [](const QVariant &item) { return Utils::transform<EnvironmentItems>(list, [](const QVariant &item) {
return itemFromVariantList(item.toList()); return itemFromVariantList(item.toList());
}); });
} }
QVariantList NameValueItem::toVariantList(const NameValueItems &list) QVariantList EnvironmentItem::toVariantList(const EnvironmentItems &list)
{ {
return Utils::transform<QVariantList>(list, [](const NameValueItem &item) { return Utils::transform<QVariantList>(list, [](const EnvironmentItem &item) {
return QVariant(toVariantList(item)); return QVariant(toVariantList(item));
}); });
} }
NameValueItem NameValueItem::itemFromVariantList(const QVariantList &list) EnvironmentItem EnvironmentItem::itemFromVariantList(const QVariantList &list)
{ {
QTC_ASSERT(list.size() == 3, return NameValueItem("", "")); QTC_ASSERT(list.size() == 3, return EnvironmentItem("", ""));
QString key = list.value(0).toString(); QString key = list.value(0).toString();
Operation operation = Operation(list.value(1).toInt()); Operation operation = Operation(list.value(1).toInt());
QString value = list.value(2).toString(); QString value = list.value(2).toString();
return NameValueItem(key, value, operation); return EnvironmentItem(key, value, operation);
} }
QVariantList NameValueItem::toVariantList(const NameValueItem &item) QVariantList EnvironmentItem::toVariantList(const EnvironmentItem &item)
{ {
return QVariantList() << item.name << item.operation << item.value; return QVariantList() << item.name << item.operation << item.value;
} }
@@ -118,7 +118,7 @@ static QString expand(const NameValueDictionary *dictionary, QString value)
return value; return value;
} }
void NameValueItem::apply(NameValueDictionary *dictionary, Operation op) const void EnvironmentItem::apply(NameValueDictionary *dictionary, Operation op) const
{ {
switch (op) { switch (op) {
case SetEnabled: case SetEnabled:
@@ -173,26 +173,26 @@ void NameValueItem::apply(NameValueDictionary *dictionary, Operation op) const
} }
} }
QDebug operator<<(QDebug debug, const NameValueItem &i) QDebug operator<<(QDebug debug, const EnvironmentItem &i)
{ {
QDebugStateSaver saver(debug); QDebugStateSaver saver(debug);
debug.noquote(); debug.noquote();
debug.nospace(); debug.nospace();
debug << "KeyValueItem("; debug << "KeyValueItem(";
switch (i.operation) { switch (i.operation) {
case NameValueItem::SetEnabled: case EnvironmentItem::SetEnabled:
debug << "set \"" << i.name << "\" to \"" << i.value << '"'; debug << "set \"" << i.name << "\" to \"" << i.value << '"';
break; break;
case NameValueItem::SetDisabled: case EnvironmentItem::SetDisabled:
debug << "set \"" << i.name << "\" to \"" << i.value << '"' << "[disabled]"; debug << "set \"" << i.name << "\" to \"" << i.value << '"' << "[disabled]";
break; break;
case NameValueItem::Unset: case EnvironmentItem::Unset:
debug << "unset \"" << i.name << '"'; debug << "unset \"" << i.name << '"';
break; break;
case NameValueItem::Prepend: case EnvironmentItem::Prepend:
debug << "prepend to \"" << i.name << "\":\"" << i.value << '"'; debug << "prepend to \"" << i.name << "\":\"" << i.value << '"';
break; break;
case NameValueItem::Append: case EnvironmentItem::Append:
debug << "append to \"" << i.name << "\":\"" << i.value << '"'; debug << "append to \"" << i.name << "\":\"" << i.value << '"';
break; break;
} }

View File

@@ -13,12 +13,12 @@
namespace Utils { namespace Utils {
class QTCREATOR_UTILS_EXPORT NameValueItem class QTCREATOR_UTILS_EXPORT EnvironmentItem
{ {
public: public:
enum Operation : char { SetEnabled, Unset, Prepend, Append, SetDisabled }; enum Operation : char { SetEnabled, Unset, Prepend, Append, SetDisabled };
NameValueItem() = default; EnvironmentItem() = default;
NameValueItem(const QString &key, const QString &value, Operation operation = SetEnabled) EnvironmentItem(const QString &key, const QString &value, Operation operation = SetEnabled)
: name(key) : name(key)
, value(value) , value(value)
, operation(operation) , operation(operation)
@@ -26,25 +26,25 @@ public:
void apply(NameValueDictionary *dictionary) const { apply(dictionary, operation); } void apply(NameValueDictionary *dictionary) const { apply(dictionary, operation); }
static void sort(NameValueItems *list); static void sort(EnvironmentItems *list);
static NameValueItems fromStringList(const QStringList &list); static EnvironmentItems fromStringList(const QStringList &list);
static QStringList toStringList(const NameValueItems &list); static QStringList toStringList(const EnvironmentItems &list);
static NameValueItems itemsFromVariantList(const QVariantList &list); static EnvironmentItems itemsFromVariantList(const QVariantList &list);
static QVariantList toVariantList(const NameValueItems &list); static QVariantList toVariantList(const EnvironmentItems &list);
static NameValueItem itemFromVariantList(const QVariantList &list); static EnvironmentItem itemFromVariantList(const QVariantList &list);
static QVariantList toVariantList(const NameValueItem &item); static QVariantList toVariantList(const EnvironmentItem &item);
friend bool operator==(const NameValueItem &first, const NameValueItem &second) friend bool operator==(const EnvironmentItem &first, const EnvironmentItem &second)
{ {
return first.operation == second.operation && first.name == second.name return first.operation == second.operation && first.name == second.name
&& first.value == second.value; && first.value == second.value;
} }
friend bool operator!=(const NameValueItem &first, const NameValueItem &second) friend bool operator!=(const EnvironmentItem &first, const EnvironmentItem &second)
{ {
return !(first == second); return !(first == second);
} }
friend QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug debug, const NameValueItem &i); friend QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug debug, const EnvironmentItem &i);
public: public:
QString name; QString name;

View File

@@ -70,7 +70,7 @@ NameValueItemsWidget::NameValueItemsWidget(QWidget *parent)
layout->addWidget(new QLabel(helpText, this)); layout->addWidget(new QLabel(helpText, this));
const auto checkForItemChange = [this] { const auto checkForItemChange = [this] {
const NameValueItems newItems = environmentItems(); const EnvironmentItems newItems = environmentItems();
if (newItems != m_originalItems) { if (newItems != m_originalItems) {
m_originalItems = newItems; m_originalItems = newItems;
emit userChangedItems(newItems); emit userChangedItems(newItems);
@@ -190,8 +190,8 @@ void NameValuesDialog::setPlaceholderText(const QString &text)
m_editor->setPlaceholderText(text); m_editor->setPlaceholderText(text);
} }
std::optional<NameValueItems> NameValuesDialog::getNameValueItems(QWidget *parent, std::optional<EnvironmentItems> NameValuesDialog::getNameValueItems(QWidget *parent,
const NameValueItems &initial, const EnvironmentItems &initial,
const QString &placeholderText, const QString &placeholderText,
Polisher polisher, Polisher polisher,
const QString &windowTitle) const QString &windowTitle)

View File

@@ -34,20 +34,20 @@ signals:
private: private:
Internal::TextEditHelper *m_editor; Internal::TextEditHelper *m_editor;
NameValueItems m_originalItems; EnvironmentItems m_originalItems;
}; };
class QTCREATOR_UTILS_EXPORT NameValuesDialog : public QDialog class QTCREATOR_UTILS_EXPORT NameValuesDialog : public QDialog
{ {
public: public:
void setNameValueItems(const NameValueItems &items); void setNameValueItems(const EnvironmentItems &items);
NameValueItems nameValueItems() const; EnvironmentItems nameValueItems() const;
void setPlaceholderText(const QString &text); void setPlaceholderText(const QString &text);
using Polisher = std::function<void(QWidget *)>; using Polisher = std::function<void(QWidget *)>;
static std::optional<NameValueItems> getNameValueItems(QWidget *parent = nullptr, static std::optional<EnvironmentItems> getNameValueItems(QWidget *parent = nullptr,
const NameValueItems &initial = {}, const EnvironmentItems &initial = {},
const QString &placeholderText = {}, const QString &placeholderText = {},
Polisher polish = {}, Polisher polish = {},
const QString &windowTitle = {}); const QString &windowTitle = {});

View File

@@ -4093,7 +4093,7 @@ void GdbEngine::setEnvironmentVariables()
Environment baseEnv = runParameters().debugger.environment; Environment baseEnv = runParameters().debugger.environment;
Environment runEnv = runParameters().inferior.environment; Environment runEnv = runParameters().inferior.environment;
const NameValueItems items = baseEnv.diff(runEnv); const EnvironmentItems items = baseEnv.diff(runEnv);
for (const EnvironmentItem &item : items) { for (const EnvironmentItem &item : items) {
// imitate the weird windows gdb behavior of setting the case of the path environment // imitate the weird windows gdb behavior of setting the case of the path environment
// variable name to an all uppercase PATH // variable name to an all uppercase PATH

View File

@@ -32,19 +32,19 @@ Utils::Id McuDependenciesKitAspect::id()
return "PE.Profile.McuCMakeDependencies"; return "PE.Profile.McuCMakeDependencies";
} }
Utils::NameValueItems McuDependenciesKitAspect::dependencies(const Kit *kit) Utils::EnvironmentItems McuDependenciesKitAspect::dependencies(const Kit *kit)
{ {
if (kit) if (kit)
return Utils::NameValueItem::fromStringList( return Utils::EnvironmentItem::fromStringList(
kit->value(McuDependenciesKitAspect::id()).toStringList()); kit->value(McuDependenciesKitAspect::id()).toStringList());
return Utils::NameValueItems(); return Utils::EnvironmentItems();
} }
void McuDependenciesKitAspect::setDependencies(Kit *k, const Utils::NameValueItems &dependencies) void McuDependenciesKitAspect::setDependencies(Kit *k, const Utils::EnvironmentItems &dependencies)
{ {
if (k) if (k)
k->setValue(McuDependenciesKitAspect::id(), k->setValue(McuDependenciesKitAspect::id(),
Utils::NameValueItem::toStringList(dependencies)); Utils::EnvironmentItem::toStringList(dependencies));
} }
Utils::NameValuePairs McuDependenciesKitAspect::configuration(const Kit *kit) Utils::NameValuePairs McuDependenciesKitAspect::configuration(const Kit *kit)
@@ -108,7 +108,7 @@ public:
if (!variant.isNull() && !variant.canConvert(QVariant::List)) { if (!variant.isNull() && !variant.canConvert(QVariant::List)) {
qWarning("Kit \"%s\" has a wrong mcu dependencies value set.", qWarning("Kit \"%s\" has a wrong mcu dependencies value set.",
qPrintable(kit->displayName())); qPrintable(kit->displayName()));
McuDependenciesKitAspect::setDependencies(kit, Utils::NameValueItems()); McuDependenciesKitAspect::setDependencies(kit, Utils::EnvironmentItems());
} }
} }

View File

@@ -11,8 +11,8 @@ class McuDependenciesKitAspect final
{ {
public: public:
static Utils::Id id(); static Utils::Id id();
static Utils::NameValueItems dependencies(const ProjectExplorer::Kit *kit); static Utils::EnvironmentItems dependencies(const ProjectExplorer::Kit *kit);
static void setDependencies(ProjectExplorer::Kit *kit, const Utils::NameValueItems &dependencies); static void setDependencies(ProjectExplorer::Kit *kit, const Utils::EnvironmentItems &dependencies);
static Utils::NameValuePairs configuration(const ProjectExplorer::Kit *kit); static Utils::NameValuePairs configuration(const ProjectExplorer::Kit *kit);
}; };

View File

@@ -180,7 +180,7 @@ public:
const McuTarget *mcuTarget, const McuTarget *mcuTarget,
const McuPackagePtr &qtForMCUsSdkPackage) const McuPackagePtr &qtForMCUsSdkPackage)
{ {
NameValueItems dependencies; EnvironmentItems dependencies;
auto processPackage = [&dependencies](const McuPackagePtr &package) { auto processPackage = [&dependencies](const McuPackagePtr &package) {
const auto cmakeVariableName = package->cmakeVariableName(); const auto cmakeVariableName = package->cmakeVariableName();

View File

@@ -481,25 +481,25 @@ void EnvironmentWidget::unsetEnvironmentButtonClicked()
d->m_editor.setEnvironmentItems(d->m_model->userChanges()); d->m_editor.setEnvironmentItems(d->m_model->userChanges());
} }
void EnvironmentWidget::amendPathList(Utils::NameValueItem::Operation op) void EnvironmentWidget::amendPathList(Utils::EnvironmentItem::Operation op)
{ {
const QString varName = d->m_model->indexToVariable(d->m_environmentView->currentIndex()); const QString varName = d->m_model->indexToVariable(d->m_environmentView->currentIndex());
const FilePath dir = FileUtils::getExistingDirectory(this, Tr::tr("Choose Directory")); const FilePath dir = FileUtils::getExistingDirectory(this, Tr::tr("Choose Directory"));
if (dir.isEmpty()) if (dir.isEmpty())
return; return;
Utils::NameValueItems changes = d->m_model->userChanges(); Utils::EnvironmentItems changes = d->m_model->userChanges();
changes.append({varName, dir.toUserOutput(), op}); changes.append({varName, dir.toUserOutput(), op});
setUserChanges(changes); setUserChanges(changes);
} }
void EnvironmentWidget::appendPathButtonClicked() void EnvironmentWidget::appendPathButtonClicked()
{ {
amendPathList(Utils::NameValueItem::Append); amendPathList(Utils::EnvironmentItem::Append);
} }
void EnvironmentWidget::prependPathButtonClicked() void EnvironmentWidget::prependPathButtonClicked()
{ {
amendPathList(Utils::NameValueItem::Prepend); amendPathList(Utils::EnvironmentItem::Prepend);
} }
void EnvironmentWidget::environmentCurrentIndexChanged(const QModelIndex &current) void EnvironmentWidget::environmentCurrentIndexChanged(const QModelIndex &current)

View File

@@ -57,7 +57,7 @@ private:
void linkActivated(const QString &link); void linkActivated(const QString &link);
using PathListModifier = std::function<QString(const QString &oldList, const QString &newDir)>; using PathListModifier = std::function<QString(const QString &oldList, const QString &newDir)>;
void amendPathList(Utils::NameValueItem::Operation op); void amendPathList(Utils::EnvironmentItem::Operation op);
class Private; class Private;
const std::unique_ptr<Private> d; const std::unique_ptr<Private> d;

View File

@@ -1063,13 +1063,13 @@ void Project::setNamedSettings(const Key &name, const QVariant &value)
void Project::setAdditionalEnvironment(const EnvironmentItems &envItems) void Project::setAdditionalEnvironment(const EnvironmentItems &envItems)
{ {
setNamedSettings(PROJECT_ENV_KEY, NameValueItem::toStringList(envItems)); setNamedSettings(PROJECT_ENV_KEY, EnvironmentItem::toStringList(envItems));
emit environmentChanged(); emit environmentChanged();
} }
EnvironmentItems Project::additionalEnvironment() const EnvironmentItems Project::additionalEnvironment() const
{ {
return NameValueItem::fromStringList(namedSettings(PROJECT_ENV_KEY).toStringList()); return EnvironmentItem::fromStringList(namedSettings(PROJECT_ENV_KEY).toStringList());
} }
bool Project::needsConfiguration() const bool Project::needsConfiguration() const

View File

@@ -286,7 +286,7 @@ const QString projectEnvironmentVariable(const QString &key)
if (auto buildSystem = getBuildSystem()) { if (auto buildSystem = getBuildSystem()) {
auto envItems = buildSystem->environment(); auto envItems = buildSystem->environment();
auto confEnv = std::find_if(envItems.begin(), envItems.end(), [key](NameValueItem &item) { auto confEnv = std::find_if(envItems.begin(), envItems.end(), [key](EnvironmentItem &item) {
return item.name == key; return item.name == key;
}); });
if (confEnv != envItems.end()) if (confEnv != envItems.end())

View File

@@ -262,11 +262,11 @@ void tst_Environment::expansion()
void tst_Environment::incrementalChanges() void tst_Environment::incrementalChanges()
{ {
const Environment origEnv({{"VAR1", "VALUE1"}, {"VAR2", "VALUE2"}, {"PATH", "/usr/bin"}}); const Environment origEnv({{"VAR1", "VALUE1"}, {"VAR2", "VALUE2"}, {"PATH", "/usr/bin"}});
const NameValueItems changes({ const EnvironmentItems changes({
{"VAR1", QString(), NameValueItem::Unset}, {"VAR1", QString(), EnvironmentItem::Unset},
{"VAR2", "VALUE2", NameValueItem::SetDisabled}, {"VAR2", "VALUE2", EnvironmentItem::SetDisabled},
{"PATH", "/usr/local/bin", NameValueItem::Append}, {"PATH", "/usr/local/bin", EnvironmentItem::Append},
{"PATH", "/tmp", NameValueItem::Prepend}}); {"PATH", "/tmp", EnvironmentItem::Prepend}});
// Check values after change application. // Check values after change application.
Environment newEnv = origEnv; Environment newEnv = origEnv;
@@ -281,8 +281,8 @@ void tst_Environment::incrementalChanges()
QString("/tmp").append(sep).append("/usr/bin").append(sep).append("/usr/local/bin")); QString("/tmp").append(sep).append("/usr/bin").append(sep).append("/usr/local/bin"));
// Check apply/diff round-trips. // Check apply/diff round-trips.
const NameValueItems diff = origEnv.diff(newEnv); const EnvironmentItems diff = origEnv.diff(newEnv);
const NameValueItems reverseDiff = newEnv.diff(origEnv); const EnvironmentItems reverseDiff = newEnv.diff(origEnv);
Environment newEnv2 = origEnv; Environment newEnv2 = origEnv;
newEnv2.modify(diff); newEnv2.modify(diff);
QCOMPARE(newEnv, newEnv2); QCOMPARE(newEnv, newEnv2);
@@ -290,12 +290,12 @@ void tst_Environment::incrementalChanges()
QCOMPARE(newEnv2, origEnv); QCOMPARE(newEnv2, origEnv);
// Check conversion round-trips. // Check conversion round-trips.
QCOMPARE(NameValueItem::fromStringList(NameValueItem::toStringList(changes)), changes); QCOMPARE(EnvironmentItem::fromStringList(EnvironmentItem::toStringList(changes)), changes);
QCOMPARE(NameValueItem::fromStringList(NameValueItem::toStringList(diff)), diff); QCOMPARE(EnvironmentItem::fromStringList(EnvironmentItem::toStringList(diff)), diff);
QCOMPARE(NameValueItem::fromStringList(NameValueItem::toStringList(reverseDiff)), reverseDiff); QCOMPARE(EnvironmentItem::fromStringList(EnvironmentItem::toStringList(reverseDiff)), reverseDiff);
QCOMPARE(NameValueItem::itemsFromVariantList(NameValueItem::toVariantList(changes)), changes); QCOMPARE(EnvironmentItem::itemsFromVariantList(EnvironmentItem::toVariantList(changes)), changes);
QCOMPARE(NameValueItem::itemsFromVariantList(NameValueItem::toVariantList(diff)), diff); QCOMPARE(EnvironmentItem::itemsFromVariantList(EnvironmentItem::toVariantList(diff)), diff);
QCOMPARE(NameValueItem::itemsFromVariantList(NameValueItem::toVariantList(reverseDiff)), QCOMPARE(EnvironmentItem::itemsFromVariantList(EnvironmentItem::toVariantList(reverseDiff)),
reverseDiff); reverseDiff);
} }