Improved Mac experience; QDoubleSpinBox for BitcoinAmountField
[novacoin.git] / src / qt / sendcoinsdialog.cpp
1 #include "sendcoinsdialog.h"
2 #include "ui_sendcoinsdialog.h"
3 #include "walletmodel.h"
4 #include "bitcoinunits.h"
5 #include "addressbookpage.h"
6 #include "optionsmodel.h"
7 #include "sendcoinsentry.h"
8 #include "guiutil.h"
9 #include "askpassphrasedialog.h"
10
11 #include <QMessageBox>
12 #include <QLocale>
13 #include <QTextDocument>
14
15 SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
16     QDialog(parent),
17     ui(new Ui::SendCoinsDialog),
18     model(0)
19 {
20     ui->setupUi(this);
21
22 #ifdef Q_WS_MAC // Icons on push buttons are very uncommon on Mac
23     ui->addButton->setIcon(QIcon());
24     ui->clearButton->setIcon(QIcon());
25     ui->sendButton->setIcon(QIcon());
26 #endif
27
28     addEntry();
29
30     connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
31     connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
32 }
33
34 void SendCoinsDialog::setModel(WalletModel *model)
35 {
36     this->model = model;
37
38     for(int i = 0; i < ui->entries->count(); ++i)
39     {
40         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
41         if(entry)
42         {
43             entry->setModel(model);
44         }
45     }
46
47     setBalance(model->getBalance(), model->getUnconfirmedBalance());
48     connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64)));
49 }
50
51 SendCoinsDialog::~SendCoinsDialog()
52 {
53     delete ui;
54 }
55
56 void SendCoinsDialog::on_sendButton_clicked()
57 {
58     QList<SendCoinsRecipient> recipients;
59     bool valid = true;
60     for(int i = 0; i < ui->entries->count(); ++i)
61     {
62         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
63         if(entry)
64         {
65             if(entry->validate())
66             {
67                 recipients.append(entry->getValue());
68             }
69             else
70             {
71                 valid = false;
72             }
73         }
74     }
75
76     if(!valid || recipients.isEmpty())
77     {
78         return;
79     }
80
81     // Format confirmation message
82     QStringList formatted;
83     foreach(const SendCoinsRecipient &rcp, recipients)
84     {
85         formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
86     }
87
88     QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
89                           tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
90           QMessageBox::Yes|QMessageBox::Cancel,
91           QMessageBox::Cancel);
92
93     if(retval != QMessageBox::Yes)
94     {
95         return;
96     }
97
98     WalletModel::UnlockContext ctx(model->requestUnlock());
99     if(!ctx.isValid())
100     {
101         // Unlock wallet was cancelled
102         return;
103     }
104
105     WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
106     switch(sendstatus.status)
107     {
108     case WalletModel::InvalidAddress:
109         QMessageBox::warning(this, tr("Send Coins"),
110             tr("The recepient address is not valid, please recheck."),
111             QMessageBox::Ok, QMessageBox::Ok);
112         break;
113     case WalletModel::InvalidAmount:
114         QMessageBox::warning(this, tr("Send Coins"),
115             tr("The amount to pay must be larger than 0."),
116             QMessageBox::Ok, QMessageBox::Ok);
117         break;
118     case WalletModel::AmountExceedsBalance:
119         QMessageBox::warning(this, tr("Send Coins"),
120             tr("Amount exceeds your balance"),
121             QMessageBox::Ok, QMessageBox::Ok);
122         break;
123     case WalletModel::AmountWithFeeExceedsBalance:
124         QMessageBox::warning(this, tr("Send Coins"),
125             tr("Total exceeds your balance when the %1 transaction fee is included").
126             arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
127             QMessageBox::Ok, QMessageBox::Ok);
128         break;
129     case WalletModel::DuplicateAddress:
130         QMessageBox::warning(this, tr("Send Coins"),
131             tr("Duplicate address found, can only send to each address once in one send operation"),
132             QMessageBox::Ok, QMessageBox::Ok);
133         break;
134     case WalletModel::TransactionCreationFailed:
135         QMessageBox::warning(this, tr("Send Coins"),
136             tr("Error: Transaction creation failed  "),
137             QMessageBox::Ok, QMessageBox::Ok);
138         break;
139     case WalletModel::TransactionCommitFailed:
140         QMessageBox::warning(this, tr("Send Coins"),
141             tr("Error: The transaction was rejected.  This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here."),
142             QMessageBox::Ok, QMessageBox::Ok);
143         break;
144     case WalletModel::OK:
145         accept();
146         break;
147     }
148 }
149
150 void SendCoinsDialog::clear()
151 {
152     // Remove entries until only one left
153     while(ui->entries->count())
154     {
155         delete ui->entries->takeAt(0)->widget();
156     }
157     addEntry();
158
159     updateRemoveEnabled();
160
161     ui->sendButton->setDefault(true);
162 }
163
164 void SendCoinsDialog::reject()
165 {
166     clear();
167 }
168
169 void SendCoinsDialog::accept()
170 {
171     clear();
172 }
173
174 SendCoinsEntry *SendCoinsDialog::addEntry()
175 {
176     SendCoinsEntry *entry = new SendCoinsEntry(this);
177     entry->setModel(model);
178     ui->entries->addWidget(entry);
179     connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
180
181     updateRemoveEnabled();
182
183     // Focus the field, so that entry can start immediately
184     entry->clear();
185     return entry;
186 }
187
188 void SendCoinsDialog::updateRemoveEnabled()
189 {
190     // Remove buttons are enabled as soon as there is more than one send-entry
191     bool enabled = (ui->entries->count() > 1);
192     for(int i = 0; i < ui->entries->count(); ++i)
193     {
194         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
195         if(entry)
196         {
197             entry->setRemoveEnabled(enabled);
198         }
199     }
200     setupTabChain(0);
201 }
202
203 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
204 {
205     delete entry;
206     updateRemoveEnabled();
207 }
208
209 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
210 {
211     for(int i = 0; i < ui->entries->count(); ++i)
212     {
213         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
214         if(entry)
215         {
216             prev = entry->setupTabChain(prev);
217         }
218     }
219     QWidget::setTabOrder(prev, ui->addButton);
220     QWidget::setTabOrder(ui->addButton, ui->sendButton);
221     return ui->sendButton;
222 }
223
224 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
225 {
226     SendCoinsEntry *entry = 0;
227     // Replace the first entry if it is still unused
228     if(ui->entries->count() == 1)
229     {
230         SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
231         if(first->isClear())
232         {
233             entry = first;
234         }
235     }
236     if(!entry)
237     {
238         entry = addEntry();
239     }
240
241     entry->setValue(rv);
242 }
243
244
245 void SendCoinsDialog::handleURL(const QUrl *url)
246 {
247     SendCoinsRecipient rv;
248     if(!GUIUtil::parseBitcoinURL(url, &rv))
249     {
250         return;
251     }
252     pasteEntry(rv);
253 }
254
255 void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance)
256 {
257     Q_UNUSED(unconfirmedBalance);
258     int unit = model->getOptionsModel()->getDisplayUnit();
259     ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));
260 }