Bitcoin ABC  0.24.7
P2P Digital Currency
receivecoinsdialog.cpp
Go to the documentation of this file.
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.
4 
5 #include <wallet/wallet.h>
6 
7 #include <qt/forms/ui_receivecoinsdialog.h>
9 
10 #include <qt/addresstablemodel.h>
11 #include <qt/optionsmodel.h>
12 #include <qt/platformstyle.h>
15 #include <qt/walletmodel.h>
16 
17 #include <QAction>
18 #include <QCursor>
19 #include <QMessageBox>
20 #include <QScrollBar>
21 #include <QTextDocument>
22 
24  QWidget *parent)
25  : QDialog(parent), ui(new Ui::ReceiveCoinsDialog),
26  columnResizingFixer(nullptr), model(nullptr),
27  platformStyle(_platformStyle) {
28  ui->setupUi(this);
29 
30  if (!_platformStyle->getImagesOnButtons()) {
31  ui->clearButton->setIcon(QIcon());
32  ui->receiveButton->setIcon(QIcon());
33  ui->showRequestButton->setIcon(QIcon());
34  ui->removeRequestButton->setIcon(QIcon());
35  } else {
36  ui->clearButton->setIcon(
37  _platformStyle->SingleColorIcon(":/icons/remove"));
38  ui->receiveButton->setIcon(
39  _platformStyle->SingleColorIcon(":/icons/receiving_addresses"));
40  ui->showRequestButton->setIcon(
41  _platformStyle->SingleColorIcon(":/icons/edit"));
42  ui->removeRequestButton->setIcon(
43  _platformStyle->SingleColorIcon(":/icons/remove"));
44  }
45 
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);
51 
52  // context menu
53  contextMenu = new QMenu(this);
54  contextMenu->addAction(copyURIAction);
55  contextMenu->addAction(copyLabelAction);
56  contextMenu->addAction(copyMessageAction);
57  contextMenu->addAction(copyAmountAction);
58 
59  // context menu signals
60  connect(ui->recentRequestsView, &QWidget::customContextMenuRequested, this,
62  connect(copyURIAction, &QAction::triggered, this,
64  connect(copyLabelAction, &QAction::triggered, this,
66  connect(copyMessageAction, &QAction::triggered, this,
68  connect(copyAmountAction, &QAction::triggered, this,
70 
71  connect(ui->clearButton, &QPushButton::clicked, this,
73 }
74 
76  this->model = _model;
77 
78  if (_model && _model->getOptionsModel()) {
80  RecentRequestsTableModel::Date, Qt::DescendingOrder);
84 
85  QTableView *tableView = ui->recentRequestsView;
86 
87  tableView->verticalHeader()->hide();
88  tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
89  tableView->setModel(_model->getRecentRequestsTableModel());
90  tableView->setAlternatingRowColors(true);
91  tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
92  tableView->setSelectionMode(QAbstractItemView::ContiguousSelection);
93  tableView->setColumnWidth(RecentRequestsTableModel::Date,
95  tableView->setColumnWidth(RecentRequestsTableModel::Label,
97  tableView->setColumnWidth(RecentRequestsTableModel::Amount,
99 
100  connect(tableView->selectionModel(),
101  &QItemSelectionModel::selectionChanged, this,
103  // Last 2 columns are set by the columnResizingFixer, when the table
104  // geometry is ready.
107 
108  // Set the button to be enabled or disabled based on whether the wallet
109  // can give out new addresses.
110  ui->receiveButton->setEnabled(model->wallet().canGetAddresses());
111 
112  // Enable/disable the receive button if the wallet is now able/unable to
113  // give out new addresses.
114  connect(model, &WalletModel::canGetAddressesChanged, [this] {
115  ui->receiveButton->setEnabled(model->wallet().canGetAddresses());
116  });
117  }
118 }
119 
121  delete ui;
122 }
123 
125  ui->reqAmount->clear();
126  ui->reqLabel->setText("");
127  ui->reqMessage->setText("");
129 }
130 
132  clear();
133 }
134 
136  clear();
137 }
138 
140  if (model && model->getOptionsModel()) {
141  ui->reqAmount->setDisplayUnit(
143  }
144 }
145 
149  return;
150  }
151 
152  QString address;
153  QString label = ui->reqLabel->text();
154  /* Generate new receiving address */
155  OutputType address_type = model->wallet().getDefaultAddressType();
157  label, "", address_type);
158 
159  switch (model->getAddressTableModel()->getEditStatus()) {
160  case AddressTableModel::EditStatus::OK: {
161  // Success
162  SendCoinsRecipient info(address, label, ui->reqAmount->value(),
163  ui->reqMessage->text());
164  ReceiveRequestDialog *dialog = new ReceiveRequestDialog(this);
165  dialog->setAttribute(Qt::WA_DeleteOnClose);
166  dialog->setModel(model);
167  dialog->setInfo(info);
168  dialog->show();
169 
170  /* Store request for later reference */
172  break;
173  }
174  case AddressTableModel::EditStatus::WALLET_UNLOCK_FAILURE:
175  QMessageBox::critical(this, windowTitle(),
176  tr("Could not unlock wallet."),
177  QMessageBox::Ok, QMessageBox::Ok);
178  break;
179  case AddressTableModel::EditStatus::KEY_GENERATION_FAILURE:
180  QMessageBox::critical(this, windowTitle(),
181  tr("Could not generate new %1 address")
182  .arg(QString::fromStdString(
183  FormatOutputType(address_type))),
184  QMessageBox::Ok, QMessageBox::Ok);
185  break;
186  // These aren't valid return values for our action
187  case AddressTableModel::EditStatus::INVALID_ADDRESS:
188  case AddressTableModel::EditStatus::DUPLICATE_ADDRESS:
189  case AddressTableModel::EditStatus::NO_CHANGES:
190  assert(false);
191  }
192  clear();
193 }
194 
196  const QModelIndex &index) {
197  const RecentRequestsTableModel *submodel =
199  ReceiveRequestDialog *dialog = new ReceiveRequestDialog(this);
200  dialog->setModel(model);
201  dialog->setInfo(submodel->entry(index.row()).recipient);
202  dialog->setAttribute(Qt::WA_DeleteOnClose);
203  dialog->show();
204 }
205 
207  const QItemSelection &selected, const QItemSelection &deselected) {
208  // Enable Show/Remove buttons only if anything is selected.
209  bool enable =
210  !ui->recentRequestsView->selectionModel()->selectedRows().isEmpty();
211  ui->showRequestButton->setEnabled(enable);
212  ui->removeRequestButton->setEnabled(enable);
213 }
214 
217  !ui->recentRequestsView->selectionModel()) {
218  return;
219  }
220  QModelIndexList selection =
221  ui->recentRequestsView->selectionModel()->selectedRows();
222 
223  for (const QModelIndex &index : selection) {
225  }
226 }
227 
230  !ui->recentRequestsView->selectionModel()) {
231  return;
232  }
233  QModelIndexList selection =
234  ui->recentRequestsView->selectionModel()->selectedRows();
235  if (selection.empty()) {
236  return;
237  }
238  // correct for selection mode ContiguousSelection
239  QModelIndex firstIndex = selection.at(0);
241  firstIndex.row(), selection.length(), firstIndex.parent());
242 }
243 
244 // We override the virtual resizeEvent of the QWidget to adjust tables column
245 // sizes as the tables width is proportional to the dialogs width.
246 void ReceiveCoinsDialog::resizeEvent(QResizeEvent *event) {
247  QWidget::resizeEvent(event);
249 }
250 
253  !ui->recentRequestsView->selectionModel()) {
254  return QModelIndex();
255  }
256  QModelIndexList selection =
257  ui->recentRequestsView->selectionModel()->selectedRows();
258  if (selection.empty()) {
259  return QModelIndex();
260  }
261  // correct for selection mode ContiguousSelection
262  QModelIndex firstIndex = selection.at(0);
263  return firstIndex;
264 }
265 
266 // copy column of selected row to clipboard
268  QModelIndex firstIndex = selectedRow();
269  if (!firstIndex.isValid()) {
270  return;
271  }
273  ->index(firstIndex.row(), column)
274  .data(Qt::EditRole)
275  .toString());
276 }
277 
278 // context menu
279 void ReceiveCoinsDialog::showMenu(const QPoint &point) {
280  if (!selectedRow().isValid()) {
281  return;
282  }
283  contextMenu->exec(QCursor::pos());
284 }
285 
286 // context menu action: copy URI
288  QModelIndex sel = selectedRow();
289  if (!sel.isValid()) {
290  return;
291  }
292 
293  const RecentRequestsTableModel *const submodel =
295  const QString uri =
296  GUIUtil::formatBitcoinURI(submodel->entry(sel.row()).recipient);
298 }
299 
300 // context menu action: copy label
303 }
304 
305 // context menu action: copy message
308 }
309 
310 // context menu action: copy amount
313 }
ReceiveCoinsDialog::AMOUNT_MINIMUM_COLUMN_WIDTH
@ AMOUNT_MINIMUM_COLUMN_WIDTH
Definition: receivecoinsdialog.h:37
WalletModel::getOptionsModel
OptionsModel * getOptionsModel()
Definition: walletmodel.cpp:315
RecentRequestEntry::recipient
SendCoinsRecipient recipient
Definition: recentrequeststablemodel.h:25
ReceiveRequestDialog::setModel
void setModel(WalletModel *model)
Definition: receiverequestdialog.cpp:32
OutputType
OutputType
Definition: outputtype.h:17
ReceiveCoinsDialog
Dialog for requesting payment of bitcoins.
Definition: receivecoinsdialog.h:30
wallet.h
ReceiveCoinsDialog::setModel
void setModel(WalletModel *model)
Definition: receivecoinsdialog.cpp:75
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:47
RecentRequestsTableModel::Amount
@ Amount
Definition: recentrequeststablemodel.h:62
ReceiveCoinsDialog::ReceiveCoinsDialog
ReceiveCoinsDialog(const PlatformStyle *platformStyle, QWidget *parent=nullptr)
Definition: receivecoinsdialog.cpp:23
ReceiveCoinsDialog::copyAmount
void copyAmount()
Definition: receivecoinsdialog.cpp:311
FormatOutputType
const std::string & FormatOutputType(OutputType type)
Definition: outputtype.cpp:27
ReceiveCoinsDialog::resizeEvent
virtual void resizeEvent(QResizeEvent *event) override
Definition: receivecoinsdialog.cpp:246
PlatformStyle::SingleColorIcon
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
Definition: platformstyle.cpp:97
RecentRequestsTableModel::entry
const RecentRequestEntry & entry(int row) const
Definition: recentrequeststablemodel.h:83
walletmodel.h
ReceiveCoinsDialog::LABEL_COLUMN_WIDTH
@ LABEL_COLUMN_WIDTH
Definition: receivecoinsdialog.h:36
WalletModel::getAddressTableModel
AddressTableModel * getAddressTableModel()
Definition: walletmodel.cpp:319
ReceiveCoinsDialog::ui
Ui::ReceiveCoinsDialog * ui
Definition: receivecoinsdialog.h:53
GUIUtil::TableViewLastColumnResizingFixer
Makes a QTableView last column feel as if it was being resized from its left border.
Definition: guiutil.h:224
ReceiveCoinsDialog::on_receiveButton_clicked
void on_receiveButton_clicked()
Definition: receivecoinsdialog.cpp:146
AddressTableModel::Receive
static const QString Receive
Specifies receive address.
Definition: addresstablemodel.h:62
OptionsModel::displayUnitChanged
void displayUnitChanged(int unit)
WalletModel::canGetAddressesChanged
void canGetAddressesChanged()
GUIUtil::TableViewLastColumnResizingFixer::stretchColumnWidth
void stretchColumnWidth(int column)
Definition: guiutil.cpp:555
GUIUtil::formatBitcoinURI
QString formatBitcoinURI(const SendCoinsRecipient &info)
Definition: guiutil.cpp:199
ReceiveCoinsDialog::on_removeRequestButton_clicked
void on_removeRequestButton_clicked()
Definition: receivecoinsdialog.cpp:228
ReceiveCoinsDialog::model
WalletModel * model
Definition: receivecoinsdialog.h:55
ReceiveCoinsDialog::updateDisplayUnit
void updateDisplayUnit()
Definition: receivecoinsdialog.cpp:139
GUIUtil::setClipboard
void setClipboard(const QString &str)
Definition: guiutil.cpp:766
RecentRequestsTableModel::removeRows
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Definition: recentrequeststablemodel.cpp:138
ReceiveRequestDialog::setInfo
void setInfo(const SendCoinsRecipient &info)
Definition: receiverequestdialog.cpp:44
SendCoinsRecipient
Definition: sendcoinsrecipient.h:23
ReceiveCoinsDialog::copyMessage
void copyMessage()
Definition: receivecoinsdialog.cpp:306
WalletModel::wallet
interfaces::Wallet & wallet() const
Definition: walletmodel.h:150
ReceiveCoinsDialog::copyColumnToClipboard
void copyColumnToClipboard(int column)
Definition: receivecoinsdialog.cpp:267
AddressTableModel::addRow
QString addRow(const QString &type, const QString &label, const QString &address, const OutputType address_type)
Definition: addresstablemodel.cpp:322
ReceiveCoinsDialog::contextMenu
QMenu * contextMenu
Definition: receivecoinsdialog.h:56
PlatformStyle::getImagesOnButtons
bool getImagesOnButtons() const
Definition: platformstyle.h:20
ReceiveCoinsDialog::selectedRow
QModelIndex selectedRow()
Definition: receivecoinsdialog.cpp:251
RecentRequestsTableModel
Model for list of recently generated payment requests / bitcoincash: URIs.
Definition: recentrequeststablemodel.h:51
recentrequeststablemodel.h
ReceiveCoinsDialog::on_recentRequestsView_doubleClicked
void on_recentRequestsView_doubleClicked(const QModelIndex &index)
Definition: receivecoinsdialog.cpp:195
ReceiveCoinsDialog::recentRequestsView_selectionChanged
void recentRequestsView_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
Definition: receivecoinsdialog.cpp:206
WalletModel::getRecentRequestsTableModel
RecentRequestsTableModel * getRecentRequestsTableModel()
Definition: walletmodel.cpp:327
ReceiveCoinsDialog::copyLabel
void copyLabel()
Definition: receivecoinsdialog.cpp:301
RecentRequestsTableModel::addNewRequest
void addNewRequest(const SendCoinsRecipient &recipient)
Definition: recentrequeststablemodel.cpp:165
ReceiveRequestDialog
Definition: receiverequestdialog.h:18
ReceiveCoinsDialog::~ReceiveCoinsDialog
~ReceiveCoinsDialog()
Definition: receivecoinsdialog.cpp:120
RecentRequestsTableModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: recentrequeststablemodel.cpp:131
AddressTableModel::getEditStatus
EditStatus getEditStatus() const
Definition: addresstablemodel.h:103
RecentRequestsTableModel::Date
@ Date
Definition: recentrequeststablemodel.h:59
platformstyle.h
ReceiveCoinsDialog::showMenu
void showMenu(const QPoint &point)
Definition: receivecoinsdialog.cpp:279
ReceiveCoinsDialog::DATE_COLUMN_WIDTH
@ DATE_COLUMN_WIDTH
Definition: receivecoinsdialog.h:35
ReceiveCoinsDialog::accept
void accept() override
Definition: receivecoinsdialog.cpp:135
interfaces::Wallet::getDefaultAddressType
virtual OutputType getDefaultAddressType()=0
Ui
Definition: addressbookpage.h:14
receivecoinsdialog.h
ReceiveCoinsDialog::columnResizingFixer
GUIUtil::TableViewLastColumnResizingFixer * columnResizingFixer
Definition: receivecoinsdialog.h:54
OptionsModel::getDisplayUnit
int getDisplayUnit() const
Definition: optionsmodel.h:97
ReceiveCoinsDialog::reject
void reject() override
Definition: receivecoinsdialog.cpp:131
RecentRequestsTableModel::Label
@ Label
Definition: recentrequeststablemodel.h:60
RecentRequestsTableModel::sort
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
Definition: recentrequeststablemodel.cpp:210
RecentRequestsTableModel::Message
@ Message
Definition: recentrequeststablemodel.h:61
ReceiveCoinsDialog::clear
void clear()
Definition: receivecoinsdialog.cpp:124
interfaces::Wallet::canGetAddresses
virtual bool canGetAddresses() const =0
optionsmodel.h
ReceiveCoinsDialog::on_showRequestButton_clicked
void on_showRequestButton_clicked()
Definition: receivecoinsdialog.cpp:215
ReceiveCoinsDialog::copyURI
void copyURI()
Definition: receivecoinsdialog.cpp:287
PlatformStyle
Definition: platformstyle.h:13
receiverequestdialog.h
addresstablemodel.h