From dbec2d21e1a7f119785f6606c68a2f05f1f7da43 Mon Sep 17 00:00:00 2001 From: Tobias Hunger <tobias.hunger@theqtcompany.com> Date: Wed, 20 May 2015 10:10:13 +0200 Subject: [PATCH] JsonFieldPage: Add conditions to combobox entries Change-Id: I4982564fcb8160302206e12e64f2f2bba338e09f Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com> --- .../jsonwizard/jsonfieldpage.cpp | 55 +++++++++++++++---- .../jsonwizard/jsonfieldpage.h | 1 + 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp index b901071c22..8923419b0f 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp @@ -676,27 +676,38 @@ JsonFieldPage::ComboBoxField::ComboBoxField() : m_index(-1), m_disabledIndex(-1), m_savedIndex(-1) { } -QPair<QString, QString> parseComboBoxItem(const QVariant &item, QString *errorMessage) +struct ComboBoxItem { + ComboBoxItem(const QString &k = QString(), const QString &v = QString(), const QVariant &c = true) : + key(k), value(v), condition(c) + { } + + QString key; + QString value; + QVariant condition; +}; + +ComboBoxItem parseComboBoxItem(const QVariant &item, QString *errorMessage) { if (item.type() == QVariant::List) { *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage", "No lists allowed inside ComboBox items list."); - return qMakePair(QString(), QString()); + return ComboBoxItem(); } else if (item.type() == QVariant::Map) { QVariantMap tmp = item.toMap(); QString key = JsonWizardFactory::localizedString(tmp.value(QLatin1String("trKey"), QString()).toString()); QString value = tmp.value(QLatin1String("value"), QString()).toString(); + QVariant condition = tmp.value(QLatin1String("condition"), true); if (key.isNull() || key.isEmpty()) { *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage", "No \"key\" found in ComboBox items."); - return qMakePair(QString(), QString()); + return ComboBoxItem(); } if (value.isNull()) value = key; - return qMakePair(key, value); + return ComboBoxItem(key, value, condition); } else { QString keyvalue = item.toString(); - return qMakePair(keyvalue, keyvalue); + return ComboBoxItem(keyvalue, keyvalue); } } @@ -737,11 +748,22 @@ bool JsonFieldPage::ComboBoxField::parseData(const QVariant &data, QString *erro } foreach (const QVariant &i, value.toList()) { - QPair<QString, QString> keyValue = parseComboBoxItem(i, errorMessage); - if (keyValue.first.isNull()) + ComboBoxItem keyValue = parseComboBoxItem(i, errorMessage); + if (keyValue.key.isNull()) return false; // an error happened... - m_itemList.append(keyValue.first); - m_itemDataList.append(keyValue.second); + m_itemList.append(keyValue.key); + m_itemDataList.append(keyValue.value); + m_itemConditionList.append(keyValue.condition); + } + + if (m_itemConditionList.count() != m_itemDataList.count() + || m_itemConditionList.count() != m_itemList.count()) { + m_itemConditionList.clear(); + m_itemDataList.clear(); + m_itemList.clear(); + *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage", + "Internal Error: ComboBox items lists got confused."); + return false; } return true; @@ -789,10 +811,23 @@ void JsonFieldPage::ComboBoxField::initializeData(MacroExpander *expander) QStringList tmpData = Utils::transform(m_itemDataList, [expander](const QString &i) { return expander->expand(i); }); + QList<bool> tmpConditions + = Utils::transform(m_itemConditionList, + [expander](const QVariant &v) { return JsonWizard::boolFromVariant(v, expander); }); + + int index = m_index; + for (int i = tmpConditions.count() - 1; i >= 0; --i) { + if (!tmpConditions.at(i)) { + tmpItems.removeAt(i); + tmpData.removeAt(i); + if (i <= index) + --index; + } + } w->setItems(tmpItems, tmpData); w->setInsertPolicy(QComboBox::NoInsert); - w->setCurrentIndex(m_index); + w->setCurrentIndex(index); } // -------------------------------------------------------------------- diff --git a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.h b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.h index 92090c62e0..6fc32ecdc1 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.h +++ b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.h @@ -235,6 +235,7 @@ public: QStringList m_itemList; QStringList m_itemDataList; + QVariantList m_itemConditionList; int m_index; int m_disabledIndex; -- GitLab