Merge pull request #853 from laanwj/2012_02_altminimizetray
[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     if(!model)
48         return;
49     AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
50     dlg.setModel(model->getAddressTableModel());
51     if(dlg.exec())
52     {
53         ui->payTo->setText(dlg.getReturnValue());
54         ui->payAmount->setFocus();
55     }
56 }
57
58 void SendCoinsEntry::on_payTo_textChanged(const QString &address)
59 {
60     if(!model)
61         return;
62     // Fill in label from address book, if no label is filled in yet
63     if(ui->addAsLabel->text().isEmpty())
64         ui->addAsLabel->setText(model->getAddressTableModel()->labelForAddress(address));}
65
66 void SendCoinsEntry::setModel(WalletModel *model)
67 {
68     this->model = model;
69 }
70
71 void SendCoinsEntry::setRemoveEnabled(bool enabled)
72 {
73     ui->deleteButton->setEnabled(enabled);
74 }
75
76 void SendCoinsEntry::clear()
77 {
78     ui->payTo->clear();
79     ui->addAsLabel->clear();
80     ui->payAmount->clear();
81     ui->payTo->setFocus();
82     if(model && model->getOptionsModel())
83     {
84         ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
85     }
86 }
87
88 void SendCoinsEntry::on_deleteButton_clicked()
89 {
90     emit removeEntry(this);
91 }
92
93 bool SendCoinsEntry::validate()
94 {
95     // Check input validity
96     bool retval = true;
97
98     if(!ui->payAmount->validate())
99     {
100         retval = false;
101     }
102     else
103     {
104         if(ui->payAmount->value() <= 0)
105         {
106             // Cannot send 0 coins or less
107             ui->payAmount->setValid(false);
108             retval = false;
109         }
110     }
111
112     if(!ui->payTo->hasAcceptableInput() ||
113        (model && !model->validateAddress(ui->payTo->text())))
114     {
115         ui->payTo->setValid(false);
116         retval = false;
117     }
118
119     return retval;
120 }
121
122 SendCoinsRecipient SendCoinsEntry::getValue()
123 {
124     SendCoinsRecipient rv;
125
126     rv.address = ui->payTo->text();
127     rv.label = ui->addAsLabel->text();
128     rv.amount = ui->payAmount->value();
129
130     return rv;
131 }
132
133 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
134 {
135     QWidget::setTabOrder(prev, ui->payTo);
136     QWidget::setTabOrder(ui->payTo, ui->addressBookButton);
137     QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
138     QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
139     QWidget::setTabOrder(ui->deleteButton, ui->addAsLabel);
140     return ui->payAmount->setupTabChain(ui->addAsLabel);
141 }
142
143 void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
144 {
145     ui->payTo->setText(value.address);
146     ui->addAsLabel->setText(value.label);
147     ui->payAmount->setValue(value.amount);
148 }
149
150 bool SendCoinsEntry::isClear()
151 {
152     return ui->payTo->text().isEmpty();
153 }
154
155 void SendCoinsEntry::setFocus()
156 {
157     ui->payTo->setFocus();
158 }
159