1 #include "overviewpage.h"
2 #include "ui_overviewpage.h"
4 #include "walletmodel.h"
5 #include "bitcoinunits.h"
6 #include "optionsmodel.h"
7 #include "transactiontablemodel.h"
8 #include "transactionfilterproxy.h"
10 #include "guiconstants.h"
12 #include <QAbstractItemDelegate>
15 #define DECORATION_SIZE 64
18 class TxViewDelegate : public QAbstractItemDelegate
22 TxViewDelegate(): QAbstractItemDelegate(), unit(BitcoinUnits::BTC)
27 inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
28 const QModelIndex &index ) const
32 QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
33 QRect mainRect = option.rect;
34 QRect decorationRect(mainRect.topLeft(), QSize(DECORATION_SIZE, DECORATION_SIZE));
35 int xspace = DECORATION_SIZE + 8;
37 int halfheight = (mainRect.height() - 2*ypad)/2;
38 QRect amountRect(mainRect.left() + xspace, mainRect.top()+ypad, mainRect.width() - xspace, halfheight);
39 QRect addressRect(mainRect.left() + xspace, mainRect.top()+ypad+halfheight, mainRect.width() - xspace, halfheight);
40 icon.paint(painter, decorationRect);
42 QDateTime date = index.data(TransactionTableModel::DateRole).toDateTime();
43 QString address = index.data(Qt::DisplayRole).toString();
44 qint64 amount = index.data(TransactionTableModel::AmountRole).toLongLong();
45 bool confirmed = index.data(TransactionTableModel::ConfirmedRole).toBool();
46 QVariant value = index.data(Qt::ForegroundRole);
47 QColor foreground = option.palette.color(QPalette::Text);
48 #if QT_VERSION < 0x050000
49 if(qVariantCanConvert<QColor>(value))
51 if(value.canConvert(QMetaType::QColor))
54 foreground = qvariant_cast<QColor>(value);
57 painter->setPen(foreground);
58 painter->drawText(addressRect, Qt::AlignLeft|Qt::AlignVCenter, address);
62 foreground = COLOR_NEGATIVE;
66 foreground = COLOR_UNCONFIRMED;
70 foreground = option.palette.color(QPalette::Text);
72 painter->setPen(foreground);
73 QString amountText = BitcoinUnits::formatWithUnit(unit, amount, true);
76 amountText = QString("[") + amountText + QString("]");
78 painter->drawText(amountRect, Qt::AlignRight|Qt::AlignVCenter, amountText);
80 painter->setPen(option.palette.color(QPalette::Text));
81 painter->drawText(amountRect, Qt::AlignLeft|Qt::AlignVCenter, GUIUtil::dateTimeStr(date));
86 inline QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
88 return QSize(DECORATION_SIZE, DECORATION_SIZE);
94 #include "overviewpage.moc"
96 OverviewPage::OverviewPage(QWidget *parent) :
98 ui(new Ui::OverviewPage),
99 currentBalanceTotal(-1),
100 currentBalanceWatchOnly(0),
102 currentUnconfirmedBalance(-1),
103 currentImmatureBalance(-1),
104 txdelegate(new TxViewDelegate()),
109 // Recent transactions
110 ui->listTransactions->setItemDelegate(txdelegate);
111 ui->listTransactions->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE));
112 ui->listTransactions->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2));
113 ui->listTransactions->setAttribute(Qt::WA_MacShowFocusRect, false);
115 connect(ui->listTransactions, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTransactionClicked(QModelIndex)));
117 // init "out of sync" warning labels
118 ui->labelWalletStatus->setText("(" + tr("out of sync") + ")");
119 ui->labelTransactionsStatus->setText("(" + tr("out of sync") + ")");
121 // start with displaying the "out of sync" warnings
122 showOutOfSyncWarning(true);
125 void OverviewPage::handleTransactionClicked(const QModelIndex &index)
128 emit transactionClicked(filter->mapToSource(index));
131 OverviewPage::~OverviewPage()
136 void OverviewPage::setBalance(qint64 total, qint64 watchOnly, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance)
138 int unit = model->getOptionsModel()->getDisplayUnit();
139 currentBalanceTotal = total;
140 currentBalanceWatchOnly = watchOnly;
141 currentStake = stake;
142 currentUnconfirmedBalance = unconfirmedBalance;
143 currentImmatureBalance = immatureBalance;
144 ui->labelBalanceTotal->setText(BitcoinUnits::formatWithUnit(unit, total));
145 ui->labelBalanceWatchOnly->setText(BitcoinUnits::formatWithUnit(unit, watchOnly));
146 ui->labelStake->setText(BitcoinUnits::formatWithUnit(unit, stake));
147 ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(unit, unconfirmedBalance));
148 ui->labelImmature->setText(BitcoinUnits::formatWithUnit(unit, immatureBalance));
150 // only show immature (newly mined) balance if it's non-zero, so as not to complicate things
151 // for the non-mining users
152 bool showImmature = immatureBalance != 0;
153 ui->labelImmature->setVisible(showImmature);
154 ui->labelImmatureText->setVisible(showImmature);
156 // only show watch-only balance if it's non-zero, so as not to complicate things
158 bool showWatchOnly = watchOnly != 0;
159 ui->labelBalanceWatchOnly->setVisible(showWatchOnly);
160 ui->labelBalanceWatchOnlyText->setVisible(showWatchOnly);
164 // show/hide watch-only labels
165 void OverviewPage::updateWatchOnlyLabels(bool showWatchOnly)
167 ui->labelBalanceWatchOnly->setVisible(showWatchOnly);
168 ui->labelBalanceWatchOnlyText->setVisible(showWatchOnly);
171 void OverviewPage::setNumTransactions(int count)
173 ui->labelNumTransactions->setText(QLocale::system().toString(count));
176 void OverviewPage::setModel(WalletModel *model)
179 if(model && model->getOptionsModel())
181 // Set up transaction list
182 filter = new TransactionFilterProxy();
183 filter->setSourceModel(model->getTransactionTableModel());
184 filter->setLimit(NUM_ITEMS);
185 filter->setDynamicSortFilter(true);
186 // filter->setSortRole(Qt::EditRole);
187 filter->setSortRole(TransactionTableModel::DateRole);
188 filter->sort(TransactionTableModel::Status, Qt::DescendingOrder);
190 ui->listTransactions->setModel(filter);
191 ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress);
193 // Keep up to date with wallet
194 setBalance(model->getBalance(), model->getBalanceWatchOnly(), model->getStake(), model->getUnconfirmedBalance(), model->getImmatureBalance());
195 connect(model, SIGNAL(balanceChanged(qint64, qint64, qint64, qint64, qint64)), this, SLOT(setBalance(qint64, qint64, qint64, qint64, qint64)));
197 setNumTransactions(model->getNumTransactions());
198 connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
200 connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
202 updateWatchOnlyLabels(model->haveWatchOnly());
203 connect(model, SIGNAL(notifyWatchonlyChanged(bool)), this, SLOT(updateWatchOnlyLabels(bool)));
206 // update the display unit, to not use the default ("BTC")
210 void OverviewPage::updateDisplayUnit()
212 if(model && model->getOptionsModel())
214 if(currentBalanceTotal != -1)
215 setBalance(currentBalanceTotal, currentBalanceWatchOnly, model->getStake(), currentUnconfirmedBalance, currentImmatureBalance);
217 // Update txdelegate->unit with the current unit
218 txdelegate->unit = model->getOptionsModel()->getDisplayUnit();
220 ui->listTransactions->update();
224 void OverviewPage::showOutOfSyncWarning(bool fShow)
226 ui->labelWalletStatus->setVisible(fShow);
227 ui->labelTransactionsStatus->setVisible(fShow);