From 1e23b0679ed2ed693dfbf68f128e21a86ed1773c Mon Sep 17 00:00:00 2001 From: tobtoht Date: Thu, 20 May 2021 15:13:50 +0200 Subject: [PATCH] Amount display improvements --- src/appcontext.cpp | 92 ++++++++++++++++--------------- src/appcontext.h | 2 +- src/libwalletqt/WalletManager.cpp | 11 +++- src/libwalletqt/WalletManager.h | 2 +- src/mainwindow.cpp | 10 +--- src/model/CoinsModel.cpp | 5 +- src/utils/utils.cpp | 9 --- src/utils/utils.h | 1 - 8 files changed, 64 insertions(+), 68 deletions(-) diff --git a/src/appcontext.cpp b/src/appcontext.cpp index 75588a4..6ae6b9f 100644 --- a/src/appcontext.cpp +++ b/src/appcontext.cpp @@ -56,26 +56,9 @@ AppContext::AppContext(Wallet *wallet) this->onPreferredFiatCurrencyChanged(config()->get(Config::preferredFiatCurrency).toString()); } -void AppContext::onCancelTransaction(PendingTransaction *tx, const QVector &address) { - // 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(); -} +// ################## Transaction creation ################## void AppContext::onCreateTransaction(const QString &address, quint64 amount, const QString &description, bool all) { - // tx creation this->tmpTxDescription = description; if (!all && amount == 0) { @@ -83,16 +66,16 @@ void AppContext::onCreateTransaction(const QString &address, quint64 amount, con return; } - auto unlocked_balance = this->wallet->unlockedBalance(); - if(!all && amount > unlocked_balance) { + quint64 unlocked_balance = this->wallet->unlockedBalance(); + if (!all && amount > unlocked_balance) { emit createTransactionError("Not enough money to spend"); return; - } else if(unlocked_balance == 0) { + } else if (unlocked_balance == 0) { emit createTransactionError("No money to spend"); return; } - qDebug() << "Creating tx"; + qInfo() << "Creating transaction"; if (all) this->wallet->createTransactionAllAsync(address, "", constants::mixin, this->tx_priority); else @@ -114,28 +97,32 @@ void AppContext::onCreateTransactionMultiDest(const QVector &addresses, emit createTransactionError("Not enough money to spend"); } - qDebug() << "Creating tx"; + qInfo() << "Creating transaction"; this->wallet->createTransactionMultiDestAsync(addresses, amounts, this->tx_priority); 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) { this->tmpTxDescription = ""; emit endTransaction(); } -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; +void AppContext::onCancelTransaction(PendingTransaction *tx, const QVector &address) { + // tx cancelled by user + emit createTransactionCancelled(address, tx->amount()); + this->wallet->disposeTransaction(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) { emit deviceButtonRequest(code); } @@ -173,6 +177,8 @@ void AppContext::onDeviceError(const QString &message) { emit deviceError(message); } +// ################## Misc ################## + void AppContext::onTorSettingsChanged() { if (Utils::isTorsocks()) { return; @@ -247,23 +253,21 @@ void AppContext::onOpenAliasResolve(const QString &openAlias) { // ########################################## LIBWALLET QT SIGNALS #################################################### void AppContext::onMoneySpent(const QString &txId, quint64 amount) { - auto amount_num = amount / constants::cdiv; - qDebug() << Q_FUNC_INFO << txId << " " << QString::number(amount_num); + // Outgoing tx included in a block + qDebug() << Q_FUNC_INFO << txId << " " << WalletManager::displayAmount(amount); } void AppContext::onMoneyReceived(const QString &txId, quint64 amount) { // Incoming tx included in a block. - auto amount_num = amount / constants::cdiv; - qDebug() << Q_FUNC_INFO << txId << " " << QString::number(amount_num); + qDebug() << Q_FUNC_INFO << txId << " " << WalletManager::displayAmount(amount); } void AppContext::onUnconfirmedMoneyReceived(const QString &txId, quint64 amount) { - // Incoming transaction in pool - auto amount_num = amount / constants::cdiv; - qDebug() << Q_FUNC_INFO << txId << " " << QString::number(amount_num); + // Incoming tx in pool + qDebug() << Q_FUNC_INFO << txId << " " << WalletManager::displayAmount(amount); - if(this->wallet->synchronized()) { - auto notify = QString("%1 XMR (pending)").arg(amount_num); + if (this->wallet->synchronized()) { + auto notify = QString("%1 XMR (pending)").arg(WalletManager::displayAmount(amount, false)); Utils::desktopNotify("Payment received", notify, 5000); } } @@ -353,7 +357,7 @@ void AppContext::onTransactionCommitted(bool status, PendingTransaction *tx, con this->updateBalance(); // this tx was a donation to Feather, stop our nagging - if(this->donationSending) { + if (this->donationSending) { this->donationSending = false; config()->set(Config::donateBeg, -1); } diff --git a/src/appcontext.h b/src/appcontext.h index 646457d..af45fa6 100644 --- a/src/appcontext.h +++ b/src/appcontext.h @@ -82,7 +82,7 @@ signals: void walletRefreshed(); void transactionCommitted(bool status, PendingTransaction *tx, const QStringList& txid); void createTransactionError(QString message); - void createTransactionCancelled(const QVector &address, double amount); + void createTransactionCancelled(const QVector &address, quint64 amount); void createTransactionSuccess(PendingTransaction *tx, const QVector &address); void openAliasResolveError(const QString &msg); void openAliasResolved(const QString &address, const QString &openAlias); diff --git a/src/libwalletqt/WalletManager.cpp b/src/libwalletqt/WalletManager.cpp index aa34a53..b91b7cf 100644 --- a/src/libwalletqt/WalletManager.cpp +++ b/src/libwalletqt/WalletManager.cpp @@ -208,9 +208,16 @@ QString WalletManager::maximumAllowedAmountAsString() const 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) diff --git a/src/libwalletqt/WalletManager.h b/src/libwalletqt/WalletManager.h index 1a02d45..156b497 100644 --- a/src/libwalletqt/WalletManager.h +++ b/src/libwalletqt/WalletManager.h @@ -108,7 +108,7 @@ public: Q_INVOKABLE QString errorString() const; //! 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 amountFromDouble(double amount); Q_INVOKABLE quint64 maximumAllowedAmount() const; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0850ded..a535881 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -465,14 +465,9 @@ void MainWindow::onBalanceUpdated(quint64 balance, quint64 spendable) { qDebug() << Q_FUNC_INFO; bool hide = config()->get(Config::hideBalance).toBool(); - QString balance_str = WalletManager::displayAmount(spendable); - balance_str.remove(QRegExp("0+$")); - - QString label_str = QString("Balance: %1 XMR").arg(balance_str); + QString label_str = QString("Balance: %1 XMR").arg(WalletManager::displayAmount(spendable, false)); if (balance > spendable) { - QString unconfirmed_str = WalletManager::displayAmount(spendable); - unconfirmed_str.remove(QRegExp("0+$")); - label_str += QString(" (+%1 XMR unconfirmed)").arg(Utils::balanceFormat(balance - spendable)); + label_str += QString(" (+%1 XMR unconfirmed)").arg(WalletManager::displayAmount(balance - spendable, false)); } if (hide) @@ -1077,7 +1072,6 @@ void MainWindow::updateNetStats() { m_statusLabelNetStats->setText(""); return; } - if (m_ctx->wallet->connectionStatus() == Wallet::ConnectionStatus_Disconnected) { m_statusLabelNetStats->setText(""); return; diff --git a/src/model/CoinsModel.cpp b/src/model/CoinsModel.cpp index 1e7d525..ca297fb 100644 --- a/src/model/CoinsModel.cpp +++ b/src/model/CoinsModel.cpp @@ -8,6 +8,7 @@ #include "constants.h" #include "utils/ColorScheme.h" #include "utils/Icons.h" +#include "libwalletqt/WalletManager.h" #include @@ -182,9 +183,9 @@ QVariant CoinsModel::parseTransactionInfo(const CoinsInfo &cInfo, int column, in case Amount: { 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: return cInfo.frozen(); diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index 60706a9..eb35274 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -453,15 +453,6 @@ int Utils::maxLength(const QVector &array) { 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) { if (index.isPrimary()) { QTextCharFormat rec; diff --git a/src/utils/utils.h b/src/utils/utils.h index e72a8e7..7b88b8d 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -75,7 +75,6 @@ public: static QString amountToCurrencyString(double amount, const QString ¤cyCode); static int maxLength(const QVector &array); static QMap localeCache; - static QString balanceFormat(quint64 balance); static QTextCharFormat addressTextFormat(const SubaddressIndex &index); static bool isTorsocks(); static QString defaultWalletDir();