coincontroldialog.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. // ECOin - Copyright (c) - 2014/2024 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include "coincontroldialog.h"
  3. #include "ui_coincontroldialog.h"
  4. #include "init.h"
  5. #include "ecoinunits.h"
  6. #include "walletmodel.h"
  7. #include "addresstablemodel.h"
  8. #include "optionsmodel.h"
  9. #include "coincontrol.h"
  10. #include <QApplication>
  11. #include <QCheckBox>
  12. #include <QClipboard>
  13. #include <QColor>
  14. #include <QCursor>
  15. #include <QDateTime>
  16. #include <QDialogButtonBox>
  17. #include <QFlags>
  18. #include <QIcon>
  19. #include <QString>
  20. #include <QTreeWidget>
  21. #include <QTreeWidgetItem>
  22. using namespace std;
  23. QList<qint64> CoinControlDialog::payAmounts;
  24. CCoinControl* CoinControlDialog::coinControl = new CCoinControl();
  25. CoinControlDialog::CoinControlDialog(QWidget *parent) :
  26. QDialog(parent),
  27. ui(new Ui::CoinControlDialog),
  28. model(0)
  29. {
  30. ui->setupUi(this);
  31. // context menu actions
  32. QAction *copyAddressAction = new QAction(tr("Copy address"), this);
  33. QAction *copyLabelAction = new QAction(tr("Copy label"), this);
  34. QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
  35. copyTransactionHashAction = new QAction(tr("Copy transaction ID"), this); // we need to enable/disable this
  36. // context menu
  37. contextMenu = new QMenu();
  38. contextMenu->addAction(copyAddressAction);
  39. contextMenu->addAction(copyLabelAction);
  40. contextMenu->addAction(copyAmountAction);
  41. contextMenu->addAction(copyTransactionHashAction);
  42. // context menu signals
  43. connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
  44. connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
  45. connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
  46. connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
  47. connect(copyTransactionHashAction, SIGNAL(triggered()), this, SLOT(copyTransactionHash()));
  48. // clipboard actions
  49. QAction *clipboardQuantityAction = new QAction(tr("Copy quantity"), this);
  50. QAction *clipboardAmountAction = new QAction(tr("Copy amount"), this);
  51. QAction *clipboardFeeAction = new QAction(tr("Copy fee"), this);
  52. QAction *clipboardAfterFeeAction = new QAction(tr("Copy after fee"), this);
  53. QAction *clipboardBytesAction = new QAction(tr("Copy bytes"), this);
  54. QAction *clipboardPriorityAction = new QAction(tr("Copy priority"), this);
  55. QAction *clipboardLowOutputAction = new QAction(tr("Copy low output"), this);
  56. QAction *clipboardChangeAction = new QAction(tr("Copy change"), this);
  57. connect(clipboardQuantityAction, SIGNAL(triggered()), this, SLOT(clipboardQuantity()));
  58. connect(clipboardAmountAction, SIGNAL(triggered()), this, SLOT(clipboardAmount()));
  59. connect(clipboardFeeAction, SIGNAL(triggered()), this, SLOT(clipboardFee()));
  60. connect(clipboardAfterFeeAction, SIGNAL(triggered()), this, SLOT(clipboardAfterFee()));
  61. connect(clipboardBytesAction, SIGNAL(triggered()), this, SLOT(clipboardBytes()));
  62. connect(clipboardPriorityAction, SIGNAL(triggered()), this, SLOT(clipboardPriority()));
  63. connect(clipboardLowOutputAction, SIGNAL(triggered()), this, SLOT(clipboardLowOutput()));
  64. connect(clipboardChangeAction, SIGNAL(triggered()), this, SLOT(clipboardChange()));
  65. ui->labelCoinControlQuantity->addAction(clipboardQuantityAction);
  66. ui->labelCoinControlAmount->addAction(clipboardAmountAction);
  67. ui->labelCoinControlFee->addAction(clipboardFeeAction);
  68. ui->labelCoinControlAfterFee->addAction(clipboardAfterFeeAction);
  69. ui->labelCoinControlBytes->addAction(clipboardBytesAction);
  70. ui->labelCoinControlPriority->addAction(clipboardPriorityAction);
  71. ui->labelCoinControlLowOutput->addAction(clipboardLowOutputAction);
  72. ui->labelCoinControlChange->addAction(clipboardChangeAction);
  73. // toggle tree/list mode
  74. connect(ui->radioTreeMode, SIGNAL(toggled(bool)), this, SLOT(radioTreeMode(bool)));
  75. connect(ui->radioListMode, SIGNAL(toggled(bool)), this, SLOT(radioListMode(bool)));
  76. // click on checkbox
  77. connect(ui->treeWidget, SIGNAL(itemChanged( QTreeWidgetItem*, int)), this, SLOT(viewItemChanged( QTreeWidgetItem*, int)));
  78. // click on header
  79. ui->treeWidget->header()->setSectionsClickable(true);
  80. connect(ui->treeWidget->header(), SIGNAL(sectionClicked(int)), this, SLOT(headerSectionClicked(int)));
  81. // ok button
  82. connect(ui->buttonBox, SIGNAL(clicked( QAbstractButton*)), this, SLOT(buttonBoxClicked(QAbstractButton*)));
  83. // (un)select all
  84. connect(ui->pushButtonSelectAll, SIGNAL(clicked()), this, SLOT(buttonSelectAllClicked()));
  85. ui->treeWidget->setColumnWidth(COLUMN_CHECKBOX, 84);
  86. ui->treeWidget->setColumnWidth(COLUMN_AMOUNT, 100);
  87. ui->treeWidget->setColumnWidth(COLUMN_LABEL, 170);
  88. ui->treeWidget->setColumnWidth(COLUMN_ADDRESS, 290);
  89. ui->treeWidget->setColumnWidth(COLUMN_DATE, 110);
  90. ui->treeWidget->setColumnWidth(COLUMN_CONFIRMATIONS, 100);
  91. ui->treeWidget->setColumnWidth(COLUMN_PRIORITY, 100);
  92. ui->treeWidget->setColumnHidden(COLUMN_TXHASH, true); // store transacton hash in this column, but dont show it
  93. ui->treeWidget->setColumnHidden(COLUMN_VOUT_INDEX, true); // store vout index in this column, but dont show it
  94. ui->treeWidget->setColumnHidden(COLUMN_AMOUNT_INT64, true); // store amount int64 in this column, but dont show it
  95. ui->treeWidget->setColumnHidden(COLUMN_PRIORITY_INT64, true); // store priority int64 in this column, but dont show it
  96. sortView(COLUMN_AMOUNT_INT64, Qt::DescendingOrder);
  97. }
  98. CoinControlDialog::~CoinControlDialog()
  99. {
  100. delete ui;
  101. }
  102. void CoinControlDialog::setModel(WalletModel *model)
  103. {
  104. this->model = model;
  105. if(model && model->getOptionsModel() && model->getAddressTableModel())
  106. {
  107. updateView();
  108. CoinControlDialog::updateLabels(model, this);
  109. }
  110. }
  111. QString CoinControlDialog::strPad(QString s, int nPadLength, QString sPadding)
  112. {
  113. while (s.length() < nPadLength)
  114. s = sPadding + s;
  115. return s;
  116. }
  117. void CoinControlDialog::buttonBoxClicked(QAbstractButton* button)
  118. {
  119. if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
  120. done(QDialog::Accepted); // closes the dialog
  121. }
  122. void CoinControlDialog::buttonSelectAllClicked()
  123. {
  124. Qt::CheckState state = Qt::Checked;
  125. for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
  126. {
  127. if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != Qt::Unchecked)
  128. {
  129. state = Qt::Unchecked;
  130. break;
  131. }
  132. }
  133. ui->treeWidget->setEnabled(false);
  134. for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
  135. if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != state)
  136. ui->treeWidget->topLevelItem(i)->setCheckState(COLUMN_CHECKBOX, state);
  137. ui->treeWidget->setEnabled(true);
  138. CoinControlDialog::updateLabels(model, this);
  139. }
  140. // context menu
  141. void CoinControlDialog::showMenu(const QPoint &point)
  142. {
  143. QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
  144. if(item)
  145. {
  146. contextMenuItem = item;
  147. // disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
  148. 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)
  149. {
  150. copyTransactionHashAction->setEnabled(true);
  151. }
  152. else // this means click on parent node in tree mode -> disable all
  153. {
  154. copyTransactionHashAction->setEnabled(false);
  155. }
  156. // show context menu
  157. contextMenu->exec(QCursor::pos());
  158. }
  159. }
  160. // context menu action: copy amount
  161. void CoinControlDialog::copyAmount()
  162. {
  163. QApplication::clipboard()->setText(contextMenuItem->text(COLUMN_AMOUNT));
  164. }
  165. // context menu action: copy label
  166. void CoinControlDialog::copyLabel()
  167. {
  168. if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_LABEL).length() == 0 && contextMenuItem->parent())
  169. QApplication::clipboard()->setText(contextMenuItem->parent()->text(COLUMN_LABEL));
  170. else
  171. QApplication::clipboard()->setText(contextMenuItem->text(COLUMN_LABEL));
  172. }
  173. // context menu action: copy address
  174. void CoinControlDialog::copyAddress()
  175. {
  176. if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_ADDRESS).length() == 0 && contextMenuItem->parent())
  177. QApplication::clipboard()->setText(contextMenuItem->parent()->text(COLUMN_ADDRESS));
  178. else
  179. QApplication::clipboard()->setText(contextMenuItem->text(COLUMN_ADDRESS));
  180. }
  181. // context menu action: copy transaction id
  182. void CoinControlDialog::copyTransactionHash()
  183. {
  184. QApplication::clipboard()->setText(contextMenuItem->text(COLUMN_TXHASH));
  185. }
  186. // copy label "Quantity" to clipboard
  187. void CoinControlDialog::clipboardQuantity()
  188. {
  189. QApplication::clipboard()->setText(ui->labelCoinControlQuantity->text());
  190. }
  191. // copy label "Amount" to clipboard
  192. void CoinControlDialog::clipboardAmount()
  193. {
  194. QApplication::clipboard()->setText(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
  195. }
  196. // copy label "Fee" to clipboard
  197. void CoinControlDialog::clipboardFee()
  198. {
  199. QApplication::clipboard()->setText(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")));
  200. }
  201. // copy label "After fee" to clipboard
  202. void CoinControlDialog::clipboardAfterFee()
  203. {
  204. QApplication::clipboard()->setText(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")));
  205. }
  206. // copy label "Bytes" to clipboard
  207. void CoinControlDialog::clipboardBytes()
  208. {
  209. QApplication::clipboard()->setText(ui->labelCoinControlBytes->text());
  210. }
  211. // copy label "Priority" to clipboard
  212. void CoinControlDialog::clipboardPriority()
  213. {
  214. QApplication::clipboard()->setText(ui->labelCoinControlPriority->text());
  215. }
  216. // copy label "Low output" to clipboard
  217. void CoinControlDialog::clipboardLowOutput()
  218. {
  219. QApplication::clipboard()->setText(ui->labelCoinControlLowOutput->text());
  220. }
  221. // copy label "Change" to clipboard
  222. void CoinControlDialog::clipboardChange()
  223. {
  224. QApplication::clipboard()->setText(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")));
  225. }
  226. // treeview: sort
  227. void CoinControlDialog::sortView(int column, Qt::SortOrder order)
  228. {
  229. sortColumn = column;
  230. sortOrder = order;
  231. ui->treeWidget->sortItems(column, order);
  232. ui->treeWidget->header()->setSortIndicator((sortColumn == COLUMN_AMOUNT_INT64 ? COLUMN_AMOUNT : (sortColumn == COLUMN_PRIORITY_INT64 ? COLUMN_PRIORITY : sortColumn)), sortOrder);
  233. }
  234. // treeview: clicked on header
  235. void CoinControlDialog::headerSectionClicked(int logicalIndex)
  236. {
  237. if (logicalIndex == COLUMN_CHECKBOX) // click on most left column -> do nothing
  238. {
  239. ui->treeWidget->header()->setSortIndicator((sortColumn == COLUMN_AMOUNT_INT64 ? COLUMN_AMOUNT : (sortColumn == COLUMN_PRIORITY_INT64 ? COLUMN_PRIORITY : sortColumn)), sortOrder);
  240. }
  241. else
  242. {
  243. if (logicalIndex == COLUMN_AMOUNT) // sort by amount
  244. logicalIndex = COLUMN_AMOUNT_INT64;
  245. if (logicalIndex == COLUMN_PRIORITY) // sort by priority
  246. logicalIndex = COLUMN_PRIORITY_INT64;
  247. if (sortColumn == logicalIndex)
  248. sortOrder = ((sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder);
  249. else
  250. {
  251. sortColumn = logicalIndex;
  252. sortOrder = ((sortColumn == COLUMN_AMOUNT_INT64 || sortColumn == COLUMN_PRIORITY_INT64 || sortColumn == COLUMN_DATE || sortColumn == COLUMN_CONFIRMATIONS) ? Qt::DescendingOrder : Qt::AscendingOrder); // if amount,date,conf,priority then default => desc, else default => asc
  253. }
  254. sortView(sortColumn, sortOrder);
  255. }
  256. }
  257. // toggle tree mode
  258. void CoinControlDialog::radioTreeMode(bool checked)
  259. {
  260. if (checked && model)
  261. updateView();
  262. }
  263. // toggle list mode
  264. void CoinControlDialog::radioListMode(bool checked)
  265. {
  266. if (checked && model)
  267. updateView();
  268. }
  269. // checkbox clicked by user
  270. void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
  271. {
  272. 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)
  273. {
  274. COutPoint outpt(uint256(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt());
  275. if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
  276. coinControl->UnSelect(outpt);
  277. else if (item->isDisabled()) // locked (this happens if "check all" through parent node)
  278. item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
  279. else
  280. coinControl->Select(outpt);
  281. // selection changed -> update labels
  282. if (ui->treeWidget->isEnabled()) // do not update on every click for (un)select all
  283. CoinControlDialog::updateLabels(model, this);
  284. }
  285. }
  286. // helper function, return human readable label for priority number
  287. QString CoinControlDialog::getPriorityLabel(double dPriority)
  288. {
  289. if (dPriority > 576000ULL) // at least medium, this number is from AllowFree(), the other thresholds are kinda random
  290. {
  291. if (dPriority > 5760000000ULL) return tr("highest");
  292. else if (dPriority > 576000000ULL) return tr("high");
  293. else if (dPriority > 57600000ULL) return tr("medium-high");
  294. else return tr("medium");
  295. }
  296. else
  297. {
  298. if (dPriority > 5760ULL) return tr("low-medium");
  299. else if (dPriority > 58ULL) return tr("low");
  300. else return tr("lowest");
  301. }
  302. }
  303. void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
  304. {
  305. if (!model) return;
  306. // nPayAmount
  307. qint64 nPayAmount = 0;
  308. bool fLowOutput = false;
  309. bool fDust = false;
  310. CTransaction txDummy;
  311. foreach(const qint64 &amount, CoinControlDialog::payAmounts)
  312. {
  313. nPayAmount += amount;
  314. if (amount > 0)
  315. {
  316. if (amount < CENT)
  317. fLowOutput = true;
  318. CTxOut txout(amount, (CScript)vector<unsigned char>(24, 0));
  319. txDummy.vout.push_back(txout);
  320. }
  321. }
  322. QString sPriorityLabel = "";
  323. int64 nAmount = 0;
  324. int64 nPayFee = 0;
  325. int64 nAfterFee = 0;
  326. int64 nChange = 0;
  327. unsigned int nBytes = 0;
  328. unsigned int nBytesInputs = 0;
  329. double dPriority = 0;
  330. double dPriorityInputs = 0;
  331. unsigned int nQuantity = 0;
  332. vector<COutPoint> vCoinControl;
  333. vector<COutput> vOutputs;
  334. coinControl->ListSelected(vCoinControl);
  335. model->getOutputs(vCoinControl, vOutputs);
  336. BOOST_FOREACH(const COutput& out, vOutputs)
  337. {
  338. // Quantity
  339. nQuantity++;
  340. // Amount
  341. nAmount += out.tx->vout[out.i].nValue;
  342. // Priority
  343. dPriorityInputs += (double)out.tx->vout[out.i].nValue * (out.nDepth+1);
  344. // Bytes
  345. CTxDestination address;
  346. if(ExtractDestination(out.tx->vout[out.i].scriptPubKey, address))
  347. {
  348. CPubKey pubkey;
  349. CKeyID *keyid = boost::get< CKeyID >(&address);
  350. if (keyid && model->getPubKey(*keyid, pubkey))
  351. nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
  352. else
  353. nBytesInputs += 148; // in all error cases, simply assume 148 here
  354. }
  355. else nBytesInputs += 148;
  356. }
  357. // calculation
  358. if (nQuantity > 0)
  359. {
  360. // Bytes
  361. nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
  362. // Priority
  363. dPriority = dPriorityInputs / nBytes;
  364. sPriorityLabel = CoinControlDialog::getPriorityLabel(dPriority);
  365. // Fee
  366. int64 nFee = nTransactionFee * (1 + (int64)nBytes / 1000);
  367. // Min Fee
  368. int64 nMinFee = txDummy.GetMinFee(1, false, GMF_SEND, nBytes);
  369. nPayFee = max(nFee, nMinFee);
  370. if (nPayAmount > 0)
  371. {
  372. nChange = nAmount - nPayFee - nPayAmount;
  373. // if sub-cent change is required, the fee must be raised to at least CTransaction::nMinTxFee
  374. if (nPayFee < CENT && nChange > 0 && nChange < CENT)
  375. {
  376. if (nChange < CENT) // change < 0.01 => simply move all change to fees
  377. {
  378. nPayFee = nChange;
  379. nChange = 0;
  380. }
  381. else
  382. {
  383. nChange = nChange + nPayFee - CENT;
  384. nPayFee = CENT;
  385. }
  386. }
  387. if (nChange == 0)
  388. nBytes -= 34;
  389. }
  390. // after fee
  391. nAfterFee = nAmount - nPayFee;
  392. if (nAfterFee < 0)
  393. nAfterFee = 0;
  394. }
  395. // actually update labels
  396. int nDisplayUnit = EcoinUnits::ECO;
  397. if (model && model->getOptionsModel())
  398. nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
  399. QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
  400. QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
  401. QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
  402. QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
  403. QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
  404. QLabel *l6 = dialog->findChild<QLabel *>("labelCoinControlPriority");
  405. QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
  406. QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
  407. // enable/disable "low output" and "change"
  408. dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
  409. dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
  410. dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
  411. dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
  412. // stats
  413. l1->setText(QString::number(nQuantity)); // Quantity
  414. l2->setText(EcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
  415. l3->setText(EcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
  416. l4->setText(EcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
  417. l5->setText(((nBytes > 0) ? "~" : "") + QString::number(nBytes)); // Bytes
  418. l6->setText(sPriorityLabel); // Priority
  419. l7->setText((fLowOutput ? (fDust ? tr("DUST") : tr("yes")) : tr("no"))); // Low Output / Dust
  420. l8->setText(EcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
  421. // turn labels "red"
  422. l5->setStyleSheet((nBytes >= 10000) ? "color:red;" : ""); // Bytes >= 10000
  423. l6->setStyleSheet((dPriority <= 576000) ? "color:red;" : ""); // Priority < "medium"
  424. l7->setStyleSheet((fLowOutput) ? "color:red;" : ""); // Low Output = "yes"
  425. l8->setStyleSheet((nChange > 0 && nChange < CENT) ? "color:red;" : ""); // Change < 0.01ECO
  426. // tool tips
  427. l5->setToolTip(tr("This label turns red, if the transaction size is bigger than 10000 bytes.\n\n This means a fee of at least %1 per kb is required.\n\n Can vary +/- 1 Byte per input.").arg(EcoinUnits::formatWithUnit(nDisplayUnit, CENT)));
  428. 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(EcoinUnits::formatWithUnit(nDisplayUnit, CENT)));
  429. 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 0.546 times the minimum relay fee are shown as DUST.").arg(EcoinUnits::formatWithUnit(nDisplayUnit, CENT)).arg(EcoinUnits::formatWithUnit(nDisplayUnit, CENT)));
  430. 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(EcoinUnits::formatWithUnit(nDisplayUnit, CENT)).arg(EcoinUnits::formatWithUnit(nDisplayUnit, CENT)));
  431. dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
  432. dialog->findChild<QLabel *>("labelCoinControlPriorityText") ->setToolTip(l6->toolTip());
  433. dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
  434. dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
  435. // Insufficient funds
  436. QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
  437. if (label)
  438. label->setVisible(nChange < 0);
  439. }
  440. void CoinControlDialog::updateView()
  441. {
  442. bool treeMode = ui->radioTreeMode->isChecked();
  443. ui->treeWidget->clear();
  444. ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
  445. ui->treeWidget->setAlternatingRowColors(!treeMode);
  446. QFlags<Qt::ItemFlag> flgCheckbox=Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
  447. QFlags<Qt::ItemFlag> flgTristate=Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
  448. int nDisplayUnit = EcoinUnits::ECO;
  449. if (model && model->getOptionsModel())
  450. nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
  451. map<QString, vector<COutput> > mapCoins;
  452. model->listCoins(mapCoins);
  453. BOOST_FOREACH(PAIRTYPE(QString, vector<COutput>) coins, mapCoins)
  454. {
  455. QTreeWidgetItem *itemWalletAddress = new QTreeWidgetItem();
  456. QString sWalletAddress = coins.first;
  457. QString sWalletLabel = "";
  458. if (model->getAddressTableModel())
  459. sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
  460. if (sWalletLabel.length() == 0)
  461. sWalletLabel = tr("(no label)");
  462. if (treeMode)
  463. {
  464. // wallet address
  465. ui->treeWidget->addTopLevelItem(itemWalletAddress);
  466. itemWalletAddress->setFlags(flgTristate);
  467. itemWalletAddress->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
  468. for (int i = 0; i < ui->treeWidget->columnCount(); i++)
  469. itemWalletAddress->setBackground(i, QColor(248, 247, 246));
  470. // label
  471. itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
  472. // address
  473. itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
  474. }
  475. int64 nSum = 0;
  476. double dPrioritySum = 0;
  477. int nChildren = 0;
  478. int nInputSum = 0;
  479. BOOST_FOREACH(const COutput& out, coins.second)
  480. {
  481. int nInputSize = 148; // 180 if uncompressed public key
  482. nSum += out.tx->vout[out.i].nValue;
  483. nChildren++;
  484. QTreeWidgetItem *itemOutput;
  485. if (treeMode) itemOutput = new QTreeWidgetItem(itemWalletAddress);
  486. else itemOutput = new QTreeWidgetItem(ui->treeWidget);
  487. itemOutput->setFlags(flgCheckbox);
  488. itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
  489. // address
  490. CTxDestination outputAddress;
  491. QString sAddress = "";
  492. if(ExtractDestination(out.tx->vout[out.i].scriptPubKey, outputAddress))
  493. {
  494. sAddress = CEcoinAddress(outputAddress).ToString().c_str();
  495. // if listMode or change => show ecoin address. In tree mode, address is not shown again for direct wallet address outputs
  496. if (!treeMode || (!(sAddress == sWalletAddress)))
  497. itemOutput->setText(COLUMN_ADDRESS, sAddress);
  498. CPubKey pubkey;
  499. CKeyID *keyid = boost::get< CKeyID >(&outputAddress);
  500. if (keyid && model->getPubKey(*keyid, pubkey) && !pubkey.IsCompressed())
  501. nInputSize = 180;
  502. }
  503. // label
  504. if (!(sAddress == sWalletAddress)) // change
  505. {
  506. // tooltip from where the change comes from
  507. itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
  508. itemOutput->setText(COLUMN_LABEL, tr("(change)"));
  509. }
  510. else if (!treeMode)
  511. {
  512. QString sLabel = "";
  513. if (model->getAddressTableModel())
  514. sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
  515. if (sLabel.length() == 0)
  516. sLabel = tr("(no label)");
  517. itemOutput->setText(COLUMN_LABEL, sLabel);
  518. }
  519. // amount
  520. itemOutput->setText(COLUMN_AMOUNT, EcoinUnits::format(nDisplayUnit, out.tx->vout[out.i].nValue));
  521. itemOutput->setText(COLUMN_AMOUNT_INT64, strPad(QString::number(out.tx->vout[out.i].nValue), 15, " ")); // padding so that sorting works correctly
  522. // date
  523. itemOutput->setText(COLUMN_DATE, QDateTime::fromTime_t(out.tx->GetTxTime()).toUTC().toString("yy-MM-dd hh:mm"));
  524. // immature PoS reward
  525. if (out.tx->IsCoinStake() && out.tx->GetBlocksToMaturity() > 0 && out.tx->GetDepthInMainChain() > 0) {
  526. itemOutput->setBackground(COLUMN_CONFIRMATIONS, Qt::red);
  527. itemOutput->setDisabled(true);
  528. }
  529. // confirmations
  530. itemOutput->setText(COLUMN_CONFIRMATIONS, strPad(QString::number(out.nDepth), 8, " "));
  531. // priority
  532. double dPriority = ((double)out.tx->vout[out.i].nValue / (nInputSize + 78)) * (out.nDepth+1); // 78 = 2 * 34 + 10
  533. itemOutput->setText(COLUMN_PRIORITY, CoinControlDialog::getPriorityLabel(dPriority));
  534. itemOutput->setText(COLUMN_PRIORITY_INT64, strPad(QString::number((int64)dPriority), 20, " "));
  535. dPrioritySum += (double)out.tx->vout[out.i].nValue * (out.nDepth+1);
  536. nInputSum += nInputSize;
  537. // transaction hash
  538. uint256 txhash = out.tx->GetHash();
  539. itemOutput->setText(COLUMN_TXHASH, txhash.GetHex().c_str());
  540. // vout index
  541. itemOutput->setText(COLUMN_VOUT_INDEX, QString::number(out.i));
  542. // set checkbox
  543. if (coinControl->IsSelected(txhash, out.i))
  544. itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Checked);
  545. }
  546. // amount
  547. if (treeMode)
  548. {
  549. dPrioritySum = dPrioritySum / (nInputSum + 78);
  550. itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
  551. itemWalletAddress->setText(COLUMN_AMOUNT, EcoinUnits::format(nDisplayUnit, nSum));
  552. itemWalletAddress->setText(COLUMN_AMOUNT_INT64, strPad(QString::number(nSum), 15, " "));
  553. itemWalletAddress->setText(COLUMN_PRIORITY, CoinControlDialog::getPriorityLabel(dPrioritySum));
  554. itemWalletAddress->setText(COLUMN_PRIORITY_INT64, strPad(QString::number((int64)dPrioritySum), 20, " "));
  555. }
  556. }
  557. // expand all partially selected
  558. if (treeMode)
  559. {
  560. for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
  561. if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
  562. ui->treeWidget->topLevelItem(i)->setExpanded(true);
  563. }
  564. // sort view
  565. sortView(sortColumn, sortOrder);
  566. ui->treeWidget->setEnabled(true);
  567. }