BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
receivecoinsdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2018 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 
8 #include <qt/forms/ui_receivecoinsdialog.h>
9 
10 #include <qt/addressbookpage.h>
11 #include <qt/addresstablemodel.h>
12 #include <qt/bitcoinunits.h>
13 #include <qt/optionsmodel.h>
14 #include <qt/platformstyle.h>
17 #include <qt/walletmodel.h>
18 
19 #include <QAction>
20 #include <QCursor>
21 #include <QMessageBox>
22 #include <QScrollBar>
23 #include <QTextDocument>
24 
25 ReceiveCoinsDialog::ReceiveCoinsDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
26  QDialog(parent),
27  ui(new Ui::ReceiveCoinsDialog),
28  columnResizingFixer(0),
29  model(0),
30  platformStyle(_platformStyle)
31 {
32  ui->setupUi(this);
33 
34  if (!_platformStyle->getImagesOnButtons()) {
35  ui->clearButton->setIcon(QIcon());
36  ui->receiveButton->setIcon(QIcon());
37  ui->showRequestButton->setIcon(QIcon());
38  ui->removeRequestButton->setIcon(QIcon());
39  } else {
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"));
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, &ReceiveCoinsDialog::showMenu);
61  connect(copyURIAction, &QAction::triggered, this, &ReceiveCoinsDialog::copyURI);
62  connect(copyLabelAction, &QAction::triggered, this, &ReceiveCoinsDialog::copyLabel);
63  connect(copyMessageAction, &QAction::triggered, this, &ReceiveCoinsDialog::copyMessage);
64  connect(copyAmountAction, &QAction::triggered, this, &ReceiveCoinsDialog::copyAmount);
65 
66  connect(ui->clearButton, &QPushButton::clicked, this, &ReceiveCoinsDialog::clear);
67 }
68 
70 {
71  this->model = _model;
72 
73  if(_model && _model->getOptionsModel())
74  {
75  _model->getRecentRequestsTableModel()->sort(RecentRequestsTableModel::Date, Qt::DescendingOrder);
78 
79  QTableView* tableView = ui->recentRequestsView;
80 
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);
90 
91  connect(tableView->selectionModel(),
92  &QItemSelectionModel::selectionChanged, this,
94  // Last 2 columns are set by the columnResizingFixer, when the table geometry is ready.
96 
98  ui->useBech32->setCheckState(Qt::Checked);
99  } else {
100  ui->useBech32->setCheckState(Qt::Unchecked);
101  }
102 
103  // eventually disable the main receive button if private key operations are disabled
104  ui->receiveButton->setEnabled(!model->privateKeysDisabled());
105  }
106 }
107 
109 {
110  delete ui;
111 }
112 
114 {
115  ui->reqAmount->clear();
116  ui->reqLabel->setText("");
117  ui->reqMessage->setText("");
119 }
120 
122 {
123  clear();
124 }
125 
127 {
128  clear();
129 }
130 
132 {
133  if(model && model->getOptionsModel())
134  {
135  ui->reqAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
136  }
137 }
138 
140 {
142  return;
143 
144  QString address;
145  QString label = ui->reqLabel->text();
146  /* Generate new receiving address */
147  OutputType address_type;
148  if (ui->useBech32->isChecked()) {
149  address_type = OutputType::BECH32;
150  } else {
151  address_type = model->wallet().getDefaultAddressType();
152  if (address_type == OutputType::BECH32) {
153  address_type = OutputType::P2SH_SEGWIT;
154  }
155  }
156  address = model->getAddressTableModel()->addRow(AddressTableModel::Receive, label, "", address_type);
157  SendCoinsRecipient info(address, label,
158  ui->reqAmount->value(), ui->reqMessage->text());
159  ReceiveRequestDialog *dialog = new ReceiveRequestDialog(this);
160  dialog->setAttribute(Qt::WA_DeleteOnClose);
161  dialog->setModel(model);
162  dialog->setInfo(info);
163  dialog->show();
164  clear();
165 
166  /* Store request for later reference */
168 }
169 
171 {
173  ReceiveRequestDialog *dialog = new ReceiveRequestDialog(this);
174  dialog->setModel(model);
175  dialog->setInfo(submodel->entry(index.row()).recipient);
176  dialog->setAttribute(Qt::WA_DeleteOnClose);
177  dialog->show();
178 }
179 
180 void ReceiveCoinsDialog::recentRequestsView_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
181 {
182  // Enable Show/Remove buttons only if anything is selected.
183  bool enable = !ui->recentRequestsView->selectionModel()->selectedRows().isEmpty();
184  ui->showRequestButton->setEnabled(enable);
185  ui->removeRequestButton->setEnabled(enable);
186 }
187 
189 {
190  if(!model || !model->getRecentRequestsTableModel() || !ui->recentRequestsView->selectionModel())
191  return;
192  QModelIndexList selection = ui->recentRequestsView->selectionModel()->selectedRows();
193 
194  for (const QModelIndex& index : selection) {
196  }
197 }
198 
200 {
201  if(!model || !model->getRecentRequestsTableModel() || !ui->recentRequestsView->selectionModel())
202  return;
203  QModelIndexList selection = ui->recentRequestsView->selectionModel()->selectedRows();
204  if(selection.empty())
205  return;
206  // correct for selection mode ContiguousSelection
207  QModelIndex firstIndex = selection.at(0);
208  model->getRecentRequestsTableModel()->removeRows(firstIndex.row(), selection.length(), firstIndex.parent());
209 }
210 
211 // We override the virtual resizeEvent of the QWidget to adjust tables column
212 // sizes as the tables width is proportional to the dialogs width.
213 void ReceiveCoinsDialog::resizeEvent(QResizeEvent *event)
214 {
215  QWidget::resizeEvent(event);
217 }
218 
219 void ReceiveCoinsDialog::keyPressEvent(QKeyEvent *event)
220 {
221  if (event->key() == Qt::Key_Return)
222  {
223  // press return -> submit form
224  if (ui->reqLabel->hasFocus() || ui->reqAmount->hasFocus() || ui->reqMessage->hasFocus())
225  {
226  event->ignore();
228  return;
229  }
230  }
231 
232  this->QDialog::keyPressEvent(event);
233 }
234 
236 {
237  if(!model || !model->getRecentRequestsTableModel() || !ui->recentRequestsView->selectionModel())
238  return QModelIndex();
239  QModelIndexList selection = ui->recentRequestsView->selectionModel()->selectedRows();
240  if(selection.empty())
241  return QModelIndex();
242  // correct for selection mode ContiguousSelection
243  QModelIndex firstIndex = selection.at(0);
244  return firstIndex;
245 }
246 
247 // copy column of selected row to clipboard
249 {
250  QModelIndex firstIndex = selectedRow();
251  if (!firstIndex.isValid()) {
252  return;
253  }
254  GUIUtil::setClipboard(model->getRecentRequestsTableModel()->data(firstIndex.child(firstIndex.row(), column), Qt::EditRole).toString());
255 }
256 
257 // context menu
258 void ReceiveCoinsDialog::showMenu(const QPoint &point)
259 {
260  if (!selectedRow().isValid()) {
261  return;
262  }
263  contextMenu->exec(QCursor::pos());
264 }
265 
266 // context menu action: copy URI
268 {
269  QModelIndex sel = selectedRow();
270  if (!sel.isValid()) {
271  return;
272  }
273 
274  const RecentRequestsTableModel * const submodel = model->getRecentRequestsTableModel();
275  const QString uri = GUIUtil::formatBitcoinURI(submodel->entry(sel.row()).recipient);
277 }
278 
279 // context menu action: copy label
281 {
283 }
284 
285 // context menu action: copy message
287 {
289 }
290 
291 // context menu action: copy amount
293 {
295 }
Model for list of recently generated payment requests / bitcoin: URIs.
void addNewRequest(const SendCoinsRecipient &recipient)
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
Dialog for requesting payment of bitcoins.
interfaces::Wallet & wallet() const
Definition: walletmodel.h:219
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
virtual OutputType getDefaultAddressType()=0
void recentRequestsView_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
GUIUtil::TableViewLastColumnResizingFixer * columnResizingFixer
ReceiveCoinsDialog(const PlatformStyle *platformStyle, QWidget *parent=0)
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:180
OutputType
Definition: outputtype.h:15
QString addRow(const QString &type, const QString &label, const QString &address, const OutputType address_type)
int getDisplayUnit() const
Definition: optionsmodel.h:74
void setModel(WalletModel *model)
void copyColumnToClipboard(int column)
void setClipboard(const QString &str)
Definition: guiutil.cpp:757
void on_recentRequestsView_doubleClicked(const QModelIndex &index)
void setInfo(const SendCoinsRecipient &info)
bool privateKeysDisabled() const
Makes a QTableView last column feel as if it was being resized from its left border.
Definition: guiutil.h:152
virtual void keyPressEvent(QKeyEvent *event)
const RecentRequestEntry & entry(int row) const
void displayUnitChanged(int unit)
QVariant data(const QModelIndex &index, int role) const
virtual void resizeEvent(QResizeEvent *event)
RecentRequestsTableModel * getRecentRequestsTableModel()
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:125
static const QString Receive
Specifies receive address.
void showMenu(const QPoint &point)
bool getImagesOnButtons() const
Definition: platformstyle.h:21
OptionsModel * getOptionsModel()
void setModel(WalletModel *model)