Bitcoin Core  22.99.0
P2P Digital Currency
coincontroldialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2021 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 
35 using wallet::MIN_CHANGE;
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, nullptr /* returned_target */, nullptr /* reason */);
481 
482  if (nPayAmount > 0)
483  {
484  nChange = nAmount - nPayAmount;
486  nChange -= nPayFee;
487 
488  // Never create dust outputs; if we would, just add the dust to the fee.
489  if (nChange > 0 && nChange < MIN_CHANGE)
490  {
491  // Assumes a p2pkh script size
492  CTxOut txout(nChange, CScript() << std::vector<unsigned char>(24, 0));
493  if (IsDust(txout, model->node().getDustRelayFee()))
494  {
495  nPayFee += nChange;
496  nChange = 0;
498  nBytes -= 34; // we didn't detect lack of change above
499  }
500  }
501 
502  if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
503  nBytes -= 34;
504  }
505 
506  // after fee
507  nAfterFee = std::max<CAmount>(nAmount - nPayFee, 0);
508  }
509 
510  // actually update labels
511  int nDisplayUnit = BitcoinUnits::BTC;
512  if (model && model->getOptionsModel())
513  nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
514 
515  QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
516  QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
517  QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
518  QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
519  QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
520  QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
521  QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
522 
523  // enable/disable "dust" and "change"
524  dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
525  dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
526  dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
527  dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
528 
529  // stats
530  l1->setText(QString::number(nQuantity)); // Quantity
531  l2->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
532  l3->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
533  l4->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
534  l5->setText(((nBytes > 0) ? ASYMP_UTF8 : "") + QString::number(nBytes)); // Bytes
535  l7->setText(fDust ? tr("yes") : tr("no")); // Dust
536  l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
537  if (nPayFee > 0)
538  {
539  l3->setText(ASYMP_UTF8 + l3->text());
540  l4->setText(ASYMP_UTF8 + l4->text());
541  if (nChange > 0 && !CoinControlDialog::fSubtractFeeFromAmount)
542  l8->setText(ASYMP_UTF8 + l8->text());
543  }
544 
545  // turn label red when dust
546  l7->setStyleSheet((fDust) ? "color:red;" : "");
547 
548  // tool tips
549  QString toolTipDust = tr("This label turns red if any recipient receives an amount smaller than the current dust threshold.");
550 
551  // how many satoshis the estimated fee can vary per byte we guess wrong
552  double dFeeVary = (nBytes != 0) ? (double)nPayFee / nBytes : 0;
553 
554  QString toolTip4 = tr("Can vary +/- %1 satoshi(s) per input.").arg(dFeeVary);
555 
556  l3->setToolTip(toolTip4);
557  l4->setToolTip(toolTip4);
558  l7->setToolTip(toolTipDust);
559  l8->setToolTip(toolTip4);
560  dialog->findChild<QLabel *>("labelCoinControlFeeText") ->setToolTip(l3->toolTip());
561  dialog->findChild<QLabel *>("labelCoinControlAfterFeeText") ->setToolTip(l4->toolTip());
562  dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
563  dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
564  dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
565 
566  // Insufficient funds
567  QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
568  if (label)
569  label->setVisible(nChange < 0);
570 }
571 
573 {
574  if (e->type() == QEvent::PaletteChange) {
575  updateView();
576  }
577 
578  QDialog::changeEvent(e);
579 }
580 
582 {
584  return;
585 
586  bool treeMode = ui->radioTreeMode->isChecked();
587 
588  ui->treeWidget->clear();
589  ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
590  ui->treeWidget->setAlternatingRowColors(!treeMode);
591  QFlags<Qt::ItemFlag> flgCheckbox = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
592  QFlags<Qt::ItemFlag> flgTristate = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
593 
594  int nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
595 
596  for (const auto& coins : model->wallet().listCoins()) {
597  CCoinControlWidgetItem* itemWalletAddress{nullptr};
598  QString sWalletAddress = QString::fromStdString(EncodeDestination(coins.first));
599  QString sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
600  if (sWalletLabel.isEmpty())
601  sWalletLabel = tr("(no label)");
602 
603  if (treeMode)
604  {
605  // wallet address
606  itemWalletAddress = new CCoinControlWidgetItem(ui->treeWidget);
607 
608  itemWalletAddress->setFlags(flgTristate);
609  itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
610 
611  // label
612  itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
613 
614  // address
615  itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
616  }
617 
618  CAmount nSum = 0;
619  int nChildren = 0;
620  for (const auto& outpair : coins.second) {
621  const COutPoint& output = std::get<0>(outpair);
622  const interfaces::WalletTxOut& out = std::get<1>(outpair);
623  nSum += out.txout.nValue;
624  nChildren++;
625 
626  CCoinControlWidgetItem *itemOutput;
627  if (treeMode) itemOutput = new CCoinControlWidgetItem(itemWalletAddress);
628  else itemOutput = new CCoinControlWidgetItem(ui->treeWidget);
629  itemOutput->setFlags(flgCheckbox);
630  itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
631 
632  // address
633  CTxDestination outputAddress;
634  QString sAddress = "";
635  if(ExtractDestination(out.txout.scriptPubKey, outputAddress))
636  {
637  sAddress = QString::fromStdString(EncodeDestination(outputAddress));
638 
639  // if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
640  if (!treeMode || (!(sAddress == sWalletAddress)))
641  itemOutput->setText(COLUMN_ADDRESS, sAddress);
642  }
643 
644  // label
645  if (!(sAddress == sWalletAddress)) // change
646  {
647  // tooltip from where the change comes from
648  itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
649  itemOutput->setText(COLUMN_LABEL, tr("(change)"));
650  }
651  else if (!treeMode)
652  {
653  QString sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
654  if (sLabel.isEmpty())
655  sLabel = tr("(no label)");
656  itemOutput->setText(COLUMN_LABEL, sLabel);
657  }
658 
659  // amount
660  itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.txout.nValue));
661  itemOutput->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)out.txout.nValue)); // padding so that sorting works correctly
662 
663  // date
664  itemOutput->setText(COLUMN_DATE, GUIUtil::dateTimeStr(out.time));
665  itemOutput->setData(COLUMN_DATE, Qt::UserRole, QVariant((qlonglong)out.time));
666 
667  // confirmations
668  itemOutput->setText(COLUMN_CONFIRMATIONS, QString::number(out.depth_in_main_chain));
669  itemOutput->setData(COLUMN_CONFIRMATIONS, Qt::UserRole, QVariant((qlonglong)out.depth_in_main_chain));
670 
671  // transaction hash
672  itemOutput->setData(COLUMN_ADDRESS, TxHashRole, QString::fromStdString(output.hash.GetHex()));
673 
674  // vout index
675  itemOutput->setData(COLUMN_ADDRESS, VOutRole, output.n);
676 
677  // disable locked coins
678  if (model->wallet().isLockedCoin(output))
679  {
680  m_coin_control.UnSelect(output); // just to be sure
681  itemOutput->setDisabled(true);
682  itemOutput->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
683  }
684 
685  // set checkbox
686  if (m_coin_control.IsSelected(output))
687  itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
688  }
689 
690  // amount
691  if (treeMode)
692  {
693  itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
694  itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
695  itemWalletAddress->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)nSum));
696  }
697  }
698 
699  // expand all partially selected
700  if (treeMode)
701  {
702  for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
703  if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
704  ui->treeWidget->topLevelItem(i)->setExpanded(true);
705  }
706 
707  // sort view
709  ui->treeWidget->setEnabled(true);
710 }
ASYMP_UTF8
#define ASYMP_UTF8
Definition: coincontroldialog.h:30
CoinControlDialog::unlockAction
QAction * unlockAction
Definition: coincontroldialog.h:70
policy.h
WalletModel::getOptionsModel
OptionsModel * getOptionsModel()
Definition: walletmodel.cpp:287
CoinControlDialog::lockCoin
void lockCoin()
Definition: coincontroldialog.cpp:245
CoinControlDialog::platformStyle
const PlatformStyle * platformStyle
Definition: coincontroldialog.h:72
CoinControlDialog::payAmounts
static QList< CAmount > payAmounts
Definition: coincontroldialog.h:53
CoinControlDialog::viewItemChanged
void viewItemChanged(QTreeWidgetItem *, int)
Definition: coincontroldialog.cpp:354
wallet.h
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:52
CoinControlDialog::showMenu
void showMenu(const QPoint &)
Definition: coincontroldialog.cpp:176
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:150
key_io.h
interfaces::WalletTxOut::depth_in_main_chain
int depth_in_main_chain
Definition: wallet.h:419
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:57
CoinControlDialog::updateView
void updateView()
Definition: coincontroldialog.cpp:581
WalletModel::getAddressTableModel
AddressTableModel * getAddressTableModel()
Definition: walletmodel.cpp:292
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
interfaces::Wallet::listLockedCoins
virtual void listLockedCoins(std::vector< COutPoint > &outputs)=0
List locked coins.
CoinControlDialog
Definition: coincontroldialog.h:42
GUIUtil
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:59
CoinControlDialog::sortColumn
int sortColumn
Definition: coincontroldialog.h:63
interfaces::Wallet::unlockCoin
virtual bool unlockCoin(const COutPoint &output)=0
Unlock coin.
CoinControlDialog::clipboardChange
void clipboardChange()
Definition: coincontroldialog.cpp:304
interfaces::WalletTxOut
Wallet transaction output.
Definition: wallet.h:415
CoinControlDialog::copyTransactionOutpoint
void copyTransactionOutpoint()
Definition: coincontroldialog.cpp:235
bitcoin-config.h
interfaces::Wallet::getMinimumFee
virtual CAmount getMinimumFee(unsigned int tx_bytes, const wallet::CCoinControl &coin_control, int *returned_target, FeeReason *reason)=0
Get minimum fee.
CoinControlDialog::radioListMode
void radioListMode(bool)
Definition: coincontroldialog.cpp:347
GUIUtil::dialog_flags
constexpr auto dialog_flags
Definition: guiutil.h:60
CoinControlDialog::model
WalletModel * model
Definition: coincontroldialog.h:62
CoinControlDialog::headerSectionClicked
void headerSectionClicked(int)
Definition: coincontroldialog.cpp:319
CoinControlDialog::clipboardLowOutput
void clipboardLowOutput()
Definition: coincontroldialog.cpp:298
GUIUtil::setClipboard
void setClipboard(const QString &str)
Definition: guiutil.cpp:646
interfaces::Wallet::lockCoin
virtual bool lockCoin(const COutPoint &output, const bool write_to_db)=0
Lock coin.
CoinControlDialog::copyLabel
void copyLabel()
Definition: coincontroldialog.cpp:217
WalletModel::node
interfaces::Node & node() const
Definition: walletmodel.h:144
CTxOut::nValue
CAmount nValue
Definition: transaction.h:131
CoinControlDialog::fSubtractFeeFromAmount
static bool fSubtractFeeFromAmount
Definition: coincontroldialog.h:54
CoinControlDialog::buttonSelectAllClicked
void buttonSelectAllClicked()
Definition: coincontroldialog.cpp:154
BitcoinUnits::removeSpaces
static QString removeSpaces(QString text)
Definition: bitcoinunits.h:99
CoinControlDialog::sortView
void sortView(int, Qt::SortOrder)
Definition: coincontroldialog.cpp:310
WalletModel::wallet
interfaces::Wallet & wallet() const
Definition: walletmodel.h:145
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
CTxOut
An output of a transaction.
Definition: transaction.h:128
CoinControlDialog::contextMenuItem
QTreeWidgetItem * contextMenuItem
Definition: coincontroldialog.h:67
CoinControlDialog::clipboardAmount
void clipboardAmount()
Definition: coincontroldialog.cpp:274
CoinControlDialog::buttonBoxClicked
void buttonBoxClicked(QAbstractButton *)
Definition: coincontroldialog.cpp:147
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:132
node.h
wallet::CCoinControl::ListSelected
void ListSelected(std::vector< COutPoint > &vOutpoints) const
Definition: coincontrol.h:113
CoinControlDialog::clipboardAfterFee
void clipboardAfterFee()
Definition: coincontroldialog.cpp:286
interfaces::Wallet::isLockedCoin
virtual bool isLockedCoin(const COutPoint &output)=0
Return whether coin is locked.
CoinControlDialog::clipboardBytes
void clipboardBytes()
Definition: coincontroldialog.cpp:292
CoinControlDialog::~CoinControlDialog
~CoinControlDialog()
Definition: coincontroldialog.cpp:136
CoinControlDialog::updateLabelLocked
void updateLabelLocked()
Definition: coincontroldialog.cpp:374
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
wallet::CCoinControl::UnSelect
void UnSelect(const COutPoint &output)
Definition: coincontrol.h:103
guiutil.h
CoinControlDialog::TxHashRole
@ TxHashRole
Definition: coincontroldialog.h:89
wallet::CCoinControl::Select
void Select(const COutPoint &output)
Definition: coincontrol.h:92
uint256S
uint256 uint256S(const char *str)
Definition: uint256.h:139
interfaces::WalletTxOut::txout
CTxOut txout
Definition: wallet.h:417
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:69
CCoinControlWidgetItem::operator<
bool operator<(const QTreeWidgetItem &other) const override
Definition: coincontroldialog.cpp:40
CoinControlDialog::unlockCoin
void unlockCoin()
Definition: coincontroldialog.cpp:258
CoinControlDialog::CCoinControlWidgetItem
friend class CCoinControlWidgetItem
Definition: coincontroldialog.h:93
CPubKey::IsCompressed
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:198
platformstyle.h
interfaces::Node::getDustRelayFee
virtual CFeeRate getDustRelayFee()=0
Get dust relay fee.
CoinControlDialog::m_copy_transaction_outpoint_action
QAction * m_copy_transaction_outpoint_action
Definition: coincontroldialog.h:68
PKHash
Definition: standard.h:79
ToKeyID
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:34
CoinControlDialog::ui
Ui::CoinControlDialog * ui
Definition: coincontroldialog.h:60
CPubKey
An encapsulated public key.
Definition: pubkey.h:33
CCoinControlWidgetItem
Definition: coincontroldialog.h:32
Ui
Definition: addressbookpage.h:14
CoinControlDialog::changeEvent
void changeEvent(QEvent *e) override
Definition: coincontroldialog.cpp:572
CoinControlDialog::clipboardQuantity
void clipboardQuantity()
Definition: coincontroldialog.cpp:268
interfaces::Wallet::listCoins
virtual CoinsList listCoins()=0
wallet::MIN_CHANGE
static constexpr CAmount MIN_CHANGE
target minimum change amount
Definition: coinselection.h:17
CoinControlDialog::COLUMN_DATE
@ COLUMN_DATE
Definition: coincontroldialog.h:83
OptionsModel::getDisplayUnit
int getDisplayUnit() const
Definition: optionsmodel.h:89
COutPoint::n
uint32_t n
Definition: transaction.h:30
CoinControlDialog::COLUMN_AMOUNT
@ COLUMN_AMOUNT
Definition: coincontroldialog.h:80
interfaces::Wallet::getPubKey
virtual bool getPubKey(const CScript &script, const CKeyID &address, CPubKey &pub_key)=0
Get public key.
CoinControlDialog::updateLabels
static void updateLabels(wallet::CCoinControl &m_coin_control, WalletModel *, QDialog *)
Definition: coincontroldialog.cpp:386
CoinControlDialog::radioTreeMode
void radioTreeMode(bool)
Definition: coincontroldialog.cpp:340
CoinControlDialog::contextMenu
QMenu * contextMenu
Definition: coincontroldialog.h:66
CoinControlDialog::m_coin_control
wallet::CCoinControl & m_coin_control
Definition: coincontroldialog.h:61
coincontroldialog.h
CoinControlDialog::COLUMN_ADDRESS
@ COLUMN_ADDRESS
Definition: coincontroldialog.h:82
IsDust
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:53
bitcoinunits.h
CoinControlDialog::copyAddress
void copyAddress()
Definition: coincontroldialog.cpp:226
GUIUtil::dateTimeStr
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:79
CoinControlDialog::copyAmount
void copyAmount()
Definition: coincontroldialog.cpp:211
GUIUtil::handleCloseWindowShortcut
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:410
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
wallet::CCoinControl::UnSelectAll
void UnSelectAll()
Definition: coincontrol.h:108
optionsmodel.h
CoinControlDialog::CoinControlDialog
CoinControlDialog(wallet::CCoinControl &coin_control, WalletModel *model, const PlatformStyle *platformStyle, QWidget *parent=nullptr)
Definition: coincontroldialog.cpp:47
wallet::CCoinControl::IsSelected
bool IsSelected(const COutPoint &output) const
Definition: coincontrol.h:72
coincontrol.h
CoinControlDialog::COLUMN_CHECKBOX
@ COLUMN_CHECKBOX
Definition: coincontroldialog.h:79
CoinControlDialog::sortOrder
Qt::SortOrder sortOrder
Definition: coincontroldialog.h:64
CoinControlDialog::COLUMN_LABEL
@ COLUMN_LABEL
Definition: coincontroldialog.h:81
PlatformStyle
Definition: platformstyle.h:13
CoinControlDialog::clipboardFee
void clipboardFee()
Definition: coincontroldialog.cpp:280
CoinControlDialog::VOutRole
@ VOutRole
Definition: coincontroldialog.h:90
wallet::CCoinControl
Coin Control Features.
Definition: coincontrol.h:29
addresstablemodel.h
interfaces::WalletTxOut::time
int64_t time
Definition: wallet.h:418
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:276
CoinControlDialog::COLUMN_CONFIRMATIONS
@ COLUMN_CONFIRMATIONS
Definition: coincontroldialog.h:84
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:103