TickerWidget: rework

This commit is contained in:
tobtoht 2021-05-25 01:10:45 +02:00
parent 3410132033
commit ba2b0ac550
No known key found for this signature in database
GPG key ID: 1CADD27F41F45C3C
7 changed files with 186 additions and 146 deletions

View file

@ -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) {

View file

@ -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<TickerWidget*> m_tickerWidgets;
TickerWidget *m_balanceWidget;
QList<PriceTickerWidget*> m_priceTickerWidgets;
BalanceTickerWidget *m_balanceTickerWidget;
// lower status bar
QPushButton *m_statusUpdateAvailable;

View file

@ -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<AppContext> 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 = "<html><head/><body><p><span style=\" color:red;\">";
if(positive) {
pctText = pctText.replace("red", "green");
pctText += QString("+%1%").arg(text);
} else
pctText += QString("%1%").arg(text);
pctText += "</span></p></body></html>";
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<AppContext> 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<AppContext> 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);
}

View file

@ -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 <QWidget>
#include "appcontext.h"
namespace Ui {
class TickerWidget;
}
class TickerWidgetBase : public QWidget
{
Q_OBJECT
public:
explicit TickerWidgetBase(QWidget *parent, QSharedPointer<AppContext> 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<AppContext> m_ctx;
};
class BalanceTickerWidget : public TickerWidgetBase
{
Q_OBJECT
public:
explicit BalanceTickerWidget(QWidget *parent, QSharedPointer<AppContext> ctx, bool totalBalance);
public slots:
void updateDisplay() override;
private:
bool m_totalBalance;
};
class PriceTickerWidget : public TickerWidgetBase
{
Q_OBJECT
public:
explicit PriceTickerWidget(QWidget *parent, QSharedPointer<AppContext> ctx, QString symbol);
public slots:
void updateDisplay() override;
private:
QString m_symbol;
};
#endif // FEATHER_TICKERWIDGET_H

View file

@ -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<AppContext> 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 = "<html><head/><body><p><span style=\" color:red;\">";
if(positive) {
pctText = pctText.replace("red", "green");
pctText += QString("+%1%").arg(text);
} else
pctText += QString("%1%").arg(text);
pctText += "</span></p></body></html>";
ui->tickerPct->setText(pctText);
}
void TickerWidget::setFontSizes() {
ui->tickerPct->setFont(Utils::relativeFont(-2));
ui->tickerFiat->setFont(Utils::relativeFont(0));
}
TickerWidget::~TickerWidget() {
delete ui;
}

View file

@ -1,38 +0,0 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#ifndef TICKERWIDGET_H
#define TICKERWIDGET_H
#include <QWidget>
#include "appcontext.h"
namespace Ui {
class TickerWidget;
}
class TickerWidget : public QWidget
{
Q_OBJECT
public:
explicit TickerWidget(QWidget *parent, QSharedPointer<AppContext> 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<AppContext> m_ctx;
QString m_symbol;
bool m_convertBalance;
bool m_hidePercent;
};
#endif // TICKERWIDGET_H