Add const to methods that do not modify the object for which it is called
[bitcoinplatinum.git] / src / qt / coincontroldialog.cpp
blobf3ee0fbe393aeace65b734393481a58623d164f7
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 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, *coinControl, ::mempool, ::feeEstimator, nullptr /* FeeCalculation */);
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 nPayFee += nChange;
528 nChange = 0;
529 if (CoinControlDialog::fSubtractFeeFromAmount)
530 nBytes -= 34; // we didn't detect lack of change above
534 if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
535 nBytes -= 34;
538 // after fee
539 nAfterFee = std::max<CAmount>(nAmount - nPayFee, 0);
542 // actually update labels
543 int nDisplayUnit = BitcoinUnits::BTC;
544 if (model && model->getOptionsModel())
545 nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
547 QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
548 QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
549 QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
550 QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
551 QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
552 QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
553 QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
555 // enable/disable "dust" and "change"
556 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
557 dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
558 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
559 dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
561 // stats
562 l1->setText(QString::number(nQuantity)); // Quantity
563 l2->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
564 l3->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
565 l4->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
566 l5->setText(((nBytes > 0) ? ASYMP_UTF8 : "") + QString::number(nBytes)); // Bytes
567 l7->setText(fDust ? tr("yes") : tr("no")); // Dust
568 l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
569 if (nPayFee > 0)
571 l3->setText(ASYMP_UTF8 + l3->text());
572 l4->setText(ASYMP_UTF8 + l4->text());
573 if (nChange > 0 && !CoinControlDialog::fSubtractFeeFromAmount)
574 l8->setText(ASYMP_UTF8 + l8->text());
577 // turn label red when dust
578 l7->setStyleSheet((fDust) ? "color:red;" : "");
580 // tool tips
581 QString toolTipDust = tr("This label turns red if any recipient receives an amount smaller than the current dust threshold.");
583 // how many satoshis the estimated fee can vary per byte we guess wrong
584 double dFeeVary = (double)nPayFee / nBytes;
586 QString toolTip4 = tr("Can vary +/- %1 satoshi(s) per input.").arg(dFeeVary);
588 l3->setToolTip(toolTip4);
589 l4->setToolTip(toolTip4);
590 l7->setToolTip(toolTipDust);
591 l8->setToolTip(toolTip4);
592 dialog->findChild<QLabel *>("labelCoinControlFeeText") ->setToolTip(l3->toolTip());
593 dialog->findChild<QLabel *>("labelCoinControlAfterFeeText") ->setToolTip(l4->toolTip());
594 dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
595 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
596 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
598 // Insufficient funds
599 QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
600 if (label)
601 label->setVisible(nChange < 0);
604 void CoinControlDialog::updateView()
606 if (!model || !model->getOptionsModel() || !model->getAddressTableModel())
607 return;
609 bool treeMode = ui->radioTreeMode->isChecked();
611 ui->treeWidget->clear();
612 ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
613 ui->treeWidget->setAlternatingRowColors(!treeMode);
614 QFlags<Qt::ItemFlag> flgCheckbox = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
615 QFlags<Qt::ItemFlag> flgTristate = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
617 int nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
619 std::map<QString, std::vector<COutput> > mapCoins;
620 model->listCoins(mapCoins);
622 for (const std::pair<QString, std::vector<COutput>>& coins : mapCoins) {
623 CCoinControlWidgetItem *itemWalletAddress = new CCoinControlWidgetItem();
624 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
625 QString sWalletAddress = coins.first;
626 QString sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
627 if (sWalletLabel.isEmpty())
628 sWalletLabel = tr("(no label)");
630 if (treeMode)
632 // wallet address
633 ui->treeWidget->addTopLevelItem(itemWalletAddress);
635 itemWalletAddress->setFlags(flgTristate);
636 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
638 // label
639 itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
641 // address
642 itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
645 CAmount nSum = 0;
646 int nChildren = 0;
647 for (const COutput& out : coins.second) {
648 nSum += out.tx->tx->vout[out.i].nValue;
649 nChildren++;
651 CCoinControlWidgetItem *itemOutput;
652 if (treeMode) itemOutput = new CCoinControlWidgetItem(itemWalletAddress);
653 else itemOutput = new CCoinControlWidgetItem(ui->treeWidget);
654 itemOutput->setFlags(flgCheckbox);
655 itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
657 // address
658 CTxDestination outputAddress;
659 QString sAddress = "";
660 if(ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, outputAddress))
662 sAddress = QString::fromStdString(CBitcoinAddress(outputAddress).ToString());
664 // if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
665 if (!treeMode || (!(sAddress == sWalletAddress)))
666 itemOutput->setText(COLUMN_ADDRESS, sAddress);
669 // label
670 if (!(sAddress == sWalletAddress)) // change
672 // tooltip from where the change comes from
673 itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
674 itemOutput->setText(COLUMN_LABEL, tr("(change)"));
676 else if (!treeMode)
678 QString sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
679 if (sLabel.isEmpty())
680 sLabel = tr("(no label)");
681 itemOutput->setText(COLUMN_LABEL, sLabel);
684 // amount
685 itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.tx->tx->vout[out.i].nValue));
686 itemOutput->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)out.tx->tx->vout[out.i].nValue)); // padding so that sorting works correctly
688 // date
689 itemOutput->setText(COLUMN_DATE, GUIUtil::dateTimeStr(out.tx->GetTxTime()));
690 itemOutput->setData(COLUMN_DATE, Qt::UserRole, QVariant((qlonglong)out.tx->GetTxTime()));
692 // confirmations
693 itemOutput->setText(COLUMN_CONFIRMATIONS, QString::number(out.nDepth));
694 itemOutput->setData(COLUMN_CONFIRMATIONS, Qt::UserRole, QVariant((qlonglong)out.nDepth));
696 // transaction hash
697 uint256 txhash = out.tx->GetHash();
698 itemOutput->setText(COLUMN_TXHASH, QString::fromStdString(txhash.GetHex()));
700 // vout index
701 itemOutput->setText(COLUMN_VOUT_INDEX, QString::number(out.i));
703 // disable locked coins
704 if (model->isLockedCoin(txhash, out.i))
706 COutPoint outpt(txhash, out.i);
707 coinControl->UnSelect(outpt); // just to be sure
708 itemOutput->setDisabled(true);
709 itemOutput->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
712 // set checkbox
713 if (coinControl->IsSelected(COutPoint(txhash, out.i)))
714 itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
717 // amount
718 if (treeMode)
720 itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
721 itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
722 itemWalletAddress->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)nSum));
726 // expand all partially selected
727 if (treeMode)
729 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
730 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
731 ui->treeWidget->topLevelItem(i)->setExpanded(true);
734 // sort view
735 sortView(sortColumn, sortOrder);
736 ui->treeWidget->setEnabled(true);