[test] Add getblockchaininfo functional test
[bitcoinplatinum.git] / src / qt / coincontroldialog.cpp
blob3ca43eae229fc1b5739d6bf87ff75a945bc70bad
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/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 <QString>
32 #include <QTreeWidget>
33 #include <QTreeWidgetItem>
35 QList<CAmount> CoinControlDialog::payAmounts;
36 CCoinControl* CoinControlDialog::coinControl = new CCoinControl();
37 bool CoinControlDialog::fSubtractFeeFromAmount = false;
39 bool CCoinControlWidgetItem::operator<(const QTreeWidgetItem &other) const {
40 int column = treeWidget()->sortColumn();
41 if (column == CoinControlDialog::COLUMN_AMOUNT || column == CoinControlDialog::COLUMN_DATE || column == CoinControlDialog::COLUMN_CONFIRMATIONS)
42 return data(column, Qt::UserRole).toLongLong() < other.data(column, Qt::UserRole).toLongLong();
43 return QTreeWidgetItem::operator<(other);
46 CoinControlDialog::CoinControlDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
47 QDialog(parent),
48 ui(new Ui::CoinControlDialog),
49 model(0),
50 platformStyle(_platformStyle)
52 ui->setupUi(this);
54 // context menu actions
55 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
56 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
57 QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
58 copyTransactionHashAction = new QAction(tr("Copy transaction ID"), this); // we need to enable/disable this
59 lockAction = new QAction(tr("Lock unspent"), this); // we need to enable/disable this
60 unlockAction = new QAction(tr("Unlock unspent"), this); // we need to enable/disable this
62 // context menu
63 contextMenu = new QMenu(this);
64 contextMenu->addAction(copyAddressAction);
65 contextMenu->addAction(copyLabelAction);
66 contextMenu->addAction(copyAmountAction);
67 contextMenu->addAction(copyTransactionHashAction);
68 contextMenu->addSeparator();
69 contextMenu->addAction(lockAction);
70 contextMenu->addAction(unlockAction);
72 // context menu signals
73 connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
74 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
75 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
76 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
77 connect(copyTransactionHashAction, SIGNAL(triggered()), this, SLOT(copyTransactionHash()));
78 connect(lockAction, SIGNAL(triggered()), this, SLOT(lockCoin()));
79 connect(unlockAction, SIGNAL(triggered()), this, SLOT(unlockCoin()));
81 // clipboard actions
82 QAction *clipboardQuantityAction = new QAction(tr("Copy quantity"), this);
83 QAction *clipboardAmountAction = new QAction(tr("Copy amount"), this);
84 QAction *clipboardFeeAction = new QAction(tr("Copy fee"), this);
85 QAction *clipboardAfterFeeAction = new QAction(tr("Copy after fee"), this);
86 QAction *clipboardBytesAction = new QAction(tr("Copy bytes"), this);
87 QAction *clipboardLowOutputAction = new QAction(tr("Copy dust"), this);
88 QAction *clipboardChangeAction = new QAction(tr("Copy change"), this);
90 connect(clipboardQuantityAction, SIGNAL(triggered()), this, SLOT(clipboardQuantity()));
91 connect(clipboardAmountAction, SIGNAL(triggered()), this, SLOT(clipboardAmount()));
92 connect(clipboardFeeAction, SIGNAL(triggered()), this, SLOT(clipboardFee()));
93 connect(clipboardAfterFeeAction, SIGNAL(triggered()), this, SLOT(clipboardAfterFee()));
94 connect(clipboardBytesAction, SIGNAL(triggered()), this, SLOT(clipboardBytes()));
95 connect(clipboardLowOutputAction, SIGNAL(triggered()), this, SLOT(clipboardLowOutput()));
96 connect(clipboardChangeAction, SIGNAL(triggered()), this, SLOT(clipboardChange()));
98 ui->labelCoinControlQuantity->addAction(clipboardQuantityAction);
99 ui->labelCoinControlAmount->addAction(clipboardAmountAction);
100 ui->labelCoinControlFee->addAction(clipboardFeeAction);
101 ui->labelCoinControlAfterFee->addAction(clipboardAfterFeeAction);
102 ui->labelCoinControlBytes->addAction(clipboardBytesAction);
103 ui->labelCoinControlLowOutput->addAction(clipboardLowOutputAction);
104 ui->labelCoinControlChange->addAction(clipboardChangeAction);
106 // toggle tree/list mode
107 connect(ui->radioTreeMode, SIGNAL(toggled(bool)), this, SLOT(radioTreeMode(bool)));
108 connect(ui->radioListMode, SIGNAL(toggled(bool)), this, SLOT(radioListMode(bool)));
110 // click on checkbox
111 connect(ui->treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(viewItemChanged(QTreeWidgetItem*, int)));
113 // click on header
114 #if QT_VERSION < 0x050000
115 ui->treeWidget->header()->setClickable(true);
116 #else
117 ui->treeWidget->header()->setSectionsClickable(true);
118 #endif
119 connect(ui->treeWidget->header(), SIGNAL(sectionClicked(int)), this, SLOT(headerSectionClicked(int)));
121 // ok button
122 connect(ui->buttonBox, SIGNAL(clicked( QAbstractButton*)), this, SLOT(buttonBoxClicked(QAbstractButton*)));
124 // (un)select all
125 connect(ui->pushButtonSelectAll, SIGNAL(clicked()), this, SLOT(buttonSelectAllClicked()));
127 // change coin control first column label due Qt4 bug.
128 // see https://github.com/bitcoin/bitcoin/issues/5716
129 ui->treeWidget->headerItem()->setText(COLUMN_CHECKBOX, QString());
131 ui->treeWidget->setColumnWidth(COLUMN_CHECKBOX, 84);
132 ui->treeWidget->setColumnWidth(COLUMN_AMOUNT, 110);
133 ui->treeWidget->setColumnWidth(COLUMN_LABEL, 190);
134 ui->treeWidget->setColumnWidth(COLUMN_ADDRESS, 320);
135 ui->treeWidget->setColumnWidth(COLUMN_DATE, 130);
136 ui->treeWidget->setColumnWidth(COLUMN_CONFIRMATIONS, 110);
137 ui->treeWidget->setColumnHidden(COLUMN_TXHASH, true); // store transaction hash in this column, but don't show it
138 ui->treeWidget->setColumnHidden(COLUMN_VOUT_INDEX, true); // store vout index in this column, but don't show it
140 // default view is sorted by amount desc
141 sortView(COLUMN_AMOUNT, Qt::DescendingOrder);
143 // restore list mode and sortorder as a convenience feature
144 QSettings settings;
145 if (settings.contains("nCoinControlMode") && !settings.value("nCoinControlMode").toBool())
146 ui->radioTreeMode->click();
147 if (settings.contains("nCoinControlSortColumn") && settings.contains("nCoinControlSortOrder"))
148 sortView(settings.value("nCoinControlSortColumn").toInt(), ((Qt::SortOrder)settings.value("nCoinControlSortOrder").toInt()));
151 CoinControlDialog::~CoinControlDialog()
153 QSettings settings;
154 settings.setValue("nCoinControlMode", ui->radioListMode->isChecked());
155 settings.setValue("nCoinControlSortColumn", sortColumn);
156 settings.setValue("nCoinControlSortOrder", (int)sortOrder);
158 delete ui;
161 void CoinControlDialog::setModel(WalletModel *_model)
163 this->model = _model;
165 if(_model && _model->getOptionsModel() && _model->getAddressTableModel())
167 updateView();
168 updateLabelLocked();
169 CoinControlDialog::updateLabels(_model, this);
173 // ok button
174 void CoinControlDialog::buttonBoxClicked(QAbstractButton* button)
176 if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
177 done(QDialog::Accepted); // closes the dialog
180 // (un)select all
181 void CoinControlDialog::buttonSelectAllClicked()
183 Qt::CheckState state = Qt::Checked;
184 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
186 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != Qt::Unchecked)
188 state = Qt::Unchecked;
189 break;
192 ui->treeWidget->setEnabled(false);
193 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
194 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != state)
195 ui->treeWidget->topLevelItem(i)->setCheckState(COLUMN_CHECKBOX, state);
196 ui->treeWidget->setEnabled(true);
197 if (state == Qt::Unchecked)
198 coinControl->UnSelectAll(); // just to be sure
199 CoinControlDialog::updateLabels(model, this);
202 // context menu
203 void CoinControlDialog::showMenu(const QPoint &point)
205 QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
206 if(item)
208 contextMenuItem = item;
210 // disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
211 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)
213 copyTransactionHashAction->setEnabled(true);
214 if (model->isLockedCoin(uint256S(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt()))
216 lockAction->setEnabled(false);
217 unlockAction->setEnabled(true);
219 else
221 lockAction->setEnabled(true);
222 unlockAction->setEnabled(false);
225 else // this means click on parent node in tree mode -> disable all
227 copyTransactionHashAction->setEnabled(false);
228 lockAction->setEnabled(false);
229 unlockAction->setEnabled(false);
232 // show context menu
233 contextMenu->exec(QCursor::pos());
237 // context menu action: copy amount
238 void CoinControlDialog::copyAmount()
240 GUIUtil::setClipboard(BitcoinUnits::removeSpaces(contextMenuItem->text(COLUMN_AMOUNT)));
243 // context menu action: copy label
244 void CoinControlDialog::copyLabel()
246 if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_LABEL).length() == 0 && contextMenuItem->parent())
247 GUIUtil::setClipboard(contextMenuItem->parent()->text(COLUMN_LABEL));
248 else
249 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_LABEL));
252 // context menu action: copy address
253 void CoinControlDialog::copyAddress()
255 if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_ADDRESS).length() == 0 && contextMenuItem->parent())
256 GUIUtil::setClipboard(contextMenuItem->parent()->text(COLUMN_ADDRESS));
257 else
258 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_ADDRESS));
261 // context menu action: copy transaction id
262 void CoinControlDialog::copyTransactionHash()
264 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_TXHASH));
267 // context menu action: lock coin
268 void CoinControlDialog::lockCoin()
270 if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
271 contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
273 COutPoint outpt(uint256S(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
274 model->lockCoin(outpt);
275 contextMenuItem->setDisabled(true);
276 contextMenuItem->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
277 updateLabelLocked();
280 // context menu action: unlock coin
281 void CoinControlDialog::unlockCoin()
283 COutPoint outpt(uint256S(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
284 model->unlockCoin(outpt);
285 contextMenuItem->setDisabled(false);
286 contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
287 updateLabelLocked();
290 // copy label "Quantity" to clipboard
291 void CoinControlDialog::clipboardQuantity()
293 GUIUtil::setClipboard(ui->labelCoinControlQuantity->text());
296 // copy label "Amount" to clipboard
297 void CoinControlDialog::clipboardAmount()
299 GUIUtil::setClipboard(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
302 // copy label "Fee" to clipboard
303 void CoinControlDialog::clipboardFee()
305 GUIUtil::setClipboard(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
308 // copy label "After fee" to clipboard
309 void CoinControlDialog::clipboardAfterFee()
311 GUIUtil::setClipboard(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
314 // copy label "Bytes" to clipboard
315 void CoinControlDialog::clipboardBytes()
317 GUIUtil::setClipboard(ui->labelCoinControlBytes->text().replace(ASYMP_UTF8, ""));
320 // copy label "Dust" to clipboard
321 void CoinControlDialog::clipboardLowOutput()
323 GUIUtil::setClipboard(ui->labelCoinControlLowOutput->text());
326 // copy label "Change" to clipboard
327 void CoinControlDialog::clipboardChange()
329 GUIUtil::setClipboard(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
332 // treeview: sort
333 void CoinControlDialog::sortView(int column, Qt::SortOrder order)
335 sortColumn = column;
336 sortOrder = order;
337 ui->treeWidget->sortItems(column, order);
338 ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
341 // treeview: clicked on header
342 void CoinControlDialog::headerSectionClicked(int logicalIndex)
344 if (logicalIndex == COLUMN_CHECKBOX) // click on most left column -> do nothing
346 ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
348 else
350 if (sortColumn == logicalIndex)
351 sortOrder = ((sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder);
352 else
354 sortColumn = logicalIndex;
355 sortOrder = ((sortColumn == COLUMN_LABEL || sortColumn == COLUMN_ADDRESS) ? Qt::AscendingOrder : Qt::DescendingOrder); // if label or address then default => asc, else default => desc
358 sortView(sortColumn, sortOrder);
362 // toggle tree mode
363 void CoinControlDialog::radioTreeMode(bool checked)
365 if (checked && model)
366 updateView();
369 // toggle list mode
370 void CoinControlDialog::radioListMode(bool checked)
372 if (checked && model)
373 updateView();
376 // checkbox clicked by user
377 void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
379 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)
381 COutPoint outpt(uint256S(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt());
383 if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
384 coinControl->UnSelect(outpt);
385 else if (item->isDisabled()) // locked (this happens if "check all" through parent node)
386 item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
387 else
388 coinControl->Select(outpt);
390 // selection changed -> update labels
391 if (ui->treeWidget->isEnabled()) // do not update on every click for (un)select all
392 CoinControlDialog::updateLabels(model, this);
395 // TODO: Remove this temporary qt5 fix after Qt5.3 and Qt5.4 are no longer used.
396 // Fixed in Qt5.5 and above: https://bugreports.qt.io/browse/QTBUG-43473
397 #if QT_VERSION >= 0x050000
398 else if (column == COLUMN_CHECKBOX && item->childCount() > 0)
400 if (item->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked && item->child(0)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
401 item->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
403 #endif
406 // shows count of locked unspent outputs
407 void CoinControlDialog::updateLabelLocked()
409 std::vector<COutPoint> vOutpts;
410 model->listLockedCoins(vOutpts);
411 if (vOutpts.size() > 0)
413 ui->labelLocked->setText(tr("(%1 locked)").arg(vOutpts.size()));
414 ui->labelLocked->setVisible(true);
416 else ui->labelLocked->setVisible(false);
419 void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
421 if (!model)
422 return;
424 // nPayAmount
425 CAmount nPayAmount = 0;
426 bool fDust = false;
427 CMutableTransaction txDummy;
428 for (const CAmount &amount : CoinControlDialog::payAmounts)
430 nPayAmount += amount;
432 if (amount > 0)
434 CTxOut txout(amount, (CScript)std::vector<unsigned char>(24, 0));
435 txDummy.vout.push_back(txout);
436 fDust |= IsDust(txout, ::dustRelayFee);
440 CAmount nAmount = 0;
441 CAmount nPayFee = 0;
442 CAmount nAfterFee = 0;
443 CAmount nChange = 0;
444 unsigned int nBytes = 0;
445 unsigned int nBytesInputs = 0;
446 unsigned int nQuantity = 0;
447 bool fWitness = false;
449 std::vector<COutPoint> vCoinControl;
450 std::vector<COutput> vOutputs;
451 coinControl->ListSelected(vCoinControl);
452 model->getOutputs(vCoinControl, vOutputs);
454 for (const COutput& out : vOutputs) {
455 // unselect already spent, very unlikely scenario, this could happen
456 // when selected are spent elsewhere, like rpc or another computer
457 uint256 txhash = out.tx->GetHash();
458 COutPoint outpt(txhash, out.i);
459 if (model->isSpent(outpt))
461 coinControl->UnSelect(outpt);
462 continue;
465 // Quantity
466 nQuantity++;
468 // Amount
469 nAmount += out.tx->tx->vout[out.i].nValue;
471 // Bytes
472 CTxDestination address;
473 int witnessversion = 0;
474 std::vector<unsigned char> witnessprogram;
475 if (out.tx->tx->vout[out.i].scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram))
477 nBytesInputs += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
478 fWitness = true;
480 else if(ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, address))
482 CPubKey pubkey;
483 CKeyID *keyid = boost::get<CKeyID>(&address);
484 if (keyid && model->getPubKey(*keyid, pubkey))
486 nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
488 else
489 nBytesInputs += 148; // in all error cases, simply assume 148 here
491 else nBytesInputs += 148;
494 // calculation
495 if (nQuantity > 0)
497 // Bytes
498 nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
499 if (fWitness)
501 // there is some fudging in these numbers related to the actual virtual transaction size calculation that will keep this estimate from being exact.
502 // usually, the result will be an overestimate within a couple of satoshis so that the confirmation dialog ends up displaying a slightly smaller fee.
503 // 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.
504 nBytes += 2; // account for the serialized marker and flag bytes
505 nBytes += nQuantity; // account for the witness byte that holds the number of stack items for each input.
508 // 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
509 if (CoinControlDialog::fSubtractFeeFromAmount)
510 if (nAmount - nPayAmount == 0)
511 nBytes -= 34;
513 // Fee
514 nPayFee = GetMinimumFee(nBytes, *coinControl, ::mempool, ::feeEstimator, nullptr /* FeeCalculation */);
516 if (nPayAmount > 0)
518 nChange = nAmount - nPayAmount;
519 if (!CoinControlDialog::fSubtractFeeFromAmount)
520 nChange -= nPayFee;
522 // Never create dust outputs; if we would, just add the dust to the fee.
523 if (nChange > 0 && nChange < MIN_CHANGE)
525 CTxOut txout(nChange, (CScript)std::vector<unsigned char>(24, 0));
526 if (IsDust(txout, ::dustRelayFee))
528 nPayFee += nChange;
529 nChange = 0;
530 if (CoinControlDialog::fSubtractFeeFromAmount)
531 nBytes -= 34; // we didn't detect lack of change above
535 if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
536 nBytes -= 34;
539 // after fee
540 nAfterFee = std::max<CAmount>(nAmount - nPayFee, 0);
543 // actually update labels
544 int nDisplayUnit = BitcoinUnits::BTC;
545 if (model && model->getOptionsModel())
546 nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
548 QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
549 QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
550 QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
551 QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
552 QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
553 QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
554 QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
556 // enable/disable "dust" and "change"
557 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
558 dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
559 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
560 dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
562 // stats
563 l1->setText(QString::number(nQuantity)); // Quantity
564 l2->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
565 l3->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
566 l4->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
567 l5->setText(((nBytes > 0) ? ASYMP_UTF8 : "") + QString::number(nBytes)); // Bytes
568 l7->setText(fDust ? tr("yes") : tr("no")); // Dust
569 l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
570 if (nPayFee > 0)
572 l3->setText(ASYMP_UTF8 + l3->text());
573 l4->setText(ASYMP_UTF8 + l4->text());
574 if (nChange > 0 && !CoinControlDialog::fSubtractFeeFromAmount)
575 l8->setText(ASYMP_UTF8 + l8->text());
578 // turn label red when dust
579 l7->setStyleSheet((fDust) ? "color:red;" : "");
581 // tool tips
582 QString toolTipDust = tr("This label turns red if any recipient receives an amount smaller than the current dust threshold.");
584 // how many satoshis the estimated fee can vary per byte we guess wrong
585 double dFeeVary = (double)nPayFee / nBytes;
587 QString toolTip4 = tr("Can vary +/- %1 satoshi(s) per input.").arg(dFeeVary);
589 l3->setToolTip(toolTip4);
590 l4->setToolTip(toolTip4);
591 l7->setToolTip(toolTipDust);
592 l8->setToolTip(toolTip4);
593 dialog->findChild<QLabel *>("labelCoinControlFeeText") ->setToolTip(l3->toolTip());
594 dialog->findChild<QLabel *>("labelCoinControlAfterFeeText") ->setToolTip(l4->toolTip());
595 dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
596 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
597 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
599 // Insufficient funds
600 QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
601 if (label)
602 label->setVisible(nChange < 0);
605 void CoinControlDialog::updateView()
607 if (!model || !model->getOptionsModel() || !model->getAddressTableModel())
608 return;
610 bool treeMode = ui->radioTreeMode->isChecked();
612 ui->treeWidget->clear();
613 ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
614 ui->treeWidget->setAlternatingRowColors(!treeMode);
615 QFlags<Qt::ItemFlag> flgCheckbox = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
616 QFlags<Qt::ItemFlag> flgTristate = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
618 int nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
620 std::map<QString, std::vector<COutput> > mapCoins;
621 model->listCoins(mapCoins);
623 for (const std::pair<QString, std::vector<COutput>>& coins : mapCoins) {
624 CCoinControlWidgetItem *itemWalletAddress = new CCoinControlWidgetItem();
625 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
626 QString sWalletAddress = coins.first;
627 QString sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
628 if (sWalletLabel.isEmpty())
629 sWalletLabel = tr("(no label)");
631 if (treeMode)
633 // wallet address
634 ui->treeWidget->addTopLevelItem(itemWalletAddress);
636 itemWalletAddress->setFlags(flgTristate);
637 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
639 // label
640 itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
642 // address
643 itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
646 CAmount nSum = 0;
647 int nChildren = 0;
648 for (const COutput& out : coins.second) {
649 nSum += out.tx->tx->vout[out.i].nValue;
650 nChildren++;
652 CCoinControlWidgetItem *itemOutput;
653 if (treeMode) itemOutput = new CCoinControlWidgetItem(itemWalletAddress);
654 else itemOutput = new CCoinControlWidgetItem(ui->treeWidget);
655 itemOutput->setFlags(flgCheckbox);
656 itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
658 // address
659 CTxDestination outputAddress;
660 QString sAddress = "";
661 if(ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, outputAddress))
663 sAddress = QString::fromStdString(EncodeDestination(outputAddress));
665 // if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
666 if (!treeMode || (!(sAddress == sWalletAddress)))
667 itemOutput->setText(COLUMN_ADDRESS, sAddress);
670 // label
671 if (!(sAddress == sWalletAddress)) // change
673 // tooltip from where the change comes from
674 itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
675 itemOutput->setText(COLUMN_LABEL, tr("(change)"));
677 else if (!treeMode)
679 QString sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
680 if (sLabel.isEmpty())
681 sLabel = tr("(no label)");
682 itemOutput->setText(COLUMN_LABEL, sLabel);
685 // amount
686 itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.tx->tx->vout[out.i].nValue));
687 itemOutput->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)out.tx->tx->vout[out.i].nValue)); // padding so that sorting works correctly
689 // date
690 itemOutput->setText(COLUMN_DATE, GUIUtil::dateTimeStr(out.tx->GetTxTime()));
691 itemOutput->setData(COLUMN_DATE, Qt::UserRole, QVariant((qlonglong)out.tx->GetTxTime()));
693 // confirmations
694 itemOutput->setText(COLUMN_CONFIRMATIONS, QString::number(out.nDepth));
695 itemOutput->setData(COLUMN_CONFIRMATIONS, Qt::UserRole, QVariant((qlonglong)out.nDepth));
697 // transaction hash
698 uint256 txhash = out.tx->GetHash();
699 itemOutput->setText(COLUMN_TXHASH, QString::fromStdString(txhash.GetHex()));
701 // vout index
702 itemOutput->setText(COLUMN_VOUT_INDEX, QString::number(out.i));
704 // disable locked coins
705 if (model->isLockedCoin(txhash, out.i))
707 COutPoint outpt(txhash, out.i);
708 coinControl->UnSelect(outpt); // just to be sure
709 itemOutput->setDisabled(true);
710 itemOutput->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
713 // set checkbox
714 if (coinControl->IsSelected(COutPoint(txhash, out.i)))
715 itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
718 // amount
719 if (treeMode)
721 itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
722 itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
723 itemWalletAddress->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)nSum));
727 // expand all partially selected
728 if (treeMode)
730 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
731 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
732 ui->treeWidget->topLevelItem(i)->setExpanded(true);
735 // sort view
736 sortView(sortColumn, sortOrder);
737 ui->treeWidget->setEnabled(true);