feather/src/calcwidget.cpp

100 lines
3.4 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: BSD-3-Clause
2020-12-26 19:56:06 +00:00
// Copyright (c) 2020-2021, The Monero Project.
#include <QList>
#include "calcwidget.h"
#include "ui_calcwidget.h"
#include "utils/ColorScheme.h"
2021-05-02 18:22:38 +00:00
#include "utils/AppData.h"
#include "utils/config.h"
2021-05-18 15:59:18 +00:00
CalcWidget::CalcWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::CalcWidget)
{
ui->setupUi(this);
ui->imageExchange->setBackgroundRole(QPalette::Base);
ui->imageExchange->setAssets(":/assets/images/exchange.png", ":/assets/images/exchange_white.png");
ui->imageExchange->setScaledContents(true);
ui->imageExchange->setFixedSize(26, 26);
// validator/locale for input
QLocale lo(QLocale::C);
lo.setNumberOptions(QLocale::RejectGroupSeparator);
auto dv = new QDoubleValidator(0.0, 2147483647, 10, this); // [0, 32bit max], 10 decimals of precision
dv->setNotation(QDoubleValidator::StandardNotation);
dv->setLocale(lo);
ui->lineFrom->setValidator(dv);
ui->lineTo->setValidator(dv);
2021-05-20 14:04:59 +00:00
connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &CalcWidget::initComboBox);
connect(&appData()->prices, &Prices::cryptoPricesUpdated, this, &CalcWidget::initComboBox);
connect(ui->lineFrom, &QLineEdit::textEdited, this, [this]{this->convert(false);});
connect(ui->lineTo, &QLineEdit::textEdited, this, [this]{this->convert(true);});
connect(ui->comboCalcFrom, QOverload<int>::of(&QComboBox::currentIndexChanged), [this]{this->convert(false);});
connect(ui->comboCalcTo, QOverload<int>::of(&QComboBox::currentIndexChanged), [this]{this->convert(true);});
}
2021-05-20 14:04:59 +00:00
void CalcWidget::convert(bool reverse) {
if (!m_comboBoxInit)
return;
2021-05-20 14:04:59 +00:00
auto lineFrom = reverse ? ui->lineTo : ui->lineFrom;
auto lineTo = reverse ? ui->lineFrom : ui->lineTo;
2021-05-20 14:04:59 +00:00
auto comboFrom = reverse ? ui->comboCalcTo : ui->comboCalcFrom;
auto comboTo = reverse ? ui->comboCalcFrom : ui->comboCalcTo;
2021-05-20 14:04:59 +00:00
QString symbolFrom = comboFrom->itemText(comboFrom->currentIndex());
QString symbolTo = comboTo->itemText(comboTo->currentIndex());
if (symbolFrom == symbolTo) {
lineTo->setText(lineFrom->text());
}
2021-05-20 14:04:59 +00:00
QString amountStr = lineFrom->text();
double amount = amountStr.toDouble();
2021-05-02 18:22:38 +00:00
double result = appData()->prices.convert(symbolFrom, symbolTo, amount);
int precision = 10;
2021-05-02 18:22:38 +00:00
if (appData()->prices.rates.contains(symbolTo))
precision = 2;
2021-05-20 14:04:59 +00:00
lineTo->setText(QString::number(result, 'f', precision));
}
2021-05-20 14:04:59 +00:00
void CalcWidget::initComboBox() {
if (m_comboBoxInit)
return;
2021-05-02 18:22:38 +00:00
QList<QString> marketsKeys = appData()->prices.markets.keys();
QList<QString> ratesKeys = appData()->prices.rates.keys();
if(marketsKeys.count() <= 0 || ratesKeys.count() <= 0) return;
ui->comboCalcFrom->addItems(marketsKeys);
ui->comboCalcFrom->insertSeparator(marketsKeys.count());
ui->comboCalcFrom->addItems(ratesKeys);
ui->comboCalcFrom->setCurrentIndex(marketsKeys.indexOf("XMR"));
ui->comboCalcTo->addItems(marketsKeys);
ui->comboCalcTo->insertSeparator(marketsKeys.count());
ui->comboCalcTo->addItems(ratesKeys);
auto preferredFiat = config()->get(Config::preferredFiatCurrency).toString();
ui->comboCalcTo->setCurrentText(preferredFiat);
this->m_comboBoxInit = true;
}
void CalcWidget::skinChanged() {
ui->imageExchange->setMode(ColorScheme::hasDarkBackground(this));
}
CalcWidget::~CalcWidget() {
delete ui;
}