2020-10-07 10:36:04 +00:00
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-12-26 19:56:06 +00:00
|
|
|
// Copyright (c) 2020-2021, The Monero Project.
|
2020-10-07 10:36:04 +00:00
|
|
|
|
|
|
|
#include <QList>
|
|
|
|
|
|
|
|
#include "calcwidget.h"
|
|
|
|
#include "ui_calcwidget.h"
|
2021-02-03 19:40:35 +00:00
|
|
|
#include "utils/ColorScheme.h"
|
2021-05-02 18:22:38 +00:00
|
|
|
#include "utils/AppData.h"
|
|
|
|
#include "utils/config.h"
|
2020-10-07 10:36:04 +00:00
|
|
|
|
2021-05-18 15:59:18 +00:00
|
|
|
CalcWidget::CalcWidget(QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
, ui(new Ui::CalcWidget)
|
2020-10-07 10:36:04 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
ui->imageExchange->setBackgroundRole(QPalette::Base);
|
2021-02-03 19:40:35 +00:00
|
|
|
ui->imageExchange->setAssets(":/assets/images/exchange.png", ":/assets/images/exchange_white.png");
|
2020-10-07 10:36:04 +00:00
|
|
|
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);});
|
2020-10-07 10:36:04 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:04:59 +00:00
|
|
|
void CalcWidget::convert(bool reverse) {
|
|
|
|
if (!m_comboBoxInit)
|
2020-10-07 10:36:04 +00:00
|
|
|
return;
|
|
|
|
|
2021-05-20 14:04:59 +00:00
|
|
|
auto lineFrom = reverse ? ui->lineTo : ui->lineFrom;
|
|
|
|
auto lineTo = reverse ? ui->lineFrom : ui->lineTo;
|
2020-10-07 10:36:04 +00:00
|
|
|
|
2021-05-20 14:04:59 +00:00
|
|
|
auto comboFrom = reverse ? ui->comboCalcTo : ui->comboCalcFrom;
|
|
|
|
auto comboTo = reverse ? ui->comboCalcFrom : ui->comboCalcTo;
|
2020-10-07 10:36:04 +00:00
|
|
|
|
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());
|
2020-10-07 10:36:04 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2020-10-07 10:36:04 +00:00
|
|
|
|
|
|
|
int precision = 10;
|
2021-05-02 18:22:38 +00:00
|
|
|
if (appData()->prices.rates.contains(symbolTo))
|
2020-10-07 10:36:04 +00:00
|
|
|
precision = 2;
|
|
|
|
|
2021-05-20 14:04:59 +00:00
|
|
|
lineTo->setText(QString::number(result, 'f', precision));
|
2020-10-07 10:36:04 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:04:59 +00:00
|
|
|
void CalcWidget::initComboBox() {
|
|
|
|
if (m_comboBoxInit)
|
2020-10-07 10:36:04 +00:00
|
|
|
return;
|
|
|
|
|
2021-05-02 18:22:38 +00:00
|
|
|
QList<QString> marketsKeys = appData()->prices.markets.keys();
|
|
|
|
QList<QString> ratesKeys = appData()->prices.rates.keys();
|
2020-10-07 10:36:04 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-03 19:40:35 +00:00
|
|
|
void CalcWidget::skinChanged() {
|
|
|
|
ui->imageExchange->setMode(ColorScheme::hasDarkBackground(this));
|
|
|
|
}
|
|
|
|
|
2020-10-07 10:36:04 +00:00
|
|
|
CalcWidget::~CalcWidget() {
|
|
|
|
delete ui;
|
|
|
|
}
|