mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-17 13:07:54 +02:00
GUI: BitcoinAmountField: Allow code to set assets not in allowed_assets
This commit is contained in:
parent
2c75c44417
commit
3e669cd31d
2 changed files with 49 additions and 9 deletions
|
|
@ -266,14 +266,7 @@ BitcoinAmountField::BitcoinAmountField(std::set<CAsset> allowed_assets, QWidget
|
||||||
unit = new QComboBox(this);
|
unit = new QComboBox(this);
|
||||||
m_allowed_assets = allowed_assets;
|
m_allowed_assets = allowed_assets;
|
||||||
for (const auto& asset : allowed_assets) {
|
for (const auto& asset : allowed_assets) {
|
||||||
if (asset == Params().GetConsensus().pegged_asset) {
|
addAssetChoice(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));
|
|
||||||
}
|
}
|
||||||
layout->addWidget(unit);
|
layout->addWidget(unit);
|
||||||
layout->addStretch(1);
|
layout->addStretch(1);
|
||||||
|
|
@ -371,8 +364,42 @@ void BitcoinAmountField::setReadOnly(bool fReadOnly)
|
||||||
amount->setReadOnly(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)
|
void BitcoinAmountField::unitChanged(int idx)
|
||||||
{
|
{
|
||||||
|
const CAsset previous_asset = amount->value().first;
|
||||||
|
|
||||||
// Use description tooltip for current unit for the combobox
|
// Use description tooltip for current unit for the combobox
|
||||||
const QVariant& userdata = unit->itemData(idx, Qt::UserRole);
|
const QVariant& userdata = unit->itemData(idx, Qt::UserRole);
|
||||||
if (userdata.type() == QVariant::UserType) {
|
if (userdata.type() == QVariant::UserType) {
|
||||||
|
|
@ -388,6 +415,10 @@ void BitcoinAmountField::unitChanged(int idx)
|
||||||
|
|
||||||
amount->setDisplayUnit(newUnit);
|
amount->setDisplayUnit(newUnit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!(m_allowed_assets.count(previous_asset) || amount->value().first == previous_asset)) {
|
||||||
|
removeAssetChoice(previous_asset);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitcoinAmountField::setDisplayUnit(const CAsset& asset)
|
void BitcoinAmountField::setDisplayUnit(const CAsset& asset)
|
||||||
|
|
@ -396,12 +427,17 @@ void BitcoinAmountField::setDisplayUnit(const CAsset& asset)
|
||||||
setDisplayUnit(amount->currentPeggedUnit());
|
setDisplayUnit(amount->currentPeggedUnit());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// TODO: make sure it's an item
|
if (!hasAssetChoice(asset)) {
|
||||||
|
addAssetChoice(asset);
|
||||||
|
}
|
||||||
unit->setCurrentIndex(unit->findData(QVariant::fromValue(asset), Qt::UserRole));
|
unit->setCurrentIndex(unit->findData(QVariant::fromValue(asset), Qt::UserRole));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitcoinAmountField::setDisplayUnit(int newUnit)
|
void BitcoinAmountField::setDisplayUnit(int newUnit)
|
||||||
{
|
{
|
||||||
|
if (!hasAssetChoice(Params().GetConsensus().pegged_asset)) {
|
||||||
|
addAssetChoice(Params().GetConsensus().pegged_asset);
|
||||||
|
}
|
||||||
unit->setCurrentIndex(unit->findData(newUnit, Qt::UserRole));
|
unit->setCurrentIndex(unit->findData(newUnit, Qt::UserRole));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,10 @@ private:
|
||||||
AmountSpinBox *amount;
|
AmountSpinBox *amount;
|
||||||
QComboBox *unit;
|
QComboBox *unit;
|
||||||
|
|
||||||
|
bool hasAssetChoice(const CAsset&) const;
|
||||||
|
void addAssetChoice(const CAsset&);
|
||||||
|
void removeAssetChoice(const CAsset&);
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void unitChanged(int idx);
|
void unitChanged(int idx);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue