dash/src/qt/overviewpage.cpp

496 lines
18 KiB
C++
Raw Normal View History

// Copyright (c) 2011-2014 The Bitcoin developers
2015-01-15 11:31:35 +01:00
// Copyright (c) 2014-2015 The Darkcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "overviewpage.h"
#include "ui_overviewpage.h"
#include "bitcoinunits.h"
#include "clientmodel.h"
2014-12-23 03:28:52 +01:00
#include "darksend.h"
#include "darksendconfig.h"
#include "guiconstants.h"
#include "guiutil.h"
#include "optionsmodel.h"
#include "transactionfilterproxy.h"
#include "transactiontablemodel.h"
#include "walletmodel.h"
2014-12-23 03:28:52 +01:00
#include "init.h"
2011-08-03 21:28:11 +02:00
#include <QAbstractItemDelegate>
#include <QPainter>
2014-12-23 03:28:52 +01:00
#include <QTimer>
#define DECORATION_SIZE 64
2011-08-03 21:04:15 +02:00
#define NUM_ITEMS 3
2011-08-03 21:28:11 +02:00
class TxViewDelegate : public QAbstractItemDelegate
{
Q_OBJECT
public:
TxViewDelegate(): QAbstractItemDelegate(), unit(BitcoinUnits::DRK)
{
}
inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index ) const
{
painter->save();
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
QRect mainRect = option.rect;
QRect decorationRect(mainRect.topLeft(), QSize(DECORATION_SIZE, DECORATION_SIZE));
int xspace = DECORATION_SIZE + 8;
int ypad = 6;
int halfheight = (mainRect.height() - 2*ypad)/2;
QRect amountRect(mainRect.left() + xspace, mainRect.top()+ypad, mainRect.width() - xspace, halfheight);
QRect addressRect(mainRect.left() + xspace, mainRect.top()+ypad+halfheight, mainRect.width() - xspace, halfheight);
icon.paint(painter, decorationRect);
QDateTime date = index.data(TransactionTableModel::DateRole).toDateTime();
QString address = index.data(Qt::DisplayRole).toString();
qint64 amount = index.data(TransactionTableModel::AmountRole).toLongLong();
bool confirmed = index.data(TransactionTableModel::ConfirmedRole).toBool();
QVariant value = index.data(Qt::ForegroundRole);
QColor foreground = option.palette.color(QPalette::Text);
if(value.canConvert<QBrush>())
{
QBrush brush = qvariant_cast<QBrush>(value);
foreground = brush.color();
}
painter->setPen(foreground);
painter->drawText(addressRect, Qt::AlignLeft|Qt::AlignVCenter, address);
if(amount < 0)
{
foreground = COLOR_NEGATIVE;
}
2011-08-03 21:04:15 +02:00
else if(!confirmed)
{
foreground = COLOR_UNCONFIRMED;
}
else
{
foreground = option.palette.color(QPalette::Text);
}
painter->setPen(foreground);
QString amountText = BitcoinUnits::formatWithUnit(unit, amount, true);
if(!confirmed)
{
amountText = QString("[") + amountText + QString("]");
}
painter->drawText(amountRect, Qt::AlignRight|Qt::AlignVCenter, amountText);
painter->setPen(option.palette.color(QPalette::Text));
painter->drawText(amountRect, Qt::AlignLeft|Qt::AlignVCenter, GUIUtil::dateTimeStr(date));
painter->restore();
}
2011-08-03 21:28:11 +02:00
inline QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QSize(DECORATION_SIZE, DECORATION_SIZE);
}
int unit;
};
#include "overviewpage.moc"
OverviewPage::OverviewPage(QWidget *parent) :
QWidget(parent),
ui(new Ui::OverviewPage),
clientModel(0),
walletModel(0),
currentBalance(-1),
currentUnconfirmedBalance(-1),
currentImmatureBalance(-1),
txdelegate(new TxViewDelegate()),
filter(0)
{
ui->setupUi(this);
// Recent transactions
ui->listTransactions->setItemDelegate(txdelegate);
ui->listTransactions->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE));
2011-08-03 21:04:15 +02:00
ui->listTransactions->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2));
ui->listTransactions->setAttribute(Qt::WA_MacShowFocusRect, false);
connect(ui->listTransactions, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTransactionClicked(QModelIndex)));
2014-12-23 03:28:52 +01:00
// init "out of sync" warning labels
ui->labelWalletStatus->setText("(" + tr("out of sync") + ")");
ui->labelTransactionsStatus->setText("(" + tr("out of sync") + ")");
2014-12-23 03:28:52 +01:00
showingDarkSendMessage = 0;
darksendActionCheck = 0;
2015-02-01 16:53:49 +01:00
lastNewBlock = 0;
2014-12-23 03:28:52 +01:00
if(fLiteMode){
ui->frameDarksend->setVisible(false);
} else {
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(darkSendStatus()));
timer->start(333);
}
if(fMasterNode || fLiteMode){
2015-02-04 12:00:23 +01:00
ui->toggleDarksend->setText("(" + tr("Disabled") + ")");
2014-12-23 03:28:52 +01:00
ui->toggleDarksend->setEnabled(false);
}else if(!fEnableDarksend){
2015-02-04 12:00:23 +01:00
ui->toggleDarksend->setText(tr("Start Darksend Mixing"));
2014-12-23 03:28:52 +01:00
} else {
2015-02-04 12:00:23 +01:00
ui->toggleDarksend->setText(tr("Stop Darksend Mixing"));
2014-12-23 03:28:52 +01:00
}
// start with displaying the "out of sync" warnings
showOutOfSyncWarning(true);
}
void OverviewPage::handleTransactionClicked(const QModelIndex &index)
{
if(filter)
emit transactionClicked(filter->mapToSource(index));
}
OverviewPage::~OverviewPage()
{
delete ui;
}
2014-12-23 03:28:52 +01:00
void OverviewPage::setBalance(qint64 balance, qint64 unconfirmedBalance, qint64 immatureBalance, qint64 anonymizedBalance)
{
int unit = walletModel->getOptionsModel()->getDisplayUnit();
currentBalance = balance;
currentUnconfirmedBalance = unconfirmedBalance;
currentImmatureBalance = immatureBalance;
2014-12-23 03:28:52 +01:00
currentAnonymizedBalance = anonymizedBalance;
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));
ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(unit, unconfirmedBalance));
ui->labelImmature->setText(BitcoinUnits::formatWithUnit(unit, immatureBalance));
2014-12-23 03:28:52 +01:00
ui->labelAnonymized->setText(BitcoinUnits::formatWithUnit(unit, anonymizedBalance));
2013-06-07 11:08:03 +02:00
ui->labelTotal->setText(BitcoinUnits::formatWithUnit(unit, balance + unconfirmedBalance + immatureBalance));
// only show immature (newly mined) balance if it's non-zero, so as not to complicate things
// for the non-mining users
bool showImmature = immatureBalance != 0;
ui->labelImmature->setVisible(showImmature);
ui->labelImmatureText->setVisible(showImmature);
2015-02-02 16:04:09 +01:00
if(cachedTxLocks != nCompleteTXLocks){
cachedTxLocks = nCompleteTXLocks;
ui->listTransactions->update();
}
}
void OverviewPage::setClientModel(ClientModel *model)
{
this->clientModel = model;
if(model)
{
// Show warning if this is a prerelease version
connect(model, SIGNAL(alertsChanged(QString)), this, SLOT(updateAlerts(QString)));
updateAlerts(model->getStatusBarWarnings());
}
}
void OverviewPage::setWalletModel(WalletModel *model)
{
this->walletModel = model;
if(model && model->getOptionsModel())
2011-11-08 21:18:36 +01:00
{
// Set up transaction list
filter = new TransactionFilterProxy();
2011-11-08 21:18:36 +01:00
filter->setSourceModel(model->getTransactionTableModel());
filter->setLimit(NUM_ITEMS);
filter->setDynamicSortFilter(true);
filter->setSortRole(Qt::EditRole);
filter->setShowInactive(false);
2011-11-08 21:18:36 +01:00
filter->sort(TransactionTableModel::Status, Qt::DescendingOrder);
2011-11-08 21:18:36 +01:00
ui->listTransactions->setModel(filter);
ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress);
2011-11-08 21:18:36 +01:00
// Keep up to date with wallet
2014-12-23 03:28:52 +01:00
setBalance(model->getBalance(), model->getUnconfirmedBalance(), model->getImmatureBalance(), model->getAnonymizedBalance());
connect(model, SIGNAL(balanceChanged(qint64, qint64, qint64, qint64)), this, SLOT(setBalance(qint64, qint64, qint64, qint64)));
connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
2014-12-23 03:28:52 +01:00
connect(ui->darksendAuto, SIGNAL(clicked()), this, SLOT(darksendAuto()));
connect(ui->darksendReset, SIGNAL(clicked()), this, SLOT(darksendReset()));
2014-12-23 03:28:52 +01:00
connect(ui->toggleDarksend, SIGNAL(clicked()), this, SLOT(toggleDarksend()));
2011-11-08 21:18:36 +01:00
}
// update the display unit, to not use the default ("DRK")
updateDisplayUnit();
}
void OverviewPage::updateDisplayUnit()
{
if(walletModel && walletModel->getOptionsModel())
{
if(currentBalance != -1)
2014-12-23 03:28:52 +01:00
setBalance(currentBalance, currentUnconfirmedBalance, currentImmatureBalance, currentAnonymizedBalance);
// Update txdelegate->unit with the current unit
txdelegate->unit = walletModel->getOptionsModel()->getDisplayUnit();
ui->listTransactions->update();
}
}
void OverviewPage::updateAlerts(const QString &warnings)
{
this->ui->labelAlerts->setVisible(!warnings.isEmpty());
this->ui->labelAlerts->setText(warnings);
}
void OverviewPage::showOutOfSyncWarning(bool fShow)
{
ui->labelWalletStatus->setVisible(fShow);
ui->labelTransactionsStatus->setVisible(fShow);
}
2014-12-23 03:28:52 +01:00
void OverviewPage::updateDarksendProgress()
{
if(IsInitialBlockDownload()) return;
int64_t nBalance = pwalletMain->GetBalance();
if(nBalance == 0)
{
2014-12-23 03:28:52 +01:00
ui->darksendProgress->setValue(0);
2015-02-04 12:00:23 +01:00
QString s(tr("No inputs detected"));
2014-12-26 04:58:39 +01:00
ui->darksendProgress->setToolTip(s);
2014-12-23 03:28:52 +01:00
return;
}
//get denominated unconfirmed inputs
if(pwalletMain->GetDenominatedBalance(true, true) > 0)
{
2015-02-04 12:00:23 +01:00
QString s(tr("Found unconfirmed denominated outputs, will wait till they confirm to recalculate."));
ui->darksendProgress->setToolTip(s);
return;
}
//Get the anon threshold
int64_t nMaxToAnonymize = nAnonymizeDarkcoinAmount*COIN;
// If it's more than the wallet amount, limit to that.
if(nMaxToAnonymize > nBalance) nMaxToAnonymize = nBalance;
if(nMaxToAnonymize == 0) return;
// calculate parts of the progress, each of them shouldn't be higher than 1:
// mixing progress of denominated balance
float denomPart = (float)pwalletMain->GetNormalizedAnonymizedBalance() / pwalletMain->GetDenominatedBalance();
denomPart = denomPart > 1 ? 1 : denomPart;
// % of fully anonymized balance
float anonPart = (float)pwalletMain->GetAnonymizedBalance() / nMaxToAnonymize;
// if anonPart is > 1 then we are done, make denomPart equal 1 too
anonPart = anonPart > 1 ? (denomPart = 1, 1) : anonPart;
// apply some weights to them (sum should be <=100) and calculate the whole progress
int progress = 80 * denomPart + 20 * anonPart;
if(progress > 100) progress = 100;
ui->darksendProgress->setValue(progress);
std::ostringstream convert;
convert << "Progress: " << progress << "%, inputs have an average of " << pwalletMain->GetAverageAnonymizedRounds() << " of " << nDarksendRounds << " rounds";
2014-12-23 03:28:52 +01:00
QString s(convert.str().c_str());
ui->darksendProgress->setToolTip(s);
}
void OverviewPage::darkSendStatus()
{
int nBestHeight = chainActive.Tip()->nHeight;
if(nBestHeight != darkSendPool.cachedNumBlocks)
{
2015-02-01 16:53:49 +01:00
//we we're processing lots of blocks, we'll just leave
if(GetTime() - lastNewBlock < 10) return;
lastNewBlock = GetTime();
2014-12-23 03:28:52 +01:00
updateDarksendProgress();
2015-02-04 12:00:23 +01:00
QString strSettings(" " + tr("Rounds"));
strSettings.prepend(QString::number(nDarksendRounds)).prepend(" / ");
strSettings.prepend(BitcoinUnits::formatWithUnit(
walletModel->getOptionsModel()->getDisplayUnit(),
nAnonymizeDarkcoinAmount * COIN)
);
ui->labelAmountRounds->setText(strSettings);
2014-12-23 03:28:52 +01:00
}
if(!fEnableDarksend) {
if(nBestHeight != darkSendPool.cachedNumBlocks)
{
darkSendPool.cachedNumBlocks = nBestHeight;
2015-02-04 12:00:23 +01:00
ui->darksendEnabled->setText(tr("Disabled"));
2014-12-23 03:28:52 +01:00
ui->darksendStatus->setText("");
2015-02-04 12:00:23 +01:00
ui->toggleDarksend->setText(tr("Start Darksend Mixing"));
2014-12-23 03:28:52 +01:00
}
return;
}
// check darksend status and unlock if needed
if(nBestHeight != darkSendPool.cachedNumBlocks)
{
// Balance and number of transactions might have changed
darkSendPool.cachedNumBlocks = nBestHeight;
/* *******************************************************/
2015-02-04 12:00:23 +01:00
ui->darksendEnabled->setText(tr("Enabled"));
2014-12-23 03:28:52 +01:00
}
int state = darkSendPool.GetState();
int entries = darkSendPool.GetEntriesCount();
int accepted = darkSendPool.GetLastEntryAccepted();
2015-02-04 12:00:23 +01:00
/* ** @TODO this string creation really needs some clean ups ---vertoe ** */
2014-12-23 03:28:52 +01:00
std::ostringstream convert;
if(state == POOL_STATUS_ACCEPTING_ENTRIES) {
if(entries == 0) {
if(darkSendPool.strAutoDenomResult.size() == 0){
2015-02-04 12:00:23 +01:00
convert << tr("Darksend is idle.").toStdString();
} else {
convert << darkSendPool.strAutoDenomResult;
}
2014-12-23 03:28:52 +01:00
showingDarkSendMessage = 0;
} else if (accepted == 1) {
2015-02-04 12:00:23 +01:00
convert << tr("Darksend request complete: Your transaction was accepted into the pool!").toStdString();
2014-12-23 03:28:52 +01:00
if(showingDarkSendMessage % 10 > 8) {
darkSendPool.lastEntryAccepted = 0;
showingDarkSendMessage = 0;
}
} else {
2015-02-04 12:00:23 +01:00
if(showingDarkSendMessage % 70 <= 40) convert << tr("Submitted following entries to masternode:").toStdString() << " " << entries << "/" << darkSendPool.GetMaxPoolTransactions();
else if(showingDarkSendMessage % 70 <= 50) convert << tr("Submitted to masternode, Waiting for more entries").toStdString() << " (" << entries << "/" << darkSendPool.GetMaxPoolTransactions() << " ) .";
else if(showingDarkSendMessage % 70 <= 60) convert << tr("Submitted to masternode, Waiting for more entries").toStdString() << " (" << entries << "/" << darkSendPool.GetMaxPoolTransactions() << " ) ..";
else if(showingDarkSendMessage % 70 <= 70) convert << tr("Submitted to masternode, Waiting for more entries").toStdString() << " (" << entries << "/" << darkSendPool.GetMaxPoolTransactions() << " ) ...";
2014-12-23 03:28:52 +01:00
}
} else if(state == POOL_STATUS_SIGNING) {
2015-02-04 12:00:23 +01:00
if(showingDarkSendMessage % 70 <= 10) convert << tr("Found enough users, signing ...").toStdString();
else if(showingDarkSendMessage % 70 <= 20) convert << tr("Found enough users, signing ( waiting. )").toStdString();
else if(showingDarkSendMessage % 70 <= 30) convert << tr("Found enough users, signing ( waiting.. )").toStdString();
else if(showingDarkSendMessage % 70 <= 40) convert << tr("Found enough users, signing ( waiting... )").toStdString();
2014-12-23 03:28:52 +01:00
} else if(state == POOL_STATUS_TRANSMISSION) {
2015-02-04 12:00:23 +01:00
convert << tr("Transmitting final transaction.").toStdString();
2014-12-23 03:28:52 +01:00
} else if (state == POOL_STATUS_IDLE) {
2015-02-04 12:00:23 +01:00
convert << tr("Darksend is idle.").toStdString();
2014-12-23 03:28:52 +01:00
} else if (state == POOL_STATUS_FINALIZE_TRANSACTION) {
2015-02-04 12:00:23 +01:00
convert << tr("Finalizing transaction.").toStdString();
2014-12-23 03:28:52 +01:00
} else if(state == POOL_STATUS_ERROR) {
2015-02-04 12:00:23 +01:00
convert << tr("Darksend request incomplete:").toStdString() << " " << darkSendPool.lastMessage << ". " << tr("Will retry...").toStdString();
2014-12-23 03:28:52 +01:00
} else if(state == POOL_STATUS_SUCCESS) {
2015-02-04 12:00:23 +01:00
convert << tr("Darksend request complete:").toStdString() << " " << darkSendPool.lastMessage;
2014-12-23 03:28:52 +01:00
} else if(state == POOL_STATUS_QUEUE) {
2015-02-04 12:00:23 +01:00
if(showingDarkSendMessage % 70 <= 50) convert << tr("Submitted to masternode, waiting in queue .").toStdString();
else if(showingDarkSendMessage % 70 <= 60) convert << tr("Submitted to masternode, waiting in queue ..").toStdString();
else if(showingDarkSendMessage % 70 <= 70) convert << tr("Submitted to masternode, waiting in queue ...").toStdString();
2014-12-23 03:28:52 +01:00
} else {
2015-02-04 12:00:23 +01:00
convert << tr("Unknown state:").toStdString() << " id = " << state;
2014-12-23 03:28:52 +01:00
}
2014-12-26 04:58:39 +01:00
if(state == POOL_STATUS_ERROR || state == POOL_STATUS_SUCCESS) darkSendPool.Check();
2014-12-23 03:28:52 +01:00
QString s(convert.str().c_str());
2015-02-04 19:17:52 +01:00
s = tr("Last Darksend message:\n") + s;
2014-12-23 03:28:52 +01:00
if(s != ui->darksendStatus->text())
2015-02-04 19:17:52 +01:00
LogPrintf("Last Darksend message: %s\n", convert.str().c_str());
2014-12-26 04:58:39 +01:00
2014-12-23 03:28:52 +01:00
ui->darksendStatus->setText(s);
if(darkSendPool.sessionDenom == 0){
2015-02-04 12:00:23 +01:00
ui->labelSubmittedDenom->setText(tr("N/A"));
} else {
std::string out;
darkSendPool.GetDenominationsToString(darkSendPool.sessionDenom, out);
QString s2(out.c_str());
ui->labelSubmittedDenom->setText(s2);
}
2014-12-23 03:28:52 +01:00
showingDarkSendMessage++;
darksendActionCheck++;
// Get DarkSend Denomination Status
}
void OverviewPage::darksendAuto(){
2014-12-23 03:28:52 +01:00
darkSendPool.DoAutomaticDenominating();
}
void OverviewPage::darksendReset(){
darkSendPool.Reset();
QMessageBox::warning(this, tr("Darksend"),
tr("Darksend was successfully reset."),
QMessageBox::Ok, QMessageBox::Ok);
}
2014-12-23 03:28:52 +01:00
void OverviewPage::toggleDarksend(){
2014-12-26 04:58:39 +01:00
if(!fEnableDarksend){
int64_t balance = pwalletMain->GetBalance();
float minAmount = 1.49 * COIN;
if(balance < minAmount){
QString strMinAmount(
BitcoinUnits::formatWithUnit(
walletModel->getOptionsModel()->getDisplayUnit(),
minAmount));
2014-12-26 04:58:39 +01:00
QMessageBox::warning(this, tr("Darksend"),
tr("Darksend requires at least %1 to use.").arg(strMinAmount),
2014-12-26 04:58:39 +01:00
QMessageBox::Ok, QMessageBox::Ok);
return;
}
// if wallet is locked, ask for a passphrase
if (walletModel->getEncryptionStatus() == WalletModel::Locked)
{
WalletModel::UnlockContext ctx(walletModel->requestUnlock(false));
if(!ctx.isValid())
{
//unlock was cancelled
darkSendPool.cachedNumBlocks = 0;
QMessageBox::warning(this, tr("Darksend"),
tr("Wallet is locked and user declined to unlock. Disabling Darksend."),
QMessageBox::Ok, QMessageBox::Ok);
if (fDebug) LogPrintf("Wallet is locked and user declined to unlock. Disabling Darksend.\n");
return;
}
}
2014-12-23 03:28:52 +01:00
}
darkSendPool.cachedNumBlocks = 0;
fEnableDarksend = !fEnableDarksend;
if(!fEnableDarksend){
2015-02-04 12:00:23 +01:00
ui->toggleDarksend->setText(tr("Start Darksend Mixing"));
2014-12-23 03:28:52 +01:00
} else {
2015-02-04 12:00:23 +01:00
ui->toggleDarksend->setText(tr("Stop Darksend Mixing"));
2014-12-23 03:28:52 +01:00
/* show darksend configuration if client has defaults set */
if(nAnonymizeDarkcoinAmount == 0){
DarksendConfig dlg(this);
dlg.setModel(walletModel);
dlg.exec();
}
darkSendPool.DoAutomaticDenominating();
}
}