1 // Copyright (c) 2011-2016 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 <qt/receivecoinsdialog.h>
6 #include <qt/forms/ui_receivecoinsdialog.h>
8 #include <qt/addressbookpage.h>
9 #include <qt/addresstablemodel.h>
10 #include <qt/bitcoinunits.h>
11 #include <qt/guiutil.h>
12 #include <qt/optionsmodel.h>
13 #include <qt/platformstyle.h>
14 #include <qt/receiverequestdialog.h>
15 #include <qt/recentrequeststablemodel.h>
16 #include <qt/walletmodel.h>
20 #include <QItemSelection>
21 #include <QMessageBox>
23 #include <QTextDocument>
25 ReceiveCoinsDialog::ReceiveCoinsDialog(const PlatformStyle
*_platformStyle
, QWidget
*parent
) :
27 ui(new Ui::ReceiveCoinsDialog
),
28 columnResizingFixer(0),
30 platformStyle(_platformStyle
)
34 if (!_platformStyle
->getImagesOnButtons()) {
35 ui
->clearButton
->setIcon(QIcon());
36 ui
->receiveButton
->setIcon(QIcon());
37 ui
->showRequestButton
->setIcon(QIcon());
38 ui
->removeRequestButton
->setIcon(QIcon());
40 ui
->clearButton
->setIcon(_platformStyle
->SingleColorIcon(":/icons/remove"));
41 ui
->receiveButton
->setIcon(_platformStyle
->SingleColorIcon(":/icons/receiving_addresses"));
42 ui
->showRequestButton
->setIcon(_platformStyle
->SingleColorIcon(":/icons/edit"));
43 ui
->removeRequestButton
->setIcon(_platformStyle
->SingleColorIcon(":/icons/remove"));
46 // context menu actions
47 QAction
*copyURIAction
= new QAction(tr("Copy URI"), this);
48 QAction
*copyLabelAction
= new QAction(tr("Copy label"), this);
49 QAction
*copyMessageAction
= new QAction(tr("Copy message"), this);
50 QAction
*copyAmountAction
= new QAction(tr("Copy amount"), this);
53 contextMenu
= new QMenu(this);
54 contextMenu
->addAction(copyURIAction
);
55 contextMenu
->addAction(copyLabelAction
);
56 contextMenu
->addAction(copyMessageAction
);
57 contextMenu
->addAction(copyAmountAction
);
59 // context menu signals
60 connect(ui
->recentRequestsView
, SIGNAL(customContextMenuRequested(QPoint
)), this, SLOT(showMenu(QPoint
)));
61 connect(copyURIAction
, SIGNAL(triggered()), this, SLOT(copyURI()));
62 connect(copyLabelAction
, SIGNAL(triggered()), this, SLOT(copyLabel()));
63 connect(copyMessageAction
, SIGNAL(triggered()), this, SLOT(copyMessage()));
64 connect(copyAmountAction
, SIGNAL(triggered()), this, SLOT(copyAmount()));
66 connect(ui
->clearButton
, SIGNAL(clicked()), this, SLOT(clear()));
69 void ReceiveCoinsDialog::setModel(WalletModel
*_model
)
73 if(_model
&& _model
->getOptionsModel())
75 _model
->getRecentRequestsTableModel()->sort(RecentRequestsTableModel::Date
, Qt::DescendingOrder
);
76 connect(_model
->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
79 QTableView
* tableView
= ui
->recentRequestsView
;
81 tableView
->verticalHeader()->hide();
82 tableView
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
83 tableView
->setModel(_model
->getRecentRequestsTableModel());
84 tableView
->setAlternatingRowColors(true);
85 tableView
->setSelectionBehavior(QAbstractItemView::SelectRows
);
86 tableView
->setSelectionMode(QAbstractItemView::ContiguousSelection
);
87 tableView
->setColumnWidth(RecentRequestsTableModel::Date
, DATE_COLUMN_WIDTH
);
88 tableView
->setColumnWidth(RecentRequestsTableModel::Label
, LABEL_COLUMN_WIDTH
);
89 tableView
->setColumnWidth(RecentRequestsTableModel::Amount
, AMOUNT_MINIMUM_COLUMN_WIDTH
);
91 connect(tableView
->selectionModel(),
92 SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)), this,
93 SLOT(recentRequestsView_selectionChanged(QItemSelection
, QItemSelection
)));
94 // Last 2 columns are set by the columnResizingFixer, when the table geometry is ready.
95 columnResizingFixer
= new GUIUtil::TableViewLastColumnResizingFixer(tableView
, AMOUNT_MINIMUM_COLUMN_WIDTH
, DATE_COLUMN_WIDTH
, this);
99 ReceiveCoinsDialog::~ReceiveCoinsDialog()
104 void ReceiveCoinsDialog::clear()
106 ui
->reqAmount
->clear();
107 ui
->reqLabel
->setText("");
108 ui
->reqMessage
->setText("");
112 void ReceiveCoinsDialog::reject()
117 void ReceiveCoinsDialog::accept()
122 void ReceiveCoinsDialog::updateDisplayUnit()
124 if(model
&& model
->getOptionsModel())
126 ui
->reqAmount
->setDisplayUnit(model
->getOptionsModel()->getDisplayUnit());
130 void ReceiveCoinsDialog::on_receiveButton_clicked()
132 if(!model
|| !model
->getOptionsModel() || !model
->getAddressTableModel() || !model
->getRecentRequestsTableModel())
136 QString label
= ui
->reqLabel
->text();
137 /* Generate new receiving address */
138 address
= model
->getAddressTableModel()->addRow(AddressTableModel::Receive
, label
, "");
139 SendCoinsRecipient
info(address
, label
,
140 ui
->reqAmount
->value(), ui
->reqMessage
->text());
141 ReceiveRequestDialog
*dialog
= new ReceiveRequestDialog(this);
142 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
143 dialog
->setModel(model
->getOptionsModel());
144 dialog
->setInfo(info
);
148 /* Store request for later reference */
149 model
->getRecentRequestsTableModel()->addNewRequest(info
);
152 void ReceiveCoinsDialog::on_recentRequestsView_doubleClicked(const QModelIndex
&index
)
154 const RecentRequestsTableModel
*submodel
= model
->getRecentRequestsTableModel();
155 ReceiveRequestDialog
*dialog
= new ReceiveRequestDialog(this);
156 dialog
->setModel(model
->getOptionsModel());
157 dialog
->setInfo(submodel
->entry(index
.row()).recipient
);
158 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
162 void ReceiveCoinsDialog::recentRequestsView_selectionChanged(const QItemSelection
&selected
, const QItemSelection
&deselected
)
164 // Enable Show/Remove buttons only if anything is selected.
165 bool enable
= !ui
->recentRequestsView
->selectionModel()->selectedRows().isEmpty();
166 ui
->showRequestButton
->setEnabled(enable
);
167 ui
->removeRequestButton
->setEnabled(enable
);
170 void ReceiveCoinsDialog::on_showRequestButton_clicked()
172 if(!model
|| !model
->getRecentRequestsTableModel() || !ui
->recentRequestsView
->selectionModel())
174 QModelIndexList selection
= ui
->recentRequestsView
->selectionModel()->selectedRows();
176 for (const QModelIndex
& index
: selection
) {
177 on_recentRequestsView_doubleClicked(index
);
181 void ReceiveCoinsDialog::on_removeRequestButton_clicked()
183 if(!model
|| !model
->getRecentRequestsTableModel() || !ui
->recentRequestsView
->selectionModel())
185 QModelIndexList selection
= ui
->recentRequestsView
->selectionModel()->selectedRows();
186 if(selection
.empty())
188 // correct for selection mode ContiguousSelection
189 QModelIndex firstIndex
= selection
.at(0);
190 model
->getRecentRequestsTableModel()->removeRows(firstIndex
.row(), selection
.length(), firstIndex
.parent());
193 // We override the virtual resizeEvent of the QWidget to adjust tables column
194 // sizes as the tables width is proportional to the dialogs width.
195 void ReceiveCoinsDialog::resizeEvent(QResizeEvent
*event
)
197 QWidget::resizeEvent(event
);
198 columnResizingFixer
->stretchColumnWidth(RecentRequestsTableModel::Message
);
201 void ReceiveCoinsDialog::keyPressEvent(QKeyEvent
*event
)
203 if (event
->key() == Qt::Key_Return
)
205 // press return -> submit form
206 if (ui
->reqLabel
->hasFocus() || ui
->reqAmount
->hasFocus() || ui
->reqMessage
->hasFocus())
209 on_receiveButton_clicked();
214 this->QDialog::keyPressEvent(event
);
217 QModelIndex
ReceiveCoinsDialog::selectedRow()
219 if(!model
|| !model
->getRecentRequestsTableModel() || !ui
->recentRequestsView
->selectionModel())
220 return QModelIndex();
221 QModelIndexList selection
= ui
->recentRequestsView
->selectionModel()->selectedRows();
222 if(selection
.empty())
223 return QModelIndex();
224 // correct for selection mode ContiguousSelection
225 QModelIndex firstIndex
= selection
.at(0);
229 // copy column of selected row to clipboard
230 void ReceiveCoinsDialog::copyColumnToClipboard(int column
)
232 QModelIndex firstIndex
= selectedRow();
233 if (!firstIndex
.isValid()) {
236 GUIUtil::setClipboard(model
->getRecentRequestsTableModel()->data(firstIndex
.child(firstIndex
.row(), column
), Qt::EditRole
).toString());
240 void ReceiveCoinsDialog::showMenu(const QPoint
&point
)
242 if (!selectedRow().isValid()) {
245 contextMenu
->exec(QCursor::pos());
248 // context menu action: copy URI
249 void ReceiveCoinsDialog::copyURI()
251 QModelIndex sel
= selectedRow();
252 if (!sel
.isValid()) {
256 const RecentRequestsTableModel
* const submodel
= model
->getRecentRequestsTableModel();
257 const QString uri
= GUIUtil::formatBitcoinURI(submodel
->entry(sel
.row()).recipient
);
258 GUIUtil::setClipboard(uri
);
261 // context menu action: copy label
262 void ReceiveCoinsDialog::copyLabel()
264 copyColumnToClipboard(RecentRequestsTableModel::Label
);
267 // context menu action: copy message
268 void ReceiveCoinsDialog::copyMessage()
270 copyColumnToClipboard(RecentRequestsTableModel::Message
);
273 // context menu action: copy amount
274 void ReceiveCoinsDialog::copyAmount()
276 copyColumnToClipboard(RecentRequestsTableModel::Amount
);