Browse Source

Save created recurring info

recurring
adityapk00 5 years ago
parent
commit
107a46ebe9
  1. 4
      src/mainwindow.h
  2. 71
      src/recurring.cpp
  3. 19
      src/recurring.h
  4. 10
      src/sendtab.cpp
  5. 6
      src/settings.cpp

4
src/mainwindow.h

@ -2,7 +2,9 @@
#define MAINWINDOW_H
#include "precompiled.h"
#include "logger.h"
#include "recurring.h"
// Forward declare to break circular dependency.
class RPC;
@ -107,6 +109,8 @@ private:
RPC* rpc = nullptr;
QCompleter* labelCompleter = nullptr;
RecurringPaymentInfo* sendTxRecurringInfo = nullptr;
QMovie* loadingMovie;
};

71
src/recurring.cpp

@ -5,7 +5,19 @@
#include "settings.h"
#include "ui_newrecurring.h"
void Recurring::showEditDialog(QWidget* parent, MainWindow* main, Tx tx) {
QString schedule_desc(Schedule s) {
switch (s) {
case Schedule::DAY: return "day";
case Schedule::WEEK: return "week";
case Schedule::MONTH: return "month";
case Schedule::YEAR: return "year";
default: return "none";
}
}
// Returns a new Recurring payment info, created from the Tx.
// The caller needs to take ownership of the returned object.
RecurringPaymentInfo* Recurring::getNewRecurringFromTx(QWidget* parent, MainWindow* main, Tx tx, RecurringPaymentInfo* rpi) {
Ui_newRecurringDialog ui;
QDialog d(parent);
ui.setupUi(&d);
@ -36,13 +48,60 @@ void Recurring::showEditDialog(QWidget* parent, MainWindow* main, Tx tx) {
ui.txtMemo->setEnabled(false);
}
ui.cmbSchedule->addItem("Every Day", QVariant(Schedule::DAY));
ui.cmbSchedule->addItem("Every Week", QVariant(Schedule::WEEK));
ui.cmbSchedule->addItem("Every Month", QVariant(Schedule::MONTH));
ui.cmbSchedule->addItem("Every Year", QVariant(Schedule::YEAR));
for (int i = Schedule::DAY; i <= Schedule::YEAR; i++) {
ui.cmbSchedule->addItem("Every " + schedule_desc((Schedule)i), QVariant(i));
}
QObject::connect(ui.cmbSchedule, QOverload<int>::of(&QComboBox::currentIndexChanged), [&](int) {
ui.lblNextPayment->setText(getNextPaymentDate((Schedule)ui.cmbSchedule->currentData().toInt()).toString("yyyy-MMM-dd"));
});
ui.lblNextPayment->setText(getNextPaymentDate((Schedule)ui.cmbSchedule->currentData().toInt()).toString("yyyy-MMM-dd"));
ui.txtNumPayments->setText("10");
// If an existing RecurringPaymentInfo was passed in, set the UI values appropriately
if (rpi != nullptr) {
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);
ui.cmbFromAddress->setCurrentText(rpi->fromAddr);
ui.txtNumPayments->setText(QString::number(rpi->numPayments));
ui.cmbSchedule->setCurrentIndex(rpi->schedule);
}
ui.txtDesc->setFocus();
d.exec();
if (d.exec() == QDialog::Accepted) {
// Construct a new Object and return it
auto r = new RecurringPaymentInfo();
r->toAddr = tx.toAddrs[0].addr;
r->memo = tx.toAddrs[0].txtMemo;
r->amt = tx.toAddrs[0].amount;
r->currency = Settings::getTokenName();
r->desc = ui.txtDesc->text();
r->fromAddr = tx.fromAddr;
r->numPayments = ui.txtNumPayments->text().toInt();
r->schedule = (Schedule)ui.cmbSchedule->currentData().toInt();
r->startDate = QDateTime::currentDateTime();
return r;
}
else {
return nullptr;
}
}
QDateTime Recurring::getNextPaymentDate(Schedule s) {
auto nextDate = QDateTime::currentDateTime();
switch (s) {
case Schedule::DAY: nextDate = nextDate.addDays(1); break;
case Schedule::WEEK: nextDate = nextDate.addDays(7); break;
case Schedule::MONTH: nextDate = nextDate.addMonths(1); break;
case Schedule::YEAR: nextDate = nextDate.addYears(1); break;
}
return nextDate;
}

19
src/recurring.h

@ -3,7 +3,8 @@
#include "precompiled.h"
#include "mainwindow.h"
class MainWindow;
struct Tx;
enum Schedule {
DAY = 1,
@ -12,25 +13,35 @@ enum Schedule {
YEAR
};
QString schedule_desc(Schedule s);
struct RecurringPaymentInfo {
QString desc;
QString fromAddr;
QString toAddr;
double amt;
QString memo;
QString currency;
Schedule schedule;
int numPayments;
long startBlock;
QDateTime startDate;
int completedPayments;
QString getScheduleDescription() {
return "Every " % schedule_desc(schedule) % ", starting " % startDate.toString("yyyy-MMM-dd")
% ", for " % QString::number(numPayments) % " payments";
}
};
class Recurring
{
public:
Recurring();
Recurring() = default;
static void showEditDialog(QWidget* parent, MainWindow* main, Tx tx);
static RecurringPaymentInfo* getNewRecurringFromTx(QWidget* parent, MainWindow* main, Tx tx, RecurringPaymentInfo* rpi);
static QDateTime getNextPaymentDate(Schedule s);
};
#endif // RECURRING_H

10
src/sendtab.cpp

@ -103,8 +103,14 @@ void MainWindow::setupSendTab() {
void MainWindow::editSchedule() {
// Open the edit schedule dialog
Recurring::showEditDialog(this, this, createTxFromSendPage());
RecurringPaymentInfo* recurringInfo = Recurring::getNewRecurringFromTx(this, this, createTxFromSendPage(), this->sendTxRecurringInfo);
if (recurringInfo == nullptr) {
}
else {
this->sendTxRecurringInfo = recurringInfo;
ui->lblRecurDesc->setText(recurringInfo->getScheduleDescription());
}
}
void MainWindow::updateLabelsAutoComplete() {
@ -384,6 +390,8 @@ void MainWindow::removeExtraAddresses() {
ui->chkRecurring->setCheckState(Qt::Unchecked);
ui->btnRecurSchedule->setEnabled(false);
ui->lblRecurDesc->setText("");
delete sendTxRecurringInfo;
sendTxRecurringInfo = nullptr;
}
void MainWindow::maxAmountChecked(int checked) {

6
src/settings.cpp

@ -153,10 +153,10 @@ void Settings::saveRestore(QDialog* d) {
}
QString Settings::getUSDFormat(double bal) {
if (!Settings::getInstance()->isTestnet() && Settings::getInstance()->getZECPrice() > 0)
//if (!Settings::getInstance()->isTestnet() && Settings::getInstance()->getZECPrice() > 0)
return "$" + QLocale(QLocale::English).toString(bal * Settings::getInstance()->getZECPrice(), 'f', 2);
else
return QString();
//else
// return QString();
}
QString Settings::getDecimalString(double amt) {

Loading…
Cancel
Save