From d26831363bd8576f2675c3ca4d26cde3aad15933 Mon Sep 17 00:00:00 2001 From: adityapk00 Date: Sun, 27 Jan 2019 14:27:52 -0800 Subject: [PATCH] USD/ZEC switch --- src/balancestablemodel.cpp | 2 +- src/recurring.cpp | 41 +++++++++++++++++++++++++++++++------- src/recurring.h | 4 +++- src/rpc.cpp | 8 ++++---- src/sendtab.cpp | 12 +++++------ src/settings.cpp | 16 ++++++++------- src/settings.h | 4 +++- src/txtablemodel.cpp | 2 +- 8 files changed, 61 insertions(+), 28 deletions(-) diff --git a/src/balancestablemodel.cpp b/src/balancestablemodel.cpp index e83a1ef..da344ee 100644 --- a/src/balancestablemodel.cpp +++ b/src/balancestablemodel.cpp @@ -93,7 +93,7 @@ QVariant BalancesTableModel::data(const QModelIndex &index, int role) const if(role == Qt::ToolTipRole) { switch (index.column()) { case 0: return AddressBook::addLabelToAddress(std::get<0>(modeldata->at(index.row()))); - case 1: return Settings::getUSDFormat(std::get<1>(modeldata->at(index.row()))); + case 1: return Settings::getUSDFromZecAmount(std::get<1>(modeldata->at(index.row()))); } } diff --git a/src/recurring.cpp b/src/recurring.cpp index 798b13d..af55787 100644 --- a/src/recurring.cpp +++ b/src/recurring.cpp @@ -33,21 +33,35 @@ RecurringPaymentInfo* Recurring::getNewRecurringFromTx(QWidget* parent, MainWind ui.cmbFromAddress->setCurrentText(tx.fromAddr); ui.cmbFromAddress->setEnabled(false); } - - ui.cmbCurrency->addItem(Settings::getTokenName()); + ui.cmbCurrency->addItem("USD"); + ui.cmbCurrency->addItem(Settings::getTokenName()); if (tx.toAddrs.length() > 0) { ui.txtToAddr->setText(tx.toAddrs[0].addr); ui.txtToAddr->setEnabled(false); - ui.txtAmt->setText(Settings::getDecimalString(tx.toAddrs[0].amount)); + // Default is USD + ui.txtAmt->setText(Settings::getUSDFromZecAmount(tx.toAddrs[0].amount)); ui.txtAmt->setEnabled(false); ui.txtMemo->setPlainText(tx.toAddrs[0].txtMemo); ui.txtMemo->setEnabled(false); } + // Wire up ZEC/USD toggle + QObject::connect(ui.cmbCurrency, QOverload::of(&QComboBox::currentIndexChanged), [&](QString c) { + if (tx.toAddrs.length() < 1) + return; + + if (c == "USD") { + ui.txtAmt->setText(Settings::getUSDFromZecAmount(tx.toAddrs[0].amount)); + } + else { + ui.txtAmt->setText(Settings::getDecimalString(tx.toAddrs[0].amount)); + } + }); + for (int i = Schedule::DAY; i <= Schedule::YEAR; i++) { ui.cmbSchedule->addItem("Every " + schedule_desc((Schedule)i), QVariant(i)); } @@ -64,8 +78,14 @@ RecurringPaymentInfo* Recurring::getNewRecurringFromTx(QWidget* parent, MainWind ui.txtDesc->setText(rpi->desc); ui.txtToAddr->setText(rpi->toAddr); ui.txtMemo->setPlainText(rpi->memo); - ui.txtAmt->setText(Settings::getDecimalString(rpi->amt)); + ui.cmbCurrency->setCurrentText(rpi->currency); + if (rpi->currency == "USD") { + ui.txtAmt->setText(Settings::getUSDFormat(rpi->amt)); + } + else { + ui.txtAmt->setText(Settings::getDecimalString(rpi->amt)); + } ui.cmbFromAddress->setCurrentText(rpi->fromAddr); ui.txtNumPayments->setText(QString::number(rpi->numPayments)); ui.cmbSchedule->setCurrentIndex(rpi->schedule); @@ -75,12 +95,13 @@ RecurringPaymentInfo* Recurring::getNewRecurringFromTx(QWidget* parent, MainWind if (d.exec() == QDialog::Accepted) { // Construct a new Object and return it auto r = new RecurringPaymentInfo(); - updateInfoWithTx(r, tx); r->desc = ui.txtDesc->text(); + r->currency = ui.cmbCurrency->currentText(); r->numPayments = ui.txtNumPayments->text().toInt(); r->schedule = (Schedule)ui.cmbSchedule->currentData().toInt(); r->startDate = QDateTime::currentDateTime(); + updateInfoWithTx(r, tx); return r; } else { @@ -91,9 +112,15 @@ RecurringPaymentInfo* Recurring::getNewRecurringFromTx(QWidget* parent, MainWind void Recurring::updateInfoWithTx(RecurringPaymentInfo* r, Tx tx) { r->toAddr = tx.toAddrs[0].addr; r->memo = tx.toAddrs[0].txtMemo; - r->amt = tx.toAddrs[0].amount; - r->currency = Settings::getTokenName(); r->fromAddr = tx.fromAddr; + if (r->currency.isEmpty() || r->currency == "USD") { + r->currency = "USD"; + r->amt = tx.toAddrs[0].amount * Settings::getInstance()->getZECPrice(); + } + else { + r->currency = Settings::getTokenName(); + r->amt = tx.toAddrs[0].amount; + } } QDateTime Recurring::getNextPaymentDate(Schedule s) { diff --git a/src/recurring.h b/src/recurring.h index 42be592..b32badb 100644 --- a/src/recurring.h +++ b/src/recurring.h @@ -2,6 +2,7 @@ #define RECURRING_H #include "precompiled.h" +#include "settings.h" class MainWindow; struct Tx; @@ -38,7 +39,8 @@ struct RecurringPaymentInfo { QList history; QString getScheduleDescription() { - return "Every " % schedule_desc(schedule) % ", starting " % startDate.toString("yyyy-MMM-dd") + return "Pay " % (currency == "USD" ? Settings::getUSDFormat(amt) : Settings::getZECDisplayFormat(amt)) + % " every " % schedule_desc(schedule) % ", starting " % startDate.toString("yyyy-MMM-dd") % ", for " % QString::number(numPayments) % " payments"; } }; diff --git a/src/rpc.cpp b/src/rpc.cpp index 738e3fa..4c5c424 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -617,7 +617,7 @@ void RPC::getInfoThenRefresh(bool force) { ")"; main->statusLabel->setText(statusText); - auto zecPrice = Settings::getUSDFormat(1); + auto zecPrice = Settings::getUSDFromZecAmount(1); QString tooltip; if (connections > 0) { tooltip = QObject::tr("Connected to zcashd"); @@ -727,9 +727,9 @@ void RPC::refreshBalances() { ui->balTransparent->setText(Settings::getZECDisplayFormat(balT)); ui->balTotal ->setText(Settings::getZECDisplayFormat(tot)); - ui->balSheilded ->setToolTip(Settings::getUSDFormat(balZ)); - ui->balTransparent->setToolTip(Settings::getUSDFormat(balT)); - ui->balTotal ->setToolTip(Settings::getUSDFormat(tot)); + ui->balSheilded ->setToolTip(Settings::getUSDFromZecAmount(balZ)); + ui->balTransparent->setToolTip(Settings::getUSDFromZecAmount(balT)); + ui->balTotal ->setToolTip(Settings::getUSDFromZecAmount(tot)); }); // 2. Get the UTXOs diff --git a/src/sendtab.cpp b/src/sendtab.cpp index ec2148a..a9d36a5 100644 --- a/src/sendtab.cpp +++ b/src/sendtab.cpp @@ -62,7 +62,7 @@ void MainWindow::setupSendTab() { // Disable custom fees if settings say no ui->minerFeeAmt->setReadOnly(!Settings::getInstance()->getAllowCustomFees()); QObject::connect(ui->minerFeeAmt, &QLineEdit::textChanged, [=](auto txt) { - ui->lblMinerFeeUSD->setText(Settings::getUSDFormat(txt.toDouble())); + ui->lblMinerFeeUSD->setText(Settings::getUSDFromZecAmount(txt.toDouble())); }); ui->minerFeeAmt->setText(Settings::getDecimalString(Settings::getMinerFee())); @@ -70,7 +70,7 @@ void MainWindow::setupSendTab() { QObject::connect(ui->tabWidget, &QTabWidget::currentChanged, [=] (int pos) { if (pos == 1) { QString txt = ui->minerFeeAmt->text(); - ui->lblMinerFeeUSD->setText(Settings::getUSDFormat(txt.toDouble())); + ui->lblMinerFeeUSD->setText(Settings::getUSDFromZecAmount(txt.toDouble())); } }); //Fees validator @@ -171,7 +171,7 @@ void MainWindow::inputComboTextChanged(int index) { auto balFmt = Settings::getZECDisplayFormat(bal); ui->sendAddressBalance->setText(balFmt); - ui->sendAddressBalanceUSD->setText(Settings::getUSDFormat(bal)); + ui->sendAddressBalanceUSD->setText(Settings::getUSDFromZecAmount(bal)); } @@ -275,7 +275,7 @@ void MainWindow::addressChanged(int itemNumber, const QString& text) { void MainWindow::amountChanged(int item, const QString& text) { auto usd = ui->sendToWidgets->findChild(QString("AmtUSD") % QString::number(item)); - usd->setText(Settings::getUSDFormat(text.toDouble())); + usd->setText(Settings::getUSDFromZecAmount(text.toDouble())); } void MainWindow::setMemoEnabled(int number, bool enabled) { @@ -562,7 +562,7 @@ bool MainWindow::confirmTx(Tx tx, RecurringPaymentInfo* rpi) { // Amount (USD) auto AmtUSD = new QLabel(confirm.sendToAddrs); AmtUSD->setObjectName(QString("AmtUSD") % QString::number(i + 1)); - AmtUSD->setText(Settings::getUSDFormat(toAddr.amount)); + AmtUSD->setText(Settings::getUSDFromZecAmount(toAddr.amount)); AmtUSD->setAlignment(Qt::AlignRight | Qt::AlignTrailing | Qt::AlignVCenter); confirm.gridLayout->addWidget(AmtUSD, row, 2, 1, 1); @@ -613,7 +613,7 @@ bool MainWindow::confirmTx(Tx tx, RecurringPaymentInfo* rpi) { minerFeeUSD->setObjectName(QStringLiteral("minerFeeUSD")); minerFeeUSD->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); confirm.gridLayout->addWidget(minerFeeUSD, row, 2, 1, 1); - minerFeeUSD->setText(Settings::getUSDFormat(tx.fee)); + minerFeeUSD->setText(Settings::getUSDFromZecAmount(tx.fee)); if (Settings::getInstance()->getAllowCustomFees() && tx.fee != Settings::getMinerFee()) { confirm.warningLabel->setVisible(true); diff --git a/src/settings.cpp b/src/settings.cpp index 45be150..4d1b1d5 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -152,11 +152,13 @@ void Settings::saveRestore(QDialog* d) { }); } -QString Settings::getUSDFormat(double bal) { - //if (!Settings::getInstance()->isTestnet() && Settings::getInstance()->getZECPrice() > 0) - return "$" + QLocale(QLocale::English).toString(bal * Settings::getInstance()->getZECPrice(), 'f', 2); - //else - // return QString(); + +QString Settings::getUSDFormat(double usdAmt) { + return "$" + QLocale(QLocale::English).toString(usdAmt, 'f', 2); +} + +QString Settings::getUSDFromZecAmount(double bal) { + return getUSDFormat(bal * Settings::getInstance()->getZECPrice()); } QString Settings::getDecimalString(double amt) { @@ -177,9 +179,9 @@ QString Settings::getZECDisplayFormat(double bal) { } QString Settings::getZECUSDDisplayFormat(double bal) { - auto usdFormat = getUSDFormat(bal); + auto usdFormat = getUSDFromZecAmount(bal); if (!usdFormat.isEmpty()) - return getZECDisplayFormat(bal) % " (" % getUSDFormat(bal) % ")"; + return getZECDisplayFormat(bal) % " (" % usdFormat % ")"; else return getZECDisplayFormat(bal); } diff --git a/src/settings.h b/src/settings.h index f282fac..fbadec0 100644 --- a/src/settings.h +++ b/src/settings.h @@ -66,7 +66,9 @@ public: static bool isTAddress(QString addr); static QString getDecimalString(double amt); - static QString getUSDFormat(double bal); + static QString getUSDFormat(double usdAmt); + + static QString getUSDFromZecAmount(double bal); static QString getZECDisplayFormat(double bal); static QString getZECUSDDisplayFormat(double bal); diff --git a/src/txtablemodel.cpp b/src/txtablemodel.cpp index a294f66..8b224b2 100644 --- a/src/txtablemodel.cpp +++ b/src/txtablemodel.cpp @@ -148,7 +148,7 @@ void TxTableModel::updateAllData() { return addr; } case 2: return QDateTime::fromMSecsSinceEpoch(modeldata->at(index.row()).datetime * (qint64)1000).toLocalTime().toString(); - case 3: return Settings::getInstance()->getUSDFormat(modeldata->at(index.row()).amount); + case 3: return Settings::getInstance()->getUSDFromZecAmount(modeldata->at(index.row()).amount); } }