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 QFont balance = QApplication::font();
110 balance.setPointSize(balance.pointSize() * 1.5);
111 balance.setBold(true);
112 ui->label_5->setFont(balance);
114 // Recent transactions
115 ui->listTransactions->setItemDelegate(txdelegate);
116 ui->listTransactions->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE));
117 ui->listTransactions->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2));
118 ui->listTransactions->setAttribute(Qt::WA_MacShowFocusRect, false);
120 connect(ui->listTransactions, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTransactionClicked(QModelIndex)));
122 // init "out of sync" warning labels
123 ui->labelWalletStatus->setText("(" + tr("out of sync") + ")");
124 ui->labelTransactionsStatus->setText("(" + tr("out of sync") + ")");
126 // start with displaying the "out of sync" warnings
127 showOutOfSyncWarning(true);
130 void OverviewPage::handleTransactionClicked(const QModelIndex &index)
133 emit transactionClicked(filter->mapToSource(index));
136 OverviewPage::~OverviewPage()
141 void OverviewPage::setBalance(qint64 total, qint64 watchOnly, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance)
143 int unit = model->getOptionsModel()->getDisplayUnit();
144 currentBalanceTotal = total;
145 currentBalanceWatchOnly = watchOnly;
146 currentStake = stake;
147 currentUnconfirmedBalance = unconfirmedBalance;
148 currentImmatureBalance = immatureBalance;
149 ui->labelAvailable->setText(BitcoinUnits::formatWithUnit(unit, total));
150 ui->labelBalanceWatchOnly->setText(BitcoinUnits::formatWithUnit(unit, watchOnly));
151 ui->labelStake->setText(BitcoinUnits::formatWithUnit(unit, stake));
152 ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(unit, unconfirmedBalance));
153 ui->labelImmature->setText(BitcoinUnits::formatWithUnit(unit, immatureBalance));
154 ui->labelTotal->setText(BitcoinUnits::formatWithUnit(unit, total+stake+unconfirmedBalance+immatureBalance));
156 // only show immature (newly mined) balance if it's non-zero, so as not to complicate things
157 // for the non-mining users
158 bool showImmature = immatureBalance != 0;
159 ui->labelImmature->setVisible(showImmature);
160 ui->labelImmatureText->setVisible(showImmature);
162 // only show watch-only balance if it's non-zero, so as not to complicate things
164 bool showWatchOnly = watchOnly != 0;
165 ui->labelBalanceWatchOnly->setVisible(showWatchOnly);
166 ui->labelBalanceWatchOnlyText->setVisible(showWatchOnly);
170 // show/hide watch-only labels
171 void OverviewPage::updateWatchOnlyLabels(bool showWatchOnly)
173 ui->labelBalanceWatchOnly->setVisible(showWatchOnly);
174 ui->labelBalanceWatchOnlyText->setVisible(showWatchOnly);
177 void OverviewPage::setNumTransactions(int count)
179 ui->labelNumTransactions->setText(QLocale::system().toString(count));
182 void OverviewPage::setModel(WalletModel *model)
185 if(model && model->getOptionsModel())
187 // Set up transaction list
188 filter = new TransactionFilterProxy();
189 filter->setSourceModel(model->getTransactionTableModel());
190 filter->setLimit(NUM_ITEMS);
191 filter->setDynamicSortFilter(true);
192 // filter->setSortRole(Qt::EditRole);
193 filter->setSortRole(TransactionTableModel::DateRole);
194 filter->sort(TransactionTableModel::Status, Qt::DescendingOrder);
196 ui->listTransactions->setModel(filter);
197 ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress);
199 // Keep up to date with wallet
200 setBalance(model->getBalance(), model->getBalanceWatchOnly(), model->getStake(), model->getUnconfirmedBalance(), model->getImmatureBalance());
201 connect(model, SIGNAL(balanceChanged(qint64, qint64, qint64, qint64, qint64)), this, SLOT(setBalance(qint64, qint64, qint64, qint64, qint64)));
203 setNumTransactions(model->getNumTransactions());
204 connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
206 connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
208 updateWatchOnlyLabels(model->haveWatchOnly());
209 connect(model, SIGNAL(notifyWatchonlyChanged(bool)), this, SLOT(updateWatchOnlyLabels(bool)));
212 // update the display unit, to not use the default ("BTC")
216 void OverviewPage::updateDisplayUnit()
218 if(model && model->getOptionsModel())
220 if(currentBalanceTotal != -1)
221 setBalance(currentBalanceTotal, currentBalanceWatchOnly, model->getStake(), currentUnconfirmedBalance, currentImmatureBalance);
223 // Update txdelegate->unit with the current unit
224 txdelegate->unit = model->getOptionsModel()->getDisplayUnit();
226 ui->listTransactions->update();
230 void OverviewPage::showOutOfSyncWarning(bool fShow)
232 ui->labelWalletStatus->setVisible(fShow);
233 ui->labelTransactionsStatus->setVisible(fShow);