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