Anoncoin  0.9.4
P2P Digital Currency
overviewpage.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "overviewpage.h"
7 #include "ui_overviewpage.h"
8 
9 #include "anoncoinunits.h"
10 #include "clientmodel.h"
11 #include "guiconstants.h"
12 #include "guiutil.h"
13 #include "optionsmodel.h"
14 #include "transactionfilterproxy.h"
15 #include "transactiontablemodel.h"
16 #include "walletmodel.h"
17 
18 #include <QAbstractItemDelegate>
19 #include <QPainter>
20 
21 #define DECORATION_SIZE 64
22 #define NUM_ITEMS 3
23 
24 class TxViewDelegate : public QAbstractItemDelegate
25 {
26  Q_OBJECT
27 public:
28  TxViewDelegate(): QAbstractItemDelegate(), unit(AnoncoinUnits::ANC)
29  {
30 
31  }
32 
33  inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
34  const QModelIndex &index ) const
35  {
36  painter->save();
37 
38  QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
39  QRect mainRect = option.rect;
40  QRect decorationRect(mainRect.topLeft(), QSize(DECORATION_SIZE, DECORATION_SIZE));
41  int xspace = DECORATION_SIZE + 8;
42  int ypad = 6;
43  int halfheight = (mainRect.height() - 2*ypad)/2;
44  QRect amountRect(mainRect.left() + xspace, mainRect.top()+ypad, mainRect.width() - xspace, halfheight);
45  QRect addressRect(mainRect.left() + xspace, mainRect.top()+ypad+halfheight, mainRect.width() - xspace, halfheight);
46  icon.paint(painter, decorationRect);
47 
48  QDateTime date = index.data(TransactionTableModel::DateRole).toDateTime();
49  QString address = index.data(Qt::DisplayRole).toString();
50  qint64 amount = index.data(TransactionTableModel::AmountRole).toLongLong();
51  bool confirmed = index.data(TransactionTableModel::ConfirmedRole).toBool();
52  QVariant value = index.data(Qt::ForegroundRole);
53  QColor foreground = option.palette.color(QPalette::Text);
54  if(value.canConvert<QBrush>())
55  {
56  QBrush brush = qvariant_cast<QBrush>(value);
57  foreground = brush.color();
58  }
59 
60  painter->setPen(foreground);
61  QRect boundingRect;
62  painter->drawText(addressRect, Qt::AlignLeft|Qt::AlignVCenter, address, &boundingRect);
63 
64  if (index.data(TransactionTableModel::WatchonlyRole).toBool())
65  {
66  QIcon iconWatchonly = qvariant_cast<QIcon>(index.data(TransactionTableModel::WatchonlyDecorationRole));
67  QRect watchonlyRect(boundingRect.right() + 5, mainRect.top()+ypad+halfheight, 16, halfheight);
68  iconWatchonly.paint(painter, watchonlyRect);
69  }
70 
71  if(amount < 0)
72  {
73  foreground = COLOR_NEGATIVE;
74  }
75  else if(!confirmed)
76  {
77  foreground = COLOR_UNCONFIRMED;
78  }
79  else
80  {
81  foreground = option.palette.color(QPalette::Text);
82  }
83  painter->setPen(foreground);
84  QString amountText = AnoncoinUnits::formatWithUnit(unit, amount, true);
85  if(!confirmed)
86  {
87  amountText = QString("[") + amountText + QString("]");
88  }
89  painter->drawText(amountRect, Qt::AlignRight|Qt::AlignVCenter, amountText);
90 
91  painter->setPen(option.palette.color(QPalette::Text));
92  painter->drawText(amountRect, Qt::AlignLeft|Qt::AlignVCenter, GUIUtil::dateTimeStr(date));
93 
94  painter->restore();
95  }
96 
97  inline QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
98  {
99  return QSize(DECORATION_SIZE, DECORATION_SIZE);
100  }
101 
102  int unit;
103 
104 };
105 #include "overviewpage.moc"
106 
107 OverviewPage::OverviewPage(QWidget *parent) :
108  QWidget(parent),
109  ui(new Ui::OverviewPage),
110  clientModel(0),
111  walletModel(0),
112  currentBalance(-1),
113  currentUnconfirmedBalance(-1),
114  currentImmatureBalance(-1),
115  currentWatchOnlyBalance(-1),
116  currentWatchUnconfBalance(-1),
117  currentWatchImmatureBalance(-1),
118  txdelegate(new TxViewDelegate()),
119  filter(0)
120 {
121  ui->setupUi(this);
122 
123  // Recent transactions
124  ui->listTransactions->setItemDelegate(txdelegate);
125  ui->listTransactions->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE));
126  ui->listTransactions->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2));
127  ui->listTransactions->setAttribute(Qt::WA_MacShowFocusRect, false);
128 
129  connect(ui->listTransactions, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTransactionClicked(QModelIndex)));
130 
131  // init "out of sync" warning labels
132  ui->labelWalletStatus->setText("(" + tr("out of sync") + ")");
133  ui->labelTransactionsStatus->setText("(" + tr("out of sync") + ")");
134 
135  // start with displaying the "out of sync" warnings
136  showOutOfSyncWarning(true);
137 }
138 
139 void OverviewPage::handleTransactionClicked(const QModelIndex &index)
140 {
141  if(filter)
142  emit transactionClicked(filter->mapToSource(index));
143 }
144 
146 {
147  delete ui;
148 }
149 
150 void OverviewPage::setBalance(qint64 balance, qint64 unconfirmedBalance, qint64 immatureBalance, qint64 watchOnlyBalance, qint64 watchUnconfBalance, qint64 watchImmatureBalance)
151 {
152  int unit = walletModel->getOptionsModel()->getDisplayUnit();
153  currentBalance = balance;
154  currentUnconfirmedBalance = unconfirmedBalance;
155  currentImmatureBalance = immatureBalance;
156  currentWatchOnlyBalance = watchOnlyBalance;
157  currentWatchUnconfBalance = watchUnconfBalance;
158  currentWatchImmatureBalance = watchImmatureBalance;
159  ui->labelBalance->setText(AnoncoinUnits::formatWithUnit(unit, balance));
160  ui->labelUnconfirmed->setText(AnoncoinUnits::formatWithUnit(unit, unconfirmedBalance));
161  ui->labelImmature->setText(AnoncoinUnits::formatWithUnit(unit, immatureBalance));
162  ui->labelTotal->setText(AnoncoinUnits::formatWithUnit(unit, balance + unconfirmedBalance + immatureBalance));
163  ui->labelWatchAvailable->setText(AnoncoinUnits::formatWithUnit(unit, watchOnlyBalance));
164  ui->labelWatchPending->setText(AnoncoinUnits::formatWithUnit(unit, watchUnconfBalance));
165  ui->labelWatchImmature->setText(AnoncoinUnits::formatWithUnit(unit, watchImmatureBalance));
166  ui->labelWatchTotal->setText(AnoncoinUnits::formatWithUnit(unit, watchOnlyBalance + watchUnconfBalance + watchImmatureBalance));
167 
168  // only show immature (newly mined) balance if it's non-zero, so as not to complicate things
169  // for the non-mining users
170  bool showImmature = immatureBalance != 0;
171  bool showWatchOnlyImmature = watchImmatureBalance != 0;
172 
173  // for symmetry reasons also show immature label when the watch-only one is shown
174  ui->labelImmature->setVisible(showImmature || showWatchOnlyImmature);
175  ui->labelImmatureText->setVisible(showImmature || showWatchOnlyImmature);
176  ui->labelWatchImmature->setVisible(showWatchOnlyImmature); // show watch-only immature balance
177 }
178 
179 // show/hide watch-only labels
180 void OverviewPage::updateWatchOnlyLabels(bool showWatchOnly)
181 {
182  ui->labelSpendable->setVisible(showWatchOnly); // show spendable label (only when watch-only is active)
183  ui->labelWatchonly->setVisible(showWatchOnly); // show watch-only label
184  ui->lineWatchBalance->setVisible(showWatchOnly); // show watch-only balance separator line
185  ui->labelWatchAvailable->setVisible(showWatchOnly); // show watch-only available balance
186  ui->labelWatchPending->setVisible(showWatchOnly); // show watch-only pending balance
187  ui->labelWatchTotal->setVisible(showWatchOnly); // show watch-only total balance
188 
189  if (!showWatchOnly)
190  ui->labelWatchImmature->hide();
191 }
192 
194 {
195  this->clientModel = model;
196  if(model)
197  {
198  // Show warning if this is a prerelease version
199  connect(model, SIGNAL(alertsChanged(QString)), this, SLOT(updateAlerts(QString)));
201  }
202 }
203 
205 {
206  this->walletModel = model;
207  if(model && model->getOptionsModel())
208  {
209  // Set up transaction list
211  filter->setSourceModel(model->getTransactionTableModel());
213  filter->setDynamicSortFilter(true);
214  filter->setSortRole(Qt::EditRole);
215  filter->setShowInactive(false);
216  filter->sort(TransactionTableModel::Status, Qt::DescendingOrder);
217 
218  ui->listTransactions->setModel(filter);
220 
221  // Keep up to date with wallet
222  setBalance(model->getBalance(), model->getUnconfirmedBalance(), model->getImmatureBalance(),
224  connect(model, SIGNAL(balanceChanged(qint64, qint64, qint64, qint64, qint64, qint64)), this, SLOT(setBalance(qint64, qint64, qint64, qint64, qint64, qint64)));
225 
226  connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
227 
229  connect(model, SIGNAL(notifyWatchonlyChanged(bool)), this, SLOT(updateWatchOnlyLabels(bool)));
230  }
231 
232  // update the display unit, to not use the default ("ANC")
234 }
235 
237 {
239  {
240  if(currentBalance != -1)
243 
244  // Update txdelegate->unit with the current unit
246 
247  ui->listTransactions->update();
248  }
249 }
250 
251 void OverviewPage::updateAlerts(const QString &warnings)
252 {
253  this->ui->labelAlerts->setVisible(!warnings.isEmpty());
254  this->ui->labelAlerts->setText(warnings);
255 }
256 
258 {
259  ui->labelWalletStatus->setVisible(fShow);
260  ui->labelTransactionsStatus->setVisible(fShow);
261 }
QLabel * labelSpendable
QLabel * labelUnconfirmed
void setWalletModel(WalletModel *walletModel)
void updateAlerts(const QString &warnings)
Anoncoin unit definitions.
Definition: anoncoinunits.h:15
qint64 currentWatchOnlyBalance
Definition: overviewpage.h:51
qint64 getImmatureBalance() const
Definition: walletmodel.cpp:79
#define DECORATION_SIZE
void handleTransactionClicked(const QModelIndex &index)
qint64 currentWatchUnconfBalance
Definition: overviewpage.h:52
static QString formatWithUnit(int unit, qint64 amount, bool plussign=false)
Format as string (with unit)
QLabel * labelTransactionsStatus
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:75
qint64 getWatchUnconfirmedBalance() const
Definition: walletmodel.cpp:94
QLabel * labelImmature
TransactionFilterProxy * filter
Definition: overviewpage.h:56
qint64 currentUnconfirmedBalance
Definition: overviewpage.h:49
QFrame * lineWatchBalance
TxViewDelegate * txdelegate
Definition: overviewpage.h:55
WalletModel * walletModel
Definition: overviewpage.h:47
QString getStatusBarWarnings() const
Return warnings to be displayed in status bar.
QLabel * labelAlerts
qint64 currentBalance
Definition: overviewpage.h:48
qint64 getWatchImmatureBalance() const
Definition: walletmodel.cpp:99
QLabel * labelWatchPending
#define NUM_ITEMS
void updateWatchOnlyLabels(bool showWatchOnly)
qint64 getUnconfirmedBalance() const
Definition: walletmodel.cpp:74
qint64 currentImmatureBalance
Definition: overviewpage.h:50
Date and time this transaction was created.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
TransactionTableModel * getTransactionTableModel()
int getDisplayUnit()
Definition: optionsmodel.h:87
void setBalance(qint64 balance, qint64 unconfirmedBalance, qint64 immatureBalance, qint64 watchOnlyBalance, qint64 watchUnconfBalance, qint64 watchImmatureBalance)
#define COLOR_UNCONFIRMED
Definition: guiconstants.h:22
Model for Anoncoin network client.
Definition: clientmodel.h:45
QLabel * labelWatchTotal
ClientModel * clientModel
Definition: overviewpage.h:46
QLabel * labelImmatureText
void transactionClicked(const QModelIndex &index)
void setLimit(int limit)
Set maximum number of rows returned, -1 if unlimited.
void showOutOfSyncWarning(bool fShow)
void setShowInactive(bool showInactive)
Set whether to show conflicted transactions.
QLabel * labelWalletStatus
QLabel * labelWatchAvailable
OverviewPage(QWidget *parent=0)
Filter the transaction list according to pre-specified rules.
void updateDisplayUnit()
Interface to Anoncoin wallet from Qt view code.
Definition: walletmodel.h:97
qint64 getBalance(const CCoinControl *coinControl=NULL) const
Definition: walletmodel.cpp:57
qint64 currentWatchImmatureBalance
Definition: overviewpage.h:53
bool haveWatchOnly() const
Definition: walletmodel.cpp:84
void setClientModel(ClientModel *clientModel)
QLabel * labelWatchImmature
QListView * listTransactions
qint64 getWatchBalance() const
Definition: walletmodel.cpp:89
Overview ("home") page widget.
Definition: overviewpage.h:25
Ui::OverviewPage * ui
Definition: overviewpage.h:45
QLabel * labelWatchonly
void setupUi(QWidget *OverviewPage)
#define COLOR_NEGATIVE
Definition: guiconstants.h:24
OptionsModel * getOptionsModel()
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
QLabel * labelBalance