From 026dae85919c5be477853494cc276d3002513f34 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Fri, 10 Jun 2022 13:42:55 +0200 Subject: [PATCH] Home: add XMR/BTC ticker --- src/MainWindow.cpp | 11 +++++---- src/MainWindow.h | 2 +- src/widgets/TickerWidget.cpp | 47 +++++++++++++++++++++++++++++++++--- src/widgets/TickerWidget.h | 16 ++++++++++++ 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index edbfd17..266fd39 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -363,9 +363,10 @@ void MainWindow::initMenu() { void MainWindow::initHome() { // Ticker widgets - m_priceTickerWidgets.append(new PriceTickerWidget(this, m_ctx, "XMR")); - m_priceTickerWidgets.append(new PriceTickerWidget(this, m_ctx, "BTC")); - for (const auto &widget : m_priceTickerWidgets) { + m_tickerWidgets.append(new PriceTickerWidget(this, m_ctx, "XMR")); + m_tickerWidgets.append(new PriceTickerWidget(this, m_ctx, "BTC")); + m_tickerWidgets.append(new RatioTickerWidget(this, m_ctx, "XMR", "BTC")); + for (const auto &widget : m_tickerWidgets) { ui->tickerLayout->addWidget(widget); } @@ -851,8 +852,8 @@ void MainWindow::menuAboutClicked() { void MainWindow::menuSettingsClicked() { Settings settings{m_ctx, this}; - for (const auto &widget: m_priceTickerWidgets) { - connect(&settings, &Settings::preferredFiatCurrencyChanged, widget, &PriceTickerWidget::updateDisplay); + for (const auto &widget: m_tickerWidgets) { + connect(&settings, &Settings::preferredFiatCurrencyChanged, widget, &TickerWidgetBase::updateDisplay); } connect(&settings, &Settings::preferredFiatCurrencyChanged, m_balanceTickerWidget, &BalanceTickerWidget::updateDisplay); connect(&settings, &Settings::preferredFiatCurrencyChanged, m_sendWidget, QOverload<>::of(&SendWidget::onPreferredFiatCurrencyChanged)); diff --git a/src/MainWindow.h b/src/MainWindow.h index 0b24c2c..f704b59 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -243,7 +243,7 @@ private: LocalMoneroWidget *m_localMoneroWidget = nullptr; #endif - QList m_priceTickerWidgets; + QList m_tickerWidgets; BalanceTickerWidget *m_balanceTickerWidget; QPointer m_clearRecentlyOpenAction; diff --git a/src/widgets/TickerWidget.cpp b/src/widgets/TickerWidget.cpp index 66e4c9e..63a2137 100644 --- a/src/widgets/TickerWidget.cpp +++ b/src/widgets/TickerWidget.cpp @@ -14,7 +14,7 @@ TickerWidgetBase::TickerWidgetBase(QWidget *parent, QSharedPointer c { ui->setupUi(this); - ui->tickerPct->setFont(Utils::relativeFont(-2)); + ui->tickerPct->setFont(Utils::relativeFont(-1)); ui->tickerFiat->setFont(Utils::relativeFont(0)); this->setPercentageText("0.0", true); @@ -48,6 +48,10 @@ void TickerWidgetBase::setFiatText(double amount, const QString &fiatCurrency) { ui->tickerFiat->setText(conversionText); } +void TickerWidgetBase::setDisplayText(const QString &text) { + ui->tickerFiat->setText(text); +} + // BalanceTickerWidget BalanceTickerWidget::BalanceTickerWidget(QWidget *parent, QSharedPointer ctx, bool totalBalance) : TickerWidgetBase(parent, std::move(ctx)) @@ -82,7 +86,7 @@ PriceTickerWidget::PriceTickerWidget(QWidget *parent, QSharedPointer this->setTitle(m_symbol); connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &PriceTickerWidget::updateDisplay); - connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &PriceTickerWidget::updateDisplay); + connect(&appData()->prices, &Prices::cryptoPricesUpdated, this, &PriceTickerWidget::updateDisplay); } void PriceTickerWidget::updateDisplay() { @@ -96,8 +100,45 @@ void PriceTickerWidget::updateDisplay() { return; double percentChange24h = markets[m_symbol].price_usd_change_pct_24h; - QString percentChange24hStr = QString::number(percentChange24h, 'f', 2); + QString percentChange24hStr = QString::number(percentChange24h, 'f', 1); this->setPercentageText(percentChange24hStr, percentChange24h >= 0.0); this->setFiatText(price, fiatCurrency); } + +//RatioTickerWidget +RatioTickerWidget::RatioTickerWidget(QWidget *parent, QSharedPointer ctx, QString symbol1, QString symbol2) + : TickerWidgetBase(parent, std::move(ctx)) + , m_symbol1(std::move(symbol1)) + , m_symbol2(std::move(symbol2)) +{ + this->setTitle(QString("%1/%2").arg(m_symbol1, m_symbol2)); + + connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &RatioTickerWidget::updateDisplay); + connect(&appData()->prices, &Prices::cryptoPricesUpdated, this, &RatioTickerWidget::updateDisplay); +} + +void RatioTickerWidget::updateDisplay() { + double ratio = appData()->prices.convert(m_symbol1, m_symbol2, 1); + if (ratio < 0) + return; + + // Approximation based on USD price + if (appData()->prices.markets.contains(m_symbol1) && appData()->prices.markets.contains(m_symbol2)) { + double price_symbol1 = appData()->prices.markets[m_symbol1].price_usd; + double price_symbol1_24h_ago = price_symbol1 - (price_symbol1 * (appData()->prices.markets[m_symbol1].price_usd_change_pct_24h / 100)); + + double price_symbol2 = appData()->prices.markets[m_symbol2].price_usd; + double price_symbol2_24h_ago = price_symbol2 - (price_symbol2 * (appData()->prices.markets[m_symbol2].price_usd_change_pct_24h / 100)); + if (price_symbol2_24h_ago == 0) return; + + double ratio_24h_ago = price_symbol1_24h_ago / price_symbol2_24h_ago; + if (ratio_24h_ago == 0) return; + + double percentage_change = ((ratio - ratio_24h_ago) / ratio_24h_ago) * 100; + + this->setPercentageText(QString::number(percentage_change, 'f', 1), ratio >= ratio_24h_ago); + } + + this->setDisplayText(QString::number(ratio, 'f', 6)); +} \ No newline at end of file diff --git a/src/widgets/TickerWidget.h b/src/widgets/TickerWidget.h index dc9c208..752b9ba 100644 --- a/src/widgets/TickerWidget.h +++ b/src/widgets/TickerWidget.h @@ -25,6 +25,7 @@ public: void setPercentageText(const QString &text, bool positive); void setFiatText(double amount, const QString &fiatCurrency); + void setDisplayText(const QString &text); public slots: virtual void updateDisplay() = 0; @@ -64,4 +65,19 @@ private: QString m_symbol; }; +class RatioTickerWidget : public TickerWidgetBase +{ + Q_OBJECT + +public: + explicit RatioTickerWidget(QWidget *parent, QSharedPointer ctx, QString symbol1, QString symbol2); + +public slots: + void updateDisplay() override; + +private: + QString m_symbol1; + QString m_symbol2; +}; + #endif // FEATHER_TICKERWIDGET_H