Improved Mac experience; QDoubleSpinBox for BitcoinAmountField
[novacoin.git] / src / qt / sendcoinsentry.cpp
1 #include "sendcoinsentry.h"
2 #include "ui_sendcoinsentry.h"
3 #include "guiutil.h"
4 #include "bitcoinunits.h"
5 #include "addressbookpage.h"
6 #include "walletmodel.h"
7 #include "optionsmodel.h"
8 #include "addresstablemodel.h"
9
10 #include <QApplication>
11 #include <QClipboard>
12
13 SendCoinsEntry::SendCoinsEntry(QWidget *parent) :
14     QFrame(parent),
15     ui(new Ui::SendCoinsEntry),
16     model(0)
17 {
18     ui->setupUi(this);
19
20 #ifdef Q_WS_MAC
21     ui->payToLayout->setSpacing(4);
22 #endif
23
24 #if QT_VERSION >= 0x040700
25     ui->payTo->setPlaceholderText(tr("Enter a Bitcoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L)"));
26     ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
27 #endif
28     setFocusPolicy(Qt::TabFocus);
29     setFocusProxy(ui->payTo);
30
31     GUIUtil::setupAddressWidget(ui->payTo, this);
32 }
33
34 SendCoinsEntry::~SendCoinsEntry()
35 {
36     delete ui;
37 }
38
39 void SendCoinsEntry::on_pasteButton_clicked()
40 {
41     // Paste text from clipboard into recipient field
42     ui->payTo->setText(QApplication::clipboard()->text());
43 }
44
45 void SendCoinsEntry::on_addressBookButton_clicked()
46 {
47     AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
48     dlg.setModel(model->getAddressTableModel());
49     if(dlg.exec())
50     {
51         ui->payTo->setText(dlg.getReturnValue());
52         ui->payAmount->setFocus();
53     }
54 }
55
56 void SendCoinsEntry::on_payTo_textChanged(const QString &address)
57 {
58     ui->addAsLabel->setText(model->getAddressTableModel()->labelForAddress(address));
59 }
60
61 void SendCoinsEntry::setModel(WalletModel *model)
62 {
63     this->model = model;
64 }
65
66 void SendCoinsEntry::setRemoveEnabled(bool enabled)
67 {
68     ui->deleteButton->setEnabled(enabled);
69 }
70
71 void SendCoinsEntry::clear()
72 {
73     ui->payTo->clear();
74     ui->addAsLabel->clear();
75     ui->payAmount->clear();
76     ui->payTo->setFocus();
77     if(model)
78     {
79         ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
80     }
81 }
82
83 void SendCoinsEntry::on_deleteButton_clicked()
84 {
85     emit removeEntry(this);
86 }
87
88 bool SendCoinsEntry::validate()
89 {
90     // Check input validity
91     bool retval = true;
92
93     if(!ui->payAmount->validate())
94     {
95         retval = false;
96     }
97     else
98     {
99         if(ui->payAmount->value() <= 0)
100         {
101             // Cannot send 0 coins or less
102             ui->payAmount->setValid(false);
103             retval = false;
104         }
105     }
106
107     if(!ui->payTo->hasAcceptableInput() ||
108        (model && !model->validateAddress(ui->payTo->text())))
109     {
110         ui->payTo->setValid(false);
111         retval = false;
112     }
113
114     return retval;
115 }
116
117 SendCoinsRecipient SendCoinsEntry::getValue()
118 {
119     SendCoinsRecipient rv;
120
121     rv.address = ui->payTo->text();
122     rv.label = ui->addAsLabel->text();
123     rv.amount = ui->payAmount->value();
124
125     return rv;
126 }
127
128 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
129 {
130     QWidget::setTabOrder(prev, ui->payTo);
131     QWidget::setTabOrder(ui->payTo, ui->addressBookButton);
132     QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
133     QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
134     QWidget::setTabOrder(ui->deleteButton, ui->addAsLabel);
135     return ui->payAmount->setupTabChain(ui->addAsLabel);
136 }
137
138 void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
139 {
140     ui->payTo->setText(value.address);
141     ui->addAsLabel->setText(value.label);
142     ui->payAmount->setValue(value.amount);
143 }
144
145 bool SendCoinsEntry::isClear()
146 {
147     return ui->payTo->text().isEmpty();
148 }
149