Bitcoin Core  21.99.0
P2P Digital Currency
overviewpage.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <qt/overviewpage.h>
6 #include <qt/forms/ui_overviewpage.h>
7 
8 #include <qt/bitcoinunits.h>
9 #include <qt/clientmodel.h>
10 #include <qt/guiconstants.h>
11 #include <qt/guiutil.h>
12 #include <qt/optionsmodel.h>
13 #include <qt/platformstyle.h>
17 #include <qt/walletmodel.h>
18 
19 #include <QAbstractItemDelegate>
20 #include <QApplication>
21 #include <QDateTime>
22 #include <QPainter>
23 #include <QStatusTipEvent>
24 
25 #include <algorithm>
26 #include <map>
27 
28 #define DECORATION_SIZE 54
29 #define NUM_ITEMS 5
30 
31 Q_DECLARE_METATYPE(interfaces::WalletBalances)
32 
33 class TxViewDelegate : public QAbstractItemDelegate
34 {
35  Q_OBJECT
36 public:
37  explicit TxViewDelegate(const PlatformStyle *_platformStyle, QObject *parent=nullptr):
38  QAbstractItemDelegate(parent), unit(BitcoinUnits::BTC),
39  platformStyle(_platformStyle)
40  {
41  connect(this, &TxViewDelegate::width_changed, this, &TxViewDelegate::sizeHintChanged);
42  }
43 
44  inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
45  const QModelIndex &index ) const override
46  {
47  painter->save();
48 
49  QIcon icon = qvariant_cast<QIcon>(index.data(TransactionTableModel::RawDecorationRole));
50  QRect mainRect = option.rect;
51  QRect decorationRect(mainRect.topLeft(), QSize(DECORATION_SIZE, DECORATION_SIZE));
52  int xspace = DECORATION_SIZE + 8;
53  int ypad = 6;
54  int halfheight = (mainRect.height() - 2*ypad)/2;
55  QRect amountRect(mainRect.left() + xspace, mainRect.top()+ypad, mainRect.width() - xspace, halfheight);
56  QRect addressRect(mainRect.left() + xspace, mainRect.top()+ypad+halfheight, mainRect.width() - xspace, halfheight);
57  icon = platformStyle->SingleColorIcon(icon);
58  icon.paint(painter, decorationRect);
59 
60  QDateTime date = index.data(TransactionTableModel::DateRole).toDateTime();
61  QString address = index.data(Qt::DisplayRole).toString();
62  qint64 amount = index.data(TransactionTableModel::AmountRole).toLongLong();
63  bool confirmed = index.data(TransactionTableModel::ConfirmedRole).toBool();
64  QVariant value = index.data(Qt::ForegroundRole);
65  QColor foreground = option.palette.color(QPalette::Text);
66  if(value.canConvert<QBrush>())
67  {
68  QBrush brush = qvariant_cast<QBrush>(value);
69  foreground = brush.color();
70  }
71 
72  painter->setPen(foreground);
73  QRect boundingRect;
74  painter->drawText(addressRect, Qt::AlignLeft | Qt::AlignVCenter, address, &boundingRect);
75  int address_rect_min_width = boundingRect.width();
76 
77  if (index.data(TransactionTableModel::WatchonlyRole).toBool())
78  {
79  QIcon iconWatchonly = qvariant_cast<QIcon>(index.data(TransactionTableModel::WatchonlyDecorationRole));
80  QRect watchonlyRect(boundingRect.right() + 5, mainRect.top()+ypad+halfheight, 16, halfheight);
81  iconWatchonly.paint(painter, watchonlyRect);
82  address_rect_min_width += 5 + watchonlyRect.width();
83  }
84 
85  if(amount < 0)
86  {
87  foreground = COLOR_NEGATIVE;
88  }
89  else if(!confirmed)
90  {
91  foreground = COLOR_UNCONFIRMED;
92  }
93  else
94  {
95  foreground = option.palette.color(QPalette::Text);
96  }
97  painter->setPen(foreground);
98  QString amountText = BitcoinUnits::formatWithUnit(unit, amount, true, BitcoinUnits::SeparatorStyle::ALWAYS);
99  if(!confirmed)
100  {
101  amountText = QString("[") + amountText + QString("]");
102  }
103 
104  QRect amount_bounding_rect;
105  painter->drawText(amountRect, Qt::AlignRight | Qt::AlignVCenter, amountText, &amount_bounding_rect);
106 
107  painter->setPen(option.palette.color(QPalette::Text));
108  QRect date_bounding_rect;
109  painter->drawText(amountRect, Qt::AlignLeft | Qt::AlignVCenter, GUIUtil::dateTimeStr(date), &date_bounding_rect);
110 
111  const int minimum_width = std::max(address_rect_min_width, amount_bounding_rect.width() + date_bounding_rect.width());
112  const auto search = m_minimum_width.find(index.row());
113  if (search == m_minimum_width.end() || search->second != minimum_width) {
114  m_minimum_width[index.row()] = minimum_width;
115  Q_EMIT width_changed(index);
116  }
117 
118  painter->restore();
119  }
120 
121  inline QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
122  {
123  const auto search = m_minimum_width.find(index.row());
124  const int minimum_text_width = search == m_minimum_width.end() ? 0 : search->second;
125  return {DECORATION_SIZE + 8 + minimum_text_width, DECORATION_SIZE};
126  }
127 
128  int unit;
129 
130 Q_SIGNALS:
132  void width_changed(const QModelIndex& index) const;
133 
134 private:
136  mutable std::map<int, int> m_minimum_width;
137 };
138 
139 #include <qt/overviewpage.moc>
140 
141 OverviewPage::OverviewPage(const PlatformStyle *platformStyle, QWidget *parent) :
142  QWidget(parent),
143  ui(new Ui::OverviewPage),
144  clientModel(nullptr),
145  walletModel(nullptr),
146  txdelegate(new TxViewDelegate(platformStyle, this))
147 {
148  ui->setupUi(this);
149 
150  m_balances.balance = -1;
151 
152  // use a SingleColorIcon for the "out of sync warning" icon
153  QIcon icon = platformStyle->SingleColorIcon(":/icons/warning");
154  ui->labelTransactionsStatus->setIcon(icon);
155  ui->labelWalletStatus->setIcon(icon);
156 
157  // Recent transactions
158  ui->listTransactions->setItemDelegate(txdelegate);
159  ui->listTransactions->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE));
160  ui->listTransactions->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2));
161  ui->listTransactions->setAttribute(Qt::WA_MacShowFocusRect, false);
162 
163  connect(ui->listTransactions, &TransactionOverviewWidget::clicked, this, &OverviewPage::handleTransactionClicked);
164 
165  // start with displaying the "out of sync" warnings
166  showOutOfSyncWarning(true);
167  connect(ui->labelWalletStatus, &QPushButton::clicked, this, &OverviewPage::handleOutOfSyncWarningClicks);
168  connect(ui->labelTransactionsStatus, &QPushButton::clicked, this, &OverviewPage::handleOutOfSyncWarningClicks);
169 }
170 
171 void OverviewPage::handleTransactionClicked(const QModelIndex &index)
172 {
173  if(filter)
174  Q_EMIT transactionClicked(filter->mapToSource(index));
175 }
176 
178 {
179  Q_EMIT outOfSyncWarningClicked();
180 }
181 
182 void OverviewPage::setPrivacy(bool privacy)
183 {
184  m_privacy = privacy;
185  if (m_balances.balance != -1) {
187  }
188 
189  ui->listTransactions->setVisible(!m_privacy);
190 
191  const QString status_tip = m_privacy ? tr("Privacy mode activated for the Overview tab. To unmask the values, uncheck Settings->Mask values.") : "";
192  setStatusTip(status_tip);
193  QStatusTipEvent event(status_tip);
194  QApplication::sendEvent(this, &event);
195 }
196 
198 {
199  delete ui;
200 }
201 
203 {
204  int unit = walletModel->getOptionsModel()->getDisplayUnit();
205  m_balances = balances;
206  if (walletModel->wallet().isLegacy()) {
212  } else {
221  }
222  } else {
227  }
228  // only show immature (newly mined) balance if it's non-zero, so as not to complicate things
229  // for the non-mining users
230  bool showImmature = balances.immature_balance != 0;
231  bool showWatchOnlyImmature = balances.immature_watch_only_balance != 0;
232 
233  // for symmetry reasons also show immature label when the watch-only one is shown
234  ui->labelImmature->setVisible(showImmature || showWatchOnlyImmature);
235  ui->labelImmatureText->setVisible(showImmature || showWatchOnlyImmature);
236  ui->labelWatchImmature->setVisible(!walletModel->wallet().privateKeysDisabled() && showWatchOnlyImmature); // show watch-only immature balance
237 }
238 
239 // show/hide watch-only labels
240 void OverviewPage::updateWatchOnlyLabels(bool showWatchOnly)
241 {
242  ui->labelSpendable->setVisible(showWatchOnly); // show spendable label (only when watch-only is active)
243  ui->labelWatchonly->setVisible(showWatchOnly); // show watch-only label
244  ui->lineWatchBalance->setVisible(showWatchOnly); // show watch-only balance separator line
245  ui->labelWatchAvailable->setVisible(showWatchOnly); // show watch-only available balance
246  ui->labelWatchPending->setVisible(showWatchOnly); // show watch-only pending balance
247  ui->labelWatchTotal->setVisible(showWatchOnly); // show watch-only total balance
248 
249  if (!showWatchOnly)
250  ui->labelWatchImmature->hide();
251 }
252 
254 {
255  this->clientModel = model;
256  if (model) {
257  // Show warning, for example if this is a prerelease version
260 
263  }
264 }
265 
267 {
268  this->walletModel = model;
269  if(model && model->getOptionsModel())
270  {
271  // Set up transaction list
272  filter.reset(new TransactionFilterProxy());
273  filter->setSourceModel(model->getTransactionTableModel());
274  filter->setLimit(NUM_ITEMS);
275  filter->setDynamicSortFilter(true);
276  filter->setSortRole(Qt::EditRole);
277  filter->setShowInactive(false);
278  filter->sort(TransactionTableModel::Date, Qt::DescendingOrder);
279 
280  ui->listTransactions->setModel(filter.get());
281  ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress);
282 
283  // Keep up to date with wallet
284  interfaces::Wallet& wallet = model->wallet();
285  interfaces::WalletBalances balances = wallet.getBalances();
286  setBalance(balances);
287  connect(model, &WalletModel::balanceChanged, this, &OverviewPage::setBalance);
288 
290 
291  updateWatchOnlyLabels(wallet.haveWatchOnly() && !model->wallet().privateKeysDisabled());
292  connect(model, &WalletModel::notifyWatchonlyChanged, [this](bool showWatchOnly) {
294  });
295  }
296 
297  // update the display unit, to not use the default ("BTC")
299 }
300 
302 {
304  {
305  if (m_balances.balance != -1) {
307  }
308 
309  // Update txdelegate->unit with the current unit
311 
312  ui->listTransactions->update();
313  }
314 }
315 
316 void OverviewPage::updateAlerts(const QString &warnings)
317 {
318  this->ui->labelAlerts->setVisible(!warnings.isEmpty());
319  this->ui->labelAlerts->setText(warnings);
320 }
321 
323 {
324  ui->labelWalletStatus->setVisible(fShow);
325  ui->labelTransactionsStatus->setVisible(fShow);
326 }
327 
328 void OverviewPage::setMonospacedFont(bool use_embedded_font)
329 {
330  QFont f = GUIUtil::fixedPitchFont(use_embedded_font);
331  f.setWeight(QFont::Bold);
332  ui->labelBalance->setFont(f);
333  ui->labelUnconfirmed->setFont(f);
334  ui->labelImmature->setFont(f);
335  ui->labelTotal->setFont(f);
336  ui->labelWatchAvailable->setFont(f);
337  ui->labelWatchPending->setFont(f);
338  ui->labelWatchImmature->setFont(f);
339  ui->labelWatchTotal->setFont(f);
340 }
ClientModel::alertsChanged
void alertsChanged(const QString &warnings)
TxViewDelegate::m_minimum_width
std::map< int, int > m_minimum_width
Definition: overviewpage.cpp:136
WalletModel::getOptionsModel
OptionsModel * getOptionsModel()
Definition: walletmodel.cpp:281
transactiontablemodel.h
OverviewPage::OverviewPage
OverviewPage(const PlatformStyle *platformStyle, QWidget *parent=nullptr)
Definition: overviewpage.cpp:141
OverviewPage::~OverviewPage
~OverviewPage()
Definition: overviewpage.cpp:197
TransactionTableModel::Date
@ Date
Definition: transactiontablemodel.h:37
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:51
OverviewPage::outOfSyncWarningClicked
void outOfSyncWarningClicked()
BitcoinUnits::formatWithUnit
static QString formatWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD)
Format as string (with unit)
Definition: bitcoinunits.cpp:148
TxViewDelegate::unit
int unit
Definition: overviewpage.cpp:128
PlatformStyle::SingleColorIcon
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
Definition: platformstyle.cpp:102
walletmodel.h
DECORATION_SIZE
#define DECORATION_SIZE
Definition: overviewpage.cpp:28
OverviewPage::updateAlerts
void updateAlerts(const QString &warnings)
Definition: overviewpage.cpp:316
TransactionTableModel::WatchonlyRole
@ WatchonlyRole
Watch-only boolean.
Definition: transactiontablemodel.h:52
transactionoverviewwidget.h
OverviewPage::ui
Ui::OverviewPage * ui
Definition: overviewpage.h:49
interfaces::WalletBalances::unconfirmed_balance
CAmount unconfirmed_balance
Definition: wallet.h:352
OverviewPage
Overview ("home") page widget.
Definition: overviewpage.h:28
OptionsModel::displayUnitChanged
void displayUnitChanged(int unit)
TxViewDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Definition: overviewpage.cpp:44
WalletModel::getTransactionTableModel
TransactionTableModel * getTransactionTableModel()
Definition: walletmodel.cpp:291
wallet
Definition: interfaces.cpp:48
OverviewPage::txdelegate
TxViewDelegate * txdelegate
Definition: overviewpage.h:55
interfaces::WalletBalances::immature_watch_only_balance
CAmount immature_watch_only_balance
Definition: wallet.h:357
ClientModel::getStatusBarWarnings
QString getStatusBarWarnings() const
Return warnings to be displayed in status bar.
Definition: clientmodel.cpp:172
OverviewPage::filter
std::unique_ptr< TransactionFilterProxy > filter
Definition: overviewpage.h:56
interfaces::WalletBalances::balance
CAmount balance
Definition: wallet.h:351
COLOR_NEGATIVE
#define COLOR_NEGATIVE
Definition: guiconstants.h:27
overviewpage.h
OverviewPage::walletModel
WalletModel * walletModel
Definition: overviewpage.h:51
interfaces::WalletBalances::watch_only_balance
CAmount watch_only_balance
Definition: wallet.h:355
ClientModel::getOptionsModel
OptionsModel * getOptionsModel()
Definition: clientmodel.cpp:177
TxViewDelegate::TxViewDelegate
TxViewDelegate(const PlatformStyle *_platformStyle, QObject *parent=nullptr)
Definition: overviewpage.cpp:37
TxViewDelegate
Definition: overviewpage.cpp:33
TransactionTableModel::RawDecorationRole
@ RawDecorationRole
Unprocessed icon.
Definition: transactiontablemodel.h:76
TransactionTableModel::AmountRole
@ AmountRole
Net amount of transaction.
Definition: transactiontablemodel.h:62
WalletModel::wallet
interfaces::Wallet & wallet() const
Definition: walletmodel.h:146
interfaces::WalletBalances
Collection of wallet balances.
Definition: wallet.h:349
OverviewPage::m_privacy
bool m_privacy
Definition: overviewpage.h:53
TransactionTableModel::ToAddress
@ ToAddress
Definition: transactiontablemodel.h:39
OverviewPage::updateWatchOnlyLabels
void updateWatchOnlyLabels(bool showWatchOnly)
Definition: overviewpage.cpp:240
OverviewPage::setClientModel
void setClientModel(ClientModel *clientModel)
Definition: overviewpage.cpp:253
transactionfilterproxy.h
BitcoinUnits
Bitcoin unit definitions.
Definition: bitcoinunits.h:31
OverviewPage::setBalance
void setBalance(const interfaces::WalletBalances &balances)
Definition: overviewpage.cpp:202
guiutil.h
interfaces::Wallet
Interface for accessing a wallet.
Definition: wallet.h:52
OverviewPage::setMonospacedFont
void setMonospacedFont(bool use_embedded_font)
Definition: overviewpage.cpp:328
GUIUtil::fixedPitchFont
QFont fixedPitchFont(bool use_embedded_font)
Definition: guiutil.cpp:83
OverviewPage::updateDisplayUnit
void updateDisplayUnit()
Definition: overviewpage.cpp:301
OverviewPage::clientModel
ClientModel * clientModel
Definition: overviewpage.h:50
interfaces::WalletBalances::immature_balance
CAmount immature_balance
Definition: wallet.h:353
OverviewPage::transactionClicked
void transactionClicked(const QModelIndex &index)
BitcoinUnits::SeparatorStyle::ALWAYS
@ ALWAYS
platformstyle.h
ClientModel
Model for Bitcoin network client.
Definition: clientmodel.h:46
TxViewDelegate::sizeHint
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Definition: overviewpage.cpp:121
Ui
Definition: addressbookpage.h:14
guiconstants.h
TxViewDelegate::platformStyle
const PlatformStyle * platformStyle
Definition: overviewpage.cpp:135
OptionsModel::getDisplayUnit
int getDisplayUnit() const
Definition: optionsmodel.h:86
TransactionTableModel::WatchonlyDecorationRole
@ WatchonlyDecorationRole
Watch-only icon.
Definition: transactiontablemodel.h:54
OverviewPage::m_balances
interfaces::WalletBalances m_balances
Definition: overviewpage.h:52
OptionsModel::useEmbeddedMonospacedFontChanged
void useEmbeddedMonospacedFontChanged(bool)
OverviewPage::handleTransactionClicked
void handleTransactionClicked(const QModelIndex &index)
Definition: overviewpage.cpp:171
bitcoinunits.h
OverviewPage::setWalletModel
void setWalletModel(WalletModel *walletModel)
Definition: overviewpage.cpp:266
OverviewPage::setPrivacy
void setPrivacy(bool privacy)
Definition: overviewpage.cpp:182
interfaces::WalletBalances::unconfirmed_watch_only_balance
CAmount unconfirmed_watch_only_balance
Definition: wallet.h:356
GUIUtil::dateTimeStr
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:73
TransactionTableModel::DateRole
@ DateRole
Date and time this transaction was created.
Definition: transactiontablemodel.h:50
optionsmodel.h
BitcoinUnits::formatWithPrivacy
static QString formatWithPrivacy(int unit, const CAmount &amount, SeparatorStyle separators, bool privacy)
Format as string (with unit) of fixed length to preserve privacy, if it is set.
Definition: bitcoinunits.cpp:160
WalletModel::balanceChanged
void balanceChanged(const interfaces::WalletBalances &balances)
PlatformStyle
Definition: platformstyle.h:13
OverviewPage::showOutOfSyncWarning
void showOutOfSyncWarning(bool fShow)
Definition: overviewpage.cpp:322
interfaces::Wallet::privateKeysDisabled
virtual bool privateKeysDisabled()=0
WalletModel::notifyWatchonlyChanged
void notifyWatchonlyChanged(bool fHaveWatchonly)
TxViewDelegate::width_changed
void width_changed(const QModelIndex &index) const
An intermediate signal for emitting from the paint() const member function.
NUM_ITEMS
#define NUM_ITEMS
Definition: overviewpage.cpp:29
COLOR_UNCONFIRMED
#define COLOR_UNCONFIRMED
Definition: guiconstants.h:25
OptionsModel::getUseEmbeddedMonospacedFont
bool getUseEmbeddedMonospacedFont() const
Definition: optionsmodel.h:88
interfaces::Wallet::isLegacy
virtual bool isLegacy()=0
Return whether is a legacy wallet.
clientmodel.h
OverviewPage::handleOutOfSyncWarningClicks
void handleOutOfSyncWarningClicks()
Definition: overviewpage.cpp:177
TransactionTableModel::ConfirmedRole
@ ConfirmedRole
Is transaction confirmed?
Definition: transactiontablemodel.h:70
TransactionFilterProxy
Filter the transaction list according to pre-specified rules.
Definition: transactionfilterproxy.h:14