From ba2b0ac5504e5b7bec2243fb18c48b01e74e3290 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Tue, 25 May 2021 01:10:45 +0200 Subject: [PATCH] TickerWidget: rework --- src/mainwindow.cpp | 21 ++-- src/mainwindow.h | 6 +- src/widgets/TickerWidget.cpp | 105 ++++++++++++++++++ src/widgets/TickerWidget.h | 67 +++++++++++ .../{tickerwidget.ui => TickerWidget.ui} | 0 src/widgets/tickerwidget.cpp | 95 ---------------- src/widgets/tickerwidget.h | 38 ------- 7 files changed, 186 insertions(+), 146 deletions(-) create mode 100644 src/widgets/TickerWidget.cpp create mode 100644 src/widgets/TickerWidget.h rename src/widgets/{tickerwidget.ui => TickerWidget.ui} (100%) delete mode 100644 src/widgets/tickerwidget.cpp delete mode 100644 src/widgets/tickerwidget.h diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 56ab81e..4de7ad6 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -68,9 +68,9 @@ MainWindow::MainWindow(WindowManager *windowManager, Wallet *wallet, QWidget *pa websocketNotifier()->emitCache(); // Get cached data // Settings - for (auto tickerWidget: m_tickerWidgets) - connect(m_windowSettings, &Settings::preferredFiatCurrencyChanged, tickerWidget, &TickerWidget::init); - connect(m_windowSettings, &Settings::preferredFiatCurrencyChanged, m_balanceWidget, &TickerWidget::init); + for (const auto &widget: m_priceTickerWidgets) + connect(m_windowSettings, &Settings::preferredFiatCurrencyChanged, widget, &PriceTickerWidget::updateDisplay); + connect(m_windowSettings, &Settings::preferredFiatCurrencyChanged, m_balanceTickerWidget, &BalanceTickerWidget::updateDisplay); connect(m_windowSettings, &Settings::preferredFiatCurrencyChanged, m_ctx.get(), &AppContext::onPreferredFiatCurrencyChanged); connect(m_windowSettings, &Settings::preferredFiatCurrencyChanged, m_sendWidget, QOverload<>::of(&SendWidget::onPreferredFiatCurrencyChanged)); connect(m_windowSettings, &Settings::amountPrecisionChanged, m_ctx.get(), &AppContext::onAmountPrecisionChanged); @@ -332,13 +332,14 @@ void MainWindow::initMenu() { void MainWindow::initHome() { // Ticker widgets - m_tickerWidgets.append(new TickerWidget(this, m_ctx, "XMR")); - m_tickerWidgets.append(new TickerWidget(this, m_ctx, "BTC")); - for (auto tickerWidget: m_tickerWidgets) { - ui->tickerLayout->addWidget(tickerWidget); + m_priceTickerWidgets.append(new PriceTickerWidget(this, m_ctx, "XMR")); + m_priceTickerWidgets.append(new PriceTickerWidget(this, m_ctx, "BTC")); + for (const auto &widget : m_priceTickerWidgets) { + ui->tickerLayout->addWidget(widget); } - m_balanceWidget = new TickerWidget(this, m_ctx, "XMR", "Balance", true, true); - ui->fiatTickerLayout->addWidget(m_balanceWidget); + + m_balanceTickerWidget = new BalanceTickerWidget(this, m_ctx, false); + ui->fiatTickerLayout->addWidget(m_balanceTickerWidget); connect(ui->ccsWidget, &CCSWidget::selected, this, &MainWindow::showSendScreen); connect(ui->redditWidget, &RedditWidget::setStatusText, this, &MainWindow::setStatusText); @@ -495,7 +496,7 @@ void MainWindow::onBalanceUpdated(quint64 balance, quint64 spendable) { m_statusLabelBalance->setToolTip("Click for details"); m_statusLabelBalance->setText(balance_str); - m_balanceWidget->setHidden(hide); + m_balanceTickerWidget->setHidden(hide); } void MainWindow::onSetStatusText(const QString &text) { diff --git a/src/mainwindow.h b/src/mainwindow.h index 5379c48..3dfb3f6 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -33,7 +33,7 @@ #include "utils/config.h" #include "widgets/ccswidget.h" #include "widgets/redditwidget.h" -#include "widgets/tickerwidget.h" +#include "widgets/TickerWidget.h" #include "wizard/WalletWizard.h" #include "contactswidget.h" @@ -225,8 +225,8 @@ private: LocalMoneroWidget *m_localMoneroWidget = nullptr; #endif - QList m_tickerWidgets; - TickerWidget *m_balanceWidget; + QList m_priceTickerWidgets; + BalanceTickerWidget *m_balanceTickerWidget; // lower status bar QPushButton *m_statusUpdateAvailable; diff --git a/src/widgets/TickerWidget.cpp b/src/widgets/TickerWidget.cpp new file mode 100644 index 0000000..2e11d36 --- /dev/null +++ b/src/widgets/TickerWidget.cpp @@ -0,0 +1,105 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) 2020-2021, The Monero Project. + +#include "TickerWidget.h" +#include "ui_TickerWidget.h" + +#include "constants.h" +#include "utils/AppData.h" + +TickerWidgetBase::TickerWidgetBase(QWidget *parent, QSharedPointer ctx) + : QWidget(parent) + , ui(new Ui::TickerWidget) + , m_ctx(std::move(ctx)) +{ + ui->setupUi(this); + + ui->tickerPct->setFont(Utils::relativeFont(-2)); + ui->tickerFiat->setFont(Utils::relativeFont(0)); + + this->setPercentageText("0.0", true); + ui->tickerFiat->setText("..."); +} + +TickerWidgetBase::~TickerWidgetBase() { + delete ui; +} + +void TickerWidgetBase::setTitle(const QString &title) { + ui->tickerBox->setTitle(title); +} + +void TickerWidgetBase::setPercentageVisible(bool visible) { + ui->tickerPct->setVisible(visible); +} + +void TickerWidgetBase::setPercentageText(const QString &text, bool positive) { + QString pctText = "

"; + if(positive) { + pctText = pctText.replace("red", "green"); + pctText += QString("+%1%").arg(text); + } else + pctText += QString("%1%").arg(text); + pctText += "

"; + + ui->tickerPct->setText(pctText); +} + +void TickerWidgetBase::setFiatText(double amount, const QString &fiatCurrency) { + QString conversionText = Utils::amountToCurrencyString(amount, fiatCurrency); + ui->tickerFiat->setText(conversionText); +} + +// BalanceTickerWidget +BalanceTickerWidget::BalanceTickerWidget(QWidget *parent, QSharedPointer ctx, bool totalBalance) + : TickerWidgetBase(parent, std::move(ctx)) + , m_totalBalance(totalBalance) +{ + if (totalBalance) + this->setTitle("Total balance"); + else + this->setTitle("Balance"); + + this->setPercentageVisible(false); + + connect(m_ctx.get(), &AppContext::balanceUpdated, this, &BalanceTickerWidget::updateDisplay); + connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &BalanceTickerWidget::updateDisplay); + connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &BalanceTickerWidget::updateDisplay); +} + +void BalanceTickerWidget::updateDisplay() { + double balance = (m_totalBalance ? m_ctx->wallet->balanceAll() : m_ctx->wallet->balanceAll()) / constants::cdiv; + QString fiatCurrency = config()->get(Config::preferredFiatCurrency).toString(); + double balanceFiatAmount = appData()->prices.convert("XMR", fiatCurrency, balance); + if (balanceFiatAmount < 0) + return; + this->setFiatText(balanceFiatAmount, fiatCurrency); +} + +// PriceTickerWidget +PriceTickerWidget::PriceTickerWidget(QWidget *parent, QSharedPointer ctx, QString symbol) + : TickerWidgetBase(parent, std::move(ctx)) + , m_symbol(std::move(symbol)) +{ + this->setTitle(m_symbol); + + connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &PriceTickerWidget::updateDisplay); + connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &PriceTickerWidget::updateDisplay); +} + +void PriceTickerWidget::updateDisplay() { + QString fiatCurrency = config()->get(Config::preferredFiatCurrency).toString(); + double price = appData()->prices.convert(m_symbol, fiatCurrency, 1.0); + if (price < 0) + return; + + auto markets = appData()->prices.markets; + if (!markets.contains(m_symbol)) + return; + + double percentChange24h = markets[m_symbol].price_usd_change_pct_24h; + QString percentChange24hStr = QString::number(percentChange24h, 'f', 2); + this->setPercentageText(percentChange24hStr, percentChange24h >= 0.0); + + this->setFiatText(price, fiatCurrency); +} diff --git a/src/widgets/TickerWidget.h b/src/widgets/TickerWidget.h new file mode 100644 index 0000000..cd586bf --- /dev/null +++ b/src/widgets/TickerWidget.h @@ -0,0 +1,67 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) 2020-2021, The Monero Project. + +#ifndef FEATHER_TICKERWIDGET_H +#define FEATHER_TICKERWIDGET_H + +#include + +#include "appcontext.h" + +namespace Ui { + class TickerWidget; +} + +class TickerWidgetBase : public QWidget +{ + Q_OBJECT + +public: + explicit TickerWidgetBase(QWidget *parent, QSharedPointer ctx); + ~TickerWidgetBase() override; + + void setTitle(const QString &title); + void setPercentageVisible(bool visible); + + void setPercentageText(const QString &text, bool positive); + void setFiatText(double amount, const QString &fiatCurrency); + +public slots: + virtual void updateDisplay() = 0; + +private: + Ui::TickerWidget *ui; + +protected: + QSharedPointer m_ctx; +}; + +class BalanceTickerWidget : public TickerWidgetBase +{ + Q_OBJECT + +public: + explicit BalanceTickerWidget(QWidget *parent, QSharedPointer ctx, bool totalBalance); + +public slots: + void updateDisplay() override; + +private: + bool m_totalBalance; +}; + +class PriceTickerWidget : public TickerWidgetBase +{ + Q_OBJECT + +public: + explicit PriceTickerWidget(QWidget *parent, QSharedPointer ctx, QString symbol); + +public slots: + void updateDisplay() override; + +private: + QString m_symbol; +}; + +#endif // FEATHER_TICKERWIDGET_H diff --git a/src/widgets/tickerwidget.ui b/src/widgets/TickerWidget.ui similarity index 100% rename from src/widgets/tickerwidget.ui rename to src/widgets/TickerWidget.ui diff --git a/src/widgets/tickerwidget.cpp b/src/widgets/tickerwidget.cpp deleted file mode 100644 index 621835d..0000000 --- a/src/widgets/tickerwidget.cpp +++ /dev/null @@ -1,95 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// Copyright (c) 2020-2021, The Monero Project. - -#include "tickerwidget.h" -#include "ui_tickerwidget.h" - -#include "constants.h" -#include "utils/AppData.h" - -TickerWidget::TickerWidget(QWidget *parent, QSharedPointer ctx, QString symbol, QString title, bool convertBalance, bool hidePercent) - : QWidget(parent) - , ui(new Ui::TickerWidget) - , m_ctx(std::move(ctx)) - , m_symbol(std::move(symbol)) - , m_convertBalance(convertBalance) - , m_hidePercent(hidePercent) -{ - ui->setupUi(this); - - // default values before API data - if (title == "") title = m_symbol; - this->ui->tickerBox->setTitle(title); - QString defaultPct = "0.0"; - QString defaultFiat = "..."; - - this->setFontSizes(); - this->setPctText(defaultPct, true); - this->setFiatText(defaultFiat, 0.0); - - ui->tickerPct->setHidden(hidePercent); - - connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &TickerWidget::init); - connect(&appData()->prices, &Prices::cryptoPricesUpdated, this, &TickerWidget::init); - - if (convertBalance) - connect(m_ctx.get(), &AppContext::balanceUpdated, this, &TickerWidget::init); -} - -void TickerWidget::init() { - if(!appData()->prices.markets.count() || !appData()->prices.rates.count()) - return; - - QString fiatCurrency = config()->get(Config::preferredFiatCurrency).toString(); - - if(!appData()->prices.rates.contains(fiatCurrency)){ - config()->set(Config::preferredFiatCurrency, "USD"); - return; - } - - double walletBalance = m_ctx->wallet ? (m_ctx->wallet->balance() / constants::cdiv) : 0; - - double amount = m_convertBalance ? walletBalance : 1.0; - double conversion = appData()->prices.convert(m_symbol, fiatCurrency, amount); - if (conversion < 0) return; - - auto markets = appData()->prices.markets; - if(!markets.contains(m_symbol)) return; - - bool hidePercent = (conversion == 0 || m_hidePercent); - if (hidePercent) { - ui->tickerPct->hide(); - } else { - auto pct24h = markets[m_symbol].price_usd_change_pct_24h; - auto pct24hText = QString::number(pct24h, 'f', 2); - this->setPctText(pct24hText, pct24h >= 0.0); - } - - this->setFiatText(fiatCurrency, conversion); -} - -void TickerWidget::setFiatText(QString &fiatCurrency, double amount) { - QString conversionText = Utils::amountToCurrencyString(amount, fiatCurrency); - ui->tickerFiat->setText(conversionText); -} - -void TickerWidget::setPctText(QString &text, bool positive) { - QString pctText = "

"; - if(positive) { - pctText = pctText.replace("red", "green"); - pctText += QString("+%1%").arg(text); - } else - pctText += QString("%1%").arg(text); - - pctText += "

"; - ui->tickerPct->setText(pctText); -} - -void TickerWidget::setFontSizes() { - ui->tickerPct->setFont(Utils::relativeFont(-2)); - ui->tickerFiat->setFont(Utils::relativeFont(0)); -} - -TickerWidget::~TickerWidget() { - delete ui; -} diff --git a/src/widgets/tickerwidget.h b/src/widgets/tickerwidget.h deleted file mode 100644 index 6c50ec9..0000000 --- a/src/widgets/tickerwidget.h +++ /dev/null @@ -1,38 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// Copyright (c) 2020-2021, The Monero Project. - -#ifndef TICKERWIDGET_H -#define TICKERWIDGET_H - -#include - -#include "appcontext.h" - -namespace Ui { - class TickerWidget; -} - -class TickerWidget : public QWidget -{ - Q_OBJECT - -public: - explicit TickerWidget(QWidget *parent, QSharedPointer ctx, QString symbol, QString title = "", bool convertBalance = false, bool hidePercent = false); - void setFiatText(QString &fiatCurrency, double amount); - void setPctText(QString &text, bool positive); - void setFontSizes(); - ~TickerWidget() override; - - -public slots: - void init(); - -private: - Ui::TickerWidget *ui; - QSharedPointer m_ctx; - QString m_symbol; - bool m_convertBalance; - bool m_hidePercent; -}; - -#endif // TICKERWIDGET_H