Bitcoin Core  0.18.99
P2P Digital Currency
coincontroldialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2018 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/fees.h>
23 #include <policy/policy.h>
24 #include <wallet/wallet.h>
25 
26 #include <QApplication>
27 #include <QCheckBox>
28 #include <QCursor>
29 #include <QDialogButtonBox>
30 #include <QFlags>
31 #include <QIcon>
32 #include <QSettings>
33 #include <QTreeWidget>
34 
35 QList<CAmount> CoinControlDialog::payAmounts;
37 
38 bool CCoinControlWidgetItem::operator<(const QTreeWidgetItem &other) const {
39  int column = treeWidget()->sortColumn();
41  return data(column, Qt::UserRole).toLongLong() < other.data(column, Qt::UserRole).toLongLong();
42  return QTreeWidgetItem::operator<(other);
43 }
44 
45 CoinControlDialog::CoinControlDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
46  QDialog(parent),
47  ui(new Ui::CoinControlDialog),
48  model(nullptr),
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 }
139 
141 {
142  QSettings settings;
143  settings.setValue("nCoinControlMode", ui->radioListMode->isChecked());
144  settings.setValue("nCoinControlSortColumn", sortColumn);
145  settings.setValue("nCoinControlSortOrder", (int)sortOrder);
146 
147  delete ui;
148 }
149 
151 {
152  this->model = _model;
153 
154  if(_model && _model->getOptionsModel() && _model->getAddressTableModel())
155  {
156  updateView();
158  CoinControlDialog::updateLabels(_model, this);
159  }
160 }
161 
162 // ok button
163 void CoinControlDialog::buttonBoxClicked(QAbstractButton* button)
164 {
165  if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
166  done(QDialog::Accepted); // closes the dialog
167 }
168 
169 // (un)select all
171 {
172  Qt::CheckState state = Qt::Checked;
173  for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
174  {
175  if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != Qt::Unchecked)
176  {
177  state = Qt::Unchecked;
178  break;
179  }
180  }
181  ui->treeWidget->setEnabled(false);
182  for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
183  if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != state)
184  ui->treeWidget->topLevelItem(i)->setCheckState(COLUMN_CHECKBOX, state);
185  ui->treeWidget->setEnabled(true);
186  if (state == Qt::Unchecked)
187  coinControl()->UnSelectAll(); // just to be sure
189 }
190 
191 // context menu
192 void CoinControlDialog::showMenu(const QPoint &point)
193 {
194  QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
195  if(item)
196  {
197  contextMenuItem = item;
198 
199  // disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
200  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)
201  {
202  copyTransactionHashAction->setEnabled(true);
203  if (model->wallet().isLockedCoin(COutPoint(uint256S(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt())))
204  {
205  lockAction->setEnabled(false);
206  unlockAction->setEnabled(true);
207  }
208  else
209  {
210  lockAction->setEnabled(true);
211  unlockAction->setEnabled(false);
212  }
213  }
214  else // this means click on parent node in tree mode -> disable all
215  {
216  copyTransactionHashAction->setEnabled(false);
217  lockAction->setEnabled(false);
218  unlockAction->setEnabled(false);
219  }
220 
221  // show context menu
222  contextMenu->exec(QCursor::pos());
223  }
224 }
225 
226 // context menu action: copy amount
228 {
230 }
231 
232 // context menu action: copy label
234 {
235  if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_LABEL).length() == 0 && contextMenuItem->parent())
237  else
239 }
240 
241 // context menu action: copy address
243 {
244  if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_ADDRESS).length() == 0 && contextMenuItem->parent())
246  else
248 }
249 
250 // context menu action: copy transaction id
252 {
254 }
255 
256 // context menu action: lock coin
258 {
259  if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
260  contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
261 
262  COutPoint outpt(uint256S(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
263  model->wallet().lockCoin(outpt);
264  contextMenuItem->setDisabled(true);
265  contextMenuItem->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
267 }
268 
269 // context menu action: unlock coin
271 {
272  COutPoint outpt(uint256S(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
273  model->wallet().unlockCoin(outpt);
274  contextMenuItem->setDisabled(false);
275  contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
277 }
278 
279 // copy label "Quantity" to clipboard
281 {
282  GUIUtil::setClipboard(ui->labelCoinControlQuantity->text());
283 }
284 
285 // copy label "Amount" to clipboard
287 {
288  GUIUtil::setClipboard(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
289 }
290 
291 // copy label "Fee" to clipboard
293 {
294  GUIUtil::setClipboard(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
295 }
296 
297 // copy label "After fee" to clipboard
299 {
300  GUIUtil::setClipboard(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
301 }
302 
303 // copy label "Bytes" to clipboard
305 {
306  GUIUtil::setClipboard(ui->labelCoinControlBytes->text().replace(ASYMP_UTF8, ""));
307 }
308 
309 // copy label "Dust" to clipboard
311 {
312  GUIUtil::setClipboard(ui->labelCoinControlLowOutput->text());
313 }
314 
315 // copy label "Change" to clipboard
317 {
318  GUIUtil::setClipboard(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
319 }
320 
321 // treeview: sort
322 void CoinControlDialog::sortView(int column, Qt::SortOrder order)
323 {
324  sortColumn = column;
325  sortOrder = order;
326  ui->treeWidget->sortItems(column, order);
327  ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
328 }
329 
330 // treeview: clicked on header
332 {
333  if (logicalIndex == COLUMN_CHECKBOX) // click on most left column -> do nothing
334  {
335  ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
336  }
337  else
338  {
339  if (sortColumn == logicalIndex)
340  sortOrder = ((sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder);
341  else
342  {
343  sortColumn = logicalIndex;
344  sortOrder = ((sortColumn == COLUMN_LABEL || sortColumn == COLUMN_ADDRESS) ? Qt::AscendingOrder : Qt::DescendingOrder); // if label or address then default => asc, else default => desc
345  }
346 
348  }
349 }
350 
351 // toggle tree mode
353 {
354  if (checked && model)
355  updateView();
356 }
357 
358 // toggle list mode
360 {
361  if (checked && model)
362  updateView();
363 }
364 
365 // checkbox clicked by user
366 void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
367 {
368  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)
369  {
370  COutPoint outpt(uint256S(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt());
371 
372  if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
373  coinControl()->UnSelect(outpt);
374  else if (item->isDisabled()) // locked (this happens if "check all" through parent node)
375  item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
376  else
377  coinControl()->Select(outpt);
378 
379  // selection changed -> update labels
380  if (ui->treeWidget->isEnabled()) // do not update on every click for (un)select all
382  }
383 
384  // TODO: Remove this temporary qt5 fix after Qt5.3 and Qt5.4 are no longer used.
385  // Fixed in Qt5.5 and above: https://bugreports.qt.io/browse/QTBUG-43473
386  else if (column == COLUMN_CHECKBOX && item->childCount() > 0)
387  {
388  if (item->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked && item->child(0)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
389  item->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
390  }
391 }
392 
393 // shows count of locked unspent outputs
395 {
396  std::vector<COutPoint> vOutpts;
397  model->wallet().listLockedCoins(vOutpts);
398  if (vOutpts.size() > 0)
399  {
400  ui->labelLocked->setText(tr("(%1 locked)").arg(vOutpts.size()));
401  ui->labelLocked->setVisible(true);
402  }
403  else ui->labelLocked->setVisible(false);
404 }
405 
407 {
408  if (!model)
409  return;
410 
411  // nPayAmount
412  CAmount nPayAmount = 0;
413  bool fDust = false;
414  CMutableTransaction txDummy;
415  for (const CAmount &amount : CoinControlDialog::payAmounts)
416  {
417  nPayAmount += amount;
418 
419  if (amount > 0)
420  {
421  // Assumes a p2pkh script size
422  CTxOut txout(amount, CScript() << std::vector<unsigned char>(24, 0));
423  txDummy.vout.push_back(txout);
424  fDust |= IsDust(txout, model->node().getDustRelayFee());
425  }
426  }
427 
428  CAmount nAmount = 0;
429  CAmount nPayFee = 0;
430  CAmount nAfterFee = 0;
431  CAmount nChange = 0;
432  unsigned int nBytes = 0;
433  unsigned int nBytesInputs = 0;
434  unsigned int nQuantity = 0;
435  bool fWitness = false;
436 
437  std::vector<COutPoint> vCoinControl;
438  coinControl()->ListSelected(vCoinControl);
439 
440  size_t i = 0;
441  for (const auto& out : model->wallet().getCoins(vCoinControl)) {
442  if (out.depth_in_main_chain < 0) continue;
443 
444  // unselect already spent, very unlikely scenario, this could happen
445  // when selected are spent elsewhere, like rpc or another computer
446  const COutPoint& outpt = vCoinControl[i++];
447  if (out.is_spent)
448  {
449  coinControl()->UnSelect(outpt);
450  continue;
451  }
452 
453  // Quantity
454  nQuantity++;
455 
456  // Amount
457  nAmount += out.txout.nValue;
458 
459  // Bytes
460  CTxDestination address;
461  int witnessversion = 0;
462  std::vector<unsigned char> witnessprogram;
463  if (out.txout.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram))
464  {
465  nBytesInputs += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
466  fWitness = true;
467  }
468  else if(ExtractDestination(out.txout.scriptPubKey, address))
469  {
470  CPubKey pubkey;
471  PKHash *pkhash = boost::get<PKHash>(&address);
472  if (pkhash && model->wallet().getPubKey(CKeyID(*pkhash), pubkey))
473  {
474  nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
475  }
476  else
477  nBytesInputs += 148; // in all error cases, simply assume 148 here
478  }
479  else nBytesInputs += 148;
480  }
481 
482  // calculation
483  if (nQuantity > 0)
484  {
485  // Bytes
486  nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
487  if (fWitness)
488  {
489  // there is some fudging in these numbers related to the actual virtual transaction size calculation that will keep this estimate from being exact.
490  // usually, the result will be an overestimate within a couple of satoshis so that the confirmation dialog ends up displaying a slightly smaller fee.
491  // 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.
492  nBytes += 2; // account for the serialized marker and flag bytes
493  nBytes += nQuantity; // account for the witness byte that holds the number of stack items for each input.
494  }
495 
496  // 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
498  if (nAmount - nPayAmount == 0)
499  nBytes -= 34;
500 
501  // Fee
502  nPayFee = model->wallet().getMinimumFee(nBytes, *coinControl(), nullptr /* returned_target */, nullptr /* reason */);
503 
504  if (nPayAmount > 0)
505  {
506  nChange = nAmount - nPayAmount;
508  nChange -= nPayFee;
509 
510  // Never create dust outputs; if we would, just add the dust to the fee.
511  if (nChange > 0 && nChange < MIN_CHANGE)
512  {
513  // Assumes a p2pkh script size
514  CTxOut txout(nChange, CScript() << std::vector<unsigned char>(24, 0));
515  if (IsDust(txout, model->node().getDustRelayFee()))
516  {
517  nPayFee += nChange;
518  nChange = 0;
520  nBytes -= 34; // we didn't detect lack of change above
521  }
522  }
523 
524  if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
525  nBytes -= 34;
526  }
527 
528  // after fee
529  nAfterFee = std::max<CAmount>(nAmount - nPayFee, 0);
530  }
531 
532  // actually update labels
533  int nDisplayUnit = BitcoinUnits::BTC;
534  if (model && model->getOptionsModel())
535  nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
536 
537  QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
538  QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
539  QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
540  QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
541  QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
542  QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
543  QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
544 
545  // enable/disable "dust" and "change"
546  dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
547  dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
548  dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
549  dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
550 
551  // stats
552  l1->setText(QString::number(nQuantity)); // Quantity
553  l2->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
554  l3->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
555  l4->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
556  l5->setText(((nBytes > 0) ? ASYMP_UTF8 : "") + QString::number(nBytes)); // Bytes
557  l7->setText(fDust ? tr("yes") : tr("no")); // Dust
558  l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
559  if (nPayFee > 0)
560  {
561  l3->setText(ASYMP_UTF8 + l3->text());
562  l4->setText(ASYMP_UTF8 + l4->text());
563  if (nChange > 0 && !CoinControlDialog::fSubtractFeeFromAmount)
564  l8->setText(ASYMP_UTF8 + l8->text());
565  }
566 
567  // turn label red when dust
568  l7->setStyleSheet((fDust) ? "color:red;" : "");
569 
570  // tool tips
571  QString toolTipDust = tr("This label turns red if any recipient receives an amount smaller than the current dust threshold.");
572 
573  // how many satoshis the estimated fee can vary per byte we guess wrong
574  double dFeeVary = (nBytes != 0) ? (double)nPayFee / nBytes : 0;
575 
576  QString toolTip4 = tr("Can vary +/- %1 satoshi(s) per input.").arg(dFeeVary);
577 
578  l3->setToolTip(toolTip4);
579  l4->setToolTip(toolTip4);
580  l7->setToolTip(toolTipDust);
581  l8->setToolTip(toolTip4);
582  dialog->findChild<QLabel *>("labelCoinControlFeeText") ->setToolTip(l3->toolTip());
583  dialog->findChild<QLabel *>("labelCoinControlAfterFeeText") ->setToolTip(l4->toolTip());
584  dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
585  dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
586  dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
587 
588  // Insufficient funds
589  QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
590  if (label)
591  label->setVisible(nChange < 0);
592 }
593 
595 {
596  static CCoinControl coin_control;
597  return &coin_control;
598 }
599 
601 {
603  return;
604 
605  bool treeMode = ui->radioTreeMode->isChecked();
606 
607  ui->treeWidget->clear();
608  ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
609  ui->treeWidget->setAlternatingRowColors(!treeMode);
610  QFlags<Qt::ItemFlag> flgCheckbox = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
611  QFlags<Qt::ItemFlag> flgTristate = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
612 
613  int nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
614 
615  for (const auto& coins : model->wallet().listCoins()) {
616  CCoinControlWidgetItem *itemWalletAddress = new CCoinControlWidgetItem();
617  itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
618  QString sWalletAddress = QString::fromStdString(EncodeDestination(coins.first));
619  QString sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
620  if (sWalletLabel.isEmpty())
621  sWalletLabel = tr("(no label)");
622 
623  if (treeMode)
624  {
625  // wallet address
626  ui->treeWidget->addTopLevelItem(itemWalletAddress);
627 
628  itemWalletAddress->setFlags(flgTristate);
629  itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
630 
631  // label
632  itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
633 
634  // address
635  itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
636  }
637 
638  CAmount nSum = 0;
639  int nChildren = 0;
640  for (const auto& outpair : coins.second) {
641  const COutPoint& output = std::get<0>(outpair);
642  const interfaces::WalletTxOut& out = std::get<1>(outpair);
643  nSum += out.txout.nValue;
644  nChildren++;
645 
646  CCoinControlWidgetItem *itemOutput;
647  if (treeMode) itemOutput = new CCoinControlWidgetItem(itemWalletAddress);
648  else itemOutput = new CCoinControlWidgetItem(ui->treeWidget);
649  itemOutput->setFlags(flgCheckbox);
650  itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
651 
652  // address
653  CTxDestination outputAddress;
654  QString sAddress = "";
655  if(ExtractDestination(out.txout.scriptPubKey, outputAddress))
656  {
657  sAddress = QString::fromStdString(EncodeDestination(outputAddress));
658 
659  // if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
660  if (!treeMode || (!(sAddress == sWalletAddress)))
661  itemOutput->setText(COLUMN_ADDRESS, sAddress);
662  }
663 
664  // label
665  if (!(sAddress == sWalletAddress)) // change
666  {
667  // tooltip from where the change comes from
668  itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
669  itemOutput->setText(COLUMN_LABEL, tr("(change)"));
670  }
671  else if (!treeMode)
672  {
673  QString sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
674  if (sLabel.isEmpty())
675  sLabel = tr("(no label)");
676  itemOutput->setText(COLUMN_LABEL, sLabel);
677  }
678 
679  // amount
680  itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.txout.nValue));
681  itemOutput->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)out.txout.nValue)); // padding so that sorting works correctly
682 
683  // date
684  itemOutput->setText(COLUMN_DATE, GUIUtil::dateTimeStr(out.time));
685  itemOutput->setData(COLUMN_DATE, Qt::UserRole, QVariant((qlonglong)out.time));
686 
687  // confirmations
688  itemOutput->setText(COLUMN_CONFIRMATIONS, QString::number(out.depth_in_main_chain));
689  itemOutput->setData(COLUMN_CONFIRMATIONS, Qt::UserRole, QVariant((qlonglong)out.depth_in_main_chain));
690 
691  // transaction hash
692  itemOutput->setData(COLUMN_ADDRESS, TxHashRole, QString::fromStdString(output.hash.GetHex()));
693 
694  // vout index
695  itemOutput->setData(COLUMN_ADDRESS, VOutRole, output.n);
696 
697  // disable locked coins
698  if (model->wallet().isLockedCoin(output))
699  {
700  coinControl()->UnSelect(output); // just to be sure
701  itemOutput->setDisabled(true);
702  itemOutput->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
703  }
704 
705  // set checkbox
706  if (coinControl()->IsSelected(output))
707  itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
708  }
709 
710  // amount
711  if (treeMode)
712  {
713  itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
714  itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
715  itemWalletAddress->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)nSum));
716  }
717  }
718 
719  // expand all partially selected
720  if (treeMode)
721  {
722  for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
723  if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
724  ui->treeWidget->topLevelItem(i)->setExpanded(true);
725  }
726 
727  // sort view
729  ui->treeWidget->setEnabled(true);
730 }
CAmount nValue
Definition: transaction.h:136
uint8_t data[WIDTH]
Definition: uint256.h:22
const PlatformStyle * platformStyle
void viewItemChanged(QTreeWidgetItem *, int)
virtual CoinsList listCoins()=0
interfaces::Wallet & wallet() const
Definition: walletmodel.h:223
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
void ListSelected(std::vector< COutPoint > &vOutpoints) const
Definition: coincontrol.h:81
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:68
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:71
#define ASYMP_UTF8
virtual CFeeRate getDustRelayFee()=0
Get dust relay fee.
Coin Control Features.
Definition: coincontrol.h:19
int getDisplayUnit() const
Definition: optionsmodel.h:74
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:785
void Select(const COutPoint &output)
Definition: coincontrol.h:66
uint256 uint256S(const char *str)
Definition: uint256.h:131
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:222
std::vector< CTxOut > vout
Definition: transaction.h:369
void UnSelectAll()
Definition: coincontrol.h:76
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:361
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:125
bool IsSelected(const COutPoint &output) const
Definition: coincontrol.h:61
static QString removeSpaces(QString text)
Definition: bitcoinunits.h:113
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
std::string GetHex() const
Definition: uint256.cpp:21
void sortView(int, Qt::SortOrder)
virtual bool getPubKey(const CKeyID &address, CPubKey &pub_key)=0
Get public key.
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:211
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:139
Wallet transaction output.
Definition: wallet.h:362
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