convert to full tab-based ui
[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 "addressbookpage.h"
7 #include "optionsmodel.h"
8
9 #include <QApplication>
10 #include <QClipboard>
11 #include <QMessageBox>
12 #include <QLocale>
13 #include <QDebug>
14 #include <QMessageBox>
15
16 SendCoinsDialog::SendCoinsDialog(QWidget *parent, const QString &address) :
17     QDialog(parent),
18     ui(new Ui::SendCoinsDialog),
19     model(0)
20 {
21     ui->setupUi(this);
22 #if QT_VERSION >= 0x040700
23     ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
24 #endif
25     GUIUtil::setupAddressWidget(ui->payTo, this);
26
27     // Set initial send-to address if provided
28     if(!address.isEmpty())
29     {
30         ui->payTo->setText(address);
31         ui->payAmount->setFocus();
32     }
33 }
34
35 void SendCoinsDialog::setModel(WalletModel *model)
36 {
37     this->model = model;
38 }
39
40 SendCoinsDialog::~SendCoinsDialog()
41 {
42     delete ui;
43 }
44
45 void SendCoinsDialog::on_sendButton_clicked()
46 {
47     bool valid;
48     QString payAmount = ui->payAmount->text();
49     QString label;
50     qint64 payAmountParsed;
51
52     valid = GUIUtil::parseMoney(payAmount, &payAmountParsed);
53
54     if(!valid || payAmount.isEmpty())
55     {
56         QMessageBox::warning(this, tr("Send Coins"),
57             tr("Must fill in an amount to pay."),
58             QMessageBox::Ok, QMessageBox::Ok);
59         return;
60     }
61
62     // Add address to address book under label, if specified
63     label = ui->addAsLabel->text();
64
65     QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
66                           tr("Are you sure you want to send %1 BTC to %2 (%3)?").arg(GUIUtil::formatMoney(payAmountParsed), label, ui->payTo->text()),
67           QMessageBox::Yes|QMessageBox::Cancel,
68           QMessageBox::Cancel);
69
70     if(retval != QMessageBox::Yes)
71     {
72         return;
73     }
74
75     switch(model->sendCoins(ui->payTo->text(), payAmountParsed, label))
76     {
77     case WalletModel::InvalidAddress:
78         QMessageBox::warning(this, tr("Send Coins"),
79             tr("The recepient address is not valid, please recheck."),
80             QMessageBox::Ok, QMessageBox::Ok);
81         ui->payTo->setFocus();
82         break;
83     case WalletModel::InvalidAmount:
84         QMessageBox::warning(this, tr("Send Coins"),
85             tr("The amount to pay must be larger than 0."),
86             QMessageBox::Ok, QMessageBox::Ok);
87         ui->payAmount->setFocus();
88         break;
89     case WalletModel::AmountExceedsBalance:
90         QMessageBox::warning(this, tr("Send Coins"),
91             tr("Amount exceeds your balance"),
92             QMessageBox::Ok, QMessageBox::Ok);
93         ui->payAmount->setFocus();
94         break;
95     case WalletModel::AmountWithFeeExceedsBalance:
96         QMessageBox::warning(this, tr("Send Coins"),
97             tr("Total exceeds your balance when the %1 transaction fee is included").
98             arg(GUIUtil::formatMoney(model->getOptionsModel()->getTransactionFee())),
99             QMessageBox::Ok, QMessageBox::Ok);
100         ui->payAmount->setFocus();
101         break;
102     case WalletModel::OK:
103         accept();
104         break;
105     }
106 }
107
108 void SendCoinsDialog::on_pasteButton_clicked()
109 {
110     // Paste text from clipboard into recipient field
111     ui->payTo->setText(QApplication::clipboard()->text());
112 }
113
114 void SendCoinsDialog::on_addressBookButton_clicked()
115 {
116     AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab);
117     dlg.setModel(model->getAddressTableModel());
118     dlg.exec();
119     ui->payTo->setText(dlg.getReturnValue());
120     ui->payAmount->setFocus();
121 }
122
123 void SendCoinsDialog::on_buttonBox_rejected()
124 {
125     reject();
126 }
127
128 void SendCoinsDialog::on_payTo_textChanged(const QString &address)
129 {
130     ui->addAsLabel->setText(model->labelForAddress(address));
131 }
132
133 void SendCoinsDialog::clear()
134 {
135     ui->payTo->setText(QString());
136     ui->addAsLabel->setText(QString());
137     ui->payAmount->setText(QString());
138     ui->payTo->setFocus();
139 }
140
141 void SendCoinsDialog::reject()
142 {
143     clear();
144 }
145
146 void SendCoinsDialog::accept()
147 {
148     clear();
149 }