59cb0acb1f9fd0d4121ba9e138a72392f8c9495c
[novacoin.git] / src / qt / overviewpage.cpp
1 #include "overviewpage.h"
2 #include "ui_overviewpage.h"
3
4 #include "walletmodel.h"
5 #include "bitcoinunits.h"
6 #include "optionsmodel.h"
7 #include "transactiontablemodel.h"
8 #include "transactionfilterproxy.h"
9 #include "guiutil.h"
10 #include "guiconstants.h"
11
12 #include <QAbstractItemDelegate>
13 #include <QPainter>
14
15 #define DECORATION_SIZE 64
16 #define NUM_ITEMS 3
17
18 class TxViewDelegate : public QAbstractItemDelegate
19 {
20     Q_OBJECT
21 public:
22     TxViewDelegate(): QAbstractItemDelegate(), unit(BitcoinUnits::BTC)
23     {
24
25     }
26
27     inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
28                       const QModelIndex &index ) const
29     {
30         painter->save();
31
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;
36         int ypad = 6;
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);
41
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))
50 #else
51         if(value.canConvert(QMetaType::QColor))
52 #endif
53         {
54             foreground = qvariant_cast<QColor>(value);
55         }
56
57         painter->setPen(foreground);
58         painter->drawText(addressRect, Qt::AlignLeft|Qt::AlignVCenter, address);
59
60         if(amount < 0)
61         {
62             foreground = COLOR_NEGATIVE;
63         }
64         else if(!confirmed)
65         {
66             foreground = COLOR_UNCONFIRMED;
67         }
68         else
69         {
70             foreground = option.palette.color(QPalette::Text);
71         }
72         painter->setPen(foreground);
73         QString amountText = BitcoinUnits::formatWithUnit(unit, amount, true);
74         if(!confirmed)
75         {
76             amountText = QString("[") + amountText + QString("]");
77         }
78         painter->drawText(amountRect, Qt::AlignRight|Qt::AlignVCenter, amountText);
79
80         painter->setPen(option.palette.color(QPalette::Text));
81         painter->drawText(amountRect, Qt::AlignLeft|Qt::AlignVCenter, GUIUtil::dateTimeStr(date));
82
83         painter->restore();
84     }
85
86     inline QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
87     {
88         return QSize(DECORATION_SIZE, DECORATION_SIZE);
89     }
90
91     int unit;
92
93 };
94 #include "overviewpage.moc"
95
96 OverviewPage::OverviewPage(QWidget *parent) :
97     QWidget(parent),
98     ui(new Ui::OverviewPage),
99     currentBalanceTotal(-1),
100     currentBalanceWatchOnly(0),
101     currentStake(0),
102     currentUnconfirmedBalance(-1),
103     currentImmatureBalance(-1),
104     txdelegate(new TxViewDelegate()),
105     filter(0)
106 {
107     ui->setupUi(this);
108
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);
114
115     connect(ui->listTransactions, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTransactionClicked(QModelIndex)));
116
117     // init "out of sync" warning labels
118     ui->labelWalletStatus->setText("(" + tr("out of sync") + ")");
119     ui->labelTransactionsStatus->setText("(" + tr("out of sync") + ")");
120
121     // start with displaying the "out of sync" warnings
122     showOutOfSyncWarning(true);
123 }
124
125 void OverviewPage::handleTransactionClicked(const QModelIndex &index)
126 {
127     if(filter)
128         emit transactionClicked(filter->mapToSource(index));
129 }
130
131 OverviewPage::~OverviewPage()
132 {
133     delete ui;
134 }
135
136 void OverviewPage::setBalance(qint64 total, qint64 watchOnly, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance)
137 {
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));
149
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);
155
156     // only show watch-only balance if it's non-zero, so as not to complicate things
157     // for users
158     bool showWatchOnly = watchOnly != 0;
159     ui->labelBalanceWatchOnly->setVisible(showWatchOnly);
160     ui->labelBalanceWatchOnlyText->setVisible(showWatchOnly);
161 }
162
163 void OverviewPage::setNumTransactions(int count)
164 {
165     ui->labelNumTransactions->setText(QLocale::system().toString(count));
166 }
167
168 void OverviewPage::setModel(WalletModel *model)
169 {
170     this->model = model;
171     if(model && model->getOptionsModel())
172     {
173         // Set up transaction list
174         filter = new TransactionFilterProxy();
175         filter->setSourceModel(model->getTransactionTableModel());
176         filter->setLimit(NUM_ITEMS);
177         filter->setDynamicSortFilter(true);
178 //        filter->setSortRole(Qt::EditRole);
179         filter->setSortRole(TransactionTableModel::DateRole);
180         filter->sort(TransactionTableModel::Status, Qt::DescendingOrder);
181
182         ui->listTransactions->setModel(filter);
183         ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress);
184
185         // Keep up to date with wallet
186         setBalance(model->getBalance(), model->getBalanceWatchOnly(), model->getStake(), model->getUnconfirmedBalance(), model->getImmatureBalance());
187         connect(model, SIGNAL(balanceChanged(qint64, qint64, qint64, qint64, qint64)), this, SLOT(setBalance(qint64, qint64, qint64, qint64, qint64)));
188
189         setNumTransactions(model->getNumTransactions());
190         connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
191
192         connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
193     }
194
195     // update the display unit, to not use the default ("BTC")
196     updateDisplayUnit();
197 }
198
199 void OverviewPage::updateDisplayUnit()
200 {
201     if(model && model->getOptionsModel())
202     {
203         if(currentBalanceTotal != -1)
204             setBalance(currentBalanceTotal, currentBalanceWatchOnly, model->getStake(), currentUnconfirmedBalance, currentImmatureBalance);
205
206         // Update txdelegate->unit with the current unit
207         txdelegate->unit = model->getOptionsModel()->getDisplayUnit();
208
209         ui->listTransactions->update();
210     }
211 }
212
213 void OverviewPage::showOutOfSyncWarning(bool fShow)
214 {
215     ui->labelWalletStatus->setVisible(fShow);
216     ui->labelTransactionsStatus->setVisible(fShow);
217 }