moveonly: move `coincontrol` to `src/wallet`
[bitcoinplatinum.git] / src / qt / coincontroldialog.cpp
blob1a1671f0ee830560f9fa9a63d05c631492cd4382
1 // Copyright (c) 2011-2015 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 "main.h" // For minRelayTxFee
19 #include "wallet/wallet.h"
21 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
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 CoinControlDialog::CoinControlDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
39 QDialog(parent),
40 ui(new Ui::CoinControlDialog),
41 model(0),
42 platformStyle(_platformStyle)
44 ui->setupUi(this);
46 // context menu actions
47 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
48 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
49 QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
50 copyTransactionHashAction = new QAction(tr("Copy transaction ID"), this); // we need to enable/disable this
51 lockAction = new QAction(tr("Lock unspent"), this); // we need to enable/disable this
52 unlockAction = new QAction(tr("Unlock unspent"), this); // we need to enable/disable this
54 // context menu
55 contextMenu = new QMenu();
56 contextMenu->addAction(copyAddressAction);
57 contextMenu->addAction(copyLabelAction);
58 contextMenu->addAction(copyAmountAction);
59 contextMenu->addAction(copyTransactionHashAction);
60 contextMenu->addSeparator();
61 contextMenu->addAction(lockAction);
62 contextMenu->addAction(unlockAction);
64 // context menu signals
65 connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
66 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
67 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
68 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
69 connect(copyTransactionHashAction, SIGNAL(triggered()), this, SLOT(copyTransactionHash()));
70 connect(lockAction, SIGNAL(triggered()), this, SLOT(lockCoin()));
71 connect(unlockAction, SIGNAL(triggered()), this, SLOT(unlockCoin()));
73 // clipboard actions
74 QAction *clipboardQuantityAction = new QAction(tr("Copy quantity"), this);
75 QAction *clipboardAmountAction = new QAction(tr("Copy amount"), this);
76 QAction *clipboardFeeAction = new QAction(tr("Copy fee"), this);
77 QAction *clipboardAfterFeeAction = new QAction(tr("Copy after fee"), this);
78 QAction *clipboardBytesAction = new QAction(tr("Copy bytes"), this);
79 QAction *clipboardLowOutputAction = new QAction(tr("Copy dust"), this);
80 QAction *clipboardChangeAction = new QAction(tr("Copy change"), this);
82 connect(clipboardQuantityAction, SIGNAL(triggered()), this, SLOT(clipboardQuantity()));
83 connect(clipboardAmountAction, SIGNAL(triggered()), this, SLOT(clipboardAmount()));
84 connect(clipboardFeeAction, SIGNAL(triggered()), this, SLOT(clipboardFee()));
85 connect(clipboardAfterFeeAction, SIGNAL(triggered()), this, SLOT(clipboardAfterFee()));
86 connect(clipboardBytesAction, SIGNAL(triggered()), this, SLOT(clipboardBytes()));
87 connect(clipboardLowOutputAction, SIGNAL(triggered()), this, SLOT(clipboardLowOutput()));
88 connect(clipboardChangeAction, SIGNAL(triggered()), this, SLOT(clipboardChange()));
90 ui->labelCoinControlQuantity->addAction(clipboardQuantityAction);
91 ui->labelCoinControlAmount->addAction(clipboardAmountAction);
92 ui->labelCoinControlFee->addAction(clipboardFeeAction);
93 ui->labelCoinControlAfterFee->addAction(clipboardAfterFeeAction);
94 ui->labelCoinControlBytes->addAction(clipboardBytesAction);
95 ui->labelCoinControlLowOutput->addAction(clipboardLowOutputAction);
96 ui->labelCoinControlChange->addAction(clipboardChangeAction);
98 // toggle tree/list mode
99 connect(ui->radioTreeMode, SIGNAL(toggled(bool)), this, SLOT(radioTreeMode(bool)));
100 connect(ui->radioListMode, SIGNAL(toggled(bool)), this, SLOT(radioListMode(bool)));
102 // click on checkbox
103 connect(ui->treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(viewItemChanged(QTreeWidgetItem*, int)));
105 // click on header
106 #if QT_VERSION < 0x050000
107 ui->treeWidget->header()->setClickable(true);
108 #else
109 ui->treeWidget->header()->setSectionsClickable(true);
110 #endif
111 connect(ui->treeWidget->header(), SIGNAL(sectionClicked(int)), this, SLOT(headerSectionClicked(int)));
113 // ok button
114 connect(ui->buttonBox, SIGNAL(clicked( QAbstractButton*)), this, SLOT(buttonBoxClicked(QAbstractButton*)));
116 // (un)select all
117 connect(ui->pushButtonSelectAll, SIGNAL(clicked()), this, SLOT(buttonSelectAllClicked()));
119 // change coin control first column label due Qt4 bug.
120 // see https://github.com/bitcoin/bitcoin/issues/5716
121 ui->treeWidget->headerItem()->setText(COLUMN_CHECKBOX, QString());
123 ui->treeWidget->setColumnWidth(COLUMN_CHECKBOX, 84);
124 ui->treeWidget->setColumnWidth(COLUMN_AMOUNT, 110);
125 ui->treeWidget->setColumnWidth(COLUMN_LABEL, 190);
126 ui->treeWidget->setColumnWidth(COLUMN_ADDRESS, 320);
127 ui->treeWidget->setColumnWidth(COLUMN_DATE, 130);
128 ui->treeWidget->setColumnWidth(COLUMN_CONFIRMATIONS, 110);
129 ui->treeWidget->setColumnHidden(COLUMN_TXHASH, true); // store transaction hash in this column, but don't show it
130 ui->treeWidget->setColumnHidden(COLUMN_VOUT_INDEX, true); // store vout index in this column, but don't show it
131 ui->treeWidget->setColumnHidden(COLUMN_AMOUNT_INT64, true); // store amount int64 in this column, but don't show it
132 ui->treeWidget->setColumnHidden(COLUMN_DATE_INT64, true); // store date int64 in this column, but don't show it
134 // default view is sorted by amount desc
135 sortView(COLUMN_AMOUNT_INT64, Qt::DescendingOrder);
137 // restore list mode and sortorder as a convenience feature
138 QSettings settings;
139 if (settings.contains("nCoinControlMode") && !settings.value("nCoinControlMode").toBool())
140 ui->radioTreeMode->click();
141 if (settings.contains("nCoinControlSortColumn") && settings.contains("nCoinControlSortOrder"))
142 sortView(settings.value("nCoinControlSortColumn").toInt(), ((Qt::SortOrder)settings.value("nCoinControlSortOrder").toInt()));
145 CoinControlDialog::~CoinControlDialog()
147 QSettings settings;
148 settings.setValue("nCoinControlMode", ui->radioListMode->isChecked());
149 settings.setValue("nCoinControlSortColumn", sortColumn);
150 settings.setValue("nCoinControlSortOrder", (int)sortOrder);
152 delete ui;
155 void CoinControlDialog::setModel(WalletModel *_model)
157 this->model = _model;
159 if(_model && _model->getOptionsModel() && _model->getAddressTableModel())
161 updateView();
162 updateLabelLocked();
163 CoinControlDialog::updateLabels(_model, this);
167 // helper function str_pad
168 QString CoinControlDialog::strPad(QString s, int nPadLength, QString sPadding)
170 while (s.length() < nPadLength)
171 s = sPadding + s;
173 return s;
176 // ok button
177 void CoinControlDialog::buttonBoxClicked(QAbstractButton* button)
179 if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
180 done(QDialog::Accepted); // closes the dialog
183 // (un)select all
184 void CoinControlDialog::buttonSelectAllClicked()
186 Qt::CheckState state = Qt::Checked;
187 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
189 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != Qt::Unchecked)
191 state = Qt::Unchecked;
192 break;
195 ui->treeWidget->setEnabled(false);
196 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
197 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != state)
198 ui->treeWidget->topLevelItem(i)->setCheckState(COLUMN_CHECKBOX, state);
199 ui->treeWidget->setEnabled(true);
200 if (state == Qt::Unchecked)
201 coinControl->UnSelectAll(); // just to be sure
202 CoinControlDialog::updateLabels(model, this);
205 // context menu
206 void CoinControlDialog::showMenu(const QPoint &point)
208 QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
209 if(item)
211 contextMenuItem = item;
213 // disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
214 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)
216 copyTransactionHashAction->setEnabled(true);
217 if (model->isLockedCoin(uint256S(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt()))
219 lockAction->setEnabled(false);
220 unlockAction->setEnabled(true);
222 else
224 lockAction->setEnabled(true);
225 unlockAction->setEnabled(false);
228 else // this means click on parent node in tree mode -> disable all
230 copyTransactionHashAction->setEnabled(false);
231 lockAction->setEnabled(false);
232 unlockAction->setEnabled(false);
235 // show context menu
236 contextMenu->exec(QCursor::pos());
240 // context menu action: copy amount
241 void CoinControlDialog::copyAmount()
243 GUIUtil::setClipboard(BitcoinUnits::removeSpaces(contextMenuItem->text(COLUMN_AMOUNT)));
246 // context menu action: copy label
247 void CoinControlDialog::copyLabel()
249 if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_LABEL).length() == 0 && contextMenuItem->parent())
250 GUIUtil::setClipboard(contextMenuItem->parent()->text(COLUMN_LABEL));
251 else
252 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_LABEL));
255 // context menu action: copy address
256 void CoinControlDialog::copyAddress()
258 if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_ADDRESS).length() == 0 && contextMenuItem->parent())
259 GUIUtil::setClipboard(contextMenuItem->parent()->text(COLUMN_ADDRESS));
260 else
261 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_ADDRESS));
264 // context menu action: copy transaction id
265 void CoinControlDialog::copyTransactionHash()
267 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_TXHASH));
270 // context menu action: lock coin
271 void CoinControlDialog::lockCoin()
273 if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
274 contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
276 COutPoint outpt(uint256S(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
277 model->lockCoin(outpt);
278 contextMenuItem->setDisabled(true);
279 contextMenuItem->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
280 updateLabelLocked();
283 // context menu action: unlock coin
284 void CoinControlDialog::unlockCoin()
286 COutPoint outpt(uint256S(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
287 model->unlockCoin(outpt);
288 contextMenuItem->setDisabled(false);
289 contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
290 updateLabelLocked();
293 // copy label "Quantity" to clipboard
294 void CoinControlDialog::clipboardQuantity()
296 GUIUtil::setClipboard(ui->labelCoinControlQuantity->text());
299 // copy label "Amount" to clipboard
300 void CoinControlDialog::clipboardAmount()
302 GUIUtil::setClipboard(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
305 // copy label "Fee" to clipboard
306 void CoinControlDialog::clipboardFee()
308 GUIUtil::setClipboard(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
311 // copy label "After fee" to clipboard
312 void CoinControlDialog::clipboardAfterFee()
314 GUIUtil::setClipboard(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
317 // copy label "Bytes" to clipboard
318 void CoinControlDialog::clipboardBytes()
320 GUIUtil::setClipboard(ui->labelCoinControlBytes->text().replace(ASYMP_UTF8, ""));
323 // copy label "Dust" to clipboard
324 void CoinControlDialog::clipboardLowOutput()
326 GUIUtil::setClipboard(ui->labelCoinControlLowOutput->text());
329 // copy label "Change" to clipboard
330 void CoinControlDialog::clipboardChange()
332 GUIUtil::setClipboard(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
335 // treeview: sort
336 void CoinControlDialog::sortView(int column, Qt::SortOrder order)
338 sortColumn = column;
339 sortOrder = order;
340 ui->treeWidget->sortItems(column, order);
341 ui->treeWidget->header()->setSortIndicator(getMappedColumn(sortColumn), sortOrder);
344 // treeview: clicked on header
345 void CoinControlDialog::headerSectionClicked(int logicalIndex)
347 if (logicalIndex == COLUMN_CHECKBOX) // click on most left column -> do nothing
349 ui->treeWidget->header()->setSortIndicator(getMappedColumn(sortColumn), sortOrder);
351 else
353 logicalIndex = getMappedColumn(logicalIndex, false);
355 if (sortColumn == logicalIndex)
356 sortOrder = ((sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder);
357 else
359 sortColumn = logicalIndex;
360 sortOrder = ((sortColumn == COLUMN_LABEL || sortColumn == COLUMN_ADDRESS) ? Qt::AscendingOrder : Qt::DescendingOrder); // if label or address then default => asc, else default => desc
363 sortView(sortColumn, sortOrder);
367 // toggle tree mode
368 void CoinControlDialog::radioTreeMode(bool checked)
370 if (checked && model)
371 updateView();
374 // toggle list mode
375 void CoinControlDialog::radioListMode(bool checked)
377 if (checked && model)
378 updateView();
381 // checkbox clicked by user
382 void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
384 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)
386 COutPoint outpt(uint256S(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt());
388 if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
389 coinControl->UnSelect(outpt);
390 else if (item->isDisabled()) // locked (this happens if "check all" through parent node)
391 item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
392 else
393 coinControl->Select(outpt);
395 // selection changed -> update labels
396 if (ui->treeWidget->isEnabled()) // do not update on every click for (un)select all
397 CoinControlDialog::updateLabels(model, this);
400 // TODO: Remove this temporary qt5 fix after Qt5.3 and Qt5.4 are no longer used.
401 // Fixed in Qt5.5 and above: https://bugreports.qt.io/browse/QTBUG-43473
402 #if QT_VERSION >= 0x050000
403 else if (column == COLUMN_CHECKBOX && item->childCount() > 0)
405 if (item->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked && item->child(0)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
406 item->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
408 #endif
411 // shows count of locked unspent outputs
412 void CoinControlDialog::updateLabelLocked()
414 std::vector<COutPoint> vOutpts;
415 model->listLockedCoins(vOutpts);
416 if (vOutpts.size() > 0)
418 ui->labelLocked->setText(tr("(%1 locked)").arg(vOutpts.size()));
419 ui->labelLocked->setVisible(true);
421 else ui->labelLocked->setVisible(false);
424 void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
426 if (!model)
427 return;
429 // nPayAmount
430 CAmount nPayAmount = 0;
431 bool fDust = false;
432 CMutableTransaction txDummy;
433 Q_FOREACH(const CAmount &amount, CoinControlDialog::payAmounts)
435 nPayAmount += amount;
437 if (amount > 0)
439 CTxOut txout(amount, (CScript)std::vector<unsigned char>(24, 0));
440 txDummy.vout.push_back(txout);
441 if (txout.IsDust(::minRelayTxFee))
442 fDust = true;
446 CAmount nAmount = 0;
447 CAmount nPayFee = 0;
448 CAmount nAfterFee = 0;
449 CAmount nChange = 0;
450 unsigned int nBytes = 0;
451 unsigned int nBytesInputs = 0;
452 double dPriority = 0;
453 double dPriorityInputs = 0;
454 unsigned int nQuantity = 0;
455 int nQuantityUncompressed = 0;
456 bool fAllowFree = false;
457 bool fWitness = false;
459 std::vector<COutPoint> vCoinControl;
460 std::vector<COutput> vOutputs;
461 coinControl->ListSelected(vCoinControl);
462 model->getOutputs(vCoinControl, vOutputs);
464 BOOST_FOREACH(const COutput& out, vOutputs) {
465 // unselect already spent, very unlikely scenario, this could happen
466 // when selected are spent elsewhere, like rpc or another computer
467 uint256 txhash = out.tx->GetHash();
468 COutPoint outpt(txhash, out.i);
469 if (model->isSpent(outpt))
471 coinControl->UnSelect(outpt);
472 continue;
475 // Quantity
476 nQuantity++;
478 // Amount
479 nAmount += out.tx->vout[out.i].nValue;
481 // Priority
482 dPriorityInputs += (double)out.tx->vout[out.i].nValue * (out.nDepth+1);
484 // Bytes
485 CTxDestination address;
486 int witnessversion = 0;
487 std::vector<unsigned char> witnessprogram;
488 if (out.tx->vout[out.i].scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram))
490 nBytesInputs += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
491 fWitness = true;
493 else if(ExtractDestination(out.tx->vout[out.i].scriptPubKey, address))
495 CPubKey pubkey;
496 CKeyID *keyid = boost::get<CKeyID>(&address);
497 if (keyid && model->getPubKey(*keyid, pubkey))
499 nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
500 if (!pubkey.IsCompressed())
501 nQuantityUncompressed++;
503 else
504 nBytesInputs += 148; // in all error cases, simply assume 148 here
506 else nBytesInputs += 148;
509 // calculation
510 if (nQuantity > 0)
512 // Bytes
513 nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
514 if (fWitness)
516 // there is some fudging in these numbers related to the actual virtual transaction size calculation that will keep this estimate from being exact.
517 // usually, the result will be an overestimate within a couple of satoshis so that the confirmation dialog ends up displaying a slightly smaller fee.
518 // 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.
519 nBytes += 2; // account for the serialized marker and flag bytes
520 nBytes += nQuantity; // account for the witness byte that holds the number of stack items for each input.
523 // 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
524 if (CoinControlDialog::fSubtractFeeFromAmount)
525 if (nAmount - nPayAmount == 0)
526 nBytes -= 34;
528 // Fee
529 nPayFee = CWallet::GetMinimumFee(nBytes, nTxConfirmTarget, mempool);
530 if (nPayFee > 0 && coinControl->nMinimumTotalFee > nPayFee)
531 nPayFee = coinControl->nMinimumTotalFee;
534 // Allow free? (require at least hard-coded threshold and default to that if no estimate)
535 double mempoolEstimatePriority = mempool.estimateSmartPriority(nTxConfirmTarget);
536 dPriority = dPriorityInputs / (nBytes - nBytesInputs + (nQuantityUncompressed * 29)); // 29 = 180 - 151 (uncompressed public keys are over the limit. max 151 bytes of the input are ignored for priority)
537 double dPriorityNeeded = std::max(mempoolEstimatePriority, AllowFreeThreshold());
538 fAllowFree = (dPriority >= dPriorityNeeded);
540 if (fSendFreeTransactions)
541 if (fAllowFree && nBytes <= MAX_FREE_TRANSACTION_CREATE_SIZE)
542 nPayFee = 0;
544 if (nPayAmount > 0)
546 nChange = nAmount - nPayAmount;
547 if (!CoinControlDialog::fSubtractFeeFromAmount)
548 nChange -= nPayFee;
550 // Never create dust outputs; if we would, just add the dust to the fee.
551 if (nChange > 0 && nChange < MIN_CHANGE)
553 CTxOut txout(nChange, (CScript)std::vector<unsigned char>(24, 0));
554 if (txout.IsDust(::minRelayTxFee))
556 if (CoinControlDialog::fSubtractFeeFromAmount) // dust-change will be raised until no dust
557 nChange = txout.GetDustThreshold(::minRelayTxFee);
558 else
560 nPayFee += nChange;
561 nChange = 0;
566 if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
567 nBytes -= 34;
570 // after fee
571 nAfterFee = nAmount - nPayFee;
572 if (nAfterFee < 0)
573 nAfterFee = 0;
576 // actually update labels
577 int nDisplayUnit = BitcoinUnits::BTC;
578 if (model && model->getOptionsModel())
579 nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
581 QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
582 QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
583 QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
584 QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
585 QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
586 QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
587 QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
589 // enable/disable "dust" and "change"
590 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
591 dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
592 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
593 dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
595 // stats
596 l1->setText(QString::number(nQuantity)); // Quantity
597 l2->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
598 l3->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
599 l4->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
600 l5->setText(((nBytes > 0) ? ASYMP_UTF8 : "") + QString::number(nBytes)); // Bytes
601 l7->setText(fDust ? tr("yes") : tr("no")); // Dust
602 l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
603 if (nPayFee > 0 && (coinControl->nMinimumTotalFee < nPayFee))
605 l3->setText(ASYMP_UTF8 + l3->text());
606 l4->setText(ASYMP_UTF8 + l4->text());
607 if (nChange > 0 && !CoinControlDialog::fSubtractFeeFromAmount)
608 l8->setText(ASYMP_UTF8 + l8->text());
611 // turn label red when dust
612 l7->setStyleSheet((fDust) ? "color:red;" : "");
614 // tool tips
615 QString toolTipDust = tr("This label turns red if any recipient receives an amount smaller than the current dust threshold.");
617 // how many satoshis the estimated fee can vary per byte we guess wrong
618 double dFeeVary;
619 if (payTxFee.GetFeePerK() > 0)
620 dFeeVary = (double)std::max(CWallet::GetRequiredFee(1000), payTxFee.GetFeePerK()) / 1000;
621 else {
622 dFeeVary = (double)std::max(CWallet::GetRequiredFee(1000), mempool.estimateSmartFee(nTxConfirmTarget).GetFeePerK()) / 1000;
624 QString toolTip4 = tr("Can vary +/- %1 satoshi(s) per input.").arg(dFeeVary);
626 l3->setToolTip(toolTip4);
627 l4->setToolTip(toolTip4);
628 l7->setToolTip(toolTipDust);
629 l8->setToolTip(toolTip4);
630 dialog->findChild<QLabel *>("labelCoinControlFeeText") ->setToolTip(l3->toolTip());
631 dialog->findChild<QLabel *>("labelCoinControlAfterFeeText") ->setToolTip(l4->toolTip());
632 dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
633 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
634 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
636 // Insufficient funds
637 QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
638 if (label)
639 label->setVisible(nChange < 0);
642 void CoinControlDialog::updateView()
644 if (!model || !model->getOptionsModel() || !model->getAddressTableModel())
645 return;
647 bool treeMode = ui->radioTreeMode->isChecked();
649 ui->treeWidget->clear();
650 ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
651 ui->treeWidget->setAlternatingRowColors(!treeMode);
652 QFlags<Qt::ItemFlag> flgCheckbox = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
653 QFlags<Qt::ItemFlag> flgTristate = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
655 int nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
657 std::map<QString, std::vector<COutput> > mapCoins;
658 model->listCoins(mapCoins);
660 BOOST_FOREACH(const PAIRTYPE(QString, std::vector<COutput>)& coins, mapCoins) {
661 QTreeWidgetItem *itemWalletAddress = new QTreeWidgetItem();
662 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
663 QString sWalletAddress = coins.first;
664 QString sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
665 if (sWalletLabel.isEmpty())
666 sWalletLabel = tr("(no label)");
668 if (treeMode)
670 // wallet address
671 ui->treeWidget->addTopLevelItem(itemWalletAddress);
673 itemWalletAddress->setFlags(flgTristate);
674 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
676 // label
677 itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
679 // address
680 itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
683 CAmount nSum = 0;
684 int nChildren = 0;
685 BOOST_FOREACH(const COutput& out, coins.second) {
686 nSum += out.tx->vout[out.i].nValue;
687 nChildren++;
689 QTreeWidgetItem *itemOutput;
690 if (treeMode) itemOutput = new QTreeWidgetItem(itemWalletAddress);
691 else itemOutput = new QTreeWidgetItem(ui->treeWidget);
692 itemOutput->setFlags(flgCheckbox);
693 itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
695 // address
696 CTxDestination outputAddress;
697 QString sAddress = "";
698 if(ExtractDestination(out.tx->vout[out.i].scriptPubKey, outputAddress))
700 sAddress = QString::fromStdString(CBitcoinAddress(outputAddress).ToString());
702 // if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
703 if (!treeMode || (!(sAddress == sWalletAddress)))
704 itemOutput->setText(COLUMN_ADDRESS, sAddress);
707 // label
708 if (!(sAddress == sWalletAddress)) // change
710 // tooltip from where the change comes from
711 itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
712 itemOutput->setText(COLUMN_LABEL, tr("(change)"));
714 else if (!treeMode)
716 QString sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
717 if (sLabel.isEmpty())
718 sLabel = tr("(no label)");
719 itemOutput->setText(COLUMN_LABEL, sLabel);
722 // amount
723 itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.tx->vout[out.i].nValue));
724 itemOutput->setText(COLUMN_AMOUNT_INT64, strPad(QString::number(out.tx->vout[out.i].nValue), 15, " ")); // padding so that sorting works correctly
726 // date
727 itemOutput->setText(COLUMN_DATE, GUIUtil::dateTimeStr(out.tx->GetTxTime()));
728 itemOutput->setText(COLUMN_DATE_INT64, strPad(QString::number(out.tx->GetTxTime()), 20, " "));
730 // confirmations
731 itemOutput->setText(COLUMN_CONFIRMATIONS, strPad(QString::number(out.nDepth), 8, " "));
733 // transaction hash
734 uint256 txhash = out.tx->GetHash();
735 itemOutput->setText(COLUMN_TXHASH, QString::fromStdString(txhash.GetHex()));
737 // vout index
738 itemOutput->setText(COLUMN_VOUT_INDEX, QString::number(out.i));
740 // disable locked coins
741 if (model->isLockedCoin(txhash, out.i))
743 COutPoint outpt(txhash, out.i);
744 coinControl->UnSelect(outpt); // just to be sure
745 itemOutput->setDisabled(true);
746 itemOutput->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
749 // set checkbox
750 if (coinControl->IsSelected(COutPoint(txhash, out.i)))
751 itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
754 // amount
755 if (treeMode)
757 itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
758 itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
759 itemWalletAddress->setText(COLUMN_AMOUNT_INT64, strPad(QString::number(nSum), 15, " "));
763 // expand all partially selected
764 if (treeMode)
766 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
767 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
768 ui->treeWidget->topLevelItem(i)->setExpanded(true);
771 // sort view
772 sortView(sortColumn, sortOrder);
773 ui->treeWidget->setEnabled(true);