Bitcoin ABC  0.22.13
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 
251 void ReceiveCoinsDialog::keyPressEvent(QKeyEvent *event) {
252  if (event->key() == Qt::Key_Return) {
253  // press return -> submit form
254  if (ui->reqLabel->hasFocus() || ui->reqAmount->hasFocus() ||
255  ui->reqMessage->hasFocus()) {
256  event->ignore();
258  return;
259  }
260  }
261 
262  this->QDialog::keyPressEvent(event);
263 }
264 
267  !ui->recentRequestsView->selectionModel()) {
268  return QModelIndex();
269  }
270  QModelIndexList selection =
271  ui->recentRequestsView->selectionModel()->selectedRows();
272  if (selection.empty()) {
273  return QModelIndex();
274  }
275  // correct for selection mode ContiguousSelection
276  QModelIndex firstIndex = selection.at(0);
277  return firstIndex;
278 }
279 
280 // copy column of selected row to clipboard
282  QModelIndex firstIndex = selectedRow();
283  if (!firstIndex.isValid()) {
284  return;
285  }
287  ->index(firstIndex.row(), column)
288  .data(Qt::EditRole)
289  .toString());
290 }
291 
292 // context menu
293 void ReceiveCoinsDialog::showMenu(const QPoint &point) {
294  if (!selectedRow().isValid()) {
295  return;
296  }
297  contextMenu->exec(QCursor::pos());
298 }
299 
300 // context menu action: copy URI
302  QModelIndex sel = selectedRow();
303  if (!sel.isValid()) {
304  return;
305  }
306 
307  const RecentRequestsTableModel *const submodel =
309  const QString uri =
310  GUIUtil::formatBitcoinURI(submodel->entry(sel.row()).recipient);
312 }
313 
314 // context menu action: copy label
317 }
318 
319 // context menu action: copy message
322 }
323 
324 // context menu action: copy amount
327 }
Model for list of recently generated payment requests / bitcoincash: URIs.
void addNewRequest(const SendCoinsRecipient &recipient)
Dialog for requesting payment of bitcoins.
interfaces::Wallet & wallet() const
Definition: walletmodel.h:150
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void canGetAddressesChanged()
virtual OutputType getDefaultAddressType()=0
void recentRequestsView_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
GUIUtil::TableViewLastColumnResizingFixer * columnResizingFixer
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
AddressTableModel * getAddressTableModel()
Ui::ReceiveCoinsDialog * ui
QString formatBitcoinURI(const SendCoinsRecipient &info)
Definition: guiutil.cpp:197
OutputType
Definition: outputtype.h:17
QString addRow(const QString &type, const QString &label, const QString &address, const OutputType address_type)
int getDisplayUnit() const
Definition: optionsmodel.h:96
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
void setModel(WalletModel *model)
void copyColumnToClipboard(int column)
void setClipboard(const QString &str)
Definition: guiutil.cpp:732
void on_recentRequestsView_doubleClicked(const QModelIndex &index)
void setInfo(const SendCoinsRecipient &info)
Makes a QTableView last column feel as if it was being resized from its left border.
Definition: guiutil.h:194
virtual void keyPressEvent(QKeyEvent *event)
const RecentRequestEntry & entry(int row) const
void displayUnitChanged(int unit)
ReceiveCoinsDialog(const PlatformStyle *platformStyle, QWidget *parent=nullptr)
const std::string & FormatOutputType(OutputType type)
Definition: outputtype.cpp:27
virtual void resizeEvent(QResizeEvent *event)
RecentRequestsTableModel * getRecentRequestsTableModel()
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:47
static const QString Receive
Specifies receive address.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
void showMenu(const QPoint &point)
EditStatus getEditStatus() const
bool getImagesOnButtons() const
Definition: platformstyle.h:20
OptionsModel * getOptionsModel()
virtual bool canGetAddresses() const =0
void setModel(WalletModel *model)