feather/src/CalcWidget.cpp

171 lines
5.6 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: BSD-3-Clause
2023-01-02 19:30:11 +00:00
// SPDX-FileCopyrightText: 2020-2023 The Monero Project
2021-06-28 17:48:23 +00:00
#include "CalcWidget.h"
#include "ui_CalcWidget.h"
#include <QList>
2021-06-28 17:48:23 +00:00
#include "dialog/CalcConfigDialog.h"
2021-05-02 18:22:38 +00:00
#include "utils/AppData.h"
2021-06-28 17:48:23 +00:00
#include "utils/ColorScheme.h"
2021-05-02 18:22:38 +00:00
#include "utils/config.h"
2022-02-23 22:27:20 +00:00
#include "utils/WebsocketClient.h"
#include "utils/WebsocketNotifier.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
QString amount_rx = R"(^\d{0,8}[\.]\d{0,12}$)";
2022-03-04 10:05:20 +00:00
QRegularExpression rx;
rx.setPattern(amount_rx);
2022-03-04 10:05:20 +00:00
QValidator *validator = new QRegularExpressionValidator(rx, this);
ui->lineFrom->setValidator(validator);
ui->lineTo->setValidator(validator);
2021-05-26 13:14:53 +00:00
connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &CalcWidget::onPricesReceived);
connect(&appData()->prices, &Prices::cryptoPricesUpdated, this, &CalcWidget::onPricesReceived);
2021-05-20 14:04:59 +00:00
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);});
2021-05-26 13:14:53 +00:00
connect(ui->comboCalcTo, QOverload<int>::of(&QComboBox::currentIndexChanged), [this]{this->convert(false);});
connect(ui->btn_configure, &QPushButton::clicked, this, &CalcWidget::showCalcConfigureDialog);
QTimer::singleShot(1, [this]{
this->skinChanged();
});
2022-02-23 22:27:20 +00:00
m_statusTimer.start(5000);
connect(&m_statusTimer, &QTimer::timeout, this, &CalcWidget::updateStatus);
QPixmap warningIcon = QPixmap(":/assets/images/warning.png");
ui->icon_warning->setPixmap(warningIcon.scaledToWidth(32, Qt::SmoothTransformation));
this->updateStatus();
}
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-26 13:14:53 +00:00
void CalcWidget::onPricesReceived() {
2021-05-20 14:04:59 +00:00
if (m_comboBoxInit)
return;
2021-05-26 13:14:53 +00:00
QList<QString> cryptoKeys = appData()->prices.markets.keys();
QList<QString> fiatKeys = appData()->prices.rates.keys();
if (cryptoKeys.empty() || fiatKeys.empty())
return;
2021-05-26 13:14:53 +00:00
ui->btn_configure->setEnabled(true);
this->initComboBox();
m_comboBoxInit = true;
2022-02-23 22:27:20 +00:00
this->updateStatus();
2021-05-26 13:14:53 +00:00
}
2021-05-26 13:14:53 +00:00
void CalcWidget::initComboBox() {
QList<QString> cryptoKeys = appData()->prices.markets.keys();
QList<QString> fiatKeys = appData()->prices.rates.keys();
QStringList enabledCrypto = config()->get(Config::cryptoSymbols).toStringList();
QStringList filteredCryptoKeys;
for (const auto& symbol : cryptoKeys) {
if (enabledCrypto.contains(symbol)) {
filteredCryptoKeys.append(symbol);
}
}
2021-05-26 13:14:53 +00:00
QStringList enabledFiat = config()->get(Config::fiatSymbols).toStringList();
auto preferredFiat = config()->get(Config::preferredFiatCurrency).toString();
2021-05-26 13:14:53 +00:00
if (!enabledFiat.contains(preferredFiat) && fiatKeys.contains(preferredFiat)) {
enabledFiat.append(preferredFiat);
config()->set(Config::fiatSymbols, enabledFiat);
}
QStringList filteredFiatKeys;
for (const auto &symbol : fiatKeys) {
if (enabledFiat.contains(symbol)) {
filteredFiatKeys.append(symbol);
}
}
2021-05-26 13:14:53 +00:00
this->setupComboBox(ui->comboCalcFrom, filteredCryptoKeys, filteredFiatKeys);
this->setupComboBox(ui->comboCalcTo, filteredCryptoKeys, filteredFiatKeys);
ui->comboCalcFrom->setCurrentIndex(ui->comboCalcFrom->findText("XMR"));
if (!preferredFiat.isEmpty()) {
ui->comboCalcTo->setCurrentIndex(ui->comboCalcTo->findText(preferredFiat));
} else {
ui->comboCalcTo->setCurrentIndex(ui->comboCalcTo->findText("USD"));
}
}
void CalcWidget::skinChanged() {
ui->imageExchange->setMode(ColorScheme::hasDarkBackground(this));
}
2021-05-26 13:14:53 +00:00
void CalcWidget::showCalcConfigureDialog() {
CalcConfigDialog dialog{this};
if (dialog.exec() == QDialog::Accepted) {
this->initComboBox();
}
}
void CalcWidget::setupComboBox(QComboBox *comboBox, const QStringList &crypto, const QStringList &fiat) {
comboBox->clear();
comboBox->addItems(crypto);
comboBox->insertSeparator(comboBox->count());
comboBox->addItems(fiat);
}
2022-02-23 22:27:20 +00:00
void CalcWidget::updateStatus() {
if (!m_comboBoxInit) {
ui->label_warning->setText("Waiting on exchange data.");
ui->frame_warning->show();
}
else if (websocketNotifier()->stale(10)) {
ui->label_warning->setText("No new exchange rates received for over 10 minutes.");
ui->frame_warning->show();
}
else {
ui->frame_warning->hide();
}
}
2021-06-27 12:13:05 +00:00
CalcWidget::~CalcWidget() = default;