From c70819be0f08c5248446f46c4af864ad6a827aeb Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Fri, 22 Mar 2019 13:27:24 -0700 Subject: [PATCH] Requst payment dialog --- src/mainwindow.cpp | 9 ++++ src/mainwindow.h | 9 +++- src/mainwindow.ui | 18 ++++--- src/memoedit.cpp | 32 ++++++++----- src/memoedit.h | 1 + src/requestdialog.cpp | 49 +++++++++++++++++++ src/requestdialog.h | 3 ++ src/requestdialog.ui | 109 ++++++++++++++++++++++++++++++++---------- src/sendtab.cpp | 8 ++-- 9 files changed, 191 insertions(+), 47 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index cab4485..01573e4 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -16,6 +16,7 @@ #include "turnstile.h" #include "senttxstore.h" #include "connection.h" +#include "requestdialog.h" #include "websockets.h" using json = nlohmann::json; @@ -45,6 +46,11 @@ MainWindow::MainWindow(QWidget *parent) : rpc->checkForUpdate(false); }); + // Request zcash + QObject::connect(ui->actionRequest_zcash, &QAction::triggered, [=]() { + RequestDialog::showRequestZcash(this); + }); + // Pay Zcash URI QObject::connect(ui->actionPay_URI, &QAction::triggered, [=] () { payZcashURI(); @@ -1505,6 +1511,9 @@ MainWindow::~MainWindow() delete rpc; delete labelCompleter; + delete amtValidator; + delete feesValidator; + delete loadingMovie; delete logger; diff --git a/src/mainwindow.h b/src/mainwindow.h index 1a80213..6a5e179 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -42,6 +42,9 @@ public: void updateLabelsAutoComplete(); RPC* getRPC() { return rpc; } + QCompleter* getLabelCompleter() { return labelCompleter; } + QRegExpValidator* getAmountValidator() { return amtValidator; } + QString doSendTxValidations(Tx tx); void setDefaultPayFrom(); @@ -126,8 +129,10 @@ private: WSServer* wsserver = nullptr; WormholeClient* wormhole = nullptr; - RPC* rpc = nullptr; - QCompleter* labelCompleter = nullptr; + RPC* rpc = nullptr; + QCompleter* labelCompleter = nullptr; + QRegExpValidator* amtValidator = nullptr; + QRegExpValidator* feesValidator = nullptr; QMovie* loadingMovie; }; diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 812ed37..718a2bf 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -22,7 +22,7 @@ - 2 + 1 @@ -1058,6 +1058,7 @@ &File + @@ -1183,19 +1184,24 @@ Ctrl+M + + + Request zcash... + + - - QRCodeLabel - QLabel -
qrcodelabel.h
-
AddressCombo QComboBox
addresscombo.h
+ + QRCodeLabel + QLabel +
qrcodelabel.h
+
FilledIconLabel QLabel diff --git a/src/memoedit.cpp b/src/memoedit.cpp index f8a3565..79589e2 100644 --- a/src/memoedit.cpp +++ b/src/memoedit.cpp @@ -1,27 +1,35 @@ #include "memoedit.h" MemoEdit::MemoEdit(QWidget* parent) : QPlainTextEdit(parent) { - QObject::connect(this, &QPlainTextEdit::textChanged, [=]() { - QString txt = this->toPlainText(); - if (lenDisplayLabel) - lenDisplayLabel->setText(QString::number(txt.toUtf8().size()) + "/" + QString::number(maxlen)); + QObject::connect(this, &QPlainTextEdit::textChanged, this, &MemoEdit::updateDisplay); +} - if (txt.toUtf8().size() <= maxlen) { - // Everything is fine +void MemoEdit::updateDisplay() { + QString txt = this->toPlainText(); + if (lenDisplayLabel) + lenDisplayLabel->setText(QString::number(txt.toUtf8().size()) + "/" + QString::number(maxlen)); + + if (txt.toUtf8().size() <= maxlen) { + // Everything is fine + if (acceptButton) acceptButton->setEnabled(true); + + if (lenDisplayLabel) lenDisplayLabel->setStyleSheet(""); - } - else { - // Overweight + } + else { + // Overweight + if (acceptButton) acceptButton->setEnabled(false); + + if (lenDisplayLabel) lenDisplayLabel->setStyleSheet("color: red;"); - } - - }); + } } void MemoEdit::setMaxLen(int len) { this->maxlen = len; + updateDisplay(); } void MemoEdit::setLenDisplayLabel(QLabel* label) { diff --git a/src/memoedit.h b/src/memoedit.h index 1c6ba93..f1b2f5c 100644 --- a/src/memoedit.h +++ b/src/memoedit.h @@ -12,6 +12,7 @@ public: void setLenDisplayLabel(QLabel* label); void setAcceptButton(QPushButton* button); void includeReplyTo(QString replyToAddress); + void updateDisplay(); private: int maxlen = 512; diff --git a/src/requestdialog.cpp b/src/requestdialog.cpp index 5a56298..abfe170 100644 --- a/src/requestdialog.cpp +++ b/src/requestdialog.cpp @@ -1,5 +1,7 @@ #include "requestdialog.h" #include "ui_requestdialog.h" +#include "settings.h" +#include "addressbook.h" RequestDialog::RequestDialog(QWidget *parent) : QDialog(parent), @@ -12,3 +14,50 @@ RequestDialog::~RequestDialog() { delete ui; } + +// Static method that shows the request dialog +void RequestDialog::showRequestZcash(MainWindow* main) { + QDialog d(main); + Ui_RequestDialog req; + req.setupUi(&d); + Settings::saveRestore(&d); + + // Setup the Label completer for the Address + req.txtFrom->setCompleter(main->getLabelCompleter()); + QObject::connect(req.txtFrom, &QLineEdit::textChanged, [=] (auto text) { + auto addr = AddressBook::addressFromAddressLabel(text); + if (!Settings::getInstance()->isSaplingAddress(addr)) { + req.lblSaplingWarning->setText(tr("Can only request from Sapling addresses")); + req.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); + } else { + req.lblSaplingWarning->setText(""); + req.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true); + } + }); + + // Wire up AddressBook button + QObject::connect(req.btnAddressBook, &QPushButton::clicked, [=] () { + AddressBook::open(main, req.txtFrom); + }); + + // Amount textbox + req.txtAmount->setValidator(main->getAmountValidator()); + QObject::connect(req.txtAmount, &QLineEdit::textChanged, [=] (auto text) { + req.txtAmountUSD->setText(Settings::getUSDFormat(text.toDouble())); + }); + req.txtAmountUSD->setText(Settings::getUSDFormat(req.txtAmount->text().toDouble())); + + req.txtMemo->setAcceptButton(req.buttonBox->button(QDialogButtonBox::Ok)); + req.txtMemo->setLenDisplayLabel(req.lblMemoLen); + req.txtMemo->setMaxLen(400); + + req.txtFrom->setFocus(); + + if (d.exec() == QDialog::Accepted) { + // Construct a zcash Payment URI with the data and pay it immediately. + QString paymentURI = "zcash:" + AddressBook::addressFromAddressLabel(req.txtFrom->text()) + + "?amt=" + Settings::getDecimalString(req.txtAmount->text().toDouble()) + + "&memo=" + QUrl::toPercentEncoding(req.txtMemo->toPlainText()); + main->payZcashURI(paymentURI); + } +} \ No newline at end of file diff --git a/src/requestdialog.h b/src/requestdialog.h index 36e2da0..eca2e5d 100644 --- a/src/requestdialog.h +++ b/src/requestdialog.h @@ -2,6 +2,7 @@ #define REQUESTDIALOG_H #include +#include "mainwindow.h" namespace Ui { class RequestDialog; @@ -15,6 +16,8 @@ public: explicit RequestDialog(QWidget *parent = nullptr); ~RequestDialog(); + static void showRequestZcash(MainWindow* main); + private: Ui::RequestDialog *ui; }; diff --git a/src/requestdialog.ui b/src/requestdialog.ui index b2a11b3..dd3614f 100644 --- a/src/requestdialog.ui +++ b/src/requestdialog.ui @@ -6,45 +6,92 @@ 0 0 - 544 - 450 + 714 + 524 Dialog - + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - amount in ZEC + Amount - - + + + + color: red; + - Amount USD + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + - Amount + 0 / 512 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - + + + + Memo + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + AddressBook + + + + - + @@ -60,32 +107,46 @@ - - + + - Memo + Request From - - + + - Request From + Amount USD + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - + + + + Amount in ZEC + + + + + Qt::Horizontal - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok -
+ + + MemoEdit + QPlainTextEdit +
memoedit.h
+
+
diff --git a/src/sendtab.cpp b/src/sendtab.cpp index a91ff4c..13f7613 100644 --- a/src/sendtab.cpp +++ b/src/sendtab.cpp @@ -12,7 +12,8 @@ using json = nlohmann::json; void MainWindow::setupSendTab() { // Create the validator for send to/amount fields - auto amtValidator = new QRegExpValidator(QRegExp("[0-9]{0,8}\\.?[0-9]{0,8}")); + amtValidator = new QRegExpValidator(QRegExp("[0-9]{0,8}\\.?[0-9]{0,8}")); + ui->Amount1->setValidator(amtValidator); // Send button @@ -72,8 +73,9 @@ void MainWindow::setupSendTab() { ui->lblMinerFeeUSD->setText(Settings::getUSDFormat(txt.toDouble())); } }); + //Fees validator - auto feesValidator = new QRegExpValidator(QRegExp("[0-9]{0,8}\\.?[0-9]{0,8}")); + feesValidator = new QRegExpValidator(QRegExp("[0-9]{0,8}\\.?[0-9]{0,8}")); ui->minerFeeAmt->setValidator(feesValidator); // Font for the first Memo label @@ -243,8 +245,8 @@ void MainWindow::addAddressSection() { Amount1->setObjectName(QString("Amount") % QString::number(itemNumber)); Amount1->setBaseSize(QSize(200, 0)); Amount1->setAlignment(Qt::AlignRight); + // Create the validator for send to/amount fields - auto amtValidator = new QRegExpValidator(QRegExp("[0-9]{0,8}\\.?[0-9]{0,8}")); Amount1->setValidator(amtValidator); QObject::connect(Amount1, &QLineEdit::textChanged, [=] (auto text) { this->amountChanged(itemNumber, text);