1 // Copyright (c) 2011-2015 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "receivecoinsdialog.h"
6 #include "ui_receivecoinsdialog.h"
8 #include "addressbookpage.h"
9 #include "addresstablemodel.h"
10 #include "bitcoinunits.h"
12 #include "optionsmodel.h"
13 #include "platformstyle.h"
14 #include "receiverequestdialog.h"
15 #include "recentrequeststablemodel.h"
16 #include "walletmodel.h"
20 #include <QItemSelection>
21 #include <QMessageBox>
23 #include <QTextDocument>
25 ReceiveCoinsDialog::ReceiveCoinsDialog(const PlatformStyle
*platformStyle
, QWidget
*parent
) :
27 ui(new Ui::ReceiveCoinsDialog
),
29 platformStyle(platformStyle
)
33 if (!platformStyle
->getImagesOnButtons()) {
34 ui
->clearButton
->setIcon(QIcon());
35 ui
->receiveButton
->setIcon(QIcon());
36 ui
->showRequestButton
->setIcon(QIcon());
37 ui
->removeRequestButton
->setIcon(QIcon());
39 ui
->clearButton
->setIcon(platformStyle
->SingleColorIcon(":/icons/remove"));
40 ui
->receiveButton
->setIcon(platformStyle
->SingleColorIcon(":/icons/receiving_addresses"));
41 ui
->showRequestButton
->setIcon(platformStyle
->SingleColorIcon(":/icons/edit"));
42 ui
->removeRequestButton
->setIcon(platformStyle
->SingleColorIcon(":/icons/remove"));
45 // context menu actions
46 QAction
*copyLabelAction
= new QAction(tr("Copy label"), this);
47 QAction
*copyMessageAction
= new QAction(tr("Copy message"), this);
48 QAction
*copyAmountAction
= new QAction(tr("Copy amount"), this);
51 contextMenu
= new QMenu();
52 contextMenu
->addAction(copyLabelAction
);
53 contextMenu
->addAction(copyMessageAction
);
54 contextMenu
->addAction(copyAmountAction
);
56 // context menu signals
57 connect(ui
->recentRequestsView
, SIGNAL(customContextMenuRequested(QPoint
)), this, SLOT(showMenu(QPoint
)));
58 connect(copyLabelAction
, SIGNAL(triggered()), this, SLOT(copyLabel()));
59 connect(copyMessageAction
, SIGNAL(triggered()), this, SLOT(copyMessage()));
60 connect(copyAmountAction
, SIGNAL(triggered()), this, SLOT(copyAmount()));
62 connect(ui
->clearButton
, SIGNAL(clicked()), this, SLOT(clear()));
65 void ReceiveCoinsDialog::setModel(WalletModel
*model
)
69 if(model
&& model
->getOptionsModel())
71 model
->getRecentRequestsTableModel()->sort(RecentRequestsTableModel::Date
, Qt::DescendingOrder
);
72 connect(model
->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
75 QTableView
* tableView
= ui
->recentRequestsView
;
77 tableView
->verticalHeader()->hide();
78 tableView
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
79 tableView
->setModel(model
->getRecentRequestsTableModel());
80 tableView
->setAlternatingRowColors(true);
81 tableView
->setSelectionBehavior(QAbstractItemView::SelectRows
);
82 tableView
->setSelectionMode(QAbstractItemView::ContiguousSelection
);
83 tableView
->setColumnWidth(RecentRequestsTableModel::Date
, DATE_COLUMN_WIDTH
);
84 tableView
->setColumnWidth(RecentRequestsTableModel::Label
, LABEL_COLUMN_WIDTH
);
85 tableView
->setColumnWidth(RecentRequestsTableModel::Amount
, AMOUNT_MINIMUM_COLUMN_WIDTH
);
87 connect(tableView
->selectionModel(),
88 SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)), this,
89 SLOT(recentRequestsView_selectionChanged(QItemSelection
, QItemSelection
)));
90 // Last 2 columns are set by the columnResizingFixer, when the table geometry is ready.
91 columnResizingFixer
= new GUIUtil::TableViewLastColumnResizingFixer(tableView
, AMOUNT_MINIMUM_COLUMN_WIDTH
, DATE_COLUMN_WIDTH
);
95 ReceiveCoinsDialog::~ReceiveCoinsDialog()
100 void ReceiveCoinsDialog::clear()
102 ui
->reqAmount
->clear();
103 ui
->reqLabel
->setText("");
104 ui
->reqMessage
->setText("");
105 ui
->reuseAddress
->setChecked(false);
109 void ReceiveCoinsDialog::reject()
114 void ReceiveCoinsDialog::accept()
119 void ReceiveCoinsDialog::updateDisplayUnit()
121 if(model
&& model
->getOptionsModel())
123 ui
->reqAmount
->setDisplayUnit(model
->getOptionsModel()->getDisplayUnit());
127 void ReceiveCoinsDialog::on_receiveButton_clicked()
129 if(!model
|| !model
->getOptionsModel() || !model
->getAddressTableModel() || !model
->getRecentRequestsTableModel())
133 QString label
= ui
->reqLabel
->text();
134 if(ui
->reuseAddress
->isChecked())
136 /* Choose existing receiving address */
137 AddressBookPage
dlg(platformStyle
, AddressBookPage::ForSelection
, AddressBookPage::ReceivingTab
, this);
138 dlg
.setModel(model
->getAddressTableModel());
141 address
= dlg
.getReturnValue();
142 if(label
.isEmpty()) /* If no label provided, use the previously used label */
144 label
= model
->getAddressTableModel()->labelForAddress(address
);
150 /* Generate new receiving address */
151 address
= model
->getAddressTableModel()->addRow(AddressTableModel::Receive
, label
, "");
153 SendCoinsRecipient
info(address
, label
,
154 ui
->reqAmount
->value(), ui
->reqMessage
->text());
155 ReceiveRequestDialog
*dialog
= new ReceiveRequestDialog(this);
156 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
157 dialog
->setModel(model
->getOptionsModel());
158 dialog
->setInfo(info
);
162 /* Store request for later reference */
163 model
->getRecentRequestsTableModel()->addNewRequest(info
);
166 void ReceiveCoinsDialog::on_recentRequestsView_doubleClicked(const QModelIndex
&index
)
168 const RecentRequestsTableModel
*submodel
= model
->getRecentRequestsTableModel();
169 ReceiveRequestDialog
*dialog
= new ReceiveRequestDialog(this);
170 dialog
->setModel(model
->getOptionsModel());
171 dialog
->setInfo(submodel
->entry(index
.row()).recipient
);
172 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
176 void ReceiveCoinsDialog::recentRequestsView_selectionChanged(const QItemSelection
&selected
, const QItemSelection
&deselected
)
178 // Enable Show/Remove buttons only if anything is selected.
179 bool enable
= !ui
->recentRequestsView
->selectionModel()->selectedRows().isEmpty();
180 ui
->showRequestButton
->setEnabled(enable
);
181 ui
->removeRequestButton
->setEnabled(enable
);
184 void ReceiveCoinsDialog::on_showRequestButton_clicked()
186 if(!model
|| !model
->getRecentRequestsTableModel() || !ui
->recentRequestsView
->selectionModel())
188 QModelIndexList selection
= ui
->recentRequestsView
->selectionModel()->selectedRows();
190 Q_FOREACH (const QModelIndex
& index
, selection
) {
191 on_recentRequestsView_doubleClicked(index
);
195 void ReceiveCoinsDialog::on_removeRequestButton_clicked()
197 if(!model
|| !model
->getRecentRequestsTableModel() || !ui
->recentRequestsView
->selectionModel())
199 QModelIndexList selection
= ui
->recentRequestsView
->selectionModel()->selectedRows();
200 if(selection
.empty())
202 // correct for selection mode ContiguousSelection
203 QModelIndex firstIndex
= selection
.at(0);
204 model
->getRecentRequestsTableModel()->removeRows(firstIndex
.row(), selection
.length(), firstIndex
.parent());
207 // We override the virtual resizeEvent of the QWidget to adjust tables column
208 // sizes as the tables width is proportional to the dialogs width.
209 void ReceiveCoinsDialog::resizeEvent(QResizeEvent
*event
)
211 QWidget::resizeEvent(event
);
212 columnResizingFixer
->stretchColumnWidth(RecentRequestsTableModel::Message
);
215 void ReceiveCoinsDialog::keyPressEvent(QKeyEvent
*event
)
217 if (event
->key() == Qt::Key_Return
)
219 // press return -> submit form
220 if (ui
->reqLabel
->hasFocus() || ui
->reqAmount
->hasFocus() || ui
->reqMessage
->hasFocus())
223 on_receiveButton_clicked();
228 this->QDialog::keyPressEvent(event
);
231 // copy column of selected row to clipboard
232 void ReceiveCoinsDialog::copyColumnToClipboard(int column
)
234 if(!model
|| !model
->getRecentRequestsTableModel() || !ui
->recentRequestsView
->selectionModel())
236 QModelIndexList selection
= ui
->recentRequestsView
->selectionModel()->selectedRows();
237 if(selection
.empty())
239 // correct for selection mode ContiguousSelection
240 QModelIndex firstIndex
= selection
.at(0);
241 GUIUtil::setClipboard(model
->getRecentRequestsTableModel()->data(firstIndex
.child(firstIndex
.row(), column
), Qt::EditRole
).toString());
245 void ReceiveCoinsDialog::showMenu(const QPoint
&point
)
247 if(!model
|| !model
->getRecentRequestsTableModel() || !ui
->recentRequestsView
->selectionModel())
249 QModelIndexList selection
= ui
->recentRequestsView
->selectionModel()->selectedRows();
250 if(selection
.empty())
252 contextMenu
->exec(QCursor::pos());
255 // context menu action: copy label
256 void ReceiveCoinsDialog::copyLabel()
258 copyColumnToClipboard(RecentRequestsTableModel::Label
);
261 // context menu action: copy message
262 void ReceiveCoinsDialog::copyMessage()
264 copyColumnToClipboard(RecentRequestsTableModel::Message
);
267 // context menu action: copy amount
268 void ReceiveCoinsDialog::copyAmount()
270 copyColumnToClipboard(RecentRequestsTableModel::Amount
);