Bitcoin Core  26.1.0
P2P Digital Currency
coincontroldialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2022 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>
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 <interfaces/node.h>
20 #include <key_io.h>
21 #include <policy/policy.h>
22 #include <wallet/coincontrol.h>
23 #include <wallet/coinselection.h>
24 #include <wallet/wallet.h>
25 
26 #include <QApplication>
27 #include <QCheckBox>
28 #include <QCursor>
29 #include <QDialogButtonBox>
30 #include <QFlags>
31 #include <QIcon>
32 #include <QSettings>
33 #include <QTreeWidget>
34 
36 
37 QList<CAmount> CoinControlDialog::payAmounts;
39 
40 bool CCoinControlWidgetItem::operator<(const QTreeWidgetItem &other) const {
41  int column = treeWidget()->sortColumn();
43  return data(column, Qt::UserRole).toLongLong() < other.data(column, Qt::UserRole).toLongLong();
44  return QTreeWidgetItem::operator<(other);
45 }
46 
47 CoinControlDialog::CoinControlDialog(CCoinControl& coin_control, WalletModel* _model, const PlatformStyle *_platformStyle, QWidget *parent) :
48  QDialog(parent, GUIUtil::dialog_flags),
49  ui(new Ui::CoinControlDialog),
50  m_coin_control(coin_control),
51  model(_model),
52  platformStyle(_platformStyle)
53 {
54  ui->setupUi(this);
55 
56  // context menu
57  contextMenu = new QMenu(this);
58  contextMenu->addAction(tr("&Copy address"), this, &CoinControlDialog::copyAddress);
59  contextMenu->addAction(tr("Copy &label"), this, &CoinControlDialog::copyLabel);
60  contextMenu->addAction(tr("Copy &amount"), this, &CoinControlDialog::copyAmount);
61  m_copy_transaction_outpoint_action = contextMenu->addAction(tr("Copy transaction &ID and output index"), this, &CoinControlDialog::copyTransactionOutpoint);
62  contextMenu->addSeparator();
63  lockAction = contextMenu->addAction(tr("L&ock unspent"), this, &CoinControlDialog::lockCoin);
64  unlockAction = contextMenu->addAction(tr("&Unlock unspent"), this, &CoinControlDialog::unlockCoin);
65  connect(ui->treeWidget, &QWidget::customContextMenuRequested, this, &CoinControlDialog::showMenu);
66 
67  // clipboard actions
68  QAction *clipboardQuantityAction = new QAction(tr("Copy quantity"), this);
69  QAction *clipboardAmountAction = new QAction(tr("Copy amount"), this);
70  QAction *clipboardFeeAction = new QAction(tr("Copy fee"), this);
71  QAction *clipboardAfterFeeAction = new QAction(tr("Copy after fee"), this);
72  QAction *clipboardBytesAction = new QAction(tr("Copy bytes"), this);
73  QAction *clipboardChangeAction = new QAction(tr("Copy change"), this);
74 
75  connect(clipboardQuantityAction, &QAction::triggered, this, &CoinControlDialog::clipboardQuantity);
76  connect(clipboardAmountAction, &QAction::triggered, this, &CoinControlDialog::clipboardAmount);
77  connect(clipboardFeeAction, &QAction::triggered, this, &CoinControlDialog::clipboardFee);
78  connect(clipboardAfterFeeAction, &QAction::triggered, this, &CoinControlDialog::clipboardAfterFee);
79  connect(clipboardBytesAction, &QAction::triggered, this, &CoinControlDialog::clipboardBytes);
80  connect(clipboardChangeAction, &QAction::triggered, this, &CoinControlDialog::clipboardChange);
81 
82  ui->labelCoinControlQuantity->addAction(clipboardQuantityAction);
83  ui->labelCoinControlAmount->addAction(clipboardAmountAction);
84  ui->labelCoinControlFee->addAction(clipboardFeeAction);
85  ui->labelCoinControlAfterFee->addAction(clipboardAfterFeeAction);
86  ui->labelCoinControlBytes->addAction(clipboardBytesAction);
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  m_copy_transaction_outpoint_action->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  m_copy_transaction_outpoint_action->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 and vout index
233 {
234  const QString address = contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString();
235  const QString vout = contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toString();
236  const QString outpoint = QString("%1:%2").arg(address).arg(vout);
237 
238  GUIUtil::setClipboard(outpoint);
239 }
240 
241 // context menu action: lock coin
243 {
244  if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
245  contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
246 
247  COutPoint outpt(uint256S(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
248  model->wallet().lockCoin(outpt, /* write_to_db = */ true);
249  contextMenuItem->setDisabled(true);
250  contextMenuItem->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
252 }
253 
254 // context menu action: unlock coin
256 {
257  COutPoint outpt(uint256S(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
258  model->wallet().unlockCoin(outpt);
259  contextMenuItem->setDisabled(false);
260  contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
262 }
263 
264 // copy label "Quantity" to clipboard
266 {
268 }
269 
270 // copy label "Amount" to clipboard
272 {
273  GUIUtil::setClipboard(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
274 }
275 
276 // copy label "Fee" to clipboard
278 {
279  GUIUtil::setClipboard(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
280 }
281 
282 // copy label "After fee" to clipboard
284 {
285  GUIUtil::setClipboard(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
286 }
287 
288 // copy label "Bytes" to clipboard
290 {
292 }
293 
294 // copy label "Change" to clipboard
296 {
297  GUIUtil::setClipboard(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
298 }
299 
300 // treeview: sort
301 void CoinControlDialog::sortView(int column, Qt::SortOrder order)
302 {
303  sortColumn = column;
304  sortOrder = order;
305  ui->treeWidget->sortItems(column, order);
306  ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
307 }
308 
309 // treeview: clicked on header
311 {
312  if (logicalIndex == COLUMN_CHECKBOX) // click on most left column -> do nothing
313  {
314  ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
315  }
316  else
317  {
318  if (sortColumn == logicalIndex)
319  sortOrder = ((sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder);
320  else
321  {
322  sortColumn = logicalIndex;
323  sortOrder = ((sortColumn == COLUMN_LABEL || sortColumn == COLUMN_ADDRESS) ? Qt::AscendingOrder : Qt::DescendingOrder); // if label or address then default => asc, else default => desc
324  }
325 
327  }
328 }
329 
330 // toggle tree mode
332 {
333  if (checked && model)
334  updateView();
335 }
336 
337 // toggle list mode
339 {
340  if (checked && model)
341  updateView();
342 }
343 
344 // checkbox clicked by user
345 void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
346 {
347  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)
348  {
349  COutPoint outpt(uint256S(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt());
350 
351  if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
352  m_coin_control.UnSelect(outpt);
353  else if (item->isDisabled()) // locked (this happens if "check all" through parent node)
354  item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
355  else
356  m_coin_control.Select(outpt);
357 
358  // selection changed -> update labels
359  if (ui->treeWidget->isEnabled()) // do not update on every click for (un)select all
361  }
362 }
363 
364 // shows count of locked unspent outputs
366 {
367  std::vector<COutPoint> vOutpts;
368  model->wallet().listLockedCoins(vOutpts);
369  if (vOutpts.size() > 0)
370  {
371  ui->labelLocked->setText(tr("(%1 locked)").arg(vOutpts.size()));
372  ui->labelLocked->setVisible(true);
373  }
374  else ui->labelLocked->setVisible(false);
375 }
376 
377 void CoinControlDialog::updateLabels(CCoinControl& m_coin_control, WalletModel *model, QDialog* dialog)
378 {
379  if (!model)
380  return;
381 
382  // nPayAmount
383  CAmount nPayAmount = 0;
384  for (const CAmount &amount : CoinControlDialog::payAmounts) {
385  nPayAmount += amount;
386  }
387 
388  CAmount nAmount = 0;
389  CAmount nPayFee = 0;
390  CAmount nAfterFee = 0;
391  CAmount nChange = 0;
392  unsigned int nBytes = 0;
393  unsigned int nBytesInputs = 0;
394  unsigned int nQuantity = 0;
395  bool fWitness = false;
396 
397  auto vCoinControl{m_coin_control.ListSelected()};
398 
399  size_t i = 0;
400  for (const auto& out : model->wallet().getCoins(vCoinControl)) {
401  if (out.depth_in_main_chain < 0) continue;
402 
403  // unselect already spent, very unlikely scenario, this could happen
404  // when selected are spent elsewhere, like rpc or another computer
405  const COutPoint& outpt = vCoinControl[i++];
406  if (out.is_spent)
407  {
408  m_coin_control.UnSelect(outpt);
409  continue;
410  }
411 
412  // Quantity
413  nQuantity++;
414 
415  // Amount
416  nAmount += out.txout.nValue;
417 
418  // Bytes
419  CTxDestination address;
420  int witnessversion = 0;
421  std::vector<unsigned char> witnessprogram;
422  if (out.txout.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram))
423  {
424  // add input skeleton bytes (outpoint, scriptSig size, nSequence)
425  nBytesInputs += (32 + 4 + 1 + 4);
426 
427  if (witnessversion == 0) { // P2WPKH
428  // 1 WU (witness item count) + 72 WU (ECDSA signature with len byte) + 34 WU (pubkey with len byte)
429  nBytesInputs += 107 / WITNESS_SCALE_FACTOR;
430  } else if (witnessversion == 1) { // P2TR key-path spend
431  // 1 WU (witness item count) + 65 WU (Schnorr signature with len byte)
432  nBytesInputs += 66 / WITNESS_SCALE_FACTOR;
433  } else {
434  // not supported, should be unreachable
435  throw std::runtime_error("Trying to spend future segwit version script");
436  }
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, /*returned_target=*/nullptr, /*reason=*/nullptr);
474 
475  if (nPayAmount > 0)
476  {
477  nChange = nAmount - nPayAmount;
479  nChange -= nPayFee;
480 
481  if (nChange > 0) {
482  // Assumes a p2pkh script size
483  CTxOut txout(nChange, CScript() << std::vector<unsigned char>(24, 0));
484  // Never create dust outputs; if we would, just add the dust to the fee.
485  if (IsDust(txout, model->node().getDustRelayFee()))
486  {
487  nPayFee += nChange;
488  nChange = 0;
490  nBytes -= 34; // we didn't detect lack of change above
491  }
492  }
493 
494  if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
495  nBytes -= 34;
496  }
497 
498  // after fee
499  nAfterFee = std::max<CAmount>(nAmount - nPayFee, 0);
500  }
501 
502  // actually update labels
503  BitcoinUnit nDisplayUnit = BitcoinUnit::BTC;
504  if (model && model->getOptionsModel())
505  nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
506 
507  QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
508  QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
509  QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
510  QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
511  QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
512  QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
513 
514  // enable/disable "change"
515  dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
516  dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
517 
518  // stats
519  l1->setText(QString::number(nQuantity)); // Quantity
520  l2->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
521  l3->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
522  l4->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
523  l5->setText(((nBytes > 0) ? ASYMP_UTF8 : "") + QString::number(nBytes)); // Bytes
524  l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
525  if (nPayFee > 0)
526  {
527  l3->setText(ASYMP_UTF8 + l3->text());
528  l4->setText(ASYMP_UTF8 + l4->text());
529  if (nChange > 0 && !CoinControlDialog::fSubtractFeeFromAmount)
530  l8->setText(ASYMP_UTF8 + l8->text());
531  }
532 
533  // how many satoshis the estimated fee can vary per byte we guess wrong
534  double dFeeVary = (nBytes != 0) ? (double)nPayFee / nBytes : 0;
535 
536  QString toolTip4 = tr("Can vary +/- %1 satoshi(s) per input.").arg(dFeeVary);
537 
538  l3->setToolTip(toolTip4);
539  l4->setToolTip(toolTip4);
540  l8->setToolTip(toolTip4);
541  dialog->findChild<QLabel *>("labelCoinControlFeeText") ->setToolTip(l3->toolTip());
542  dialog->findChild<QLabel *>("labelCoinControlAfterFeeText") ->setToolTip(l4->toolTip());
543  dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
544  dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
545 
546  // Insufficient funds
547  QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
548  if (label)
549  label->setVisible(nChange < 0);
550 }
551 
553 {
554  if (e->type() == QEvent::PaletteChange) {
555  updateView();
556  }
557 
558  QDialog::changeEvent(e);
559 }
560 
562 {
564  return;
565 
566  bool treeMode = ui->radioTreeMode->isChecked();
567 
568  ui->treeWidget->clear();
569  ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
570  ui->treeWidget->setAlternatingRowColors(!treeMode);
571  QFlags<Qt::ItemFlag> flgCheckbox = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
572  QFlags<Qt::ItemFlag> flgTristate = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsAutoTristate;
573 
574  BitcoinUnit nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
575 
576  for (const auto& coins : model->wallet().listCoins()) {
577  CCoinControlWidgetItem* itemWalletAddress{nullptr};
578  QString sWalletAddress = QString::fromStdString(EncodeDestination(coins.first));
579  QString sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
580  if (sWalletLabel.isEmpty())
581  sWalletLabel = tr("(no label)");
582 
583  if (treeMode)
584  {
585  // wallet address
586  itemWalletAddress = new CCoinControlWidgetItem(ui->treeWidget);
587 
588  itemWalletAddress->setFlags(flgTristate);
589  itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
590 
591  // label
592  itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
593 
594  // address
595  itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
596  }
597 
598  CAmount nSum = 0;
599  int nChildren = 0;
600  for (const auto& outpair : coins.second) {
601  const COutPoint& output = std::get<0>(outpair);
602  const interfaces::WalletTxOut& out = std::get<1>(outpair);
603  nSum += out.txout.nValue;
604  nChildren++;
605 
606  CCoinControlWidgetItem *itemOutput;
607  if (treeMode) itemOutput = new CCoinControlWidgetItem(itemWalletAddress);
608  else itemOutput = new CCoinControlWidgetItem(ui->treeWidget);
609  itemOutput->setFlags(flgCheckbox);
610  itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
611 
612  // address
613  CTxDestination outputAddress;
614  QString sAddress = "";
615  if(ExtractDestination(out.txout.scriptPubKey, outputAddress))
616  {
617  sAddress = QString::fromStdString(EncodeDestination(outputAddress));
618 
619  // if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
620  if (!treeMode || (!(sAddress == sWalletAddress)))
621  itemOutput->setText(COLUMN_ADDRESS, sAddress);
622  }
623 
624  // label
625  if (!(sAddress == sWalletAddress)) // change
626  {
627  // tooltip from where the change comes from
628  itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
629  itemOutput->setText(COLUMN_LABEL, tr("(change)"));
630  }
631  else if (!treeMode)
632  {
633  QString sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
634  if (sLabel.isEmpty())
635  sLabel = tr("(no label)");
636  itemOutput->setText(COLUMN_LABEL, sLabel);
637  }
638 
639  // amount
640  itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.txout.nValue));
641  itemOutput->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)out.txout.nValue)); // padding so that sorting works correctly
642 
643  // date
644  itemOutput->setText(COLUMN_DATE, GUIUtil::dateTimeStr(out.time));
645  itemOutput->setData(COLUMN_DATE, Qt::UserRole, QVariant((qlonglong)out.time));
646 
647  // confirmations
648  itemOutput->setText(COLUMN_CONFIRMATIONS, QString::number(out.depth_in_main_chain));
649  itemOutput->setData(COLUMN_CONFIRMATIONS, Qt::UserRole, QVariant((qlonglong)out.depth_in_main_chain));
650 
651  // transaction hash
652  itemOutput->setData(COLUMN_ADDRESS, TxHashRole, QString::fromStdString(output.hash.GetHex()));
653 
654  // vout index
655  itemOutput->setData(COLUMN_ADDRESS, VOutRole, output.n);
656 
657  // disable locked coins
658  if (model->wallet().isLockedCoin(output))
659  {
660  m_coin_control.UnSelect(output); // just to be sure
661  itemOutput->setDisabled(true);
662  itemOutput->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
663  }
664 
665  // set checkbox
666  if (m_coin_control.IsSelected(output))
667  itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
668  }
669 
670  // amount
671  if (treeMode)
672  {
673  itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
674  itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
675  itemWalletAddress->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)nSum));
676  }
677  }
678 
679  // expand all partially selected
680  if (treeMode)
681  {
682  for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
683  if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
684  ui->treeWidget->topLevelItem(i)->setExpanded(true);
685  }
686 
687  // sort view
689  ui->treeWidget->setEnabled(true);
690 }
const PlatformStyle * platformStyle
void viewItemChanged(QTreeWidgetItem *, int)
virtual CoinsList listCoins()=0
OptionsModel * getOptionsModel() const
Unit
Bitcoin units.
Definition: bitcoinunits.h:42
interfaces::Wallet & wallet() const
Definition: walletmodel.h:142
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:60
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
CoinControlDialog(wallet::CCoinControl &coin_control, WalletModel *model, const PlatformStyle *platformStyle, QWidget *parent=nullptr)
QRadioButton * radioTreeMode
virtual bool getPubKey(const CScript &script, const CKeyID &address, CPubKey &pub_key)=0
Get public key.
void setupUi(QDialog *CoinControlDialog)
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:90
BitcoinUnit getDisplayUnit() const
Definition: optionsmodel.h:94
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
virtual bool lockCoin(const COutPoint &output, const bool write_to_db)=0
Lock coin.
#define ASYMP_UTF8
bool IsSelected(const COutPoint &output) const
Returns true if the given output is pre-selected.
Definition: coincontrol.cpp:20
virtual CFeeRate getDustRelayFee()=0
Get dust relay fee.
static QString formatWithUnit(Unit unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD)
Format as string (with unit)
constexpr auto dialog_flags
Definition: guiutil.h:60
void UnSelect(const COutPoint &output)
Unselects the given output.
Definition: coincontrol.cpp:51
void Select(const COutPoint &output)
Lock-in the given output for spending.
Definition: coincontrol.cpp:40
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
QAction * m_copy_transaction_outpoint_action
static QList< CAmount > payAmounts
QPushButton * pushButtonSelectAll
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
Definition: addresstype.cpp:49
Ui::CoinControlDialog * ui
virtual bool unlockCoin(const COutPoint &output)=0
Unlock coin.
static QString format(Unit unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
void setClipboard(const QString &str)
Definition: guiutil.cpp:656
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:419
wallet::CCoinControl & m_coin_control
CoinControlTreeWidget * treeWidget
uint256 uint256S(const char *str)
Definition: uint256.h:119
An encapsulated public key.
Definition: pubkey.h:33
uint32_t n
Definition: transaction.h:39
QString labelForAddress(const QString &address) const
Look up label for address in address book, if not found return empty string.
void UnSelectAll()
Unselects all outputs.
Definition: coincontrol.cpp:56
friend class CCoinControlWidgetItem
An output of a transaction.
Definition: transaction.h:157
void changeEvent(QEvent *e) override
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:35
interfaces::Node & node() const
Definition: walletmodel.h:141
virtual std::vector< WalletTxOut > getCoins(const std::vector< COutPoint > &outputs)=0
Return wallet transaction output information.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:129
bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:609
static bool fSubtractFeeFromAmount
bool operator<(const QTreeWidgetItem &other) const override
QTreeWidgetItem * contextMenuItem
constexpr const unsigned char * data() const
Definition: uint256.h:65
static void updateLabels(wallet::CCoinControl &m_coin_control, WalletModel *, QDialog *)
virtual void listLockedCoins(std::vector< COutPoint > &outputs)=0
List locked coins.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:412
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:51
static QString removeSpaces(QString text)
Definition: bitcoinunits.h:98
std::string GetHex() const
Definition: uint256.cpp:11
void sortView(int, Qt::SortOrder)
virtual bool isLockedCoin(const COutPoint &output)=0
Return whether coin is locked.
Qt::SortOrder sortOrder
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:287
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:65
AddressTableModel * getAddressTableModel() const
void buttonBoxClicked(QAbstractButton *)
QRadioButton * radioListMode
WalletModel * model
void showMenu(const QPoint &)
QDialogButtonBox * buttonBox
Wallet transaction output.
Definition: wallet.h:424
std::vector< COutPoint > ListSelected() const
List the selected inputs.
Definition: coincontrol.cpp:61
virtual CAmount getMinimumFee(unsigned int tx_bytes, const wallet::CCoinControl &coin_control, int *returned_target, FeeReason *reason)=0
Get minimum fee.
Coin Control Features.
Definition: coincontrol.h:28
CKeyID ToKeyID(const PKHash &key_hash)
Definition: addresstype.cpp:29
uint256 hash
Definition: transaction.h:38
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:204