From 3e669cd31de2b47ade6a75c46cc58ccd7859ed2e Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Mon, 7 Jan 2019 12:25:39 +0000 Subject: [PATCH] GUI: BitcoinAmountField: Allow code to set assets not in allowed_assets --- src/qt/bitcoinamountfield.cpp | 54 +++++++++++++++++++++++++++++------ src/qt/bitcoinamountfield.h | 4 +++ 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/qt/bitcoinamountfield.cpp b/src/qt/bitcoinamountfield.cpp index 36797eefd1..80d25d6b01 100644 --- a/src/qt/bitcoinamountfield.cpp +++ b/src/qt/bitcoinamountfield.cpp @@ -266,14 +266,7 @@ BitcoinAmountField::BitcoinAmountField(std::set allowed_assets, QWidget unit = new QComboBox(this); m_allowed_assets = allowed_assets; for (const auto& asset : allowed_assets) { - if (asset == Params().GetConsensus().pegged_asset) { - // Special handling - for (const auto& pegged_unit : BitcoinUnits::availableUnits()) { - unit->addItem(BitcoinUnits::shortName(pegged_unit), int(pegged_unit)); - } - continue; - } - unit->addItem(QString::fromStdString(gAssetsDir.GetIdentifier(asset)), QVariant::fromValue(asset)); + addAssetChoice(asset); } layout->addWidget(unit); layout->addStretch(1); @@ -371,8 +364,42 @@ void BitcoinAmountField::setReadOnly(bool fReadOnly) amount->setReadOnly(fReadOnly); } +bool BitcoinAmountField::hasAssetChoice(const CAsset& asset) const +{ + if (asset == Params().GetConsensus().pegged_asset) { + return -1 != unit->findData(0, Qt::UserRole); + } + return -1 != unit->findData(QVariant::fromValue(asset), Qt::UserRole); +} + +void BitcoinAmountField::addAssetChoice(const CAsset& asset) +{ + if (asset == Params().GetConsensus().pegged_asset) { + // Special handling + for (const auto& pegged_unit : BitcoinUnits::availableUnits()) { + unit->addItem(BitcoinUnits::shortName(pegged_unit), int(pegged_unit)); + } + return; + } + unit->addItem(QString::fromStdString(gAssetsDir.GetIdentifier(asset)), QVariant::fromValue(asset)); +} + +void BitcoinAmountField::removeAssetChoice(const CAsset& asset) +{ + if (asset == Params().GetConsensus().pegged_asset) { + // Special handling + for (const auto& pegged_unit : BitcoinUnits::availableUnits()) { + unit->removeItem(unit->findData(int(pegged_unit), Qt::UserRole)); + } + return; + } + unit->removeItem(unit->findData(QVariant::fromValue(asset), Qt::UserRole)); +} + void BitcoinAmountField::unitChanged(int idx) { + const CAsset previous_asset = amount->value().first; + // Use description tooltip for current unit for the combobox const QVariant& userdata = unit->itemData(idx, Qt::UserRole); if (userdata.type() == QVariant::UserType) { @@ -388,6 +415,10 @@ void BitcoinAmountField::unitChanged(int idx) amount->setDisplayUnit(newUnit); } + + if (!(m_allowed_assets.count(previous_asset) || amount->value().first == previous_asset)) { + removeAssetChoice(previous_asset); + } } void BitcoinAmountField::setDisplayUnit(const CAsset& asset) @@ -396,12 +427,17 @@ void BitcoinAmountField::setDisplayUnit(const CAsset& asset) setDisplayUnit(amount->currentPeggedUnit()); return; } - // TODO: make sure it's an item + if (!hasAssetChoice(asset)) { + addAssetChoice(asset); + } unit->setCurrentIndex(unit->findData(QVariant::fromValue(asset), Qt::UserRole)); } void BitcoinAmountField::setDisplayUnit(int newUnit) { + if (!hasAssetChoice(Params().GetConsensus().pegged_asset)) { + addAssetChoice(Params().GetConsensus().pegged_asset); + } unit->setCurrentIndex(unit->findData(newUnit, Qt::UserRole)); } diff --git a/src/qt/bitcoinamountfield.h b/src/qt/bitcoinamountfield.h index 83ec6f3981..b6a8b6c90e 100644 --- a/src/qt/bitcoinamountfield.h +++ b/src/qt/bitcoinamountfield.h @@ -76,6 +76,10 @@ private: AmountSpinBox *amount; QComboBox *unit; + bool hasAssetChoice(const CAsset&) const; + void addAssetChoice(const CAsset&); + void removeAssetChoice(const CAsset&); + private Q_SLOTS: void unitChanged(int idx);