Merge #10114: [tests] sync_with_ping should assert that ping hasn't timed out
[bitcoinplatinum.git] / src / qt / coincontroldialog.cpp
blob1d19c65753d3d8b7aac6595da025d6a579865f7e
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/policy.h"
19 #include "validation.h" // For mempool
20 #include "wallet/wallet.h"
22 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
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 Q_FOREACH(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 if (txout.IsDust(dustRelayFee))
437 fDust = true;
441 CAmount nAmount = 0;
442 CAmount nPayFee = 0;
443 CAmount nAfterFee = 0;
444 CAmount nChange = 0;
445 unsigned int nBytes = 0;
446 unsigned int nBytesInputs = 0;
447 unsigned int nQuantity = 0;
448 bool fWitness = false;
450 std::vector<COutPoint> vCoinControl;
451 std::vector<COutput> vOutputs;
452 coinControl->ListSelected(vCoinControl);
453 model->getOutputs(vCoinControl, vOutputs);
455 BOOST_FOREACH(const COutput& out, vOutputs) {
456 // unselect already spent, very unlikely scenario, this could happen
457 // when selected are spent elsewhere, like rpc or another computer
458 uint256 txhash = out.tx->GetHash();
459 COutPoint outpt(txhash, out.i);
460 if (model->isSpent(outpt))
462 coinControl->UnSelect(outpt);
463 continue;
466 // Quantity
467 nQuantity++;
469 // Amount
470 nAmount += out.tx->tx->vout[out.i].nValue;
472 // Bytes
473 CTxDestination address;
474 int witnessversion = 0;
475 std::vector<unsigned char> witnessprogram;
476 if (out.tx->tx->vout[out.i].scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram))
478 nBytesInputs += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
479 fWitness = true;
481 else if(ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, address))
483 CPubKey pubkey;
484 CKeyID *keyid = boost::get<CKeyID>(&address);
485 if (keyid && model->getPubKey(*keyid, pubkey))
487 nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
489 else
490 nBytesInputs += 148; // in all error cases, simply assume 148 here
492 else nBytesInputs += 148;
495 // calculation
496 if (nQuantity > 0)
498 // Bytes
499 nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
500 if (fWitness)
502 // there is some fudging in these numbers related to the actual virtual transaction size calculation that will keep this estimate from being exact.
503 // usually, the result will be an overestimate within a couple of satoshis so that the confirmation dialog ends up displaying a slightly smaller fee.
504 // 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.
505 nBytes += 2; // account for the serialized marker and flag bytes
506 nBytes += nQuantity; // account for the witness byte that holds the number of stack items for each input.
509 // 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
510 if (CoinControlDialog::fSubtractFeeFromAmount)
511 if (nAmount - nPayAmount == 0)
512 nBytes -= 34;
514 // Fee
515 nPayFee = CWallet::GetMinimumFee(nBytes, nTxConfirmTarget, mempool);
516 if (nPayFee > 0 && coinControl->nMinimumTotalFee > nPayFee)
517 nPayFee = coinControl->nMinimumTotalFee;
519 if (nPayAmount > 0)
521 nChange = nAmount - nPayAmount;
522 if (!CoinControlDialog::fSubtractFeeFromAmount)
523 nChange -= nPayFee;
525 // Never create dust outputs; if we would, just add the dust to the fee.
526 if (nChange > 0 && nChange < MIN_CHANGE)
528 CTxOut txout(nChange, (CScript)std::vector<unsigned char>(24, 0));
529 if (txout.IsDust(dustRelayFee))
531 if (CoinControlDialog::fSubtractFeeFromAmount) // dust-change will be raised until no dust
532 nChange = txout.GetDustThreshold(dustRelayFee);
533 else
535 nPayFee += nChange;
536 nChange = 0;
541 if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
542 nBytes -= 34;
545 // after fee
546 nAfterFee = std::max<CAmount>(nAmount - nPayFee, 0);
549 // actually update labels
550 int nDisplayUnit = BitcoinUnits::BTC;
551 if (model && model->getOptionsModel())
552 nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
554 QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
555 QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
556 QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
557 QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
558 QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
559 QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
560 QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
562 // enable/disable "dust" and "change"
563 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
564 dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
565 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
566 dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
568 // stats
569 l1->setText(QString::number(nQuantity)); // Quantity
570 l2->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
571 l3->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
572 l4->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
573 l5->setText(((nBytes > 0) ? ASYMP_UTF8 : "") + QString::number(nBytes)); // Bytes
574 l7->setText(fDust ? tr("yes") : tr("no")); // Dust
575 l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
576 if (nPayFee > 0 && (coinControl->nMinimumTotalFee < nPayFee))
578 l3->setText(ASYMP_UTF8 + l3->text());
579 l4->setText(ASYMP_UTF8 + l4->text());
580 if (nChange > 0 && !CoinControlDialog::fSubtractFeeFromAmount)
581 l8->setText(ASYMP_UTF8 + l8->text());
584 // turn label red when dust
585 l7->setStyleSheet((fDust) ? "color:red;" : "");
587 // tool tips
588 QString toolTipDust = tr("This label turns red if any recipient receives an amount smaller than the current dust threshold.");
590 // how many satoshis the estimated fee can vary per byte we guess wrong
591 double dFeeVary;
592 if (payTxFee.GetFeePerK() > 0)
593 dFeeVary = (double)std::max(CWallet::GetRequiredFee(1000), payTxFee.GetFeePerK()) / 1000;
594 else {
595 dFeeVary = (double)std::max(CWallet::GetRequiredFee(1000), mempool.estimateSmartFee(nTxConfirmTarget).GetFeePerK()) / 1000;
597 QString toolTip4 = tr("Can vary +/- %1 satoshi(s) per input.").arg(dFeeVary);
599 l3->setToolTip(toolTip4);
600 l4->setToolTip(toolTip4);
601 l7->setToolTip(toolTipDust);
602 l8->setToolTip(toolTip4);
603 dialog->findChild<QLabel *>("labelCoinControlFeeText") ->setToolTip(l3->toolTip());
604 dialog->findChild<QLabel *>("labelCoinControlAfterFeeText") ->setToolTip(l4->toolTip());
605 dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
606 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
607 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
609 // Insufficient funds
610 QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
611 if (label)
612 label->setVisible(nChange < 0);
615 void CoinControlDialog::updateView()
617 if (!model || !model->getOptionsModel() || !model->getAddressTableModel())
618 return;
620 bool treeMode = ui->radioTreeMode->isChecked();
622 ui->treeWidget->clear();
623 ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
624 ui->treeWidget->setAlternatingRowColors(!treeMode);
625 QFlags<Qt::ItemFlag> flgCheckbox = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
626 QFlags<Qt::ItemFlag> flgTristate = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
628 int nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
630 std::map<QString, std::vector<COutput> > mapCoins;
631 model->listCoins(mapCoins);
633 BOOST_FOREACH(const PAIRTYPE(QString, std::vector<COutput>)& coins, mapCoins) {
634 CCoinControlWidgetItem *itemWalletAddress = new CCoinControlWidgetItem();
635 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
636 QString sWalletAddress = coins.first;
637 QString sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
638 if (sWalletLabel.isEmpty())
639 sWalletLabel = tr("(no label)");
641 if (treeMode)
643 // wallet address
644 ui->treeWidget->addTopLevelItem(itemWalletAddress);
646 itemWalletAddress->setFlags(flgTristate);
647 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
649 // label
650 itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
652 // address
653 itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
656 CAmount nSum = 0;
657 int nChildren = 0;
658 BOOST_FOREACH(const COutput& out, coins.second) {
659 nSum += out.tx->tx->vout[out.i].nValue;
660 nChildren++;
662 CCoinControlWidgetItem *itemOutput;
663 if (treeMode) itemOutput = new CCoinControlWidgetItem(itemWalletAddress);
664 else itemOutput = new CCoinControlWidgetItem(ui->treeWidget);
665 itemOutput->setFlags(flgCheckbox);
666 itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
668 // address
669 CTxDestination outputAddress;
670 QString sAddress = "";
671 if(ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, outputAddress))
673 sAddress = QString::fromStdString(CBitcoinAddress(outputAddress).ToString());
675 // if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
676 if (!treeMode || (!(sAddress == sWalletAddress)))
677 itemOutput->setText(COLUMN_ADDRESS, sAddress);
680 // label
681 if (!(sAddress == sWalletAddress)) // change
683 // tooltip from where the change comes from
684 itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
685 itemOutput->setText(COLUMN_LABEL, tr("(change)"));
687 else if (!treeMode)
689 QString sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
690 if (sLabel.isEmpty())
691 sLabel = tr("(no label)");
692 itemOutput->setText(COLUMN_LABEL, sLabel);
695 // amount
696 itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.tx->tx->vout[out.i].nValue));
697 itemOutput->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)out.tx->tx->vout[out.i].nValue)); // padding so that sorting works correctly
699 // date
700 itemOutput->setText(COLUMN_DATE, GUIUtil::dateTimeStr(out.tx->GetTxTime()));
701 itemOutput->setData(COLUMN_DATE, Qt::UserRole, QVariant((qlonglong)out.tx->GetTxTime()));
703 // confirmations
704 itemOutput->setText(COLUMN_CONFIRMATIONS, QString::number(out.nDepth));
705 itemOutput->setData(COLUMN_CONFIRMATIONS, Qt::UserRole, QVariant((qlonglong)out.nDepth));
707 // transaction hash
708 uint256 txhash = out.tx->GetHash();
709 itemOutput->setText(COLUMN_TXHASH, QString::fromStdString(txhash.GetHex()));
711 // vout index
712 itemOutput->setText(COLUMN_VOUT_INDEX, QString::number(out.i));
714 // disable locked coins
715 if (model->isLockedCoin(txhash, out.i))
717 COutPoint outpt(txhash, out.i);
718 coinControl->UnSelect(outpt); // just to be sure
719 itemOutput->setDisabled(true);
720 itemOutput->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
723 // set checkbox
724 if (coinControl->IsSelected(COutPoint(txhash, out.i)))
725 itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
728 // amount
729 if (treeMode)
731 itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
732 itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
733 itemWalletAddress->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)nSum));
737 // expand all partially selected
738 if (treeMode)
740 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
741 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
742 ui->treeWidget->topLevelItem(i)->setExpanded(true);
745 // sort view
746 sortView(sortColumn, sortOrder);
747 ui->treeWidget->setEnabled(true);