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());
}
void AppContext::onCancelTransaction(PendingTransaction *tx, const QVector<QString> &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<QString> &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<QString> &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);
}

View file

@ -82,7 +82,7 @@ signals:
void walletRefreshed();
void transactionCommitted(bool status, PendingTransaction *tx, const QStringList& txid);
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 openAliasResolveError(const QString &msg);
void openAliasResolved(const QString &address, const QString &openAlias);

View file

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

View file

@ -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;

View file

@ -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;

View file

@ -8,6 +8,7 @@
#include "constants.h"
#include "utils/ColorScheme.h"
#include "utils/Icons.h"
#include "libwalletqt/WalletManager.h"
#include <QBrush>
@ -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();

View file

@ -453,15 +453,6 @@ int Utils::maxLength(const QVector<QString> &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;

View file

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