Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / src / qt / coincontroldialog.cpp
blobea4d6a481651ff562792f0c57ec7216661fe9760
1 // Copyright (c) 2011-2017 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 <qt/coincontroldialog.h>
6 #include <qt/forms/ui_coincontroldialog.h>
8 #include <qt/addresstablemodel.h>
9 #include <qt/bitcoinunits.h>
10 #include <qt/guiutil.h>
11 #include <qt/optionsmodel.h>
12 #include <qt/platformstyle.h>
13 #include <txmempool.h>
14 #include <qt/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/fees.h>
22 #include <wallet/wallet.h>
24 #include <QApplication>
25 #include <QCheckBox>
26 #include <QCursor>
27 #include <QDialogButtonBox>
28 #include <QFlags>
29 #include <QIcon>
30 #include <QSettings>
31 #include <QTreeWidget>
33 QList<CAmount> CoinControlDialog::payAmounts;
34 CCoinControl* CoinControlDialog::coinControl = new CCoinControl();
35 bool CoinControlDialog::fSubtractFeeFromAmount = false;
37 bool CCoinControlWidgetItem::operator<(const QTreeWidgetItem &other) const {
38 int column = treeWidget()->sortColumn();
39 if (column == CoinControlDialog::COLUMN_AMOUNT || column == CoinControlDialog::COLUMN_DATE || column == CoinControlDialog::COLUMN_CONFIRMATIONS)
40 return data(column, Qt::UserRole).toLongLong() < other.data(column, Qt::UserRole).toLongLong();
41 return QTreeWidgetItem::operator<(other);
44 CoinControlDialog::CoinControlDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
45 QDialog(parent),
46 ui(new Ui::CoinControlDialog),
47 model(0),
48 platformStyle(_platformStyle)
50 ui->setupUi(this);
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
60 // context menu
61 contextMenu = new QMenu(this);
62 contextMenu->addAction(copyAddressAction);
63 contextMenu->addAction(copyLabelAction);
64 contextMenu->addAction(copyAmountAction);
65 contextMenu->addAction(copyTransactionHashAction);
66 contextMenu->addSeparator();
67 contextMenu->addAction(lockAction);
68 contextMenu->addAction(unlockAction);
70 // context menu signals
71 connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
72 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
73 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
74 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
75 connect(copyTransactionHashAction, SIGNAL(triggered()), this, SLOT(copyTransactionHash()));
76 connect(lockAction, SIGNAL(triggered()), this, SLOT(lockCoin()));
77 connect(unlockAction, SIGNAL(triggered()), this, SLOT(unlockCoin()));
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);
88 connect(clipboardQuantityAction, SIGNAL(triggered()), this, SLOT(clipboardQuantity()));
89 connect(clipboardAmountAction, SIGNAL(triggered()), this, SLOT(clipboardAmount()));
90 connect(clipboardFeeAction, SIGNAL(triggered()), this, SLOT(clipboardFee()));
91 connect(clipboardAfterFeeAction, SIGNAL(triggered()), this, SLOT(clipboardAfterFee()));
92 connect(clipboardBytesAction, SIGNAL(triggered()), this, SLOT(clipboardBytes()));
93 connect(clipboardLowOutputAction, SIGNAL(triggered()), this, SLOT(clipboardLowOutput()));
94 connect(clipboardChangeAction, SIGNAL(triggered()), this, SLOT(clipboardChange()));
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);
104 // toggle tree/list mode
105 connect(ui->radioTreeMode, SIGNAL(toggled(bool)), this, SLOT(radioTreeMode(bool)));
106 connect(ui->radioListMode, SIGNAL(toggled(bool)), this, SLOT(radioListMode(bool)));
108 // click on checkbox
109 connect(ui->treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(viewItemChanged(QTreeWidgetItem*, int)));
111 // click on header
112 #if QT_VERSION < 0x050000
113 ui->treeWidget->header()->setClickable(true);
114 #else
115 ui->treeWidget->header()->setSectionsClickable(true);
116 #endif
117 connect(ui->treeWidget->header(), SIGNAL(sectionClicked(int)), this, SLOT(headerSectionClicked(int)));
119 // ok button
120 connect(ui->buttonBox, SIGNAL(clicked( QAbstractButton*)), this, SLOT(buttonBoxClicked(QAbstractButton*)));
122 // (un)select all
123 connect(ui->pushButtonSelectAll, SIGNAL(clicked()), this, SLOT(buttonSelectAllClicked()));
125 // change coin control first column label due Qt4 bug.
126 // see https://github.com/bitcoin/bitcoin/issues/5716
127 ui->treeWidget->headerItem()->setText(COLUMN_CHECKBOX, QString());
129 ui->treeWidget->setColumnWidth(COLUMN_CHECKBOX, 84);
130 ui->treeWidget->setColumnWidth(COLUMN_AMOUNT, 110);
131 ui->treeWidget->setColumnWidth(COLUMN_LABEL, 190);
132 ui->treeWidget->setColumnWidth(COLUMN_ADDRESS, 320);
133 ui->treeWidget->setColumnWidth(COLUMN_DATE, 130);
134 ui->treeWidget->setColumnWidth(COLUMN_CONFIRMATIONS, 110);
135 ui->treeWidget->setColumnHidden(COLUMN_TXHASH, true); // store transaction hash in this column, but don't show it
136 ui->treeWidget->setColumnHidden(COLUMN_VOUT_INDEX, true); // store vout index in this column, but don't show it
138 // default view is sorted by amount desc
139 sortView(COLUMN_AMOUNT, Qt::DescendingOrder);
141 // restore list mode and sortorder as a convenience feature
142 QSettings settings;
143 if (settings.contains("nCoinControlMode") && !settings.value("nCoinControlMode").toBool())
144 ui->radioTreeMode->click();
145 if (settings.contains("nCoinControlSortColumn") && settings.contains("nCoinControlSortOrder"))
146 sortView(settings.value("nCoinControlSortColumn").toInt(), ((Qt::SortOrder)settings.value("nCoinControlSortOrder").toInt()));
149 CoinControlDialog::~CoinControlDialog()
151 QSettings settings;
152 settings.setValue("nCoinControlMode", ui->radioListMode->isChecked());
153 settings.setValue("nCoinControlSortColumn", sortColumn);
154 settings.setValue("nCoinControlSortOrder", (int)sortOrder);
156 delete ui;
159 void CoinControlDialog::setModel(WalletModel *_model)
161 this->model = _model;
163 if(_model && _model->getOptionsModel() && _model->getAddressTableModel())
165 updateView();
166 updateLabelLocked();
167 CoinControlDialog::updateLabels(_model, this);
171 // ok button
172 void CoinControlDialog::buttonBoxClicked(QAbstractButton* button)
174 if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
175 done(QDialog::Accepted); // closes the dialog
178 // (un)select all
179 void CoinControlDialog::buttonSelectAllClicked()
181 Qt::CheckState state = Qt::Checked;
182 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
184 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != Qt::Unchecked)
186 state = Qt::Unchecked;
187 break;
190 ui->treeWidget->setEnabled(false);
191 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
192 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != state)
193 ui->treeWidget->topLevelItem(i)->setCheckState(COLUMN_CHECKBOX, state);
194 ui->treeWidget->setEnabled(true);
195 if (state == Qt::Unchecked)
196 coinControl->UnSelectAll(); // just to be sure
197 CoinControlDialog::updateLabels(model, this);
200 // context menu
201 void CoinControlDialog::showMenu(const QPoint &point)
203 QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
204 if(item)
206 contextMenuItem = item;
208 // disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
209 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)
211 copyTransactionHashAction->setEnabled(true);
212 if (model->isLockedCoin(uint256S(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt()))
214 lockAction->setEnabled(false);
215 unlockAction->setEnabled(true);
217 else
219 lockAction->setEnabled(true);
220 unlockAction->setEnabled(false);
223 else // this means click on parent node in tree mode -> disable all
225 copyTransactionHashAction->setEnabled(false);
226 lockAction->setEnabled(false);
227 unlockAction->setEnabled(false);
230 // show context menu
231 contextMenu->exec(QCursor::pos());
235 // context menu action: copy amount
236 void CoinControlDialog::copyAmount()
238 GUIUtil::setClipboard(BitcoinUnits::removeSpaces(contextMenuItem->text(COLUMN_AMOUNT)));
241 // context menu action: copy label
242 void CoinControlDialog::copyLabel()
244 if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_LABEL).length() == 0 && contextMenuItem->parent())
245 GUIUtil::setClipboard(contextMenuItem->parent()->text(COLUMN_LABEL));
246 else
247 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_LABEL));
250 // context menu action: copy address
251 void CoinControlDialog::copyAddress()
253 if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_ADDRESS).length() == 0 && contextMenuItem->parent())
254 GUIUtil::setClipboard(contextMenuItem->parent()->text(COLUMN_ADDRESS));
255 else
256 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_ADDRESS));
259 // context menu action: copy transaction id
260 void CoinControlDialog::copyTransactionHash()
262 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_TXHASH));
265 // context menu action: lock coin
266 void CoinControlDialog::lockCoin()
268 if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
269 contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
271 COutPoint outpt(uint256S(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
272 model->lockCoin(outpt);
273 contextMenuItem->setDisabled(true);
274 contextMenuItem->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
275 updateLabelLocked();
278 // context menu action: unlock coin
279 void CoinControlDialog::unlockCoin()
281 COutPoint outpt(uint256S(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
282 model->unlockCoin(outpt);
283 contextMenuItem->setDisabled(false);
284 contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
285 updateLabelLocked();
288 // copy label "Quantity" to clipboard
289 void CoinControlDialog::clipboardQuantity()
291 GUIUtil::setClipboard(ui->labelCoinControlQuantity->text());
294 // copy label "Amount" to clipboard
295 void CoinControlDialog::clipboardAmount()
297 GUIUtil::setClipboard(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
300 // copy label "Fee" to clipboard
301 void CoinControlDialog::clipboardFee()
303 GUIUtil::setClipboard(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
306 // copy label "After fee" to clipboard
307 void CoinControlDialog::clipboardAfterFee()
309 GUIUtil::setClipboard(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
312 // copy label "Bytes" to clipboard
313 void CoinControlDialog::clipboardBytes()
315 GUIUtil::setClipboard(ui->labelCoinControlBytes->text().replace(ASYMP_UTF8, ""));
318 // copy label "Dust" to clipboard
319 void CoinControlDialog::clipboardLowOutput()
321 GUIUtil::setClipboard(ui->labelCoinControlLowOutput->text());
324 // copy label "Change" to clipboard
325 void CoinControlDialog::clipboardChange()
327 GUIUtil::setClipboard(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
330 // treeview: sort
331 void CoinControlDialog::sortView(int column, Qt::SortOrder order)
333 sortColumn = column;
334 sortOrder = order;
335 ui->treeWidget->sortItems(column, order);
336 ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
339 // treeview: clicked on header
340 void CoinControlDialog::headerSectionClicked(int logicalIndex)
342 if (logicalIndex == COLUMN_CHECKBOX) // click on most left column -> do nothing
344 ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
346 else
348 if (sortColumn == logicalIndex)
349 sortOrder = ((sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder);
350 else
352 sortColumn = logicalIndex;
353 sortOrder = ((sortColumn == COLUMN_LABEL || sortColumn == COLUMN_ADDRESS) ? Qt::AscendingOrder : Qt::DescendingOrder); // if label or address then default => asc, else default => desc
356 sortView(sortColumn, sortOrder);
360 // toggle tree mode
361 void CoinControlDialog::radioTreeMode(bool checked)
363 if (checked && model)
364 updateView();
367 // toggle list mode
368 void CoinControlDialog::radioListMode(bool checked)
370 if (checked && model)
371 updateView();
374 // checkbox clicked by user
375 void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
377 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)
379 COutPoint outpt(uint256S(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt());
381 if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
382 coinControl->UnSelect(outpt);
383 else if (item->isDisabled()) // locked (this happens if "check all" through parent node)
384 item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
385 else
386 coinControl->Select(outpt);
388 // selection changed -> update labels
389 if (ui->treeWidget->isEnabled()) // do not update on every click for (un)select all
390 CoinControlDialog::updateLabels(model, this);
393 // TODO: Remove this temporary qt5 fix after Qt5.3 and Qt5.4 are no longer used.
394 // Fixed in Qt5.5 and above: https://bugreports.qt.io/browse/QTBUG-43473
395 #if QT_VERSION >= 0x050000
396 else if (column == COLUMN_CHECKBOX && item->childCount() > 0)
398 if (item->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked && item->child(0)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
399 item->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
401 #endif
404 // shows count of locked unspent outputs
405 void CoinControlDialog::updateLabelLocked()
407 std::vector<COutPoint> vOutpts;
408 model->listLockedCoins(vOutpts);
409 if (vOutpts.size() > 0)
411 ui->labelLocked->setText(tr("(%1 locked)").arg(vOutpts.size()));
412 ui->labelLocked->setVisible(true);
414 else ui->labelLocked->setVisible(false);
417 void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
419 if (!model)
420 return;
422 // nPayAmount
423 CAmount nPayAmount = 0;
424 bool fDust = false;
425 CMutableTransaction txDummy;
426 for (const CAmount &amount : CoinControlDialog::payAmounts)
428 nPayAmount += amount;
430 if (amount > 0)
432 CTxOut txout(amount, (CScript)std::vector<unsigned char>(24, 0));
433 txDummy.vout.push_back(txout);
434 fDust |= IsDust(txout, ::dustRelayFee);
438 CAmount nAmount = 0;
439 CAmount nPayFee = 0;
440 CAmount nAfterFee = 0;
441 CAmount nChange = 0;
442 unsigned int nBytes = 0;
443 unsigned int nBytesInputs = 0;
444 unsigned int nQuantity = 0;
445 bool fWitness = false;
447 std::vector<COutPoint> vCoinControl;
448 std::vector<COutput> vOutputs;
449 coinControl->ListSelected(vCoinControl);
450 model->getOutputs(vCoinControl, vOutputs);
452 for (const COutput& out : vOutputs) {
453 // unselect already spent, very unlikely scenario, this could happen
454 // when selected are spent elsewhere, like rpc or another computer
455 uint256 txhash = out.tx->GetHash();
456 COutPoint outpt(txhash, out.i);
457 if (model->isSpent(outpt))
459 coinControl->UnSelect(outpt);
460 continue;
463 // Quantity
464 nQuantity++;
466 // Amount
467 nAmount += out.tx->tx->vout[out.i].nValue;
469 // Bytes
470 CTxDestination address;
471 int witnessversion = 0;
472 std::vector<unsigned char> witnessprogram;
473 if (out.tx->tx->vout[out.i].scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram))
475 nBytesInputs += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
476 fWitness = true;
478 else if(ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, address))
480 CPubKey pubkey;
481 CKeyID *keyid = boost::get<CKeyID>(&address);
482 if (keyid && model->getPubKey(*keyid, pubkey))
484 nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
486 else
487 nBytesInputs += 148; // in all error cases, simply assume 148 here
489 else nBytesInputs += 148;
492 // calculation
493 if (nQuantity > 0)
495 // Bytes
496 nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
497 if (fWitness)
499 // there is some fudging in these numbers related to the actual virtual transaction size calculation that will keep this estimate from being exact.
500 // usually, the result will be an overestimate within a couple of satoshis so that the confirmation dialog ends up displaying a slightly smaller fee.
501 // 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.
502 nBytes += 2; // account for the serialized marker and flag bytes
503 nBytes += nQuantity; // account for the witness byte that holds the number of stack items for each input.
506 // 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
507 if (CoinControlDialog::fSubtractFeeFromAmount)
508 if (nAmount - nPayAmount == 0)
509 nBytes -= 34;
511 // Fee
512 nPayFee = GetMinimumFee(nBytes, *coinControl, ::mempool, ::feeEstimator, nullptr /* FeeCalculation */);
514 if (nPayAmount > 0)
516 nChange = nAmount - nPayAmount;
517 if (!CoinControlDialog::fSubtractFeeFromAmount)
518 nChange -= nPayFee;
520 // Never create dust outputs; if we would, just add the dust to the fee.
521 if (nChange > 0 && nChange < MIN_CHANGE)
523 CTxOut txout(nChange, (CScript)std::vector<unsigned char>(24, 0));
524 if (IsDust(txout, ::dustRelayFee))
526 nPayFee += nChange;
527 nChange = 0;
528 if (CoinControlDialog::fSubtractFeeFromAmount)
529 nBytes -= 34; // we didn't detect lack of change above
533 if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
534 nBytes -= 34;
537 // after fee
538 nAfterFee = std::max<CAmount>(nAmount - nPayFee, 0);
541 // actually update labels
542 int nDisplayUnit = BitcoinUnits::BTC;
543 if (model && model->getOptionsModel())
544 nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
546 QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
547 QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
548 QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
549 QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
550 QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
551 QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
552 QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
554 // enable/disable "dust" and "change"
555 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
556 dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
557 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
558 dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
560 // stats
561 l1->setText(QString::number(nQuantity)); // Quantity
562 l2->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
563 l3->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
564 l4->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
565 l5->setText(((nBytes > 0) ? ASYMP_UTF8 : "") + QString::number(nBytes)); // Bytes
566 l7->setText(fDust ? tr("yes") : tr("no")); // Dust
567 l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
568 if (nPayFee > 0)
570 l3->setText(ASYMP_UTF8 + l3->text());
571 l4->setText(ASYMP_UTF8 + l4->text());
572 if (nChange > 0 && !CoinControlDialog::fSubtractFeeFromAmount)
573 l8->setText(ASYMP_UTF8 + l8->text());
576 // turn label red when dust
577 l7->setStyleSheet((fDust) ? "color:red;" : "");
579 // tool tips
580 QString toolTipDust = tr("This label turns red if any recipient receives an amount smaller than the current dust threshold.");
582 // how many satoshis the estimated fee can vary per byte we guess wrong
583 double dFeeVary = (nBytes != 0) ? (double)nPayFee / nBytes : 0;
585 QString toolTip4 = tr("Can vary +/- %1 satoshi(s) per input.").arg(dFeeVary);
587 l3->setToolTip(toolTip4);
588 l4->setToolTip(toolTip4);
589 l7->setToolTip(toolTipDust);
590 l8->setToolTip(toolTip4);
591 dialog->findChild<QLabel *>("labelCoinControlFeeText") ->setToolTip(l3->toolTip());
592 dialog->findChild<QLabel *>("labelCoinControlAfterFeeText") ->setToolTip(l4->toolTip());
593 dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
594 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
595 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
597 // Insufficient funds
598 QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
599 if (label)
600 label->setVisible(nChange < 0);
603 void CoinControlDialog::updateView()
605 if (!model || !model->getOptionsModel() || !model->getAddressTableModel())
606 return;
608 bool treeMode = ui->radioTreeMode->isChecked();
610 ui->treeWidget->clear();
611 ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
612 ui->treeWidget->setAlternatingRowColors(!treeMode);
613 QFlags<Qt::ItemFlag> flgCheckbox = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
614 QFlags<Qt::ItemFlag> flgTristate = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
616 int nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
618 std::map<QString, std::vector<COutput> > mapCoins;
619 model->listCoins(mapCoins);
621 for (const std::pair<QString, std::vector<COutput>>& coins : mapCoins) {
622 CCoinControlWidgetItem *itemWalletAddress = new CCoinControlWidgetItem();
623 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
624 QString sWalletAddress = coins.first;
625 QString sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
626 if (sWalletLabel.isEmpty())
627 sWalletLabel = tr("(no label)");
629 if (treeMode)
631 // wallet address
632 ui->treeWidget->addTopLevelItem(itemWalletAddress);
634 itemWalletAddress->setFlags(flgTristate);
635 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
637 // label
638 itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
640 // address
641 itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
644 CAmount nSum = 0;
645 int nChildren = 0;
646 for (const COutput& out : coins.second) {
647 nSum += out.tx->tx->vout[out.i].nValue;
648 nChildren++;
650 CCoinControlWidgetItem *itemOutput;
651 if (treeMode) itemOutput = new CCoinControlWidgetItem(itemWalletAddress);
652 else itemOutput = new CCoinControlWidgetItem(ui->treeWidget);
653 itemOutput->setFlags(flgCheckbox);
654 itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
656 // address
657 CTxDestination outputAddress;
658 QString sAddress = "";
659 if(ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, outputAddress))
661 sAddress = QString::fromStdString(EncodeDestination(outputAddress));
663 // if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
664 if (!treeMode || (!(sAddress == sWalletAddress)))
665 itemOutput->setText(COLUMN_ADDRESS, sAddress);
668 // label
669 if (!(sAddress == sWalletAddress)) // change
671 // tooltip from where the change comes from
672 itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
673 itemOutput->setText(COLUMN_LABEL, tr("(change)"));
675 else if (!treeMode)
677 QString sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
678 if (sLabel.isEmpty())
679 sLabel = tr("(no label)");
680 itemOutput->setText(COLUMN_LABEL, sLabel);
683 // amount
684 itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.tx->tx->vout[out.i].nValue));
685 itemOutput->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)out.tx->tx->vout[out.i].nValue)); // padding so that sorting works correctly
687 // date
688 itemOutput->setText(COLUMN_DATE, GUIUtil::dateTimeStr(out.tx->GetTxTime()));
689 itemOutput->setData(COLUMN_DATE, Qt::UserRole, QVariant((qlonglong)out.tx->GetTxTime()));
691 // confirmations
692 itemOutput->setText(COLUMN_CONFIRMATIONS, QString::number(out.nDepth));
693 itemOutput->setData(COLUMN_CONFIRMATIONS, Qt::UserRole, QVariant((qlonglong)out.nDepth));
695 // transaction hash
696 uint256 txhash = out.tx->GetHash();
697 itemOutput->setText(COLUMN_TXHASH, QString::fromStdString(txhash.GetHex()));
699 // vout index
700 itemOutput->setText(COLUMN_VOUT_INDEX, QString::number(out.i));
702 // disable locked coins
703 if (model->isLockedCoin(txhash, out.i))
705 COutPoint outpt(txhash, out.i);
706 coinControl->UnSelect(outpt); // just to be sure
707 itemOutput->setDisabled(true);
708 itemOutput->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
711 // set checkbox
712 if (coinControl->IsSelected(COutPoint(txhash, out.i)))
713 itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
716 // amount
717 if (treeMode)
719 itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
720 itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
721 itemWalletAddress->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)nSum));
725 // expand all partially selected
726 if (treeMode)
728 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
729 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
730 ui->treeWidget->topLevelItem(i)->setExpanded(true);
733 // sort view
734 sortView(sortColumn, sortOrder);
735 ui->treeWidget->setEnabled(true);