Bitcoin Core  27.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  clientModel->getOptionsModel()->setOption(OptionsModel::OptionID::MaskValues, privacy);
177  const auto& balances = walletModel->getCachedBalance();
178  if (balances.balance != -1) {
179  setBalance(balances);
180  }
181 
182  ui->listTransactions->setVisible(!m_privacy);
183 
184  const QString status_tip = m_privacy ? tr("Privacy mode activated for the Overview tab. To unmask the values, uncheck Settings->Mask values.") : "";
185  setStatusTip(status_tip);
186  QStatusTipEvent event(status_tip);
187  QApplication::sendEvent(this, &event);
188 }
189 
191 {
192  delete ui;
193 }
194 
196 {
198  if (walletModel->wallet().isLegacy()) {
204  } else {
213  }
214  } else {
219  }
220  // only show immature (newly mined) balance if it's non-zero, so as not to complicate things
221  // for the non-mining users
222  bool showImmature = balances.immature_balance != 0;
223  bool showWatchOnlyImmature = balances.immature_watch_only_balance != 0;
224 
225  // for symmetry reasons also show immature label when the watch-only one is shown
226  ui->labelImmature->setVisible(showImmature || showWatchOnlyImmature);
227  ui->labelImmatureText->setVisible(showImmature || showWatchOnlyImmature);
228  ui->labelWatchImmature->setVisible(!walletModel->wallet().privateKeysDisabled() && showWatchOnlyImmature); // show watch-only immature balance
229 }
230 
231 // show/hide watch-only labels
232 void OverviewPage::updateWatchOnlyLabels(bool showWatchOnly)
233 {
234  ui->labelSpendable->setVisible(showWatchOnly); // show spendable label (only when watch-only is active)
235  ui->labelWatchonly->setVisible(showWatchOnly); // show watch-only label
236  ui->lineWatchBalance->setVisible(showWatchOnly); // show watch-only balance separator line
237  ui->labelWatchAvailable->setVisible(showWatchOnly); // show watch-only available balance
238  ui->labelWatchPending->setVisible(showWatchOnly); // show watch-only pending balance
239  ui->labelWatchTotal->setVisible(showWatchOnly); // show watch-only total balance
240 
241  if (!showWatchOnly)
242  ui->labelWatchImmature->hide();
243 }
244 
246 {
247  this->clientModel = model;
248  if (model) {
249  // Show warning, for example if this is a prerelease version
252 
255  }
256 }
257 
259 {
260  this->walletModel = model;
261  if(model && model->getOptionsModel())
262  {
263  // Set up transaction list
264  filter.reset(new TransactionFilterProxy());
265  filter->setSourceModel(model->getTransactionTableModel());
266  filter->setDynamicSortFilter(true);
267  filter->setSortRole(Qt::EditRole);
268  filter->setShowInactive(false);
269  filter->sort(TransactionTableModel::Date, Qt::DescendingOrder);
270 
271  ui->listTransactions->setModel(filter.get());
272  ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress);
273 
274  connect(filter.get(), &TransactionFilterProxy::rowsInserted, this, &OverviewPage::LimitTransactionRows);
275  connect(filter.get(), &TransactionFilterProxy::rowsRemoved, this, &OverviewPage::LimitTransactionRows);
276  connect(filter.get(), &TransactionFilterProxy::rowsMoved, this, &OverviewPage::LimitTransactionRows);
278  // Keep up to date with wallet
279  setBalance(model->getCachedBalance());
280  connect(model, &WalletModel::balanceChanged, this, &OverviewPage::setBalance);
281 
283 
284  interfaces::Wallet& wallet = model->wallet();
285  updateWatchOnlyLabels(wallet.haveWatchOnly() && !wallet.privateKeysDisabled());
286  connect(model, &WalletModel::notifyWatchonlyChanged, [this](bool showWatchOnly) {
288  });
289  }
290 
291  // update the display unit, to not use the default ("BTC")
293 }
294 
296 {
297  if (e->type() == QEvent::PaletteChange) {
298  QIcon icon = m_platform_style->SingleColorIcon(QStringLiteral(":/icons/warning"));
299  ui->labelTransactionsStatus->setIcon(icon);
300  ui->labelWalletStatus->setIcon(icon);
301  }
302 
303  QWidget::changeEvent(e);
304 }
305 
306 // Only show most recent NUM_ITEMS rows
308 {
309  if (filter && ui->listTransactions && ui->listTransactions->model() && filter.get() == ui->listTransactions->model()) {
310  for (int i = 0; i < filter->rowCount(); ++i) {
311  ui->listTransactions->setRowHidden(i, i >= NUM_ITEMS);
312  }
313  }
314 }
315 
317 {
319  const auto& balances = walletModel->getCachedBalance();
320  if (balances.balance != -1) {
321  setBalance(balances);
322  }
323 
324  // Update txdelegate->unit with the current unit
326 
327  ui->listTransactions->update();
328  }
329 }
330 
331 void OverviewPage::updateAlerts(const QString &warnings)
332 {
333  this->ui->labelAlerts->setVisible(!warnings.isEmpty());
334  this->ui->labelAlerts->setText(warnings);
335 }
336 
338 {
339  ui->labelWalletStatus->setVisible(fShow);
340  ui->labelTransactionsStatus->setVisible(fShow);
341 }
342 
344 {
345  ui->labelBalance->setFont(f);
346  ui->labelUnconfirmed->setFont(f);
347  ui->labelImmature->setFont(f);
348  ui->labelTotal->setFont(f);
349  ui->labelWatchAvailable->setFont(f);
350  ui->labelWatchPending->setFont(f);
351  ui->labelWatchImmature->setFont(f);
352  ui->labelWatchTotal->setFont(f);
353 }
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()
QFont getFontForMoney() const
bool setOption(OptionID option, const QVariant &value, const std::string &suffix="")
void displayUnitChanged(BitcoinUnit unit)
BitcoinUnit getDisplayUnit() const
Definition: optionsmodel.h:104
void fontForMoneyChanged(const QFont &)
Overview ("home") page widget.
Definition: overviewpage.h:29
void setMonospacedFont(const QFont &)
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
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:48
void notifyWatchonlyChanged(bool fHaveWatchonly)
interfaces::Wallet & wallet() const
Definition: walletmodel.h:138
TransactionTableModel * getTransactionTableModel() const
OptionsModel * getOptionsModel() const
void balanceChanged(const interfaces::WalletBalances &balances)
interfaces::WalletBalances getCachedBalance() const
Interface for accessing a wallet.
Definition: wallet.h:61
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
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:92
#define DECORATION_SIZE
#define NUM_ITEMS
Collection of wallet balances.
Definition: wallet.h:372
CAmount unconfirmed_watch_only_balance
Definition: wallet.h:378
CAmount immature_watch_only_balance
Definition: wallet.h:379