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