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