Amount display improvements

This commit is contained in:
tobtoht 2021-05-20 15:13:50 +02:00
parent f43949a346
commit 1e23b0679e
No known key found for this signature in database
GPG key ID: 1CADD27F41F45C3C
8 changed files with 64 additions and 68 deletions

View file

@ -56,26 +56,9 @@ AppContext::AppContext(Wallet *wallet)
this->onPreferredFiatCurrencyChanged(config()->get(Config::preferredFiatCurrency).toString()); this->onPreferredFiatCurrencyChanged(config()->get(Config::preferredFiatCurrency).toString());
} }
void AppContext::onCancelTransaction(PendingTransaction *tx, const QVector<QString> &address) { // ################## Transaction creation ##################
// tx cancelled by user
double amount = tx->amount() / constants::cdiv;
emit createTransactionCancelled(address, amount);
this->wallet->disposeTransaction(tx);
}
void AppContext::onSweepOutput(const QString &keyImage, QString address, bool churn, int outputs) {
if (churn) {
address = this->wallet->address(0, 0); // primary address
}
qCritical() << "Creating transaction";
this->wallet->createTransactionSingleAsync(keyImage, address, outputs, this->tx_priority);
emit initiateTransaction();
}
void AppContext::onCreateTransaction(const QString &address, quint64 amount, const QString &description, bool all) { void AppContext::onCreateTransaction(const QString &address, quint64 amount, const QString &description, bool all) {
// tx creation
this->tmpTxDescription = description; this->tmpTxDescription = description;
if (!all && amount == 0) { if (!all && amount == 0) {
@ -83,16 +66,16 @@ void AppContext::onCreateTransaction(const QString &address, quint64 amount, con
return; return;
} }
auto unlocked_balance = this->wallet->unlockedBalance(); quint64 unlocked_balance = this->wallet->unlockedBalance();
if(!all && amount > unlocked_balance) { if (!all && amount > unlocked_balance) {
emit createTransactionError("Not enough money to spend"); emit createTransactionError("Not enough money to spend");
return; return;
} else if(unlocked_balance == 0) { } else if (unlocked_balance == 0) {
emit createTransactionError("No money to spend"); emit createTransactionError("No money to spend");
return; return;
} }
qDebug() << "Creating tx"; qInfo() << "Creating transaction";
if (all) if (all)
this->wallet->createTransactionAllAsync(address, "", constants::mixin, this->tx_priority); this->wallet->createTransactionAllAsync(address, "", constants::mixin, this->tx_priority);
else else
@ -114,28 +97,32 @@ void AppContext::onCreateTransactionMultiDest(const QVector<QString> &addresses,
emit createTransactionError("Not enough money to spend"); emit createTransactionError("Not enough money to spend");
} }
qDebug() << "Creating tx"; qInfo() << "Creating transaction";
this->wallet->createTransactionMultiDestAsync(addresses, amounts, this->tx_priority); this->wallet->createTransactionMultiDestAsync(addresses, amounts, this->tx_priority);
emit initiateTransaction(); emit initiateTransaction();
} }
void AppContext::onSweepOutput(const QString &keyImage, QString address, bool churn, int outputs) {
if (churn) {
address = this->wallet->address(0, 0); // primary address
}
qInfo() << "Creating transaction";
this->wallet->createTransactionSingleAsync(keyImage, address, outputs, this->tx_priority);
emit initiateTransaction();
}
void AppContext::onCreateTransactionError(const QString &msg) { void AppContext::onCreateTransactionError(const QString &msg) {
this->tmpTxDescription = ""; this->tmpTxDescription = "";
emit endTransaction(); emit endTransaction();
} }
void AppContext::onPreferredFiatCurrencyChanged(const QString &symbol) { void AppContext::onCancelTransaction(PendingTransaction *tx, const QVector<QString> &address) {
auto *model = this->wallet->transactionHistoryModel(); // tx cancelled by user
if (model != nullptr) { emit createTransactionCancelled(address, tx->amount());
model->preferredFiatSymbol = symbol; this->wallet->disposeTransaction(tx);
}
}
void AppContext::onAmountPrecisionChanged(int precision) {
auto *model = this->wallet->transactionHistoryModel();
if (!model) return;
model->amountPrecision = precision;
} }
void AppContext::commitTransaction(PendingTransaction *tx) { void AppContext::commitTransaction(PendingTransaction *tx) {
@ -164,6 +151,23 @@ void AppContext::onMultiBroadcast(PendingTransaction *tx) {
} }
} }
// ################## Models ##################
void AppContext::onPreferredFiatCurrencyChanged(const QString &symbol) {
auto *model = this->wallet->transactionHistoryModel();
if (model != nullptr) {
model->preferredFiatSymbol = symbol;
}
}
void AppContext::onAmountPrecisionChanged(int precision) {
auto *model = this->wallet->transactionHistoryModel();
if (!model) return;
model->amountPrecision = precision;
}
// ################## Device ##################
void AppContext::onDeviceButtonRequest(quint64 code) { void AppContext::onDeviceButtonRequest(quint64 code) {
emit deviceButtonRequest(code); emit deviceButtonRequest(code);
} }
@ -173,6 +177,8 @@ void AppContext::onDeviceError(const QString &message) {
emit deviceError(message); emit deviceError(message);
} }
// ################## Misc ##################
void AppContext::onTorSettingsChanged() { void AppContext::onTorSettingsChanged() {
if (Utils::isTorsocks()) { if (Utils::isTorsocks()) {
return; return;
@ -247,23 +253,21 @@ void AppContext::onOpenAliasResolve(const QString &openAlias) {
// ########################################## LIBWALLET QT SIGNALS #################################################### // ########################################## LIBWALLET QT SIGNALS ####################################################
void AppContext::onMoneySpent(const QString &txId, quint64 amount) { void AppContext::onMoneySpent(const QString &txId, quint64 amount) {
auto amount_num = amount / constants::cdiv; // Outgoing tx included in a block
qDebug() << Q_FUNC_INFO << txId << " " << QString::number(amount_num); qDebug() << Q_FUNC_INFO << txId << " " << WalletManager::displayAmount(amount);
} }
void AppContext::onMoneyReceived(const QString &txId, quint64 amount) { void AppContext::onMoneyReceived(const QString &txId, quint64 amount) {
// Incoming tx included in a block. // Incoming tx included in a block.
auto amount_num = amount / constants::cdiv; qDebug() << Q_FUNC_INFO << txId << " " << WalletManager::displayAmount(amount);
qDebug() << Q_FUNC_INFO << txId << " " << QString::number(amount_num);
} }
void AppContext::onUnconfirmedMoneyReceived(const QString &txId, quint64 amount) { void AppContext::onUnconfirmedMoneyReceived(const QString &txId, quint64 amount) {
// Incoming transaction in pool // Incoming tx in pool
auto amount_num = amount / constants::cdiv; qDebug() << Q_FUNC_INFO << txId << " " << WalletManager::displayAmount(amount);
qDebug() << Q_FUNC_INFO << txId << " " << QString::number(amount_num);
if(this->wallet->synchronized()) { if (this->wallet->synchronized()) {
auto notify = QString("%1 XMR (pending)").arg(amount_num); auto notify = QString("%1 XMR (pending)").arg(WalletManager::displayAmount(amount, false));
Utils::desktopNotify("Payment received", notify, 5000); Utils::desktopNotify("Payment received", notify, 5000);
} }
} }
@ -353,7 +357,7 @@ void AppContext::onTransactionCommitted(bool status, PendingTransaction *tx, con
this->updateBalance(); this->updateBalance();
// this tx was a donation to Feather, stop our nagging // this tx was a donation to Feather, stop our nagging
if(this->donationSending) { if (this->donationSending) {
this->donationSending = false; this->donationSending = false;
config()->set(Config::donateBeg, -1); config()->set(Config::donateBeg, -1);
} }

View file

@ -82,7 +82,7 @@ signals:
void walletRefreshed(); void walletRefreshed();
void transactionCommitted(bool status, PendingTransaction *tx, const QStringList& txid); void transactionCommitted(bool status, PendingTransaction *tx, const QStringList& txid);
void createTransactionError(QString message); void createTransactionError(QString message);
void createTransactionCancelled(const QVector<QString> &address, double amount); void createTransactionCancelled(const QVector<QString> &address, quint64 amount);
void createTransactionSuccess(PendingTransaction *tx, const QVector<QString> &address); void createTransactionSuccess(PendingTransaction *tx, const QVector<QString> &address);
void openAliasResolveError(const QString &msg); void openAliasResolveError(const QString &msg);
void openAliasResolved(const QString &address, const QString &openAlias); void openAliasResolved(const QString &address, const QString &openAlias);

View file

@ -208,9 +208,16 @@ QString WalletManager::maximumAllowedAmountAsString() const
return WalletManager::displayAmount(WalletManager::maximumAllowedAmount()); return WalletManager::displayAmount(WalletManager::maximumAllowedAmount());
} }
QString WalletManager::displayAmount(quint64 amount) QString WalletManager::displayAmount(quint64 amount, bool trailing_zeroes)
{ {
return QString::fromStdString(Monero::Wallet::displayAmount(amount)); auto amountStr = QString::fromStdString(Monero::Wallet::displayAmount(amount));
if (!trailing_zeroes) {
amountStr.remove(QRegExp("0+$"));
amountStr.remove(QRegExp("\\.$"));
}
return amountStr;
} }
quint64 WalletManager::amountFromString(const QString &amount) quint64 WalletManager::amountFromString(const QString &amount)

View file

@ -108,7 +108,7 @@ public:
Q_INVOKABLE QString errorString() const; Q_INVOKABLE QString errorString() const;
//! since we can't call static method from QML, move it to this class //! since we can't call static method from QML, move it to this class
Q_INVOKABLE static QString displayAmount(quint64 amount); Q_INVOKABLE static QString displayAmount(quint64 amount, bool trailing_zeroes = true);
Q_INVOKABLE static quint64 amountFromString(const QString &amount); Q_INVOKABLE static quint64 amountFromString(const QString &amount);
Q_INVOKABLE static quint64 amountFromDouble(double amount); Q_INVOKABLE static quint64 amountFromDouble(double amount);
Q_INVOKABLE quint64 maximumAllowedAmount() const; Q_INVOKABLE quint64 maximumAllowedAmount() const;

View file

@ -465,14 +465,9 @@ void MainWindow::onBalanceUpdated(quint64 balance, quint64 spendable) {
qDebug() << Q_FUNC_INFO; qDebug() << Q_FUNC_INFO;
bool hide = config()->get(Config::hideBalance).toBool(); bool hide = config()->get(Config::hideBalance).toBool();
QString balance_str = WalletManager::displayAmount(spendable); QString label_str = QString("Balance: %1 XMR").arg(WalletManager::displayAmount(spendable, false));
balance_str.remove(QRegExp("0+$"));
QString label_str = QString("Balance: %1 XMR").arg(balance_str);
if (balance > spendable) { if (balance > spendable) {
QString unconfirmed_str = WalletManager::displayAmount(spendable); label_str += QString(" (+%1 XMR unconfirmed)").arg(WalletManager::displayAmount(balance - spendable, false));
unconfirmed_str.remove(QRegExp("0+$"));
label_str += QString(" (+%1 XMR unconfirmed)").arg(Utils::balanceFormat(balance - spendable));
} }
if (hide) if (hide)
@ -1077,7 +1072,6 @@ void MainWindow::updateNetStats() {
m_statusLabelNetStats->setText(""); m_statusLabelNetStats->setText("");
return; return;
} }
if (m_ctx->wallet->connectionStatus() == Wallet::ConnectionStatus_Disconnected) { if (m_ctx->wallet->connectionStatus() == Wallet::ConnectionStatus_Disconnected) {
m_statusLabelNetStats->setText(""); m_statusLabelNetStats->setText("");
return; return;

View file

@ -8,6 +8,7 @@
#include "constants.h" #include "constants.h"
#include "utils/ColorScheme.h" #include "utils/ColorScheme.h"
#include "utils/Icons.h" #include "utils/Icons.h"
#include "libwalletqt/WalletManager.h"
#include <QBrush> #include <QBrush>
@ -182,9 +183,9 @@ QVariant CoinsModel::parseTransactionInfo(const CoinsInfo &cInfo, int column, in
case Amount: case Amount:
{ {
if (role == Qt::UserRole) { if (role == Qt::UserRole) {
return cInfo.amount() / constants::cdiv; return cInfo.amount();
} }
return QString::number(cInfo.amount() / constants::cdiv, 'f', 12); return cInfo.displayAmount();
} }
case Frozen: case Frozen:
return cInfo.frozen(); return cInfo.frozen();

View file

@ -453,15 +453,6 @@ int Utils::maxLength(const QVector<QString> &array) {
return maxLength; return maxLength;
} }
QString Utils::balanceFormat(quint64 balance) {
QString str = QString::number(balance / constants::cdiv, 'f', 4);
str.remove(QRegExp("0+$"));
str.remove(QRegExp("\\.$"));
return str;
}
QTextCharFormat Utils::addressTextFormat(const SubaddressIndex &index) { QTextCharFormat Utils::addressTextFormat(const SubaddressIndex &index) {
if (index.isPrimary()) { if (index.isPrimary()) {
QTextCharFormat rec; QTextCharFormat rec;

View file

@ -75,7 +75,6 @@ public:
static QString amountToCurrencyString(double amount, const QString &currencyCode); static QString amountToCurrencyString(double amount, const QString &currencyCode);
static int maxLength(const QVector<QString> &array); static int maxLength(const QVector<QString> &array);
static QMap<QString, QLocale> localeCache; static QMap<QString, QLocale> localeCache;
static QString balanceFormat(quint64 balance);
static QTextCharFormat addressTextFormat(const SubaddressIndex &index); static QTextCharFormat addressTextFormat(const SubaddressIndex &index);
static bool isTorsocks(); static bool isTorsocks();
static QString defaultWalletDir(); static QString defaultWalletDir();