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