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