Bugfix: Unspendable inputs handling
[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(qVariantCanConvert<QColor>(value))
49         {
50             foreground = qvariant_cast<QColor>(value);
51         }
52
53         painter->setPen(foreground);
54         painter->drawText(addressRect, Qt::AlignLeft|Qt::AlignVCenter, address);
55
56         if(amount < 0)
57         {
58             foreground = COLOR_NEGATIVE;
59         }
60         else if(!confirmed)
61         {
62             foreground = COLOR_UNCONFIRMED;
63         }
64         else
65         {
66             foreground = option.palette.color(QPalette::Text);
67         }
68         painter->setPen(foreground);
69         QString amountText = BitcoinUnits::formatWithUnit(unit, amount, true);
70         if(!confirmed)
71         {
72             amountText = QString("[") + amountText + QString("]");
73         }
74         painter->drawText(amountRect, Qt::AlignRight|Qt::AlignVCenter, amountText);
75
76         painter->setPen(option.palette.color(QPalette::Text));
77         painter->drawText(amountRect, Qt::AlignLeft|Qt::AlignVCenter, GUIUtil::dateTimeStr(date));
78
79         painter->restore();
80     }
81
82     inline QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
83     {
84         return QSize(DECORATION_SIZE, DECORATION_SIZE);
85     }
86
87     int unit;
88
89 };
90 #include "overviewpage.moc"
91
92 OverviewPage::OverviewPage(QWidget *parent) :
93     QWidget(parent),
94     ui(new Ui::OverviewPage),
95     currentBalanceTotal(-1),
96     currentBalanceWatchOnly(0),
97     currentStake(0),
98     currentUnconfirmedBalance(-1),
99     currentImmatureBalance(-1),
100     txdelegate(new TxViewDelegate()),
101     filter(0)
102 {
103     ui->setupUi(this);
104
105     // Recent transactions
106     ui->listTransactions->setItemDelegate(txdelegate);
107     ui->listTransactions->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE));
108     ui->listTransactions->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2));
109     ui->listTransactions->setAttribute(Qt::WA_MacShowFocusRect, false);
110
111     connect(ui->listTransactions, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTransactionClicked(QModelIndex)));
112
113     // init "out of sync" warning labels
114     ui->labelWalletStatus->setText("(" + tr("out of sync") + ")");
115     ui->labelTransactionsStatus->setText("(" + tr("out of sync") + ")");
116
117     // start with displaying the "out of sync" warnings
118     showOutOfSyncWarning(true);
119 }
120
121 void OverviewPage::handleTransactionClicked(const QModelIndex &index)
122 {
123     if(filter)
124         emit transactionClicked(filter->mapToSource(index));
125 }
126
127 OverviewPage::~OverviewPage()
128 {
129     delete ui;
130 }
131
132 void OverviewPage::setBalance(qint64 total, qint64 watchOnly, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance)
133 {
134     int unit = model->getOptionsModel()->getDisplayUnit();
135     currentBalanceTotal = total;
136     currentBalanceWatchOnly = watchOnly;
137     currentStake = stake;
138     currentUnconfirmedBalance = unconfirmedBalance;
139     currentImmatureBalance = immatureBalance;
140     ui->labelBalanceTotal->setText(BitcoinUnits::formatWithUnit(unit, total));
141     ui->labelBalanceWatchOnly->setText(BitcoinUnits::formatWithUnit(unit, watchOnly));
142     ui->labelStake->setText(BitcoinUnits::formatWithUnit(unit, stake));
143     ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(unit, unconfirmedBalance));
144     ui->labelImmature->setText(BitcoinUnits::formatWithUnit(unit, immatureBalance));
145
146     // only show immature (newly mined) balance if it's non-zero, so as not to complicate things
147     // for the non-mining users
148     bool showImmature = immatureBalance != 0;
149     ui->labelImmature->setVisible(showImmature);
150     ui->labelImmatureText->setVisible(showImmature);
151
152     // only show watch-only balance if it's non-zero, so as not to complicate things
153     // for users
154     bool showWatchOnly = watchOnly != 0;
155     ui->labelBalanceWatchOnly->setVisible(showWatchOnly);
156     ui->labelBalanceWatchOnlyText->setVisible(showWatchOnly);
157 }
158
159 void OverviewPage::setNumTransactions(int count)
160 {
161     ui->labelNumTransactions->setText(QLocale::system().toString(count));
162 }
163
164 void OverviewPage::setModel(WalletModel *model)
165 {
166     this->model = model;
167     if(model && model->getOptionsModel())
168     {
169         // Set up transaction list
170         filter = new TransactionFilterProxy();
171         filter->setSourceModel(model->getTransactionTableModel());
172         filter->setLimit(NUM_ITEMS);
173         filter->setDynamicSortFilter(true);
174 //        filter->setSortRole(Qt::EditRole);
175         filter->setSortRole(TransactionTableModel::DateRole);
176         filter->sort(TransactionTableModel::Status, Qt::DescendingOrder);
177
178         ui->listTransactions->setModel(filter);
179         ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress);
180
181         // Keep up to date with wallet
182         setBalance(model->getBalance(), model->getBalanceWatchOnly(), model->getStake(), model->getUnconfirmedBalance(), model->getImmatureBalance());
183         connect(model, SIGNAL(balanceChanged(qint64, qint64, qint64, qint64, qint64)), this, SLOT(setBalance(qint64, qint64, qint64, qint64, qint64)));
184
185         setNumTransactions(model->getNumTransactions());
186         connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
187
188         connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
189     }
190
191     // update the display unit, to not use the default ("BTC")
192     updateDisplayUnit();
193 }
194
195 void OverviewPage::updateDisplayUnit()
196 {
197     if(model && model->getOptionsModel())
198     {
199         if(currentBalanceTotal != -1)
200             setBalance(currentBalanceTotal, currentBalanceWatchOnly, model->getStake(), currentUnconfirmedBalance, currentImmatureBalance);
201
202         // Update txdelegate->unit with the current unit
203         txdelegate->unit = model->getOptionsModel()->getDisplayUnit();
204
205         ui->listTransactions->update();
206     }
207 }
208
209 void OverviewPage::showOutOfSyncWarning(bool fShow)
210 {
211     ui->labelWalletStatus->setVisible(fShow);
212     ui->labelTransactionsStatus->setVisible(fShow);
213 }