Bitcoin Core  22.99.0
P2P Digital Currency
coincontroldialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 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 #if defined(HAVE_CONFIG_H)
7 #endif
8 
9 #include <qt/coincontroldialog.h>
10 #include <qt/forms/ui_coincontroldialog.h>
11 
12 #include <qt/addresstablemodel.h>
13 #include <qt/bitcoinunits.h>
14 #include <qt/guiutil.h>
15 #include <qt/optionsmodel.h>
16 #include <qt/platformstyle.h>
17 #include <qt/walletmodel.h>
18 
19 #include <wallet/coincontrol.h>
20 #include <interfaces/node.h>
21 #include <key_io.h>
22 #include <policy/policy.h>
23 #include <wallet/wallet.h>
24 
25 #include <QApplication>
26 #include <QCheckBox>
27 #include <QCursor>
28 #include <QDialogButtonBox>
29 #include <QFlags>
30 #include <QIcon>
31 #include <QSettings>
32 #include <QTreeWidget>
33 
34 QList<CAmount> CoinControlDialog::payAmounts;
36 
37 bool CCoinControlWidgetItem::operator<(const QTreeWidgetItem &other) const {
38  int column = treeWidget()->sortColumn();
40  return data(column, Qt::UserRole).toLongLong() < other.data(column, Qt::UserRole).toLongLong();
41  return QTreeWidgetItem::operator<(other);
42 }
43 
44 CoinControlDialog::CoinControlDialog(CCoinControl& coin_control, WalletModel* _model, const PlatformStyle *_platformStyle, QWidget *parent) :
45  QDialog(parent, GUIUtil::dialog_flags),
46  ui(new Ui::CoinControlDialog),
47  m_coin_control(coin_control),
48  model(_model),
49  platformStyle(_platformStyle)
50 {
51  ui->setupUi(this);
52 
53  // context menu
54  contextMenu = new QMenu(this);
55  contextMenu->addAction(tr("&Copy address"), this, &CoinControlDialog::copyAddress);
56  contextMenu->addAction(tr("Copy &label"), this, &CoinControlDialog::copyLabel);
57  contextMenu->addAction(tr("Copy &amount"), this, &CoinControlDialog::copyAmount);
58  copyTransactionHashAction = contextMenu->addAction(tr("Copy transaction &ID"), this, &CoinControlDialog::copyTransactionHash);
59  contextMenu->addSeparator();
60  lockAction = contextMenu->addAction(tr("L&ock unspent"), this, &CoinControlDialog::lockCoin);
61  unlockAction = contextMenu->addAction(tr("&Unlock unspent"), this, &CoinControlDialog::unlockCoin);
62  connect(ui->treeWidget, &QWidget::customContextMenuRequested, this, &CoinControlDialog::showMenu);
63 
64  // clipboard actions
65  QAction *clipboardQuantityAction = new QAction(tr("Copy quantity"), this);
66  QAction *clipboardAmountAction = new QAction(tr("Copy amount"), this);
67  QAction *clipboardFeeAction = new QAction(tr("Copy fee"), this);
68  QAction *clipboardAfterFeeAction = new QAction(tr("Copy after fee"), this);
69  QAction *clipboardBytesAction = new QAction(tr("Copy bytes"), this);
70  QAction *clipboardLowOutputAction = new QAction(tr("Copy dust"), this);
71  QAction *clipboardChangeAction = new QAction(tr("Copy change"), this);
72 
73  connect(clipboardQuantityAction, &QAction::triggered, this, &CoinControlDialog::clipboardQuantity);
74  connect(clipboardAmountAction, &QAction::triggered, this, &CoinControlDialog::clipboardAmount);
75  connect(clipboardFeeAction, &QAction::triggered, this, &CoinControlDialog::clipboardFee);
76  connect(clipboardAfterFeeAction, &QAction::triggered, this, &CoinControlDialog::clipboardAfterFee);
77  connect(clipboardBytesAction, &QAction::triggered, this, &CoinControlDialog::clipboardBytes);
78  connect(clipboardLowOutputAction, &QAction::triggered, this, &CoinControlDialog::clipboardLowOutput);
79  connect(clipboardChangeAction, &QAction::triggered, this, &CoinControlDialog::clipboardChange);
80 
81  ui->labelCoinControlQuantity->addAction(clipboardQuantityAction);
82  ui->labelCoinControlAmount->addAction(clipboardAmountAction);
83  ui->labelCoinControlFee->addAction(clipboardFeeAction);
84  ui->labelCoinControlAfterFee->addAction(clipboardAfterFeeAction);
85  ui->labelCoinControlBytes->addAction(clipboardBytesAction);
86  ui->labelCoinControlLowOutput->addAction(clipboardLowOutputAction);
87  ui->labelCoinControlChange->addAction(clipboardChangeAction);
88 
89  // toggle tree/list mode
90  connect(ui->radioTreeMode, &QRadioButton::toggled, this, &CoinControlDialog::radioTreeMode);
91  connect(ui->radioListMode, &QRadioButton::toggled, this, &CoinControlDialog::radioListMode);
92 
93  // click on checkbox
94  connect(ui->treeWidget, &QTreeWidget::itemChanged, this, &CoinControlDialog::viewItemChanged);
95 
96  // click on header
97  ui->treeWidget->header()->setSectionsClickable(true);
98  connect(ui->treeWidget->header(), &QHeaderView::sectionClicked, this, &CoinControlDialog::headerSectionClicked);
99 
100  // ok button
101  connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &CoinControlDialog::buttonBoxClicked);
102 
103  // (un)select all
104  connect(ui->pushButtonSelectAll, &QPushButton::clicked, this, &CoinControlDialog::buttonSelectAllClicked);
105 
106  ui->treeWidget->setColumnWidth(COLUMN_CHECKBOX, 84);
107  ui->treeWidget->setColumnWidth(COLUMN_AMOUNT, 110);
108  ui->treeWidget->setColumnWidth(COLUMN_LABEL, 190);
109  ui->treeWidget->setColumnWidth(COLUMN_ADDRESS, 320);
110  ui->treeWidget->setColumnWidth(COLUMN_DATE, 130);
111  ui->treeWidget->setColumnWidth(COLUMN_CONFIRMATIONS, 110);
112 
113  // default view is sorted by amount desc
114  sortView(COLUMN_AMOUNT, Qt::DescendingOrder);
115 
116  // restore list mode and sortorder as a convenience feature
117  QSettings settings;
118  if (settings.contains("nCoinControlMode") && !settings.value("nCoinControlMode").toBool())
119  ui->radioTreeMode->click();
120  if (settings.contains("nCoinControlSortColumn") && settings.contains("nCoinControlSortOrder"))
121  sortView(settings.value("nCoinControlSortColumn").toInt(), (static_cast<Qt::SortOrder>(settings.value("nCoinControlSortOrder").toInt())));
122 
124 
125  if(_model->getOptionsModel() && _model->getAddressTableModel())
126  {
127  updateView();
130  }
131 }
132 
134 {
135  QSettings settings;
136  settings.setValue("nCoinControlMode", ui->radioListMode->isChecked());
137  settings.setValue("nCoinControlSortColumn", sortColumn);
138  settings.setValue("nCoinControlSortOrder", (int)sortOrder);
139 
140  delete ui;
141 }
142 
143 // ok button
144 void CoinControlDialog::buttonBoxClicked(QAbstractButton* button)
145 {
146  if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
147  done(QDialog::Accepted); // closes the dialog
148 }
149 
150 // (un)select all
152 {
153  Qt::CheckState state = Qt::Checked;
154  for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
155  {
156  if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != Qt::Unchecked)
157  {
158  state = Qt::Unchecked;
159  break;
160  }
161  }
162  ui->treeWidget->setEnabled(false);
163  for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
164  if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != state)
165  ui->treeWidget->topLevelItem(i)->setCheckState(COLUMN_CHECKBOX, state);
166  ui->treeWidget->setEnabled(true);
167  if (state == Qt::Unchecked)
168  m_coin_control.UnSelectAll(); // just to be sure
170 }
171 
172 // context menu
173 void CoinControlDialog::showMenu(const QPoint &point)
174 {
175  QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
176  if(item)
177  {
178  contextMenuItem = item;
179 
180  // disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
181  if (item->data(COLUMN_ADDRESS, TxHashRole).toString().length() == 64) // transaction hash is 64 characters (this means it is a child node, so it is not a parent node in tree mode)
182  {
183  copyTransactionHashAction->setEnabled(true);
184  if (model->wallet().isLockedCoin(COutPoint(uint256S(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt())))
185  {
186  lockAction->setEnabled(false);
187  unlockAction->setEnabled(true);
188  }
189  else
190  {
191  lockAction->setEnabled(true);
192  unlockAction->setEnabled(false);
193  }
194  }
195  else // this means click on parent node in tree mode -> disable all
196  {
197  copyTransactionHashAction->setEnabled(false);
198  lockAction->setEnabled(false);
199  unlockAction->setEnabled(false);
200  }
201 
202  // show context menu
203  contextMenu->exec(QCursor::pos());
204  }
205 }
206 
207 // context menu action: copy amount
209 {
211 }
212 
213 // context menu action: copy label
215 {
216  if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_LABEL).length() == 0 && contextMenuItem->parent())
218  else
220 }
221 
222 // context menu action: copy address
224 {
225  if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_ADDRESS).length() == 0 && contextMenuItem->parent())
227  else
229 }
230 
231 // context menu action: copy transaction id
233 {
235 }
236 
237 // context menu action: lock coin
239 {
240  if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
241  contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
242 
243  COutPoint outpt(uint256S(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
244  model->wallet().lockCoin(outpt);
245  contextMenuItem->setDisabled(true);
246  contextMenuItem->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
248 }
249 
250 // context menu action: unlock coin
252 {
253  COutPoint outpt(uint256S(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
254  model->wallet().unlockCoin(outpt);
255  contextMenuItem->setDisabled(false);
256  contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
258 }
259 
260 // copy label "Quantity" to clipboard
262 {
263  GUIUtil::setClipboard(ui->labelCoinControlQuantity->text());
264 }
265 
266 // copy label "Amount" to clipboard
268 {
269  GUIUtil::setClipboard(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
270 }
271 
272 // copy label "Fee" to clipboard
274 {
275  GUIUtil::setClipboard(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
276 }
277 
278 // copy label "After fee" to clipboard
280 {
281  GUIUtil::setClipboard(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
282 }
283 
284 // copy label "Bytes" to clipboard
286 {
287  GUIUtil::setClipboard(ui->labelCoinControlBytes->text().replace(ASYMP_UTF8, ""));
288 }
289 
290 // copy label "Dust" to clipboard
292 {
293  GUIUtil::setClipboard(ui->labelCoinControlLowOutput->text());
294 }
295 
296 // copy label "Change" to clipboard
298 {
299  GUIUtil::setClipboard(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
300 }
301 
302 // treeview: sort
303 void CoinControlDialog::sortView(int column, Qt::SortOrder order)
304 {
305  sortColumn = column;
306  sortOrder = order;
307  ui->treeWidget->sortItems(column, order);
308  ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
309 }
310 
311 // treeview: clicked on header
313 {
314  if (logicalIndex == COLUMN_CHECKBOX) // click on most left column -> do nothing
315  {
316  ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
317  }
318  else
319  {
320  if (sortColumn == logicalIndex)
321  sortOrder = ((sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder);
322  else
323  {
324  sortColumn = logicalIndex;
325  sortOrder = ((sortColumn == COLUMN_LABEL || sortColumn == COLUMN_ADDRESS) ? Qt::AscendingOrder : Qt::DescendingOrder); // if label or address then default => asc, else default => desc
326  }
327 
329  }
330 }
331 
332 // toggle tree mode
334 {
335  if (checked && model)
336  updateView();
337 }
338 
339 // toggle list mode
341 {
342  if (checked && model)
343  updateView();
344 }
345 
346 // checkbox clicked by user
347 void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
348 {
349  if (column == COLUMN_CHECKBOX && item->data(COLUMN_ADDRESS, TxHashRole).toString().length() == 64) // transaction hash is 64 characters (this means it is a child node, so it is not a parent node in tree mode)
350  {
351  COutPoint outpt(uint256S(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt());
352 
353  if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
354  m_coin_control.UnSelect(outpt);
355  else if (item->isDisabled()) // locked (this happens if "check all" through parent node)
356  item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
357  else
358  m_coin_control.Select(outpt);
359 
360  // selection changed -> update labels
361  if (ui->treeWidget->isEnabled()) // do not update on every click for (un)select all
363  }
364 }
365 
366 // shows count of locked unspent outputs
368 {
369  std::vector<COutPoint> vOutpts;
370  model->wallet().listLockedCoins(vOutpts);
371  if (vOutpts.size() > 0)
372  {
373  ui->labelLocked->setText(tr("(%1 locked)").arg(vOutpts.size()));
374  ui->labelLocked->setVisible(true);
375  }
376  else ui->labelLocked->setVisible(false);
377 }
378 
379 void CoinControlDialog::updateLabels(CCoinControl& m_coin_control, WalletModel *model, QDialog* dialog)
380 {
381  if (!model)
382  return;
383 
384  // nPayAmount
385  CAmount nPayAmount = 0;
386  bool fDust = false;
387  for (const CAmount &amount : CoinControlDialog::payAmounts)
388  {
389  nPayAmount += amount;
390 
391  if (amount > 0)
392  {
393  // Assumes a p2pkh script size
394  CTxOut txout(amount, CScript() << std::vector<unsigned char>(24, 0));
395  fDust |= IsDust(txout, model->node().getDustRelayFee());
396  }
397  }
398 
399  CAmount nAmount = 0;
400  CAmount nPayFee = 0;
401  CAmount nAfterFee = 0;
402  CAmount nChange = 0;
403  unsigned int nBytes = 0;
404  unsigned int nBytesInputs = 0;
405  unsigned int nQuantity = 0;
406  bool fWitness = false;
407 
408  std::vector<COutPoint> vCoinControl;
409  m_coin_control.ListSelected(vCoinControl);
410 
411  size_t i = 0;
412  for (const auto& out : model->wallet().getCoins(vCoinControl)) {
413  if (out.depth_in_main_chain < 0) continue;
414 
415  // unselect already spent, very unlikely scenario, this could happen
416  // when selected are spent elsewhere, like rpc or another computer
417  const COutPoint& outpt = vCoinControl[i++];
418  if (out.is_spent)
419  {
420  m_coin_control.UnSelect(outpt);
421  continue;
422  }
423 
424  // Quantity
425  nQuantity++;
426 
427  // Amount
428  nAmount += out.txout.nValue;
429 
430  // Bytes
431  CTxDestination address;
432  int witnessversion = 0;
433  std::vector<unsigned char> witnessprogram;
434  if (out.txout.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram))
435  {
436  nBytesInputs += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
437  fWitness = true;
438  }
439  else if(ExtractDestination(out.txout.scriptPubKey, address))
440  {
441  CPubKey pubkey;
442  PKHash* pkhash = std::get_if<PKHash>(&address);
443  if (pkhash && model->wallet().getPubKey(out.txout.scriptPubKey, ToKeyID(*pkhash), pubkey))
444  {
445  nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
446  }
447  else
448  nBytesInputs += 148; // in all error cases, simply assume 148 here
449  }
450  else nBytesInputs += 148;
451  }
452 
453  // calculation
454  if (nQuantity > 0)
455  {
456  // Bytes
457  nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
458  if (fWitness)
459  {
460  // there is some fudging in these numbers related to the actual virtual transaction size calculation that will keep this estimate from being exact.
461  // usually, the result will be an overestimate within a couple of satoshis so that the confirmation dialog ends up displaying a slightly smaller fee.
462  // also, the witness stack size value is a variable sized integer. usually, the number of stack items will be well under the single byte var int limit.
463  nBytes += 2; // account for the serialized marker and flag bytes
464  nBytes += nQuantity; // account for the witness byte that holds the number of stack items for each input.
465  }
466 
467  // in the subtract fee from amount case, we can tell if zero change already and subtract the bytes, so that fee calculation afterwards is accurate
469  if (nAmount - nPayAmount == 0)
470  nBytes -= 34;
471 
472  // Fee
473  nPayFee = model->wallet().getMinimumFee(nBytes, m_coin_control, nullptr /* returned_target */, nullptr /* reason */);
474 
475  if (nPayAmount > 0)
476  {
477  nChange = nAmount - nPayAmount;
479  nChange -= nPayFee;
480 
481  // Never create dust outputs; if we would, just add the dust to the fee.
482  if (nChange > 0 && nChange < MIN_CHANGE)
483  {
484  // Assumes a p2pkh script size
485  CTxOut txout(nChange, CScript() << std::vector<unsigned char>(24, 0));
486  if (IsDust(txout, model->node().getDustRelayFee()))
487  {
488  nPayFee += nChange;
489  nChange = 0;
491  nBytes -= 34; // we didn't detect lack of change above
492  }
493  }
494 
495  if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
496  nBytes -= 34;
497  }
498 
499  // after fee
500  nAfterFee = std::max<CAmount>(nAmount - nPayFee, 0);
501  }
502 
503  // actually update labels
504  int nDisplayUnit = BitcoinUnits::BTC;
505  if (model && model->getOptionsModel())
506  nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
507 
508  QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
509  QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
510  QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
511  QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
512  QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
513  QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
514  QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
515 
516  // enable/disable "dust" and "change"
517  dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
518  dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
519  dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
520  dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
521 
522  // stats
523  l1->setText(QString::number(nQuantity)); // Quantity
524  l2->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
525  l3->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
526  l4->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
527  l5->setText(((nBytes > 0) ? ASYMP_UTF8 : "") + QString::number(nBytes)); // Bytes
528  l7->setText(fDust ? tr("yes") : tr("no")); // Dust
529  l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
530  if (nPayFee > 0)
531  {
532  l3->setText(ASYMP_UTF8 + l3->text());
533  l4->setText(ASYMP_UTF8 + l4->text());
534  if (nChange > 0 && !CoinControlDialog::fSubtractFeeFromAmount)
535  l8->setText(ASYMP_UTF8 + l8->text());
536  }
537 
538  // turn label red when dust
539  l7->setStyleSheet((fDust) ? "color:red;" : "");
540 
541  // tool tips
542  QString toolTipDust = tr("This label turns red if any recipient receives an amount smaller than the current dust threshold.");
543 
544  // how many satoshis the estimated fee can vary per byte we guess wrong
545  double dFeeVary = (nBytes != 0) ? (double)nPayFee / nBytes : 0;
546 
547  QString toolTip4 = tr("Can vary +/- %1 satoshi(s) per input.").arg(dFeeVary);
548 
549  l3->setToolTip(toolTip4);
550  l4->setToolTip(toolTip4);
551  l7->setToolTip(toolTipDust);
552  l8->setToolTip(toolTip4);
553  dialog->findChild<QLabel *>("labelCoinControlFeeText") ->setToolTip(l3->toolTip());
554  dialog->findChild<QLabel *>("labelCoinControlAfterFeeText") ->setToolTip(l4->toolTip());
555  dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
556  dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
557  dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
558 
559  // Insufficient funds
560  QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
561  if (label)
562  label->setVisible(nChange < 0);
563 }
564 
566 {
567  if (e->type() == QEvent::PaletteChange) {
568  updateView();
569  }
570 
571  QDialog::changeEvent(e);
572 }
573 
575 {
577  return;
578 
579  bool treeMode = ui->radioTreeMode->isChecked();
580 
581  ui->treeWidget->clear();
582  ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
583  ui->treeWidget->setAlternatingRowColors(!treeMode);
584  QFlags<Qt::ItemFlag> flgCheckbox = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
585  QFlags<Qt::ItemFlag> flgTristate = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
586 
587  int nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
588 
589  for (const auto& coins : model->wallet().listCoins()) {
590  CCoinControlWidgetItem* itemWalletAddress{nullptr};
591  QString sWalletAddress = QString::fromStdString(EncodeDestination(coins.first));
592  QString sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
593  if (sWalletLabel.isEmpty())
594  sWalletLabel = tr("(no label)");
595 
596  if (treeMode)
597  {
598  // wallet address
599  itemWalletAddress = new CCoinControlWidgetItem(ui->treeWidget);
600 
601  itemWalletAddress->setFlags(flgTristate);
602  itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
603 
604  // label
605  itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
606 
607  // address
608  itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
609  }
610 
611  CAmount nSum = 0;
612  int nChildren = 0;
613  for (const auto& outpair : coins.second) {
614  const COutPoint& output = std::get<0>(outpair);
615  const interfaces::WalletTxOut& out = std::get<1>(outpair);
616  nSum += out.txout.nValue;
617  nChildren++;
618 
619  CCoinControlWidgetItem *itemOutput;
620  if (treeMode) itemOutput = new CCoinControlWidgetItem(itemWalletAddress);
621  else itemOutput = new CCoinControlWidgetItem(ui->treeWidget);
622  itemOutput->setFlags(flgCheckbox);
623  itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
624 
625  // address
626  CTxDestination outputAddress;
627  QString sAddress = "";
628  if(ExtractDestination(out.txout.scriptPubKey, outputAddress))
629  {
630  sAddress = QString::fromStdString(EncodeDestination(outputAddress));
631 
632  // if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
633  if (!treeMode || (!(sAddress == sWalletAddress)))
634  itemOutput->setText(COLUMN_ADDRESS, sAddress);
635  }
636 
637  // label
638  if (!(sAddress == sWalletAddress)) // change
639  {
640  // tooltip from where the change comes from
641  itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
642  itemOutput->setText(COLUMN_LABEL, tr("(change)"));
643  }
644  else if (!treeMode)
645  {
646  QString sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
647  if (sLabel.isEmpty())
648  sLabel = tr("(no label)");
649  itemOutput->setText(COLUMN_LABEL, sLabel);
650  }
651 
652  // amount
653  itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.txout.nValue));
654  itemOutput->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)out.txout.nValue)); // padding so that sorting works correctly
655 
656  // date
657  itemOutput->setText(COLUMN_DATE, GUIUtil::dateTimeStr(out.time));
658  itemOutput->setData(COLUMN_DATE, Qt::UserRole, QVariant((qlonglong)out.time));
659 
660  // confirmations
661  itemOutput->setText(COLUMN_CONFIRMATIONS, QString::number(out.depth_in_main_chain));
662  itemOutput->setData(COLUMN_CONFIRMATIONS, Qt::UserRole, QVariant((qlonglong)out.depth_in_main_chain));
663 
664  // transaction hash
665  itemOutput->setData(COLUMN_ADDRESS, TxHashRole, QString::fromStdString(output.hash.GetHex()));
666 
667  // vout index
668  itemOutput->setData(COLUMN_ADDRESS, VOutRole, output.n);
669 
670  // disable locked coins
671  if (model->wallet().isLockedCoin(output))
672  {
673  m_coin_control.UnSelect(output); // just to be sure
674  itemOutput->setDisabled(true);
675  itemOutput->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
676  }
677 
678  // set checkbox
679  if (m_coin_control.IsSelected(output))
680  itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
681  }
682 
683  // amount
684  if (treeMode)
685  {
686  itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
687  itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
688  itemWalletAddress->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)nSum));
689  }
690  }
691 
692  // expand all partially selected
693  if (treeMode)
694  {
695  for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
696  if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
697  ui->treeWidget->topLevelItem(i)->setExpanded(true);
698  }
699 
700  // sort view
702  ui->treeWidget->setEnabled(true);
703 }
ASYMP_UTF8
#define ASYMP_UTF8
Definition: coincontroldialog.h:28
CoinControlDialog::unlockAction
QAction * unlockAction
Definition: coincontroldialog.h:68
CCoinControl::Select
void Select(const COutPoint &output)
Definition: coincontrol.h:69
policy.h
interfaces::Wallet::getMinimumFee
virtual CAmount getMinimumFee(unsigned int tx_bytes, const CCoinControl &coin_control, int *returned_target, FeeReason *reason)=0
Get minimum fee.
WalletModel::getOptionsModel
OptionsModel * getOptionsModel()
Definition: walletmodel.cpp:284
CoinControlDialog::lockCoin
void lockCoin()
Definition: coincontroldialog.cpp:238
CoinControlDialog::platformStyle
const PlatformStyle * platformStyle
Definition: coincontroldialog.h:70
CoinControlDialog::payAmounts
static QList< CAmount > payAmounts
Definition: coincontroldialog.h:51
MIN_CHANGE
static constexpr CAmount MIN_CHANGE
target minimum change amount
Definition: coinselection.h:14
CoinControlDialog::viewItemChanged
void viewItemChanged(QTreeWidgetItem *, int)
Definition: coincontroldialog.cpp:347
wallet.h
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:51
CoinControlDialog::showMenu
void showMenu(const QPoint &)
Definition: coincontroldialog.cpp:173
CCoinControl::ListSelected
void ListSelected(std::vector< COutPoint > &vOutpoints) const
Definition: coincontrol.h:84
BitcoinUnits::formatWithUnit
static QString formatWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD)
Format as string (with unit)
Definition: bitcoinunits.cpp:148
key_io.h
interfaces::WalletTxOut::depth_in_main_chain
int depth_in_main_chain
Definition: wallet.h:410
operator<
bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:641
PlatformStyle::SingleColorIcon
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
Definition: platformstyle.cpp:105
walletmodel.h
COutPoint::hash
uint256 hash
Definition: transaction.h:29
base_blob::data
const unsigned char * data() const
Definition: uint256.h:55
CoinControlDialog::updateView
void updateView()
Definition: coincontroldialog.cpp:574
WalletModel::getAddressTableModel
AddressTableModel * getAddressTableModel()
Definition: walletmodel.cpp:289
AddressTableModel::labelForAddress
QString labelForAddress(const QString &address) const
Look up label for address in address book, if not found return empty string.
Definition: addresstablemodel.cpp:412
CCoinControl
Coin Control Features.
Definition: coincontrol.h:23
interfaces::Wallet::listLockedCoins
virtual void listLockedCoins(std::vector< COutPoint > &outputs)=0
List locked coins.
CoinControlDialog
Definition: coincontroldialog.h:40
GUIUtil
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:59
CoinControlDialog::m_coin_control
CCoinControl & m_coin_control
Definition: coincontroldialog.h:59
CoinControlDialog::sortColumn
int sortColumn
Definition: coincontroldialog.h:61
CoinControlDialog::clipboardChange
void clipboardChange()
Definition: coincontroldialog.cpp:297
CCoinControl::IsSelected
bool IsSelected(const COutPoint &output) const
Definition: coincontrol.h:64
interfaces::WalletTxOut
Wallet transaction output.
Definition: wallet.h:406
CoinControlDialog::updateLabels
static void updateLabels(CCoinControl &m_coin_control, WalletModel *, QDialog *)
Definition: coincontroldialog.cpp:379
bitcoin-config.h
CoinControlDialog::radioListMode
void radioListMode(bool)
Definition: coincontroldialog.cpp:340
GUIUtil::dialog_flags
constexpr auto dialog_flags
Definition: guiutil.h:59
CoinControlDialog::model
WalletModel * model
Definition: coincontroldialog.h:60
CoinControlDialog::headerSectionClicked
void headerSectionClicked(int)
Definition: coincontroldialog.cpp:312
CoinControlDialog::clipboardLowOutput
void clipboardLowOutput()
Definition: coincontroldialog.cpp:291
GUIUtil::setClipboard
void setClipboard(const QString &str)
Definition: guiutil.cpp:638
CoinControlDialog::copyTransactionHashAction
QAction * copyTransactionHashAction
Definition: coincontroldialog.h:66
CoinControlDialog::copyLabel
void copyLabel()
Definition: coincontroldialog.cpp:214
WalletModel::node
interfaces::Node & node() const
Definition: walletmodel.h:143
CTxOut::nValue
CAmount nValue
Definition: transaction.h:131
CoinControlDialog::fSubtractFeeFromAmount
static bool fSubtractFeeFromAmount
Definition: coincontroldialog.h:52
CoinControlDialog::buttonSelectAllClicked
void buttonSelectAllClicked()
Definition: coincontroldialog.cpp:151
BitcoinUnits::removeSpaces
static QString removeSpaces(QString text)
Definition: bitcoinunits.h:99
CoinControlDialog::sortView
void sortView(int, Qt::SortOrder)
Definition: coincontroldialog.cpp:303
WalletModel::wallet
interfaces::Wallet & wallet() const
Definition: walletmodel.h:144
interfaces::Wallet::unlockCoin
virtual void unlockCoin(const COutPoint &output)=0
Unlock coin.
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
CoinControlDialog::copyTransactionHash
void copyTransactionHash()
Definition: coincontroldialog.cpp:232
CTxOut
An output of a transaction.
Definition: transaction.h:128
CoinControlDialog::contextMenuItem
QTreeWidgetItem * contextMenuItem
Definition: coincontroldialog.h:65
CoinControlDialog::clipboardAmount
void clipboardAmount()
Definition: coincontroldialog.cpp:267
CoinControlDialog::buttonBoxClicked
void buttonBoxClicked(QAbstractButton *)
Definition: coincontroldialog.cpp:144
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:132
node.h
CoinControlDialog::clipboardAfterFee
void clipboardAfterFee()
Definition: coincontroldialog.cpp:279
interfaces::Wallet::isLockedCoin
virtual bool isLockedCoin(const COutPoint &output)=0
Return whether coin is locked.
CoinControlDialog::clipboardBytes
void clipboardBytes()
Definition: coincontroldialog.cpp:285
CoinControlDialog::~CoinControlDialog
~CoinControlDialog()
Definition: coincontroldialog.cpp:133
CoinControlDialog::updateLabelLocked
void updateLabelLocked()
Definition: coincontroldialog.cpp:367
CCoinControl::UnSelectAll
void UnSelectAll()
Definition: coincontrol.h:79
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
base_blob::GetHex
std::string GetHex() const
Definition: uint256.cpp:20
guiutil.h
CoinControlDialog::TxHashRole
@ TxHashRole
Definition: coincontroldialog.h:87
uint256S
uint256 uint256S(const char *str)
Definition: uint256.h:137
interfaces::WalletTxOut::txout
CTxOut txout
Definition: wallet.h:408
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
ExtractDestination
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:213
CoinControlDialog::lockAction
QAction * lockAction
Definition: coincontroldialog.h:67
CoinControlDialog::CoinControlDialog
CoinControlDialog(CCoinControl &coin_control, WalletModel *model, const PlatformStyle *platformStyle, QWidget *parent=nullptr)
Definition: coincontroldialog.cpp:44
CCoinControlWidgetItem::operator<
bool operator<(const QTreeWidgetItem &other) const override
Definition: coincontroldialog.cpp:37
CoinControlDialog::unlockCoin
void unlockCoin()
Definition: coincontroldialog.cpp:251
CoinControlDialog::CCoinControlWidgetItem
friend class CCoinControlWidgetItem
Definition: coincontroldialog.h:91
CPubKey::IsCompressed
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:194
platformstyle.h
interfaces::Node::getDustRelayFee
virtual CFeeRate getDustRelayFee()=0
Get dust relay fee.
PKHash
Definition: standard.h:79
ToKeyID
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:34
CoinControlDialog::ui
Ui::CoinControlDialog * ui
Definition: coincontroldialog.h:58
CPubKey
An encapsulated public key.
Definition: pubkey.h:32
CCoinControlWidgetItem
Definition: coincontroldialog.h:30
Ui
Definition: addressbookpage.h:14
CoinControlDialog::changeEvent
void changeEvent(QEvent *e) override
Definition: coincontroldialog.cpp:565
CoinControlDialog::clipboardQuantity
void clipboardQuantity()
Definition: coincontroldialog.cpp:261
interfaces::Wallet::listCoins
virtual CoinsList listCoins()=0
CoinControlDialog::COLUMN_DATE
@ COLUMN_DATE
Definition: coincontroldialog.h:81
OptionsModel::getDisplayUnit
int getDisplayUnit() const
Definition: optionsmodel.h:88
interfaces::Wallet::lockCoin
virtual void lockCoin(const COutPoint &output)=0
Lock coin.
COutPoint::n
uint32_t n
Definition: transaction.h:30
CoinControlDialog::COLUMN_AMOUNT
@ COLUMN_AMOUNT
Definition: coincontroldialog.h:78
interfaces::Wallet::getPubKey
virtual bool getPubKey(const CScript &script, const CKeyID &address, CPubKey &pub_key)=0
Get public key.
CoinControlDialog::radioTreeMode
void radioTreeMode(bool)
Definition: coincontroldialog.cpp:333
CoinControlDialog::contextMenu
QMenu * contextMenu
Definition: coincontroldialog.h:64
coincontroldialog.h
CoinControlDialog::COLUMN_ADDRESS
@ COLUMN_ADDRESS
Definition: coincontroldialog.h:80
IsDust
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:48
bitcoinunits.h
CoinControlDialog::copyAddress
void copyAddress()
Definition: coincontroldialog.cpp:223
GUIUtil::dateTimeStr
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:77
CoinControlDialog::copyAmount
void copyAmount()
Definition: coincontroldialog.cpp:208
GUIUtil::handleCloseWindowShortcut
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:402
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
optionsmodel.h
coincontrol.h
CoinControlDialog::COLUMN_CHECKBOX
@ COLUMN_CHECKBOX
Definition: coincontroldialog.h:77
CoinControlDialog::sortOrder
Qt::SortOrder sortOrder
Definition: coincontroldialog.h:62
CoinControlDialog::COLUMN_LABEL
@ COLUMN_LABEL
Definition: coincontroldialog.h:79
PlatformStyle
Definition: platformstyle.h:13
CoinControlDialog::clipboardFee
void clipboardFee()
Definition: coincontroldialog.cpp:273
CoinControlDialog::VOutRole
@ VOutRole
Definition: coincontroldialog.h:88
addresstablemodel.h
interfaces::WalletTxOut::time
int64_t time
Definition: wallet.h:409
CCoinControl::UnSelect
void UnSelect(const COutPoint &output)
Definition: coincontrol.h:74
WITNESS_SCALE_FACTOR
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
interfaces::Wallet::getCoins
virtual std::vector< WalletTxOut > getCoins(const std::vector< COutPoint > &outputs)=0
Return wallet transaction output information.
EncodeDestination
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:256
CoinControlDialog::COLUMN_CONFIRMATIONS
@ COLUMN_CONFIRMATIONS
Definition: coincontroldialog.h:82
BitcoinUnits::BTC
@ BTC
Definition: bitcoinunits.h:43
BitcoinUnits::format
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
Definition: bitcoinunits.cpp:101