#include "walletmodel.h"
#include "bitcoinunits.h"
#include "optionsmodel.h"
+#include "transactiontablemodel.h"
+#include "transactionfilterproxy.h"
+#include "guiutil.h"
+#include "guiconstants.h"
-#include <QDebug>
+#include <QAbstractItemDelegate>
+#include <QPainter>
+
+#define DECORATION_SIZE 64
+#define NUM_ITEMS 3
+
+class TxViewDelegate : public QAbstractItemDelegate
+{
+ Q_OBJECT
+public:
+ TxViewDelegate(): QAbstractItemDelegate(), unit(BitcoinUnits::BTC)
+ {
+
+ }
+
+ 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(qVariantCanConvert<QColor>(value))
+ {
+ foreground = qvariant_cast<QColor>(value);
+ }
+
+ painter->setPen(foreground);
+ painter->drawText(addressRect, Qt::AlignLeft|Qt::AlignVCenter, address);
+
+ if(amount < 0)
+ {
+ foreground = COLOR_NEGATIVE;
+ }
+ 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();
+ }
+
+ 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),
currentBalance(-1),
- currentUnconfirmedBalance(-1)
+ currentStake(0),
+ currentUnconfirmedBalance(-1),
+ currentImmatureBalance(-1),
+ txdelegate(new TxViewDelegate()),
+ filter(0)
{
ui->setupUi(this);
- // Balance: <balance>
- ui->labelBalance->setFont(QFont("Monospace", -1, QFont::Bold));
- ui->labelBalance->setToolTip(tr("Your current balance"));
- ui->labelBalance->setTextInteractionFlags(Qt::TextSelectableByMouse|Qt::TextSelectableByKeyboard);
+ // Recent transactions
+ ui->listTransactions->setItemDelegate(txdelegate);
+ ui->listTransactions->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE));
+ 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)));
- // Balance: <balance>
- ui->labelUnconfirmed->setFont(QFont("Monospace", -1, QFont::Bold));
- ui->labelUnconfirmed->setToolTip(tr("Balance of transactions that have yet to be confirmed"));
- ui->labelUnconfirmed->setTextInteractionFlags(Qt::TextSelectableByMouse|Qt::TextSelectableByKeyboard);
+ // init "out of sync" warning labels
+ ui->labelWalletStatus->setText("(" + tr("out of sync") + ")");
+ ui->labelTransactionsStatus->setText("(" + tr("out of sync") + ")");
- ui->labelNumTransactions->setToolTip(tr("Total number of transactions in wallet"));
+ // start with displaying the "out of sync" warnings
+ showOutOfSyncWarning(true);
+}
- // Overview page should show:
- // Last received transaction(s)
- // Last sent transaction(s)
+void OverviewPage::handleTransactionClicked(const QModelIndex &index)
+{
+ if(filter)
+ emit transactionClicked(filter->mapToSource(index));
}
OverviewPage::~OverviewPage()
delete ui;
}
-void OverviewPage::setBalance(qint64 balance, qint64 unconfirmedBalance)
+void OverviewPage::setBalance(qint64 balance, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance)
{
int unit = model->getOptionsModel()->getDisplayUnit();
currentBalance = balance;
+ currentStake = stake;
currentUnconfirmedBalance = unconfirmedBalance;
+ currentImmatureBalance = immatureBalance;
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));
+ ui->labelStake->setText(BitcoinUnits::formatWithUnit(unit, stake));
ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(unit, unconfirmedBalance));
+ ui->labelImmature->setText(BitcoinUnits::formatWithUnit(unit, 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);
}
void OverviewPage::setNumTransactions(int count)
void OverviewPage::setModel(WalletModel *model)
{
this->model = model;
+ if(model && model->getOptionsModel())
+ {
+ // Set up transaction list
+ filter = new TransactionFilterProxy();
+ filter->setSourceModel(model->getTransactionTableModel());
+ filter->setLimit(NUM_ITEMS);
+ filter->setDynamicSortFilter(true);
+// filter->setSortRole(Qt::EditRole);
+ filter->setSortRole(TransactionTableModel::DateRole);
+ filter->sort(TransactionTableModel::Status, Qt::DescendingOrder);
+
+ ui->listTransactions->setModel(filter);
+ ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress);
- // Keep up to date with wallet
- setBalance(model->getBalance(), model->getUnconfirmedBalance());
- connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64)));
+ // Keep up to date with wallet
+ setBalance(model->getBalance(), model->getStake(), model->getUnconfirmedBalance(), model->getImmatureBalance());
+ connect(model, SIGNAL(balanceChanged(qint64, qint64, qint64, qint64)), this, SLOT(setBalance(qint64, qint64, qint64, qint64)));
+
+ setNumTransactions(model->getNumTransactions());
+ connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
+
+ connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
+ }
+
+ // update the display unit, to not use the default ("BTC")
+ updateDisplayUnit();
+}
+
+void OverviewPage::updateDisplayUnit()
+{
+ if(model && model->getOptionsModel())
+ {
+ if(currentBalance != -1)
+ setBalance(currentBalance, model->getStake(), currentUnconfirmedBalance, currentImmatureBalance);
- setNumTransactions(model->getNumTransactions());
- connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
+ // Update txdelegate->unit with the current unit
+ txdelegate->unit = model->getOptionsModel()->getDisplayUnit();
- connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(displayUnitChanged()));
+ ui->listTransactions->update();
+ }
}
-void OverviewPage::displayUnitChanged()
+void OverviewPage::showOutOfSyncWarning(bool fShow)
{
- if(currentBalance != -1)
- setBalance(currentBalance, currentUnconfirmedBalance);
+ ui->labelWalletStatus->setVisible(fShow);
+ ui->labelTransactionsStatus->setVisible(fShow);
}