1 #include "coincontroldialog.h"
2 #include "ui_coincontroldialog.h"
5 #include "bitcoinunits.h"
6 #include "walletmodel.h"
7 #include "addresstablemodel.h"
8 #include "optionsmodel.h"
9 #include "coincontrol.h"
11 #include <QApplication>
17 #include <QDialogButtonBox>
21 #include <QTreeWidget>
22 #include <QTreeWidgetItem>
25 QList<qint64> CoinControlDialog::payAmounts;
26 CCoinControl* CoinControlDialog::coinControl = new CCoinControl();
28 CoinControlDialog::CoinControlDialog(QWidget *parent) :
30 ui(new Ui::CoinControlDialog),
35 // context menu actions
36 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
37 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
38 QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
39 copyTransactionHashAction = new QAction(tr("Copy transaction ID"), this); // we need to enable/disable this
40 //lockAction = new QAction(tr("Lock unspent"), this); // we need to enable/disable this
41 //unlockAction = new QAction(tr("Unlock unspent"), this); // we need to enable/disable this
44 contextMenu = new QMenu();
45 contextMenu->addAction(copyAddressAction);
46 contextMenu->addAction(copyLabelAction);
47 contextMenu->addAction(copyAmountAction);
48 contextMenu->addAction(copyTransactionHashAction);
49 //contextMenu->addSeparator();
50 //contextMenu->addAction(lockAction);
51 //contextMenu->addAction(unlockAction);
53 // context menu signals
54 connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
55 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
56 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
57 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
58 connect(copyTransactionHashAction, SIGNAL(triggered()), this, SLOT(copyTransactionHash()));
59 //connect(lockAction, SIGNAL(triggered()), this, SLOT(lockCoin()));
60 //connect(unlockAction, SIGNAL(triggered()), this, SLOT(unlockCoin()));
63 QAction *clipboardQuantityAction = new QAction(tr("Copy quantity"), this);
64 QAction *clipboardAmountAction = new QAction(tr("Copy amount"), this);
65 QAction *clipboardFeeAction = new QAction(tr("Copy fee"), this);
66 QAction *clipboardAfterFeeAction = new QAction(tr("Copy after fee"), this);
67 QAction *clipboardBytesAction = new QAction(tr("Copy bytes"), this);
68 QAction *clipboardPriorityAction = new QAction(tr("Copy priority"), this);
69 QAction *clipboardLowOutputAction = new QAction(tr("Copy low output"), this);
70 QAction *clipboardChangeAction = new QAction(tr("Copy change"), this);
72 connect(clipboardQuantityAction, SIGNAL(triggered()), this, SLOT(clipboardQuantity()));
73 connect(clipboardAmountAction, SIGNAL(triggered()), this, SLOT(clipboardAmount()));
74 connect(clipboardFeeAction, SIGNAL(triggered()), this, SLOT(clipboardFee()));
75 connect(clipboardAfterFeeAction, SIGNAL(triggered()), this, SLOT(clipboardAfterFee()));
76 connect(clipboardBytesAction, SIGNAL(triggered()), this, SLOT(clipboardBytes()));
77 connect(clipboardPriorityAction, SIGNAL(triggered()), this, SLOT(clipboardPriority()));
78 connect(clipboardLowOutputAction, SIGNAL(triggered()), this, SLOT(clipboardLowOutput()));
79 connect(clipboardChangeAction, SIGNAL(triggered()), this, SLOT(clipboardChange()));
81 ui->labelCoinControlQuantity->addAction(clipboardQuantityAction);
82 ui->labelCoinControlAmount->addAction(clipboardAmountAction);
83 ui->labelCoinControlFee->addAction(clipboardFeeAction);
84 ui->labelCoinControlAfterFee->addAction(clipboardAfterFeeAction);
85 ui->labelCoinControlBytes->addAction(clipboardBytesAction);
86 ui->labelCoinControlPriority->addAction(clipboardPriorityAction);
87 ui->labelCoinControlLowOutput->addAction(clipboardLowOutputAction);
88 ui->labelCoinControlChange->addAction(clipboardChangeAction);
90 // toggle tree/list mode
91 connect(ui->radioTreeMode, SIGNAL(toggled(bool)), this, SLOT(radioTreeMode(bool)));
92 connect(ui->radioListMode, SIGNAL(toggled(bool)), this, SLOT(radioListMode(bool)));
95 connect(ui->treeWidget, SIGNAL(itemChanged( QTreeWidgetItem*, int)), this, SLOT(viewItemChanged( QTreeWidgetItem*, int)));
98 ui->treeWidget->header()->setClickable(true);
99 connect(ui->treeWidget->header(), SIGNAL(sectionClicked(int)), this, SLOT(headerSectionClicked(int)));
102 connect(ui->buttonBox, SIGNAL(clicked( QAbstractButton*)), this, SLOT(buttonBoxClicked(QAbstractButton*)));
105 connect(ui->pushButtonSelectAll, SIGNAL(clicked()), this, SLOT(buttonSelectAllClicked()));
107 ui->treeWidget->setColumnWidth(COLUMN_CHECKBOX, 84);
108 ui->treeWidget->setColumnWidth(COLUMN_AMOUNT, 100);
109 ui->treeWidget->setColumnWidth(COLUMN_LABEL, 170);
110 ui->treeWidget->setColumnWidth(COLUMN_ADDRESS, 250);
111 ui->treeWidget->setColumnWidth(COLUMN_DATE, 90);
112 ui->treeWidget->setColumnWidth(COLUMN_CONFIRMATIONS, 70);
113 ui->treeWidget->setColumnWidth(COLUMN_PRIORITY, 100);
114 ui->treeWidget->setColumnWidth(COLUMN_WEIGHT, 100);
115 ui->treeWidget->setColumnHidden(COLUMN_TXHASH, true); // store transacton hash in this column, but dont show it
116 ui->treeWidget->setColumnHidden(COLUMN_VOUT_INDEX, true); // store vout index in this column, but dont show it
117 ui->treeWidget->setColumnHidden(COLUMN_AMOUNT_INT64, true); // store amount int64 in this column, but dont show it
118 ui->treeWidget->setColumnHidden(COLUMN_PRIORITY_INT64, true); // store priority int64 in this column, but dont show it
120 // default view is sorted by amount desc
121 sortView(COLUMN_AMOUNT_INT64, Qt::DescendingOrder);
124 CoinControlDialog::~CoinControlDialog()
129 void CoinControlDialog::setModel(WalletModel *model)
133 if(model && model->getOptionsModel() && model->getAddressTableModel())
136 //updateLabelLocked();
137 CoinControlDialog::updateLabels(model, this);
141 // helper function str_pad
142 QString CoinControlDialog::strPad(QString s, int nPadLength, QString sPadding)
144 while (s.length() < nPadLength)
151 void CoinControlDialog::buttonBoxClicked(QAbstractButton* button)
153 if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
154 done(QDialog::Accepted); // closes the dialog
158 void CoinControlDialog::buttonSelectAllClicked()
160 Qt::CheckState state = Qt::Checked;
161 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
163 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != Qt::Unchecked)
165 state = Qt::Unchecked;
169 ui->treeWidget->setEnabled(false);
170 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
171 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != state)
172 ui->treeWidget->topLevelItem(i)->setCheckState(COLUMN_CHECKBOX, state);
173 ui->treeWidget->setEnabled(true);
174 CoinControlDialog::updateLabels(model, this);
178 void CoinControlDialog::showMenu(const QPoint &point)
180 QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
183 contextMenuItem = item;
185 // disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
186 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)
188 copyTransactionHashAction->setEnabled(true);
189 //if (model->isLockedCoin(uint256(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt()))
191 // lockAction->setEnabled(false);
192 // unlockAction->setEnabled(true);
196 // lockAction->setEnabled(true);
197 // unlockAction->setEnabled(false);
200 else // this means click on parent node in tree mode -> disable all
202 copyTransactionHashAction->setEnabled(false);
203 //lockAction->setEnabled(false);
204 //unlockAction->setEnabled(false);
208 contextMenu->exec(QCursor::pos());
212 // context menu action: copy amount
213 void CoinControlDialog::copyAmount()
215 QApplication::clipboard()->setText(contextMenuItem->text(COLUMN_AMOUNT));
218 // context menu action: copy label
219 void CoinControlDialog::copyLabel()
221 if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_LABEL).length() == 0 && contextMenuItem->parent())
222 QApplication::clipboard()->setText(contextMenuItem->parent()->text(COLUMN_LABEL));
224 QApplication::clipboard()->setText(contextMenuItem->text(COLUMN_LABEL));
227 // context menu action: copy address
228 void CoinControlDialog::copyAddress()
230 if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_ADDRESS).length() == 0 && contextMenuItem->parent())
231 QApplication::clipboard()->setText(contextMenuItem->parent()->text(COLUMN_ADDRESS));
233 QApplication::clipboard()->setText(contextMenuItem->text(COLUMN_ADDRESS));
236 // context menu action: copy transaction id
237 void CoinControlDialog::copyTransactionHash()
239 QApplication::clipboard()->setText(contextMenuItem->text(COLUMN_TXHASH));
242 // context menu action: lock coin
243 /*void CoinControlDialog::lockCoin()
245 if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
246 contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
248 COutPoint outpt(uint256(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
249 model->lockCoin(outpt);
250 contextMenuItem->setDisabled(true);
251 contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon(":/icons/lock_closed"));
255 // context menu action: unlock coin
256 /*void CoinControlDialog::unlockCoin()
258 COutPoint outpt(uint256(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
259 model->unlockCoin(outpt);
260 contextMenuItem->setDisabled(false);
261 contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
265 // copy label "Quantity" to clipboard
266 void CoinControlDialog::clipboardQuantity()
268 QApplication::clipboard()->setText(ui->labelCoinControlQuantity->text());
271 // copy label "Amount" to clipboard
272 void CoinControlDialog::clipboardAmount()
274 QApplication::clipboard()->setText(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
277 // copy label "Fee" to clipboard
278 void CoinControlDialog::clipboardFee()
280 QApplication::clipboard()->setText(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")));
283 // copy label "After fee" to clipboard
284 void CoinControlDialog::clipboardAfterFee()
286 QApplication::clipboard()->setText(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")));
289 // copy label "Bytes" to clipboard
290 void CoinControlDialog::clipboardBytes()
292 QApplication::clipboard()->setText(ui->labelCoinControlBytes->text());
295 // copy label "Priority" to clipboard
296 void CoinControlDialog::clipboardPriority()
298 QApplication::clipboard()->setText(ui->labelCoinControlPriority->text());
301 // copy label "Low output" to clipboard
302 void CoinControlDialog::clipboardLowOutput()
304 QApplication::clipboard()->setText(ui->labelCoinControlLowOutput->text());
307 // copy label "Change" to clipboard
308 void CoinControlDialog::clipboardChange()
310 QApplication::clipboard()->setText(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")));
314 void CoinControlDialog::sortView(int column, Qt::SortOrder order)
318 ui->treeWidget->sortItems(column, order);
319 ui->treeWidget->header()->setSortIndicator((sortColumn == COLUMN_AMOUNT_INT64 ? COLUMN_AMOUNT : (sortColumn == COLUMN_PRIORITY_INT64 ? COLUMN_PRIORITY : sortColumn)), sortOrder);
322 // treeview: clicked on header
323 void CoinControlDialog::headerSectionClicked(int logicalIndex)
325 if (logicalIndex == COLUMN_CHECKBOX) // click on most left column -> do nothing
327 ui->treeWidget->header()->setSortIndicator((sortColumn == COLUMN_AMOUNT_INT64 ? COLUMN_AMOUNT : (sortColumn == COLUMN_PRIORITY_INT64 ? COLUMN_PRIORITY : sortColumn)), sortOrder);
331 if (logicalIndex == COLUMN_AMOUNT) // sort by amount
332 logicalIndex = COLUMN_AMOUNT_INT64;
334 if (logicalIndex == COLUMN_PRIORITY) // sort by priority
335 logicalIndex = COLUMN_PRIORITY_INT64;
337 if (sortColumn == logicalIndex)
338 sortOrder = ((sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder);
341 sortColumn = logicalIndex;
342 sortOrder = ((sortColumn == COLUMN_AMOUNT_INT64 || sortColumn == COLUMN_PRIORITY_INT64 || sortColumn == COLUMN_DATE || sortColumn == COLUMN_CONFIRMATIONS || sortColumn == COLUMN_WEIGHT) ? Qt::DescendingOrder : Qt::AscendingOrder); // if amount,date,conf,priority then default => desc, else default => asc
345 sortView(sortColumn, sortOrder);
350 void CoinControlDialog::radioTreeMode(bool checked)
352 if (checked && model)
357 void CoinControlDialog::radioListMode(bool checked)
359 if (checked && model)
363 // checkbox clicked by user
364 void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
366 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)
368 COutPoint outpt(uint256(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt());
370 if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
371 coinControl->UnSelect(outpt);
372 else if (item->isDisabled()) // locked (this happens if "check all" through parent node)
373 item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
375 coinControl->Select(outpt);
377 // selection changed -> update labels
378 if (ui->treeWidget->isEnabled()) // do not update on every click for (un)select all
379 CoinControlDialog::updateLabels(model, this);
383 // helper function, return human readable label for priority number
384 QString CoinControlDialog::getPriorityLabel(double dPriority)
386 if (dPriority > 576000ULL) // at least medium, this number is from AllowFree(), the other thresholds are kinda random
388 if (dPriority > 5760000000ULL) return tr("highest");
389 else if (dPriority > 576000000ULL) return tr("high");
390 else if (dPriority > 57600000ULL) return tr("medium-high");
391 else return tr("medium");
395 if (dPriority > 5760ULL) return tr("low-medium");
396 else if (dPriority > 58ULL) return tr("low");
397 else return tr("lowest");
401 // shows count of locked unspent outputs
402 /*void CoinControlDialog::updateLabelLocked()
404 vector<COutPoint> vOutpts;
405 model->listLockedCoins(vOutpts);
406 if (vOutpts.size() > 0)
408 ui->labelLocked->setText(tr("(%1 locked)").arg(vOutpts.size()));
409 ui->labelLocked->setVisible(true);
411 else ui->labelLocked->setVisible(false);
414 void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
419 qint64 nPayAmount = 0;
420 bool fLowOutput = false;
422 CTransaction txDummy;
423 foreach(const qint64 &amount, CoinControlDialog::payAmounts)
425 nPayAmount += amount;
431 if (amount < MIN_TX_FEE)
434 CTxOut txout(amount, (CScript)vector<unsigned char>(24, 0));
435 txDummy.vout.push_back(txout);
439 QString sPriorityLabel = "";
444 unsigned int nBytes = 0;
445 unsigned int nBytesInputs = 0;
446 double dPriority = 0;
447 double dPriorityInputs = 0;
448 unsigned int nQuantity = 0;
450 vector<COutPoint> vCoinControl;
451 vector<COutput> vOutputs;
452 coinControl->ListSelected(vCoinControl);
453 model->getOutputs(vCoinControl, vOutputs);
455 BOOST_FOREACH(const COutput& out, vOutputs)
461 nAmount += out.tx->vout[out.i].nValue;
464 dPriorityInputs += (double)out.tx->vout[out.i].nValue * (out.nDepth+1);
467 CTxDestination address;
468 if(ExtractDestination(out.tx->vout[out.i].scriptPubKey, address))
471 CKeyID *keyid = boost::get< CKeyID >(&address);
472 if (keyid && model->getPubKey(*keyid, pubkey))
473 nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
475 nBytesInputs += 148; // in all error cases, simply assume 148 here
477 else nBytesInputs += 148;
484 nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
487 dPriority = dPriorityInputs / nBytes;
488 sPriorityLabel = CoinControlDialog::getPriorityLabel(dPriority);
491 int64 nFee = nTransactionFee * (1 + (int64)nBytes / 1000);
494 bool fAllowFree = CTransaction::AllowFree(dPriority);
496 // Disable free transactions until 1 July 2014
497 if (!fTestNet && txDummy.nTime < FEE_SWITCH_TIME)
502 int64 nMinFee = txDummy.GetMinFee(1, fAllowFree, GMF_SEND, nBytes);
504 nPayFee = max(nFee, nMinFee);
508 nChange = nAmount - nPayFee - nPayAmount;
510 // if sub-cent change is required, the fee must be raised to at least CTransaction::nMinTxFee
511 if (txDummy.nTime < FEE_SWITCH_TIME && !fTestNet)
513 if (nPayFee < CENT && nChange > 0 && nChange < CENT)
515 if (nChange < CENT) // change < 0.01 => simply move all change to fees
522 nChange = nChange + nPayFee - CENT;
533 nAfterFee = nAmount - nPayFee;
538 // actually update labels
539 int nDisplayUnit = BitcoinUnits::BTC;
540 if (model && model->getOptionsModel())
541 nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
543 QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
544 QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
545 QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
546 QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
547 QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
548 QLabel *l6 = dialog->findChild<QLabel *>("labelCoinControlPriority");
549 QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
550 QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
552 // enable/disable "low output" and "change"
553 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
554 dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
555 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
556 dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
559 l1->setText(QString::number(nQuantity)); // Quantity
560 l2->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
561 l3->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
562 l4->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
563 l5->setText(((nBytes > 0) ? "~" : "") + QString::number(nBytes)); // Bytes
564 l6->setText(sPriorityLabel); // Priority
565 l7->setText((fLowOutput ? (fDust ? tr("DUST") : tr("yes")) : tr("no"))); // Low Output / Dust
566 l8->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
569 l5->setStyleSheet((nBytes >= 1000) ? "color:red;" : ""); // Bytes >= 1000
570 l6->setStyleSheet((dPriority <= 576000) ? "color:red;" : ""); // Priority < "medium"
571 l7->setStyleSheet((fLowOutput) ? "color:red;" : ""); // Low Output = "yes"
572 l8->setStyleSheet((nChange > 0 && nChange < CENT) ? "color:red;" : ""); // Change < 0.01BTC
575 l5->setToolTip(tr("This label turns red, if the transaction size is bigger than 1000 bytes.\n\n This means a fee of at least %1 per kb is required.\n\n Can vary +/- 1 Byte per input.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, fTestNet || FEE_SWITCH_TIME < GetTime() ? MIN_TX_FEE : CENT)));
576 l6->setToolTip(tr("Transactions with higher priority get more likely into a block.\n\nThis label turns red, if the priority is smaller than \"medium\".\n\n This means a fee of at least %1 per kb is required.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, fTestNet || FEE_SWITCH_TIME < GetTime() ? MIN_TX_FEE : CENT)));
577 l7->setToolTip(tr("This label turns red, if any recipient receives an amount smaller than %1.\n\n This means a fee of at least %2 is required. \n\n Amounts below the minimum fee are shown as DUST.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CENT)).arg(BitcoinUnits::formatWithUnit(nDisplayUnit, fTestNet || FEE_SWITCH_TIME < GetTime() ? MIN_RELAY_TX_FEE : CENT)));
578 l8->setToolTip(tr("This label turns red, if the change is smaller than %1.\n\n This means a fee of at least %2 is required.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CENT)).arg(BitcoinUnits::formatWithUnit(nDisplayUnit, fTestNet || FEE_SWITCH_TIME < GetTime() ? MIN_TX_FEE : CENT)));
579 dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
580 dialog->findChild<QLabel *>("labelCoinControlPriorityText") ->setToolTip(l6->toolTip());
581 dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
582 dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
584 // Insufficient funds
585 QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
587 label->setVisible(nChange < 0);
590 void CoinControlDialog::updateView()
592 bool treeMode = ui->radioTreeMode->isChecked();
594 ui->treeWidget->clear();
595 ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
596 ui->treeWidget->setAlternatingRowColors(!treeMode);
597 QFlags<Qt::ItemFlag> flgCheckbox=Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
598 QFlags<Qt::ItemFlag> flgTristate=Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
600 int nDisplayUnit = BitcoinUnits::BTC;
601 if (model && model->getOptionsModel())
602 nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
604 map<QString, vector<COutput> > mapCoins;
605 model->listCoins(mapCoins);
607 BOOST_FOREACH(PAIRTYPE(QString, vector<COutput>) coins, mapCoins)
609 QTreeWidgetItem *itemWalletAddress = new QTreeWidgetItem();
610 QString sWalletAddress = coins.first;
611 QString sWalletLabel = "";
612 if (model->getAddressTableModel())
613 sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
614 if (sWalletLabel.length() == 0)
615 sWalletLabel = tr("(no label)");
620 ui->treeWidget->addTopLevelItem(itemWalletAddress);
622 itemWalletAddress->setFlags(flgTristate);
623 itemWalletAddress->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
625 for (int i = 0; i < ui->treeWidget->columnCount(); i++)
626 itemWalletAddress->setBackground(i, QColor(248, 247, 246));
629 itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
632 itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
636 double dPrioritySum = 0;
639 uint64 nTxWeight = 0, nTxWeightSum = 0;
640 BOOST_FOREACH(const COutput& out, coins.second)
642 int nInputSize = 148; // 180 if uncompressed public key
643 nSum += out.tx->vout[out.i].nValue;
644 model->getStakeWeightFromValue(out.tx->GetTxTime(), out.tx->vout[out.i].nValue, nTxWeight);
645 nTxWeightSum += nTxWeight;
648 QTreeWidgetItem *itemOutput;
649 if (treeMode) itemOutput = new QTreeWidgetItem(itemWalletAddress);
650 else itemOutput = new QTreeWidgetItem(ui->treeWidget);
651 itemOutput->setFlags(flgCheckbox);
652 itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
655 CTxDestination outputAddress;
656 QString sAddress = "";
657 if(ExtractDestination(out.tx->vout[out.i].scriptPubKey, outputAddress))
659 sAddress = CBitcoinAddress(outputAddress).ToString().c_str();
661 // if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
662 if (!treeMode || (!(sAddress == sWalletAddress)))
663 itemOutput->setText(COLUMN_ADDRESS, sAddress);
666 CKeyID *keyid = boost::get< CKeyID >(&outputAddress);
667 if (keyid && model->getPubKey(*keyid, pubkey) && !pubkey.IsCompressed())
672 if (!(sAddress == sWalletAddress)) // change
674 // tooltip from where the change comes from
675 itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
676 itemOutput->setText(COLUMN_LABEL, tr("(change)"));
681 if (model->getAddressTableModel())
682 sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
683 if (sLabel.length() == 0)
684 sLabel = tr("(no label)");
685 itemOutput->setText(COLUMN_LABEL, sLabel);
689 itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.tx->vout[out.i].nValue));
690 itemOutput->setText(COLUMN_AMOUNT_INT64, strPad(QString::number(out.tx->vout[out.i].nValue), 15, " ")); // padding so that sorting works correctly
693 itemOutput->setText(COLUMN_DATE, QDateTime::fromTime_t(out.tx->GetTxTime()).toUTC().toString("yy-MM-dd hh:mm"));
695 // immature PoS reward
696 if (out.tx->IsCoinStake() && out.tx->GetBlocksToMaturity() > 0 && out.tx->GetDepthInMainChain() > 0) {
697 itemOutput->setBackground(COLUMN_CONFIRMATIONS, Qt::red);
698 itemOutput->setDisabled(true);
702 itemOutput->setText(COLUMN_CONFIRMATIONS, strPad(QString::number(out.nDepth), 8, " "));
705 double dPriority = ((double)out.tx->vout[out.i].nValue / (nInputSize + 78)) * (out.nDepth+1); // 78 = 2 * 34 + 10
706 itemOutput->setText(COLUMN_PRIORITY, CoinControlDialog::getPriorityLabel(dPriority));
707 itemOutput->setText(COLUMN_PRIORITY_INT64, strPad(QString::number((int64)dPriority), 20, " "));
708 dPrioritySum += (double)out.tx->vout[out.i].nValue * (out.nDepth+1);
709 nInputSum += nInputSize;
712 itemOutput->setText(COLUMN_WEIGHT, strPad(QString::number(nTxWeight), 8, " "));
715 uint256 txhash = out.tx->GetHash();
716 itemOutput->setText(COLUMN_TXHASH, txhash.GetHex().c_str());
719 itemOutput->setText(COLUMN_VOUT_INDEX, QString::number(out.i));
721 // disable locked coins
722 /*if (model->isLockedCoin(txhash, out.i))
724 COutPoint outpt(txhash, out.i);
725 coinControl->UnSelect(outpt); // just to be sure
726 itemOutput->setDisabled(true);
727 itemOutput->setIcon(COLUMN_CHECKBOX, QIcon(":/icons/lock_closed"));
731 if (coinControl->IsSelected(txhash, out.i))
732 itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Checked);
738 dPrioritySum = dPrioritySum / (nInputSum + 78);
739 itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
740 itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
741 itemWalletAddress->setText(COLUMN_AMOUNT_INT64, strPad(QString::number(nSum), 15, " "));
742 itemWalletAddress->setText(COLUMN_PRIORITY, CoinControlDialog::getPriorityLabel(dPrioritySum));
743 itemWalletAddress->setText(COLUMN_PRIORITY_INT64, strPad(QString::number((int64)dPrioritySum), 20, " "));
744 itemWalletAddress->setText(COLUMN_WEIGHT, strPad(QString::number((uint64)nTxWeightSum),8," "));
749 // expand all partially selected
752 for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
753 if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
754 ui->treeWidget->topLevelItem(i)->setExpanded(true);
758 sortView(sortColumn, sortOrder);
759 ui->treeWidget->setEnabled(true);