14d50963f2d1d3dd90ebfe92ebbe571dfd589efd
[novacoin.git] / src / qt / sendcoinsdialog.cpp
1 #include "sendcoinsdialog.h"
2 #include "ui_sendcoinsdialog.h"
3 #include "walletmodel.h"
4 #include "guiutil.h"
5
6 #include "addressbookdialog.h"
7 #include "optionsmodel.h"
8
9 #include <QApplication>
10 #include <QClipboard>
11 #include <QMessageBox>
12 #include <QLocale>
13 #include <QDebug>
14
15 SendCoinsDialog::SendCoinsDialog(QWidget *parent, const QString &address) :
16     QDialog(parent),
17     ui(new Ui::SendCoinsDialog),
18     model(0)
19 {
20     ui->setupUi(this);
21
22     GUIUtil::setupAddressWidget(ui->payTo, this);
23
24     // Set initial send-to address if provided
25     if(!address.isEmpty())
26     {
27         ui->payTo->setText(address);
28         ui->payAmount->setFocus();
29     }
30 }
31
32 void SendCoinsDialog::setModel(WalletModel *model)
33 {
34     this->model = model;
35 }
36
37 SendCoinsDialog::~SendCoinsDialog()
38 {
39     delete ui;
40 }
41
42 void SendCoinsDialog::on_sendButton_clicked()
43 {
44     bool valid;
45     QString payAmount = ui->payAmount->text();
46     QString label;
47     qint64 payAmountParsed;
48
49     valid = GUIUtil::parseMoney(payAmount, &payAmountParsed);
50
51     if(!valid || payAmount.isEmpty())
52     {
53         QMessageBox::warning(this, tr("Send Coins"),
54             tr("Must fill in an amount to pay."),
55             QMessageBox::Ok, QMessageBox::Ok);
56         return;
57     }
58
59     // Add address to address book under label, if specified
60     label = ui->addAsLabel->text();
61
62     switch(model->sendCoins(ui->payTo->text(), payAmountParsed, label))
63     {
64     case WalletModel::InvalidAddress:
65         QMessageBox::warning(this, tr("Send Coins"),
66             tr("The recepient address is not valid, please recheck."),
67             QMessageBox::Ok, QMessageBox::Ok);
68         ui->payTo->setFocus();
69         break;
70     case WalletModel::InvalidAmount:
71         QMessageBox::warning(this, tr("Send Coins"),
72             tr("The amount to pay must be larger than 0."),
73             QMessageBox::Ok, QMessageBox::Ok);
74         ui->payAmount->setFocus();
75         break;
76     case WalletModel::AmountExceedsBalance:
77         QMessageBox::warning(this, tr("Send Coins"),
78             tr("Amount exceeds your balance"),
79             QMessageBox::Ok, QMessageBox::Ok);
80         ui->payAmount->setFocus();
81         break;
82     case WalletModel::AmountWithFeeExceedsBalance:
83         QMessageBox::warning(this, tr("Send Coins"),
84             tr("Total exceeds your balance when the %1 transaction fee is included").
85             arg(GUIUtil::formatMoney(model->getOptionsModel()->getTransactionFee())),
86             QMessageBox::Ok, QMessageBox::Ok);
87         ui->payAmount->setFocus();
88         break;
89     case WalletModel::OK:
90         accept();
91         break;
92     }
93 }
94
95 void SendCoinsDialog::on_pasteButton_clicked()
96 {
97     // Paste text from clipboard into recipient field
98     ui->payTo->setText(QApplication::clipboard()->text());
99 }
100
101 void SendCoinsDialog::on_addressBookButton_clicked()
102 {
103     AddressBookDialog dlg(AddressBookDialog::ForSending);
104     dlg.setModel(model->getAddressTableModel());
105     dlg.setTab(AddressBookDialog::SendingTab);
106     dlg.exec();
107     ui->payTo->setText(dlg.getReturnValue());
108     ui->payAmount->setFocus();
109 }
110
111 void SendCoinsDialog::on_buttonBox_rejected()
112 {
113     reject();
114 }
115
116 void SendCoinsDialog::on_payTo_textChanged(const QString &address)
117 {
118     ui->addAsLabel->setText(model->labelForAddress(address));
119 }