Add context menu on transaction list: copy label, copy address, edit label, show...
[novacoin.git] / src / qt / sendcoinsdialog.cpp
index 721ab14..4adda7e 100644 (file)
@@ -1,9 +1,10 @@
 #include "sendcoinsdialog.h"
 #include "ui_sendcoinsdialog.h"
-#include "clientmodel.h"
+#include "walletmodel.h"
+#include "addresstablemodel.h"
 #include "guiutil.h"
 
-#include "addressbookdialog.h"
+#include "addressbookpage.h"
 #include "optionsmodel.h"
 
 #include <QApplication>
@@ -11,9 +12,7 @@
 #include <QMessageBox>
 #include <QLocale>
 #include <QDebug>
-
-#include "util.h"
-#include "base58.h"
+#include <QMessageBox>
 
 SendCoinsDialog::SendCoinsDialog(QWidget *parent, const QString &address) :
     QDialog(parent),
@@ -21,11 +20,14 @@ SendCoinsDialog::SendCoinsDialog(QWidget *parent, const QString &address) :
     model(0)
 {
     ui->setupUi(this);
+#if QT_VERSION >= 0x040700
+    ui->payTo->setPlaceholderText(tr("Enter a Bitcoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L)"));
+    ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
+#endif
 
     GUIUtil::setupAddressWidget(ui->payTo, this);
-    GUIUtil::setupAmountWidget(ui->payAmount, this);
 
-    /* Set initial address if provided */
+    // Set initial send-to address if provided
     if(!address.isEmpty())
     {
         ui->payTo->setText(address);
@@ -33,7 +35,7 @@ SendCoinsDialog::SendCoinsDialog(QWidget *parent, const QString &address) :
     }
 }
 
-void SendCoinsDialog::setModel(ClientModel *model)
+void SendCoinsDialog::setModel(WalletModel *model)
 {
     this->model = model;
 }
@@ -47,46 +49,60 @@ void SendCoinsDialog::on_sendButton_clicked()
 {
     bool valid;
     QString payAmount = ui->payAmount->text();
+    QString label;
     qint64 payAmountParsed;
 
-    valid = ParseMoney(payAmount.toStdString(), payAmountParsed);
+    valid = GUIUtil::parseMoney(payAmount, &payAmountParsed);
 
-    if(!valid)
+    if(!valid || payAmount.isEmpty())
     {
         QMessageBox::warning(this, tr("Send Coins"),
-            tr("The amount to pay must be a valid number."),
+            tr("Must fill in an amount to pay."),
             QMessageBox::Ok, QMessageBox::Ok);
         return;
     }
 
-    switch(model->sendCoins(ui->payTo->text(), payAmountParsed))
+    // Add address to address book under label, if specified
+    label = ui->addAsLabel->text();
+
+    QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
+                          tr("Are you sure you want to send %1 BTC to %2 (%3)?").arg(GUIUtil::formatMoney(payAmountParsed), label, ui->payTo->text()),
+          QMessageBox::Yes|QMessageBox::Cancel,
+          QMessageBox::Cancel);
+
+    if(retval != QMessageBox::Yes)
+    {
+        return;
+    }
+
+    switch(model->sendCoins(ui->payTo->text(), payAmountParsed, label))
     {
-    case ClientModel::InvalidAddress:
+    case WalletModel::InvalidAddress:
         QMessageBox::warning(this, tr("Send Coins"),
             tr("The recepient address is not valid, please recheck."),
             QMessageBox::Ok, QMessageBox::Ok);
         ui->payTo->setFocus();
         break;
-    case ClientModel::InvalidAmount:
+    case WalletModel::InvalidAmount:
         QMessageBox::warning(this, tr("Send Coins"),
             tr("The amount to pay must be larger than 0."),
             QMessageBox::Ok, QMessageBox::Ok);
         ui->payAmount->setFocus();
         break;
-    case ClientModel::AmountExceedsBalance:
+    case WalletModel::AmountExceedsBalance:
         QMessageBox::warning(this, tr("Send Coins"),
             tr("Amount exceeds your balance"),
             QMessageBox::Ok, QMessageBox::Ok);
         ui->payAmount->setFocus();
         break;
-    case ClientModel::AmountWithFeeExceedsBalance:
+    case WalletModel::AmountWithFeeExceedsBalance:
         QMessageBox::warning(this, tr("Send Coins"),
             tr("Total exceeds your balance when the %1 transaction fee is included").
-                arg(QString::fromStdString(FormatMoney(model->getOptionsModel()->getTransactionFee()))),
+            arg(GUIUtil::formatMoney(model->getOptionsModel()->getTransactionFee())),
             QMessageBox::Ok, QMessageBox::Ok);
         ui->payAmount->setFocus();
         break;
-    case ClientModel::OK:
+    case WalletModel::OK:
         accept();
         break;
     }
@@ -94,20 +110,46 @@ void SendCoinsDialog::on_sendButton_clicked()
 
 void SendCoinsDialog::on_pasteButton_clicked()
 {
-    /* Paste text from clipboard into recipient field */
+    // Paste text from clipboard into recipient field
     ui->payTo->setText(QApplication::clipboard()->text());
 }
 
 void SendCoinsDialog::on_addressBookButton_clicked()
 {
-    AddressBookDialog dlg;
+    AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
     dlg.setModel(model->getAddressTableModel());
-    dlg.setTab(AddressBookDialog::SendingTab);
-    dlg.exec();
-    ui->payTo->setText(dlg.getReturnValue());
+    if(dlg.exec())
+    {
+        ui->payTo->setText(dlg.getReturnValue());
+        ui->payAmount->setFocus();
+    }
 }
 
 void SendCoinsDialog::on_buttonBox_rejected()
 {
     reject();
 }
+
+void SendCoinsDialog::on_payTo_textChanged(const QString &address)
+{
+    ui->addAsLabel->setText(model->getAddressTableModel()->labelForAddress(address));
+}
+
+void SendCoinsDialog::clear()
+{
+    ui->payTo->setText(QString());
+    ui->addAsLabel->setText(QString());
+    ui->payAmount->setText(QString());
+    ui->payTo->setFocus();
+    ui->sendButton->setDefault(true);
+}
+
+void SendCoinsDialog::reject()
+{
+    clear();
+}
+
+void SendCoinsDialog::accept()
+{
+    clear();
+}