Bitcoin Core  24.99.0
P2P Digital Currency
overviewpage.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2022 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), platformStyle(_platformStyle)
39  {
40  connect(this, &TxViewDelegate::width_changed, this, &TxViewDelegate::sizeHintChanged);
41  }
42 
43  inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
44  const QModelIndex &index ) const override
45  {
46  painter->save();
47 
48  QIcon icon = qvariant_cast<QIcon>(index.data(TransactionTableModel::RawDecorationRole));
49  QRect mainRect = option.rect;
50  QRect decorationRect(mainRect.topLeft(), QSize(DECORATION_SIZE, DECORATION_SIZE));
51  int xspace = DECORATION_SIZE + 8;
52  int ypad = 6;
53  int halfheight = (mainRect.height() - 2*ypad)/2;
54  QRect amountRect(mainRect.left() + xspace, mainRect.top()+ypad, mainRect.width() - xspace, halfheight);
55  QRect addressRect(mainRect.left() + xspace, mainRect.top()+ypad+halfheight, mainRect.width() - xspace, halfheight);
56  icon = platformStyle->SingleColorIcon(icon);
57  icon.paint(painter, decorationRect);
58 
59  QDateTime date = index.data(TransactionTableModel::DateRole).toDateTime();
60  QString address = index.data(Qt::DisplayRole).toString();
61  qint64 amount = index.data(TransactionTableModel::AmountRole).toLongLong();
62  bool confirmed = index.data(TransactionTableModel::ConfirmedRole).toBool();
63  QVariant value = index.data(Qt::ForegroundRole);
64  QColor foreground = option.palette.color(QPalette::Text);
65  if(value.canConvert<QBrush>())
66  {
67  QBrush brush = qvariant_cast<QBrush>(value);
68  foreground = brush.color();
69  }
70 
71  if (index.data(TransactionTableModel::WatchonlyRole).toBool()) {
72  QIcon iconWatchonly = qvariant_cast<QIcon>(index.data(TransactionTableModel::WatchonlyDecorationRole));
73  QRect watchonlyRect(addressRect.left(), addressRect.top(), 16, addressRect.height());
74  iconWatchonly = platformStyle->TextColorIcon(iconWatchonly);
75  iconWatchonly.paint(painter, watchonlyRect);
76  addressRect.setLeft(addressRect.left() + watchonlyRect.width() + 5);
77  }
78 
79  painter->setPen(foreground);
80  QRect boundingRect;
81  painter->drawText(addressRect, Qt::AlignLeft | Qt::AlignVCenter, address, &boundingRect);
82 
83  if(amount < 0)
84  {
85  foreground = COLOR_NEGATIVE;
86  }
87  else if(!confirmed)
88  {
89  foreground = COLOR_UNCONFIRMED;
90  }
91  else
92  {
93  foreground = option.palette.color(QPalette::Text);
94  }
95  painter->setPen(foreground);
96  QString amountText = BitcoinUnits::formatWithUnit(unit, amount, true, BitcoinUnits::SeparatorStyle::ALWAYS);
97  if(!confirmed)
98  {
99  amountText = QString("[") + amountText + QString("]");
100  }
101 
102  QRect amount_bounding_rect;
103  painter->drawText(amountRect, Qt::AlignRight | Qt::AlignVCenter, amountText, &amount_bounding_rect);
104 
105  painter->setPen(option.palette.color(QPalette::Text));
106  QRect date_bounding_rect;
107  painter->drawText(amountRect, Qt::AlignLeft | Qt::AlignVCenter, GUIUtil::dateTimeStr(date), &date_bounding_rect);
108 
109  // 0.4*date_bounding_rect.width() is used to visually distinguish a date from an amount.
110  const int minimum_width = 1.4 * date_bounding_rect.width() + amount_bounding_rect.width();
111  const auto search = m_minimum_width.find(index.row());
112  if (search == m_minimum_width.end() || search->second != minimum_width) {
113  m_minimum_width[index.row()] = minimum_width;
114  Q_EMIT width_changed(index);
115  }
116 
117  painter->restore();
118  }
119 
120  inline QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
121  {
122  const auto search = m_minimum_width.find(index.row());
123  const int minimum_text_width = search == m_minimum_width.end() ? 0 : search->second;
124  return {DECORATION_SIZE + 8 + minimum_text_width, DECORATION_SIZE};
125  }
126 
128 
129 Q_SIGNALS:
131  void width_changed(const QModelIndex& index) const;
132 
133 private:
135  mutable std::map<int, int> m_minimum_width;
136 };
137 
138 #include <qt/overviewpage.moc>
139 
140 OverviewPage::OverviewPage(const PlatformStyle *platformStyle, QWidget *parent) :
141  QWidget(parent),
142  ui(new Ui::OverviewPage),
143  m_platform_style{platformStyle},
144  txdelegate(new TxViewDelegate(platformStyle, this))
145 {
146  ui->setupUi(this);
147 
148  // use a SingleColorIcon for the "out of sync warning" icon
149  QIcon icon = m_platform_style->SingleColorIcon(QStringLiteral(":/icons/warning"));
150  ui->labelTransactionsStatus->setIcon(icon);
151  ui->labelWalletStatus->setIcon(icon);
152 
153  // Recent transactions
154  ui->listTransactions->setItemDelegate(txdelegate);
155  ui->listTransactions->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE));
156  ui->listTransactions->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2));
157  ui->listTransactions->setAttribute(Qt::WA_MacShowFocusRect, false);
158 
159  connect(ui->listTransactions, &TransactionOverviewWidget::clicked, this, &OverviewPage::handleTransactionClicked);
160 
161  // start with displaying the "out of sync" warnings
162  showOutOfSyncWarning(true);
163  connect(ui->labelWalletStatus, &QPushButton::clicked, this, &OverviewPage::outOfSyncWarningClicked);
164  connect(ui->labelTransactionsStatus, &QPushButton::clicked, this, &OverviewPage::outOfSyncWarningClicked);
165 }
166 
167 void OverviewPage::handleTransactionClicked(const QModelIndex &index)
168 {
169  if(filter)
170  Q_EMIT transactionClicked(filter->mapToSource(index));
171 }
172 
173 void OverviewPage::setPrivacy(bool privacy)
174 {
175  m_privacy = privacy;
176  const auto& balances = walletModel->getCachedBalance();
177  if (balances.balance != -1) {
178  setBalance(balances);
179  }
180 
181  ui->listTransactions->setVisible(!m_privacy);
182 
183  const QString status_tip = m_privacy ? tr("Privacy mode activated for the Overview tab. To unmask the values, uncheck Settings->Mask values.") : "";
184  setStatusTip(status_tip);
185  QStatusTipEvent event(status_tip);
186  QApplication::sendEvent(this, &event);
187 }
188 
190 {
191  delete ui;
192 }
193 
195 {
197  if (walletModel->wallet().isLegacy()) {
203  } else {
212  }
213  } else {
218  }
219  // only show immature (newly mined) balance if it's non-zero, so as not to complicate things
220  // for the non-mining users
221  bool showImmature = balances.immature_balance != 0;
222  bool showWatchOnlyImmature = balances.immature_watch_only_balance != 0;
223 
224  // for symmetry reasons also show immature label when the watch-only one is shown
225  ui->labelImmature->setVisible(showImmature || showWatchOnlyImmature);
226  ui->labelImmatureText->setVisible(showImmature || showWatchOnlyImmature);
227  ui->labelWatchImmature->setVisible(!walletModel->wallet().privateKeysDisabled() && showWatchOnlyImmature); // show watch-only immature balance
228 }
229 
230 // show/hide watch-only labels
231 void OverviewPage::updateWatchOnlyLabels(bool showWatchOnly)
232 {
233  ui->labelSpendable->setVisible(showWatchOnly); // show spendable label (only when watch-only is active)
234  ui->labelWatchonly->setVisible(showWatchOnly); // show watch-only label
235  ui->lineWatchBalance->setVisible(showWatchOnly); // show watch-only balance separator line
236  ui->labelWatchAvailable->setVisible(showWatchOnly); // show watch-only available balance
237  ui->labelWatchPending->setVisible(showWatchOnly); // show watch-only pending balance
238  ui->labelWatchTotal->setVisible(showWatchOnly); // show watch-only total balance
239 
240  if (!showWatchOnly)
241  ui->labelWatchImmature->hide();
242 }
243 
245 {
246  this->clientModel = model;
247  if (model) {
248  // Show warning, for example if this is a prerelease version
251 
254  }
255 }
256 
258 {
259  this->walletModel = model;
260  if(model && model->getOptionsModel())
261  {
262  // Set up transaction list
263  filter.reset(new TransactionFilterProxy());
264  filter->setSourceModel(model->getTransactionTableModel());
265  filter->setDynamicSortFilter(true);
266  filter->setSortRole(Qt::EditRole);
267  filter->setShowInactive(false);
268  filter->sort(TransactionTableModel::Date, Qt::DescendingOrder);
269 
270  ui->listTransactions->setModel(filter.get());
271  ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress);
272 
273  connect(filter.get(), &TransactionFilterProxy::rowsInserted, this, &OverviewPage::LimitTransactionRows);
274  connect(filter.get(), &TransactionFilterProxy::rowsRemoved, this, &OverviewPage::LimitTransactionRows);
275  connect(filter.get(), &TransactionFilterProxy::rowsMoved, this, &OverviewPage::LimitTransactionRows);
277  // Keep up to date with wallet
278  setBalance(model->getCachedBalance());
279  connect(model, &WalletModel::balanceChanged, this, &OverviewPage::setBalance);
280 
282 
283  interfaces::Wallet& wallet = model->wallet();
284  updateWatchOnlyLabels(wallet.haveWatchOnly() && !wallet.privateKeysDisabled());
285  connect(model, &WalletModel::notifyWatchonlyChanged, [this](bool showWatchOnly) {
287  });
288  }
289 
290  // update the display unit, to not use the default ("BTC")
292 }
293 
295 {
296  if (e->type() == QEvent::PaletteChange) {
297  QIcon icon = m_platform_style->SingleColorIcon(QStringLiteral(":/icons/warning"));
298  ui->labelTransactionsStatus->setIcon(icon);
299  ui->labelWalletStatus->setIcon(icon);
300  }
301 
302  QWidget::changeEvent(e);
303 }
304 
305 // Only show most recent NUM_ITEMS rows
307 {
308  if (filter && ui->listTransactions && ui->listTransactions->model() && filter.get() == ui->listTransactions->model()) {
309  for (int i = 0; i < filter->rowCount(); ++i) {
310  ui->listTransactions->setRowHidden(i, i >= NUM_ITEMS);
311  }
312  }
313 }
314 
316 {
318  const auto& balances = walletModel->getCachedBalance();
319  if (balances.balance != -1) {
320  setBalance(balances);
321  }
322 
323  // Update txdelegate->unit with the current unit
325 
326  ui->listTransactions->update();
327  }
328 }
329 
330 void OverviewPage::updateAlerts(const QString &warnings)
331 {
332  this->ui->labelAlerts->setVisible(!warnings.isEmpty());
333  this->ui->labelAlerts->setText(warnings);
334 }
335 
337 {
338  ui->labelWalletStatus->setVisible(fShow);
339  ui->labelTransactionsStatus->setVisible(fShow);
340 }
341 
342 void OverviewPage::setMonospacedFont(bool use_embedded_font)
343 {
344  QFont f = GUIUtil::fixedPitchFont(use_embedded_font);
345  f.setWeight(QFont::Bold);
346  ui->labelBalance->setFont(f);
347  ui->labelUnconfirmed->setFont(f);
348  ui->labelImmature->setFont(f);
349  ui->labelTotal->setFont(f);
350  ui->labelWatchAvailable->setFont(f);
351  ui->labelWatchPending->setFont(f);
352  ui->labelWatchImmature->setFont(f);
353  ui->labelWatchTotal->setFont(f);
354 }
static QString formatWithPrivacy(Unit unit, const CAmount &amount, SeparatorStyle separators, bool privacy)
Format as string (with unit) of fixed length to preserve privacy, if it is set.
static QString formatWithUnit(Unit unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD)
Format as string (with unit)
Unit
Bitcoin units.
Definition: bitcoinunits.h:42
Model for Bitcoin network client.
Definition: clientmodel.h:54
QString getStatusBarWarnings() const
Return warnings to be displayed in status bar.
void alertsChanged(const QString &warnings)
OptionsModel * getOptionsModel()
bool getUseEmbeddedMonospacedFont() const
Definition: optionsmodel.h:95
void displayUnitChanged(BitcoinUnit unit)
BitcoinUnit getDisplayUnit() const
Definition: optionsmodel.h:93
void useEmbeddedMonospacedFontChanged(bool)
Overview ("home") page widget.
Definition: overviewpage.h:29
void updateDisplayUnit()
const PlatformStyle * m_platform_style
Definition: overviewpage.h:57
void setWalletModel(WalletModel *walletModel)
void updateAlerts(const QString &warnings)
void updateWatchOnlyLabels(bool showWatchOnly)
void setClientModel(ClientModel *clientModel)
WalletModel * walletModel
Definition: overviewpage.h:54
void setMonospacedFont(bool use_embedded_font)
Ui::OverviewPage * ui
Definition: overviewpage.h:52
void LimitTransactionRows()
void handleTransactionClicked(const QModelIndex &index)
void changeEvent(QEvent *e) override
void transactionClicked(const QModelIndex &index)
OverviewPage(const PlatformStyle *platformStyle, QWidget *parent=nullptr)
std::unique_ptr< TransactionFilterProxy > filter
Definition: overviewpage.h:60
void outOfSyncWarningClicked()
void showOutOfSyncWarning(bool fShow)
ClientModel * clientModel
Definition: overviewpage.h:53
void setBalance(const interfaces::WalletBalances &balances)
TxViewDelegate * txdelegate
Definition: overviewpage.h:59
void setPrivacy(bool privacy)
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
Filter the transaction list according to pre-specified rules.
@ DateRole
Date and time this transaction was created.
@ RawDecorationRole
Unprocessed icon.
@ WatchonlyDecorationRole
Watch-only icon.
@ WatchonlyRole
Watch-only boolean.
@ AmountRole
Net amount of transaction.
@ ConfirmedRole
Is transaction confirmed?
TxViewDelegate(const PlatformStyle *_platformStyle, QObject *parent=nullptr)
std::map< int, int > m_minimum_width
BitcoinUnit unit
void width_changed(const QModelIndex &index) const
An intermediate signal for emitting from the paint() const member function.
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
const PlatformStyle * platformStyle
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:53
void notifyWatchonlyChanged(bool fHaveWatchonly)
interfaces::Wallet & wallet() const
Definition: walletmodel.h:145
TransactionTableModel * getTransactionTableModel() const
OptionsModel * getOptionsModel() const
void balanceChanged(const interfaces::WalletBalances &balances)
interfaces::WalletBalances getCachedBalance() const
Interface for accessing a wallet.
Definition: wallet.h:58
virtual bool isLegacy()=0
Return whether is a legacy wallet.
virtual bool privateKeysDisabled()=0
#define COLOR_UNCONFIRMED
Definition: guiconstants.h:31
#define COLOR_NEGATIVE
Definition: guiconstants.h:33
QFont fixedPitchFont(bool use_embedded_font)
Definition: guiutil.cpp:96
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:86
Definition: node.h:39
#define DECORATION_SIZE
#define NUM_ITEMS
Collection of wallet balances.
Definition: wallet.h:366
CAmount unconfirmed_watch_only_balance
Definition: wallet.h:372
CAmount immature_watch_only_balance
Definition: wallet.h:373