Implement filter row instead of tabs, allows for more expressive filters
[novacoin.git] / src / qt / sendcoinsdialog.cpp
1 #include "sendcoinsdialog.h"
2 #include "ui_sendcoinsdialog.h"
3 #include "clientmodel.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(ClientModel *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)
52     {
53         QMessageBox::warning(this, tr("Send Coins"),
54             tr("The amount to pay must be a valid number."),
55             QMessageBox::Ok, QMessageBox::Ok);
56         return;
57     }
58
59     if(ui->addToAddressBook->isChecked())
60     {
61         // Add address to address book under label, if specified
62         label = ui->addAsLabel->text();
63     }
64
65     switch(model->sendCoins(ui->payTo->text(), payAmountParsed, label))
66     {
67     case ClientModel::InvalidAddress:
68         QMessageBox::warning(this, tr("Send Coins"),
69             tr("The recepient address is not valid, please recheck."),
70             QMessageBox::Ok, QMessageBox::Ok);
71         ui->payTo->setFocus();
72         break;
73     case ClientModel::InvalidAmount:
74         QMessageBox::warning(this, tr("Send Coins"),
75             tr("The amount to pay must be larger than 0."),
76             QMessageBox::Ok, QMessageBox::Ok);
77         ui->payAmount->setFocus();
78         break;
79     case ClientModel::AmountExceedsBalance:
80         QMessageBox::warning(this, tr("Send Coins"),
81             tr("Amount exceeds your balance"),
82             QMessageBox::Ok, QMessageBox::Ok);
83         ui->payAmount->setFocus();
84         break;
85     case ClientModel::AmountWithFeeExceedsBalance:
86         QMessageBox::warning(this, tr("Send Coins"),
87             tr("Total exceeds your balance when the %1 transaction fee is included").
88             arg(GUIUtil::formatMoney(model->getOptionsModel()->getTransactionFee())),
89             QMessageBox::Ok, QMessageBox::Ok);
90         ui->payAmount->setFocus();
91         break;
92     case ClientModel::OK:
93         accept();
94         break;
95     }
96 }
97
98 void SendCoinsDialog::on_pasteButton_clicked()
99 {
100     // Paste text from clipboard into recipient field
101     ui->payTo->setText(QApplication::clipboard()->text());
102 }
103
104 void SendCoinsDialog::on_addressBookButton_clicked()
105 {
106     AddressBookDialog dlg(AddressBookDialog::ForSending);
107     dlg.setModel(model->getAddressTableModel());
108     dlg.setTab(AddressBookDialog::SendingTab);
109     dlg.exec();
110     ui->payTo->setText(dlg.getReturnValue());
111 }
112
113 void SendCoinsDialog::on_buttonBox_rejected()
114 {
115     reject();
116 }
117
118 void SendCoinsDialog::on_addToAddressBook_toggled(bool checked)
119 {
120     ui->addAsLabel->setEnabled(checked);
121 }