1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 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"
12 #include "optionsmodel.h"
13 #include "walletmodel.h"
15 #include "coincontrol.h"
19 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
21 #include <QApplication>
24 #include <QDialogButtonBox>
28 #include <QTreeWidget>
29 #include <QTreeWidgetItem>
32 QList<qint64> CoinControlDialog::payAmounts;
33 CCoinControl* CoinControlDialog::coinControl = new CCoinControl();
35 CoinControlDialog::CoinControlDialog(QWidget *parent) :
37 ui(new Ui::CoinControlDialog),
42 // context menu actions
43 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
44 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
45 QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
46 copyTransactionHashAction = new QAction(tr("Copy transaction ID"), this); // we need to enable/disable this
47 lockAction = new QAction(tr("Lock unspent"), this); // we need to enable/disable this
48 unlockAction = new QAction(tr("Unlock unspent"), this); // we need to enable/disable this
51 contextMenu = new QMenu();
52 contextMenu->addAction(copyAddressAction);
53 contextMenu->addAction(copyLabelAction);
54 contextMenu->addAction(copyAmountAction);
55 contextMenu->addAction(copyTransactionHashAction);
56 contextMenu->addSeparator();
57 contextMenu->addAction(lockAction);
58 contextMenu->addAction(unlockAction);
60 // context menu signals
61 connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
62 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
63 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
64 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
65 connect(copyTransactionHashAction, SIGNAL(triggered()), this, SLOT(copyTransactionHash()));
66 connect(lockAction, SIGNAL(triggered()), this, SLOT(lockCoin()));
67 connect(unlockAction, SIGNAL(triggered()), this, SLOT(unlockCoin()));
70 QAction *clipboardQuantityAction = new QAction(tr("Copy quantity"), this);
71 QAction *clipboardAmountAction = new QAction(tr("Copy amount"), this);
72 QAction *clipboardFeeAction = new QAction(tr("Copy fee"), this);
73 QAction *clipboardAfterFeeAction = new QAction(tr("Copy after fee"), this);
74 QAction *clipboardBytesAction = new QAction(tr("Copy bytes"), this);
75 QAction *clipboardPriorityAction = new QAction(tr("Copy priority"), this);
76 QAction *clipboardLowOutputAction = new QAction(tr("Copy dust"), this);
77 QAction *clipboardChangeAction = new QAction(tr("Copy change"), this);
79 connect(clipboardQuantityAction, SIGNAL(triggered()), this, SLOT(clipboardQuantity()));
80 connect(clipboardAmountAction, SIGNAL(triggered()), this, SLOT(clipboardAmount()));
81 connect(clipboardFeeAction, SIGNAL(triggered()), this, SLOT(clipboardFee()));
82 connect(clipboardAfterFeeAction, SIGNAL(triggered()), this, SLOT(clipboardAfterFee()));
83 connect(clipboardBytesAction, SIGNAL(triggered()), this, SLOT(clipboardBytes()));
84 connect(clipboardPriorityAction, SIGNAL(triggered()), this, SLOT(clipboardPriority()));
85 connect(clipboardLowOutputAction, SIGNAL(triggered()), this, SLOT(clipboardLowOutput()));
86 connect(clipboardChangeAction, SIGNAL(triggered()), this, SLOT(clipboardChange()));
88 ui->labelCoinControlQuantity->addAction(clipboardQuantityAction);
89 ui->labelCoinControlAmount->addAction(clipboardAmountAction);
90 ui->labelCoinControlFee->addAction(clipboardFeeAction);
91 ui->labelCoinControlAfterFee->addAction(clipboardAfterFeeAction);
92 ui->labelCoinControlBytes->addAction(clipboardBytesAction);
93 ui->labelCoinControlPriority->addAction(clipboardPriorityAction);
94 ui->labelCoinControlLowOutput->addAction(clipboardLowOutputAction);
95 ui->labelCoinControlChange->addAction(clipboardChangeAction);
97 // toggle tree/list mode
98 connect(ui->radioTreeMode, SIGNAL(toggled(bool)), this, SLOT(radioTreeMode(bool)));
99 connect(ui->radioListMode, SIGNAL(toggled(bool)), this, SLOT(radioListMode(bool)));
102 connect(ui->treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(viewItemChanged(QTreeWidgetItem*, int)));
105 #if QT_VERSION < 0x050000
106 ui->treeWidget->header()->setClickable(true);
108 ui->treeWidget->header()->setSectionsClickable(true);
110 connect(ui->treeWidget->header(), SIGNAL(sectionClicked(int)), this, SLOT(headerSectionClicked(int)));
113 connect(ui->buttonBox, SIGNAL(clicked( QAbstractButton*)), this, SLOT(buttonBoxClicked(QAbstractButton*)));
116 connect(ui->pushButtonSelectAll, SIGNAL(clicked()), this, SLOT(buttonSelectAllClicked()));
118 ui->treeWidget->setColumnWidth(COLUMN_CHECKBOX, 84);
119 ui->treeWidget->setColumnWidth(COLUMN_AMOUNT, 100);
120 ui->treeWidget->setColumnWidth(COLUMN_LABEL, 170);
121 ui->treeWidget->setColumnWidth(COLUMN_ADDRESS, 290);
122 ui->treeWidget->setColumnWidth(COLUMN_DATE, 110);
123 ui->treeWidget->setColumnWidth(COLUMN_CONFIRMATIONS, 100);
124 ui->treeWidget->setColumnWidth(COLUMN_PRIORITY, 100);
125 ui->treeWidget->setColumnHidden(COLUMN_TXHASH, true); // store transacton hash in this column, but dont show it
126 ui->treeWidget->setColumnHidden(COLUMN_VOUT_INDEX, true); // store vout index in this column, but dont show it
127 ui->treeWidget->setColumnHidden(COLUMN_AMOUNT_INT64, true); // store amount int64 in this column, but dont show it
128 ui->treeWidget->setColumnHidden(COLUMN_PRIORITY_INT64, true); // store priority int64 in this column, but dont show it
129 ui->treeWidget->setColumnHidden(COLUMN_DATE_INT64, true); // store date int64 in this column, but dont show it
131 // default view is sorted by amount desc
132 sortView(COLUMN_AMOUNT_INT64, Qt::DescendingOrder);
135 CoinControlDialog::~CoinControlDialog()
140 void CoinControlDialog::setModel(WalletModel *model)
144 if(model && model->getOptionsModel() && model->getAddressTableModel())
148 CoinControlDialog::updateLabels(model, this);
152 // helper function str_pad
153 QString CoinControlDialog::strPad(QString s, int nPadLength, QString sPadding)
155 while (s.length() < nPadLength)
162 void CoinControlDialog::buttonBoxClicked(QAbstractButton* button)
164 if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
165 done(QDialog::Accepted); // closes the dialog
169 void CoinControlDialog::buttonSelectAllClicked()
171 Qt::CheckState state = Qt::Checked;
172 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
174 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != Qt::Unchecked)
176 state = Qt::Unchecked;
180 ui->treeWidget->setEnabled(false);
181 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
182 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != state)
183 ui->treeWidget->topLevelItem(i)->setCheckState(COLUMN_CHECKBOX, state);
184 ui->treeWidget->setEnabled(true);
185 if (state == Qt::Unchecked)
186 coinControl->UnSelectAll(); // just to be sure
187 CoinControlDialog::updateLabels(model, this);
191 void CoinControlDialog::showMenu(const QPoint &point)
193 QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
196 contextMenuItem = item;
198 // disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
199 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)
201 copyTransactionHashAction->setEnabled(true);
202 if (model->isLockedCoin(uint256(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt()))
204 lockAction->setEnabled(false);
205 unlockAction->setEnabled(true);
209 lockAction->setEnabled(true);
210 unlockAction->setEnabled(false);
213 else // this means click on parent node in tree mode -> disable all
215 copyTransactionHashAction->setEnabled(false);
216 lockAction->setEnabled(false);
217 unlockAction->setEnabled(false);
221 contextMenu->exec(QCursor::pos());
225 // context menu action: copy amount
226 void CoinControlDialog::copyAmount()
228 GUIUtil::setClipboard(BitcoinUnits::removeSpaces(contextMenuItem->text(COLUMN_AMOUNT)));
231 // context menu action: copy label
232 void CoinControlDialog::copyLabel()
234 if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_LABEL).length() == 0 && contextMenuItem->parent())
235 GUIUtil::setClipboard(contextMenuItem->parent()->text(COLUMN_LABEL));
237 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_LABEL));
240 // context menu action: copy address
241 void CoinControlDialog::copyAddress()
243 if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_ADDRESS).length() == 0 && contextMenuItem->parent())
244 GUIUtil::setClipboard(contextMenuItem->parent()->text(COLUMN_ADDRESS));
246 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_ADDRESS));
249 // context menu action: copy transaction id
250 void CoinControlDialog::copyTransactionHash()
252 GUIUtil::setClipboard(contextMenuItem->text(COLUMN_TXHASH));
255 // context menu action: lock coin
256 void CoinControlDialog::lockCoin()
258 if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
259 contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
261 COutPoint outpt(uint256(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
262 model->lockCoin(outpt);
263 contextMenuItem->setDisabled(true);
264 contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon(":/icons/lock_closed"));
268 // context menu action: unlock coin
269 void CoinControlDialog::unlockCoin()
271 COutPoint outpt(uint256(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
272 model->unlockCoin(outpt);
273 contextMenuItem->setDisabled(false);
274 contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
278 // copy label "Quantity" to clipboard
279 void CoinControlDialog::clipboardQuantity()
281 GUIUtil::setClipboard(ui->labelCoinControlQuantity->text());
284 // copy label "Amount" to clipboard
285 void CoinControlDialog::clipboardAmount()
287 GUIUtil::setClipboard(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
290 // copy label "Fee" to clipboard
291 void CoinControlDialog::clipboardFee()
293 GUIUtil::setClipboard(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")));
296 // copy label "After fee" to clipboard
297 void CoinControlDialog::clipboardAfterFee()
299 GUIUtil::setClipboard(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")));
302 // copy label "Bytes" to clipboard
303 void CoinControlDialog::clipboardBytes()
305 GUIUtil::setClipboard(ui->labelCoinControlBytes->text());
308 // copy label "Priority" to clipboard
309 void CoinControlDialog::clipboardPriority()
311 GUIUtil::setClipboard(ui->labelCoinControlPriority->text());
314 // copy label "Dust" to clipboard
315 void CoinControlDialog::clipboardLowOutput()
317 GUIUtil::setClipboard(ui->labelCoinControlLowOutput->text());
320 // copy label "Change" to clipboard
321 void CoinControlDialog::clipboardChange()
323 GUIUtil::setClipboard(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")));
327 void CoinControlDialog::sortView(int column, Qt::SortOrder order)
331 ui->treeWidget->sortItems(column, order);
332 ui->treeWidget->header()->setSortIndicator(getMappedColumn(sortColumn), sortOrder);
335 // treeview: clicked on header
336 void CoinControlDialog::headerSectionClicked(int logicalIndex)
338 if (logicalIndex == COLUMN_CHECKBOX) // click on most left column -> do nothing
340 ui->treeWidget->header()->setSortIndicator(getMappedColumn(sortColumn), sortOrder);
344 logicalIndex = getMappedColumn(logicalIndex, false);
346 if (sortColumn == logicalIndex)
347 sortOrder = ((sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder);
350 sortColumn = logicalIndex;
351 sortOrder = ((sortColumn == COLUMN_LABEL || sortColumn == COLUMN_ADDRESS) ? Qt::AscendingOrder : Qt::DescendingOrder); // if label or address then default => asc, else default => desc
354 sortView(sortColumn, sortOrder);
359 void CoinControlDialog::radioTreeMode(bool checked)
361 if (checked && model)
366 void CoinControlDialog::radioListMode(bool checked)
368 if (checked && model)
372 // checkbox clicked by user
373 void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
375 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)
377 COutPoint outpt(uint256(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt());
379 if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
380 coinControl->UnSelect(outpt);
381 else if (item->isDisabled()) // locked (this happens if "check all" through parent node)
382 item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
384 coinControl->Select(outpt);
386 // selection changed -> update labels
387 if (ui->treeWidget->isEnabled()) // do not update on every click for (un)select all
388 CoinControlDialog::updateLabels(model, this);
391 // todo: this is a temporary qt5 fix: when clicking a parent node in tree mode, the parent node
392 // including all childs are partially selected. But the parent node should be fully selected
393 // as well as the childs. Childs should never be partially selected in the first place.
394 // Please remove this ugly fix, once the bug is solved upstream.
395 #if QT_VERSION >= 0x050000
396 else if (column == COLUMN_CHECKBOX && item->childCount() > 0)
398 if (item->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked && item->child(0)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
399 item->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
404 // return human readable label for priority number
405 QString CoinControlDialog::getPriorityLabel(const CTxMemPool& pool, double dPriority)
407 // confirmations -> textual description
408 typedef std::map<unsigned int, QString> PriorityDescription;
409 const static PriorityDescription priorityDescriptions = boost::assign::map_list_of
410 (1, tr("highest"))(2, tr("higher"))(3, tr("high"))
411 (5, tr("medium-high"))(6, tr("medium"))
412 (10, tr("low-medium"))(15, tr("low"))
415 BOOST_FOREACH(const PriorityDescription::value_type& i, priorityDescriptions)
417 double p = mempool.estimatePriority(i.first);
418 if (p > 0 && dPriority >= p) return i.second;
420 // Note: if mempool hasn't accumulated enough history (estimatePriority
421 // returns -1) we're conservative and classify as "lowest"
422 if (mempool.estimatePriority(nTxConfirmTarget) <= 0 && AllowFree(dPriority))
423 return ">=" + tr("medium");
427 // shows count of locked unspent outputs
428 void CoinControlDialog::updateLabelLocked()
430 vector<COutPoint> vOutpts;
431 model->listLockedCoins(vOutpts);
432 if (vOutpts.size() > 0)
434 ui->labelLocked->setText(tr("(%1 locked)").arg(vOutpts.size()));
435 ui->labelLocked->setVisible(true);
437 else ui->labelLocked->setVisible(false);
440 void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
446 qint64 nPayAmount = 0;
448 CMutableTransaction txDummy;
449 foreach(const qint64 &amount, CoinControlDialog::payAmounts)
451 nPayAmount += amount;
455 CTxOut txout(amount, (CScript)vector<unsigned char>(24, 0));
456 txDummy.vout.push_back(txout);
457 if (txout.IsDust(::minRelayTxFee))
462 QString sPriorityLabel = tr("none");
465 int64_t nAfterFee = 0;
467 unsigned int nBytes = 0;
468 unsigned int nBytesInputs = 0;
469 double dPriority = 0;
470 double dPriorityInputs = 0;
471 unsigned int nQuantity = 0;
472 int nQuantityUncompressed = 0;
474 vector<COutPoint> vCoinControl;
475 vector<COutput> vOutputs;
476 coinControl->ListSelected(vCoinControl);
477 model->getOutputs(vCoinControl, vOutputs);
479 BOOST_FOREACH(const COutput& out, vOutputs)
481 // unselect already spent, very unlikely scenario, this could happen
482 // when selected are spent elsewhere, like rpc or another computer
483 uint256 txhash = out.tx->GetHash();
484 COutPoint outpt(txhash, out.i);
485 if (model->isSpent(outpt))
487 coinControl->UnSelect(outpt);
495 nAmount += out.tx->vout[out.i].nValue;
498 dPriorityInputs += (double)out.tx->vout[out.i].nValue * (out.nDepth+1);
501 CTxDestination address;
502 if(ExtractDestination(out.tx->vout[out.i].scriptPubKey, address))
505 CKeyID *keyid = boost::get<CKeyID>(&address);
506 if (keyid && model->getPubKey(*keyid, pubkey))
508 nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
509 if (!pubkey.IsCompressed())
510 nQuantityUncompressed++;
513 nBytesInputs += 148; // in all error cases, simply assume 148 here
515 else nBytesInputs += 148;
522 nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
525 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)
526 sPriorityLabel = CoinControlDialog::getPriorityLabel(mempool, dPriority);
529 nPayFee = payTxFee.GetFee(max((unsigned int)1000, nBytes));
534 nPayFee = CWallet::GetMinimumFee(nBytes, nTxConfirmTarget, mempool);
536 double dPriorityNeeded = mempool.estimatePriority(nTxConfirmTarget);
537 if (dPriorityNeeded <= 0 && !AllowFree(dPriority)) // not enough mempool history: never send free
538 dPriorityNeeded = std::numeric_limits<double>::max();
540 if (nBytes <= MAX_FREE_TRANSACTION_CREATE_SIZE && dPriority >= dPriorityNeeded)
546 nChange = nAmount - nPayFee - nPayAmount;
548 // Never create dust outputs; if we would, just add the dust to the fee.
549 if (nChange > 0 && nChange < CENT)
551 CTxOut txout(nChange, (CScript)vector<unsigned char>(24, 0));
552 if (txout.IsDust(::minRelayTxFee))
564 nAfterFee = nAmount - nPayFee;
569 // actually update labels
570 int nDisplayUnit = BitcoinUnits::BTC;
571 if (model && model->getOptionsModel())
572 nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
574 QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
575 QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
576 QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
577 QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
578 QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
579 QLabel *l6 = dialog->findChild<QLabel *>("labelCoinControlPriority");
580 QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
581 QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
583 // enable/disable "dust" and "change"
584 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
585 dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
586 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
587 dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
590 l1->setText(QString::number(nQuantity)); // Quantity
591 l2->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
592 l3->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
593 l4->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
594 l5->setText(((nBytes > 0) ? "~" : "") + QString::number(nBytes)); // Bytes
595 l6->setText(sPriorityLabel); // Priority
596 l7->setText(fDust ? tr("yes") : tr("no")); // Dust
597 l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
600 l3->setText("~" + l3->text());
601 l4->setText("~" + l4->text());
603 l8->setText("~" + l8->text());
607 l5->setStyleSheet((nBytes >= MAX_FREE_TRANSACTION_CREATE_SIZE) ? "color:red;" : "");// Bytes >= 1000
608 l6->setStyleSheet((dPriority > 0 && !AllowFree(dPriority)) ? "color:red;" : ""); // Priority < "medium"
609 l7->setStyleSheet((fDust) ? "color:red;" : ""); // Dust = "yes"
612 QString toolTip1 = tr("This label turns red, if the transaction size is greater than 1000 bytes.") + "<br /><br />";
613 toolTip1 += tr("This means a fee of at least %1 per kB is required.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CWallet::minTxFee.GetFeePerK())) + "<br /><br />";
614 toolTip1 += tr("Can vary +/- 1 byte per input.");
616 QString toolTip2 = tr("Transactions with higher priority are more likely to get included into a block.") + "<br /><br />";
617 toolTip2 += tr("This label turns red, if the priority is smaller than \"medium\".") + "<br /><br />";
618 toolTip2 += tr("This means a fee of at least %1 per kB is required.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CWallet::minTxFee.GetFeePerK()));
620 QString toolTip3 = tr("This label turns red, if any recipient receives an amount smaller than %1.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, ::minRelayTxFee.GetFee(546)));
622 // how many satoshis the estimated fee can vary per byte we guess wrong
623 double dFeeVary = (double)std::max(CWallet::minTxFee.GetFeePerK(), std::max(payTxFee.GetFeePerK(), mempool.estimateFee(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 l5->setToolTip(toolTip1);
629 l6->setToolTip(toolTip2);
630 l7->setToolTip(toolTip3);
631 l8->setToolTip(toolTip4);
632 dialog->findChild<QLabel *>("labelCoinControlFeeText") ->setToolTip(l3->toolTip());
633 dialog->findChild<QLabel *>("labelCoinControlAfterFeeText") ->setToolTip(l4->toolTip());
634 dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
635 dialog->findChild<QLabel *>("labelCoinControlPriorityText") ->setToolTip(l6->toolTip());
636 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
637 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
639 // Insufficient funds
640 QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
642 label->setVisible(nChange < 0);
645 void CoinControlDialog::updateView()
647 if (!model || !model->getOptionsModel() || !model->getAddressTableModel())
650 bool treeMode = ui->radioTreeMode->isChecked();
652 ui->treeWidget->clear();
653 ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
654 ui->treeWidget->setAlternatingRowColors(!treeMode);
655 QFlags<Qt::ItemFlag> flgCheckbox = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
656 QFlags<Qt::ItemFlag> flgTristate = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
658 int nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
660 map<QString, vector<COutput> > mapCoins;
661 model->listCoins(mapCoins);
663 BOOST_FOREACH(PAIRTYPE(QString, vector<COutput>) coins, mapCoins)
665 QTreeWidgetItem *itemWalletAddress = new QTreeWidgetItem();
666 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
667 QString sWalletAddress = coins.first;
668 QString sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
669 if (sWalletLabel.isEmpty())
670 sWalletLabel = tr("(no label)");
675 ui->treeWidget->addTopLevelItem(itemWalletAddress);
677 itemWalletAddress->setFlags(flgTristate);
678 itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
681 itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
684 itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
688 double dPrioritySum = 0;
691 BOOST_FOREACH(const COutput& out, coins.second)
694 nSum += out.tx->vout[out.i].nValue;
697 QTreeWidgetItem *itemOutput;
698 if (treeMode) itemOutput = new QTreeWidgetItem(itemWalletAddress);
699 else itemOutput = new QTreeWidgetItem(ui->treeWidget);
700 itemOutput->setFlags(flgCheckbox);
701 itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
704 CTxDestination outputAddress;
705 QString sAddress = "";
706 if(ExtractDestination(out.tx->vout[out.i].scriptPubKey, outputAddress))
708 sAddress = QString::fromStdString(CBitcoinAddress(outputAddress).ToString());
710 // if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
711 if (!treeMode || (!(sAddress == sWalletAddress)))
712 itemOutput->setText(COLUMN_ADDRESS, sAddress);
715 CKeyID *keyid = boost::get<CKeyID>(&outputAddress);
716 if (keyid && model->getPubKey(*keyid, pubkey) && !pubkey.IsCompressed())
717 nInputSize = 29; // 29 = 180 - 151 (public key is 180 bytes, priority free area is 151 bytes)
721 if (!(sAddress == sWalletAddress)) // change
723 // tooltip from where the change comes from
724 itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
725 itemOutput->setText(COLUMN_LABEL, tr("(change)"));
729 QString sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
730 if (sLabel.isEmpty())
731 sLabel = tr("(no label)");
732 itemOutput->setText(COLUMN_LABEL, sLabel);
736 itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.tx->vout[out.i].nValue));
737 itemOutput->setText(COLUMN_AMOUNT_INT64, strPad(QString::number(out.tx->vout[out.i].nValue), 15, " ")); // padding so that sorting works correctly
740 itemOutput->setText(COLUMN_DATE, GUIUtil::dateTimeStr(out.tx->GetTxTime()));
741 itemOutput->setText(COLUMN_DATE_INT64, strPad(QString::number(out.tx->GetTxTime()), 20, " "));
744 itemOutput->setText(COLUMN_CONFIRMATIONS, strPad(QString::number(out.nDepth), 8, " "));
747 double dPriority = ((double)out.tx->vout[out.i].nValue / (nInputSize + 78)) * (out.nDepth+1); // 78 = 2 * 34 + 10
748 itemOutput->setText(COLUMN_PRIORITY, CoinControlDialog::getPriorityLabel(mempool, dPriority));
749 itemOutput->setText(COLUMN_PRIORITY_INT64, strPad(QString::number((int64_t)dPriority), 20, " "));
750 dPrioritySum += (double)out.tx->vout[out.i].nValue * (out.nDepth+1);
751 nInputSum += nInputSize;
754 uint256 txhash = out.tx->GetHash();
755 itemOutput->setText(COLUMN_TXHASH, QString::fromStdString(txhash.GetHex()));
758 itemOutput->setText(COLUMN_VOUT_INDEX, QString::number(out.i));
760 // disable locked coins
761 if (model->isLockedCoin(txhash, out.i))
763 COutPoint outpt(txhash, out.i);
764 coinControl->UnSelect(outpt); // just to be sure
765 itemOutput->setDisabled(true);
766 itemOutput->setIcon(COLUMN_CHECKBOX, QIcon(":/icons/lock_closed"));
770 if (coinControl->IsSelected(txhash, out.i))
771 itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
777 dPrioritySum = dPrioritySum / (nInputSum + 78);
778 itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
779 itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
780 itemWalletAddress->setText(COLUMN_AMOUNT_INT64, strPad(QString::number(nSum), 15, " "));
781 itemWalletAddress->setText(COLUMN_PRIORITY, CoinControlDialog::getPriorityLabel(mempool, dPrioritySum));
782 itemWalletAddress->setText(COLUMN_PRIORITY_INT64, strPad(QString::number((int64_t)dPrioritySum), 20, " "));
786 // expand all partially selected
789 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
790 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
791 ui->treeWidget->topLevelItem(i)->setExpanded(true);
795 sortView(sortColumn, sortOrder);
796 ui->treeWidget->setEnabled(true);