Merge #10502: scripted-diff: Remove BOOST_FOREACH, Q_FOREACH and PAIRTYPE
[bitcoinplatinum.git] / src / qt / coincontroldialog.cpp
blob06b599f3e2068251e737abe713de86f02510081b
1 // Copyright (c) 2011-2016 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.
5 #include "coincontroldialog.h"
6 #include "ui_coincontroldialog.h"
8 #include "addresstablemodel.h"
9 #include "bitcoinunits.h"
10 #include "guiutil.h"
11 #include "optionsmodel.h"
12 #include "platformstyle.h"
13 #include "txmempool.h"
14 #include "walletmodel.h"
16 #include "wallet/coincontrol.h"
17 #include "init.h"
18 #include "policy/fees.h"
19 #include "policy/policy.h"
20 #include "validation.h" // For mempool
21 #include "wallet/wallet.h"
23 #include <QApplication>
24 #include <QCheckBox>
25 #include <QCursor>
26 #include <QDialogButtonBox>
27 #include <QFlags>
28 #include <QIcon>
29 #include <QSettings>
30 #include <QString>
31 #include <QTreeWidget>
32 #include <QTreeWidgetItem>
34 QList<CAmount> CoinControlDialog::payAmounts;
35 CCoinControl* CoinControlDialog::coinControl = new CCoinControl();
36 bool CoinControlDialog::fSubtractFeeFromAmount = false;
38 bool CCoinControlWidgetItem::operator<(const QTreeWidgetItem &other) const {
39 int column = treeWidget()->sortColumn();
40 if (column == CoinControlDialog::COLUMN_AMOUNT || column == CoinControlDialog::COLUMN_DATE || column == CoinControlDialog::COLUMN_CONFIRMATIONS)
41 return data(column, Qt::UserRole).toLongLong() < other.data(column, Qt::UserRole).toLongLong();
42 return QTreeWidgetItem::operator<(other);
45 CoinControlDialog::CoinControlDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
46 QDialog(parent),
47 ui(new Ui::CoinControlDialog),
48 model(0),
49 platformStyle(_platformStyle)
51 ui->setupUi(this);
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
61 // context menu
62 contextMenu = new QMenu(this);
63 contextMenu->addAction(copyAddressAction);
64 contextMenu->addAction(copyLabelAction);
65 contextMenu->addAction(copyAmountAction);
66 contextMenu->addAction(copyTransactionHashAction);
67 contextMenu->addSeparator();
68 contextMenu->addAction(lockAction);
69 contextMenu->addAction(unlockAction);
71 // context menu signals
72 connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
73 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
74 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
75 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
76 connect(copyTransactionHashAction, SIGNAL(triggered()), this, SLOT(copyTransactionHash()));
77 connect(lockAction, SIGNAL(triggered()), this, SLOT(lockCoin()));
78 connect(unlockAction, SIGNAL(triggered()), this, SLOT(unlockCoin()));
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);
89 connect(clipboardQuantityAction, SIGNAL(triggered()), this, SLOT(clipboardQuantity()));
90 connect(clipboardAmountAction, SIGNAL(triggered()), this, SLOT(clipboardAmount()));
91 connect(clipboardFeeAction, SIGNAL(triggered()), this, SLOT(clipboardFee()));
92 connect(clipboardAfterFeeAction, SIGNAL(triggered()), this, SLOT(clipboardAfterFee()));
93 connect(clipboardBytesAction, SIGNAL(triggered()), this, SLOT(clipboardBytes()));
94 connect(clipboardLowOutputAction, SIGNAL(triggered()), this, SLOT(clipboardLowOutput()));
95 connect(clipboardChangeAction, SIGNAL(triggered()), this, SLOT(clipboardChange()));
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);
105 // toggle tree/list mode
106 connect(ui->radioTreeMode, SIGNAL(toggled(bool)), this, SLOT(radioTreeMode(bool)));
107 connect(ui->radioListMode, SIGNAL(toggled(bool)), this, SLOT(radioListMode(bool)));
109 // click on checkbox
110 connect(ui->treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(viewItemChanged(QTreeWidgetItem*, int)));
112 // click on header
113 #if QT_VERSION < 0x050000
114 ui->treeWidget->header()->setClickable(true);
115 #else
116 ui->treeWidget->header()->setSectionsClickable(true);
117 #endif
118 connect(ui->treeWidget->header(), SIGNAL(sectionClicked(int)), this, SLOT(headerSectionClicked(int)));
120 // ok button
121 connect(ui->buttonBox, SIGNAL(clicked( QAbstractButton*)), this, SLOT(buttonBoxClicked(QAbstractButton*)));
123 // (un)select all
124 connect(ui->pushButtonSelectAll, SIGNAL(clicked()), this, SLOT(buttonSelectAllClicked()));
126 // change coin control first column label due Qt4 bug.
127 // see https://github.com/bitcoin/bitcoin/issues/5716
128 ui->treeWidget->headerItem()->setText(COLUMN_CHECKBOX, QString());
130 ui->treeWidget->setColumnWidth(COLUMN_CHECKBOX, 84);
131 ui->treeWidget->setColumnWidth(COLUMN_AMOUNT, 110);
132 ui->treeWidget->setColumnWidth(COLUMN_LABEL, 190);
133 ui->treeWidget->setColumnWidth(COLUMN_ADDRESS, 320);
134 ui->treeWidget->setColumnWidth(COLUMN_DATE, 130);
135 ui->treeWidget->setColumnWidth(COLUMN_CONFIRMATIONS, 110);
136 ui->treeWidget->setColumnHidden(COLUMN_TXHASH, true); // store transaction hash in this column, but don't show it
137 ui->treeWidget->setColumnHidden(COLUMN_VOUT_INDEX, true); // store vout index in this column, but don't show it
139 // default view is sorted by amount desc
140 sortView(COLUMN_AMOUNT, Qt::DescendingOrder);
142 // restore list mode and sortorder as a convenience feature
143 QSettings settings;
144 if (settings.contains("nCoinControlMode") && !settings.value("nCoinControlMode").toBool())
145 ui->radioTreeMode->click();
146 if (settings.contains("nCoinControlSortColumn") && settings.contains("nCoinControlSortOrder"))
147 sortView(settings.value("nCoinControlSortColumn").toInt(), ((Qt::SortOrder)settings.value("nCoinControlSortOrder").toInt()));
150 CoinControlDialog::~CoinControlDialog()
152 QSettings settings;
153 settings.setValue("nCoinControlMode", ui->radioListMode->isChecked());
154 settings.setValue("nCoinControlSortColumn", sortColumn);
155 settings.setValue("nCoinControlSortOrder", (int)sortOrder);
157 delete ui;
160 void CoinControlDialog::setModel(WalletModel *_model)
162 this->model = _model;
164 if(_model && _model->getOptionsModel() && _model->getAddressTableModel())
166 updateView();
167 updateLabelLocked();
168 CoinControlDialog::updateLabels(_model, this);
172 // ok button
173 void CoinControlDialog::buttonBoxClicked(QAbstractButton* button)
175 if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
176 done(QDialog::Accepted); // closes the dialog
179 // (un)select all
180 void CoinControlDialog::buttonSelectAllClicked()
182 Qt::CheckState state = Qt::Checked;
183 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
185 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != Qt::Unchecked)
187 state = Qt::Unchecked;
188 break;
191 ui->treeWidget->setEnabled(false);
192 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
193 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != state)
194 ui->treeWidget->topLevelItem(i)->setCheckState(COLUMN_CHECKBOX, state);
195 ui->treeWidget->setEnabled(true);
196 if (state == Qt::Unchecked)
197 coinControl->UnSelectAll(); // just to be sure
198 CoinControlDialog::updateLabels(model, this);
201 // context menu
202 void CoinControlDialog::showMenu(const QPoint &point)
204 QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
205 if(item)
207 contextMenuItem = item;
209 // disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
210 if (item->text(COLUMN_TXHASH).length() == 64) // transaction hash is 64 characters (this means its a child node, so its not a parent node in tree mode)
212 copyTransactionHashAction->setEnabled(true);
213 if (model->isLockedCoin(uint256S(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt()))
215 lockAction->setEnabled(false);
216 unlockAction->setEnabled(true);
218 else
220 lockAction->setEnabled(true);
221 unlockAction->setEnabled(false);
224 else // this means click on parent node in tree mode -> disable all
226 copyTransactionHashAction->setEnabled(false);
227 lockAction->setEnabled(false);
228 unlockAction->setEnabled(false);
231 // show context menu
232 contextMenu->exec(QCursor::pos());
236 // context menu action: copy amount
237 void CoinControlDialog::copyAmount()
239 GUIUtil::setClipboard(BitcoinUnits::removeSpaces(contextMenuItem->text(COLUMN_AMOUNT)));
242 // context menu action: copy label
243 void CoinControlDialog::copyLabel()
245 if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_LABEL).length() == 0 && contextMenuItem->parent())
246 GUIUtil::setClipboard(contextMenuItem->parent()->text(COLUMN_LABEL));
247 else
248 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_LABEL));
251 // context menu action: copy address
252 void CoinControlDialog::copyAddress()
254 if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_ADDRESS).length() == 0 && contextMenuItem->parent())
255 GUIUtil::setClipboard(contextMenuItem->parent()->text(COLUMN_ADDRESS));
256 else
257 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_ADDRESS));
260 // context menu action: copy transaction id
261 void CoinControlDialog::copyTransactionHash()
263 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_TXHASH));
266 // context menu action: lock coin
267 void CoinControlDialog::lockCoin()
269 if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
270 contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
272 COutPoint outpt(uint256S(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
273 model->lockCoin(outpt);
274 contextMenuItem->setDisabled(true);
275 contextMenuItem->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
276 updateLabelLocked();
279 // context menu action: unlock coin
280 void CoinControlDialog::unlockCoin()
282 COutPoint outpt(uint256S(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
283 model->unlockCoin(outpt);
284 contextMenuItem->setDisabled(false);
285 contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
286 updateLabelLocked();
289 // copy label "Quantity" to clipboard
290 void CoinControlDialog::clipboardQuantity()
292 GUIUtil::setClipboard(ui->labelCoinControlQuantity->text());
295 // copy label "Amount" to clipboard
296 void CoinControlDialog::clipboardAmount()
298 GUIUtil::setClipboard(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
301 // copy label "Fee" to clipboard
302 void CoinControlDialog::clipboardFee()
304 GUIUtil::setClipboard(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
307 // copy label "After fee" to clipboard
308 void CoinControlDialog::clipboardAfterFee()
310 GUIUtil::setClipboard(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
313 // copy label "Bytes" to clipboard
314 void CoinControlDialog::clipboardBytes()
316 GUIUtil::setClipboard(ui->labelCoinControlBytes->text().replace(ASYMP_UTF8, ""));
319 // copy label "Dust" to clipboard
320 void CoinControlDialog::clipboardLowOutput()
322 GUIUtil::setClipboard(ui->labelCoinControlLowOutput->text());
325 // copy label "Change" to clipboard
326 void CoinControlDialog::clipboardChange()
328 GUIUtil::setClipboard(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
331 // treeview: sort
332 void CoinControlDialog::sortView(int column, Qt::SortOrder order)
334 sortColumn = column;
335 sortOrder = order;
336 ui->treeWidget->sortItems(column, order);
337 ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
340 // treeview: clicked on header
341 void CoinControlDialog::headerSectionClicked(int logicalIndex)
343 if (logicalIndex == COLUMN_CHECKBOX) // click on most left column -> do nothing
345 ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
347 else
349 if (sortColumn == logicalIndex)
350 sortOrder = ((sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder);
351 else
353 sortColumn = logicalIndex;
354 sortOrder = ((sortColumn == COLUMN_LABEL || sortColumn == COLUMN_ADDRESS) ? Qt::AscendingOrder : Qt::DescendingOrder); // if label or address then default => asc, else default => desc
357 sortView(sortColumn, sortOrder);
361 // toggle tree mode
362 void CoinControlDialog::radioTreeMode(bool checked)
364 if (checked && model)
365 updateView();
368 // toggle list mode
369 void CoinControlDialog::radioListMode(bool checked)
371 if (checked && model)
372 updateView();
375 // checkbox clicked by user
376 void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
378 if (column == COLUMN_CHECKBOX && item->text(COLUMN_TXHASH).length() == 64) // transaction hash is 64 characters (this means its a child node, so its not a parent node in tree mode)
380 COutPoint outpt(uint256S(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt());
382 if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
383 coinControl->UnSelect(outpt);
384 else if (item->isDisabled()) // locked (this happens if "check all" through parent node)
385 item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
386 else
387 coinControl->Select(outpt);
389 // selection changed -> update labels
390 if (ui->treeWidget->isEnabled()) // do not update on every click for (un)select all
391 CoinControlDialog::updateLabels(model, this);
394 // TODO: Remove this temporary qt5 fix after Qt5.3 and Qt5.4 are no longer used.
395 // Fixed in Qt5.5 and above: https://bugreports.qt.io/browse/QTBUG-43473
396 #if QT_VERSION >= 0x050000
397 else if (column == COLUMN_CHECKBOX && item->childCount() > 0)
399 if (item->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked && item->child(0)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
400 item->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
402 #endif
405 // shows count of locked unspent outputs
406 void CoinControlDialog::updateLabelLocked()
408 std::vector<COutPoint> vOutpts;
409 model->listLockedCoins(vOutpts);
410 if (vOutpts.size() > 0)
412 ui->labelLocked->setText(tr("(%1 locked)").arg(vOutpts.size()));
413 ui->labelLocked->setVisible(true);
415 else ui->labelLocked->setVisible(false);
418 void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
420 if (!model)
421 return;
423 // nPayAmount
424 CAmount nPayAmount = 0;
425 bool fDust = false;
426 CMutableTransaction txDummy;
427 for (const CAmount &amount : CoinControlDialog::payAmounts)
429 nPayAmount += amount;
431 if (amount > 0)
433 CTxOut txout(amount, (CScript)std::vector<unsigned char>(24, 0));
434 txDummy.vout.push_back(txout);
435 fDust |= IsDust(txout, ::dustRelayFee);
439 CAmount nAmount = 0;
440 CAmount nPayFee = 0;
441 CAmount nAfterFee = 0;
442 CAmount nChange = 0;
443 unsigned int nBytes = 0;
444 unsigned int nBytesInputs = 0;
445 unsigned int nQuantity = 0;
446 bool fWitness = false;
448 std::vector<COutPoint> vCoinControl;
449 std::vector<COutput> vOutputs;
450 coinControl->ListSelected(vCoinControl);
451 model->getOutputs(vCoinControl, vOutputs);
453 for (const COutput& out : vOutputs) {
454 // unselect already spent, very unlikely scenario, this could happen
455 // when selected are spent elsewhere, like rpc or another computer
456 uint256 txhash = out.tx->GetHash();
457 COutPoint outpt(txhash, out.i);
458 if (model->isSpent(outpt))
460 coinControl->UnSelect(outpt);
461 continue;
464 // Quantity
465 nQuantity++;
467 // Amount
468 nAmount += out.tx->tx->vout[out.i].nValue;
470 // Bytes
471 CTxDestination address;
472 int witnessversion = 0;
473 std::vector<unsigned char> witnessprogram;
474 if (out.tx->tx->vout[out.i].scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram))
476 nBytesInputs += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
477 fWitness = true;
479 else if(ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, address))
481 CPubKey pubkey;
482 CKeyID *keyid = boost::get<CKeyID>(&address);
483 if (keyid && model->getPubKey(*keyid, pubkey))
485 nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
487 else
488 nBytesInputs += 148; // in all error cases, simply assume 148 here
490 else nBytesInputs += 148;
493 // calculation
494 if (nQuantity > 0)
496 // Bytes
497 nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
498 if (fWitness)
500 // there is some fudging in these numbers related to the actual virtual transaction size calculation that will keep this estimate from being exact.
501 // usually, the result will be an overestimate within a couple of satoshis so that the confirmation dialog ends up displaying a slightly smaller fee.
502 // also, the witness stack size value value is a variable sized integer. usually, the number of stack items will be well under the single byte var int limit.
503 nBytes += 2; // account for the serialized marker and flag bytes
504 nBytes += nQuantity; // account for the witness byte that holds the number of stack items for each input.
507 // 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
508 if (CoinControlDialog::fSubtractFeeFromAmount)
509 if (nAmount - nPayAmount == 0)
510 nBytes -= 34;
512 // Fee
513 nPayFee = CWallet::GetMinimumFee(nBytes, nTxConfirmTarget, ::mempool, ::feeEstimator);
515 if (nPayAmount > 0)
517 nChange = nAmount - nPayAmount;
518 if (!CoinControlDialog::fSubtractFeeFromAmount)
519 nChange -= nPayFee;
521 // Never create dust outputs; if we would, just add the dust to the fee.
522 if (nChange > 0 && nChange < MIN_CHANGE)
524 CTxOut txout(nChange, (CScript)std::vector<unsigned char>(24, 0));
525 if (IsDust(txout, ::dustRelayFee))
527 if (CoinControlDialog::fSubtractFeeFromAmount) // dust-change will be raised until no dust
528 nChange = GetDustThreshold(txout, ::dustRelayFee);
529 else
531 nPayFee += nChange;
532 nChange = 0;
537 if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
538 nBytes -= 34;
541 // after fee
542 nAfterFee = std::max<CAmount>(nAmount - nPayFee, 0);
545 // actually update labels
546 int nDisplayUnit = BitcoinUnits::BTC;
547 if (model && model->getOptionsModel())
548 nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
550 QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
551 QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
552 QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
553 QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
554 QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
555 QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
556 QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
558 // enable/disable "dust" and "change"
559 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
560 dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
561 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
562 dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
564 // stats
565 l1->setText(QString::number(nQuantity)); // Quantity
566 l2->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
567 l3->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
568 l4->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
569 l5->setText(((nBytes > 0) ? ASYMP_UTF8 : "") + QString::number(nBytes)); // Bytes
570 l7->setText(fDust ? tr("yes") : tr("no")); // Dust
571 l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
572 if (nPayFee > 0)
574 l3->setText(ASYMP_UTF8 + l3->text());
575 l4->setText(ASYMP_UTF8 + l4->text());
576 if (nChange > 0 && !CoinControlDialog::fSubtractFeeFromAmount)
577 l8->setText(ASYMP_UTF8 + l8->text());
580 // turn label red when dust
581 l7->setStyleSheet((fDust) ? "color:red;" : "");
583 // tool tips
584 QString toolTipDust = tr("This label turns red if any recipient receives an amount smaller than the current dust threshold.");
586 // how many satoshis the estimated fee can vary per byte we guess wrong
587 double dFeeVary;
588 if (payTxFee.GetFeePerK() > 0)
589 dFeeVary = (double)std::max(CWallet::GetRequiredFee(1000), payTxFee.GetFeePerK()) / 1000;
590 else {
591 dFeeVary = (double)std::max(CWallet::GetRequiredFee(1000), ::feeEstimator.estimateSmartFee(nTxConfirmTarget, NULL, ::mempool).GetFeePerK()) / 1000;
593 QString toolTip4 = tr("Can vary +/- %1 satoshi(s) per input.").arg(dFeeVary);
595 l3->setToolTip(toolTip4);
596 l4->setToolTip(toolTip4);
597 l7->setToolTip(toolTipDust);
598 l8->setToolTip(toolTip4);
599 dialog->findChild<QLabel *>("labelCoinControlFeeText") ->setToolTip(l3->toolTip());
600 dialog->findChild<QLabel *>("labelCoinControlAfterFeeText") ->setToolTip(l4->toolTip());
601 dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
602 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
603 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
605 // Insufficient funds
606 QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
607 if (label)
608 label->setVisible(nChange < 0);
611 void CoinControlDialog::updateView()
613 if (!model || !model->getOptionsModel() || !model->getAddressTableModel())
614 return;
616 bool treeMode = ui->radioTreeMode->isChecked();
618 ui->treeWidget->clear();
619 ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
620 ui->treeWidget->setAlternatingRowColors(!treeMode);
621 QFlags<Qt::ItemFlag> flgCheckbox = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
622 QFlags<Qt::ItemFlag> flgTristate = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
624 int nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
626 std::map<QString, std::vector<COutput> > mapCoins;
627 model->listCoins(mapCoins);
629 for (const std::pair<QString, std::vector<COutput>>& coins : mapCoins) {
630 CCoinControlWidgetItem *itemWalletAddress = new CCoinControlWidgetItem();
631 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
632 QString sWalletAddress = coins.first;
633 QString sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
634 if (sWalletLabel.isEmpty())
635 sWalletLabel = tr("(no label)");
637 if (treeMode)
639 // wallet address
640 ui->treeWidget->addTopLevelItem(itemWalletAddress);
642 itemWalletAddress->setFlags(flgTristate);
643 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
645 // label
646 itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
648 // address
649 itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
652 CAmount nSum = 0;
653 int nChildren = 0;
654 for (const COutput& out : coins.second) {
655 nSum += out.tx->tx->vout[out.i].nValue;
656 nChildren++;
658 CCoinControlWidgetItem *itemOutput;
659 if (treeMode) itemOutput = new CCoinControlWidgetItem(itemWalletAddress);
660 else itemOutput = new CCoinControlWidgetItem(ui->treeWidget);
661 itemOutput->setFlags(flgCheckbox);
662 itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
664 // address
665 CTxDestination outputAddress;
666 QString sAddress = "";
667 if(ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, outputAddress))
669 sAddress = QString::fromStdString(CBitcoinAddress(outputAddress).ToString());
671 // if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
672 if (!treeMode || (!(sAddress == sWalletAddress)))
673 itemOutput->setText(COLUMN_ADDRESS, sAddress);
676 // label
677 if (!(sAddress == sWalletAddress)) // change
679 // tooltip from where the change comes from
680 itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
681 itemOutput->setText(COLUMN_LABEL, tr("(change)"));
683 else if (!treeMode)
685 QString sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
686 if (sLabel.isEmpty())
687 sLabel = tr("(no label)");
688 itemOutput->setText(COLUMN_LABEL, sLabel);
691 // amount
692 itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.tx->tx->vout[out.i].nValue));
693 itemOutput->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)out.tx->tx->vout[out.i].nValue)); // padding so that sorting works correctly
695 // date
696 itemOutput->setText(COLUMN_DATE, GUIUtil::dateTimeStr(out.tx->GetTxTime()));
697 itemOutput->setData(COLUMN_DATE, Qt::UserRole, QVariant((qlonglong)out.tx->GetTxTime()));
699 // confirmations
700 itemOutput->setText(COLUMN_CONFIRMATIONS, QString::number(out.nDepth));
701 itemOutput->setData(COLUMN_CONFIRMATIONS, Qt::UserRole, QVariant((qlonglong)out.nDepth));
703 // transaction hash
704 uint256 txhash = out.tx->GetHash();
705 itemOutput->setText(COLUMN_TXHASH, QString::fromStdString(txhash.GetHex()));
707 // vout index
708 itemOutput->setText(COLUMN_VOUT_INDEX, QString::number(out.i));
710 // disable locked coins
711 if (model->isLockedCoin(txhash, out.i))
713 COutPoint outpt(txhash, out.i);
714 coinControl->UnSelect(outpt); // just to be sure
715 itemOutput->setDisabled(true);
716 itemOutput->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
719 // set checkbox
720 if (coinControl->IsSelected(COutPoint(txhash, out.i)))
721 itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
724 // amount
725 if (treeMode)
727 itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
728 itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
729 itemWalletAddress->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)nSum));
733 // expand all partially selected
734 if (treeMode)
736 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
737 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
738 ui->treeWidget->topLevelItem(i)->setExpanded(true);
741 // sort view
742 sortView(sortColumn, sortOrder);
743 ui->treeWidget->setEnabled(true);