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