Bitcoin ABC  0.24.7
P2P Digital Currency
modaloverlay.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016 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/forms/ui_modaloverlay.h>
6 #include <qt/modaloverlay.h>
7 
8 #include <chainparams.h>
9 #include <qt/guiutil.h>
10 
11 #include <QEasingCurve>
12 #include <QPropertyAnimation>
13 #include <QResizeEvent>
14 
15 ModalOverlay::ModalOverlay(bool enable_wallet, QWidget *parent)
16  : QWidget(parent), ui(new Ui::ModalOverlay), bestHeaderHeight(0),
17  bestHeaderDate(QDateTime()), layerIsVisible(false), userClosed(false) {
18  ui->setupUi(this);
19  connect(ui->closeButton, &QPushButton::clicked, this,
21  if (parent) {
22  parent->installEventFilter(this);
23  raise();
24  }
25 
26  blockProcessTime.clear();
27  setVisible(false);
28  if (!enable_wallet) {
29  ui->infoText->setVisible(false);
30  ui->infoTextStrong->setText(
31  tr("%1 is currently syncing. It will download headers "
32  "and blocks from peers and validate them until reaching the tip "
33  "of the block chain.")
34  .arg(PACKAGE_NAME));
35  }
36 
37  m_animation.setTargetObject(this);
38  m_animation.setPropertyName("pos");
39  m_animation.setDuration(300 /* ms */);
40  m_animation.setEasingCurve(QEasingCurve::OutQuad);
41 }
42 
44  delete ui;
45 }
46 
47 bool ModalOverlay::eventFilter(QObject *obj, QEvent *ev) {
48  if (obj == parent()) {
49  if (ev->type() == QEvent::Resize) {
50  QResizeEvent *rev = static_cast<QResizeEvent *>(ev);
51  resize(rev->size());
52  if (!layerIsVisible) {
53  setGeometry(0, height(), width(), height());
54  }
55 
56  if (m_animation.endValue().toPoint().y() > 0) {
57  m_animation.setEndValue(QPoint(0, height()));
58  }
59  } else if (ev->type() == QEvent::ChildAdded) {
60  raise();
61  }
62  }
63  return QWidget::eventFilter(obj, ev);
64 }
65 
67 bool ModalOverlay::event(QEvent *ev) {
68  if (ev->type() == QEvent::ParentAboutToChange) {
69  if (parent()) {
70  parent()->removeEventFilter(this);
71  }
72  } else if (ev->type() == QEvent::ParentChange) {
73  if (parent()) {
74  parent()->installEventFilter(this);
75  raise();
76  }
77  }
78  return QWidget::event(ev);
79 }
80 
81 void ModalOverlay::setKnownBestHeight(int count, const QDateTime &blockDate) {
82  if (count > bestHeaderHeight) {
84  bestHeaderDate = blockDate;
86  }
87 }
88 
89 void ModalOverlay::tipUpdate(int count, const QDateTime &blockDate,
90  double nVerificationProgress) {
91  QDateTime currentDate = QDateTime::currentDateTime();
92 
93  // keep a vector of samples of verification progress at height
94  blockProcessTime.push_front(
95  qMakePair(currentDate.toMSecsSinceEpoch(), nVerificationProgress));
96 
97  // show progress speed if we have more than one sample
98  if (blockProcessTime.size() >= 2) {
99  double progressDelta = 0;
100  double progressPerHour = 0;
101  qint64 timeDelta = 0;
102  qint64 remainingMSecs = 0;
103  double remainingProgress = 1.0 - nVerificationProgress;
104  for (int i = 1; i < blockProcessTime.size(); i++) {
105  QPair<qint64, double> sample = blockProcessTime[i];
106 
107  // take first sample after 500 seconds or last available one
108  if (sample.first < (currentDate.toMSecsSinceEpoch() - 500 * 1000) ||
109  i == blockProcessTime.size() - 1) {
110  progressDelta = blockProcessTime[0].second - sample.second;
111  timeDelta = blockProcessTime[0].first - sample.first;
112  progressPerHour =
113  progressDelta / (double)timeDelta * 1000 * 3600;
114  remainingMSecs =
115  (progressDelta > 0)
116  ? remainingProgress / progressDelta * timeDelta
117  : -1;
118  break;
119  }
120  }
121  // show progress increase per hour
122  ui->progressIncreasePerH->setText(
123  QString::number(progressPerHour * 100, 'f', 2) + "%");
124 
125  // show expected remaining time
126  if (remainingMSecs >= 0) {
127  ui->expectedTimeLeft->setText(
128  GUIUtil::formatNiceTimeOffset(remainingMSecs / 1000.0));
129  } else {
130  ui->expectedTimeLeft->setText(QObject::tr("unknown"));
131  }
132 
133  static const int MAX_SAMPLES = 5000;
134  if (blockProcessTime.count() > MAX_SAMPLES) {
135  blockProcessTime.remove(MAX_SAMPLES,
136  blockProcessTime.count() - MAX_SAMPLES);
137  }
138  }
139 
140  // show the last block date
141  ui->newestBlockDate->setText(blockDate.toString());
142 
143  // show the percentage done according to nVerificationProgress
144  ui->percentageProgress->setText(
145  QString::number(nVerificationProgress * 100, 'f', 2) + "%");
146  ui->progressBar->setValue(nVerificationProgress * 100);
147 
148  if (!bestHeaderDate.isValid()) {
149  // not syncing
150  return;
151  }
152 
153  // estimate the number of headers left based on nPowTargetSpacing
154  // and check if the gui is not aware of the best header (happens rarely)
155  int estimateNumHeadersLeft = bestHeaderDate.secsTo(currentDate) /
157  bool hasBestHeader = bestHeaderHeight >= count;
158 
159  // show remaining number of blocks
160  if (estimateNumHeadersLeft < HEADER_HEIGHT_DELTA_SYNC && hasBestHeader) {
161  ui->numberOfBlocksLeft->setText(
162  QString::number(bestHeaderHeight - count));
163  } else {
165  ui->expectedTimeLeft->setText(tr("Unknown..."));
166  }
167 }
168 
170  int est_headers_left = bestHeaderDate.secsTo(QDateTime::currentDateTime()) /
172  ui->numberOfBlocksLeft->setText(
173  tr("Unknown. Syncing Headers (%1, %2%)...")
174  .arg(bestHeaderHeight)
175  .arg(QString::number(100.0 / (bestHeaderHeight + est_headers_left) *
177  'f', 1)));
178 }
179 
181  showHide(layerIsVisible, true);
182  if (!layerIsVisible) {
183  userClosed = true;
184  }
185 }
186 
187 void ModalOverlay::showHide(bool hide, bool userRequested) {
188  if ((layerIsVisible && !hide) || (!layerIsVisible && hide) ||
189  (!hide && userClosed && !userRequested)) {
190  return;
191  }
192 
193  Q_EMIT triggered(hide);
194 
195  if (!isVisible() && !hide) {
196  setVisible(true);
197  }
198 
199  m_animation.setStartValue(QPoint(0, hide ? 0 : height()));
200  // The eventFilter() updates the endValue if it is required for
201  // QEvent::Resize.
202  m_animation.setEndValue(QPoint(0, hide ? height() : 0));
203  m_animation.start(QAbstractAnimation::KeepWhenStopped);
204  layerIsVisible = !hide;
205 }
206 
208  showHide(true);
209  userClosed = true;
210 }
ModalOverlay::closeClicked
void closeClicked()
Definition: modaloverlay.cpp:207
ModalOverlay::UpdateHeaderSyncLabel
void UpdateHeaderSyncLabel()
Definition: modaloverlay.cpp:169
ModalOverlay
Modal overlay to display information about the chain-sync state.
Definition: modaloverlay.h:21
count
static int count
Definition: tests.c:41
ModalOverlay::triggered
void triggered(bool hidden)
ModalOverlay::bestHeaderDate
QDateTime bestHeaderDate
Definition: modaloverlay.h:50
CChainParams::GetConsensus
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:59
chainparams.h
Consensus::Params::nPowTargetSpacing
int64_t nPowTargetSpacing
Definition: params.h:108
GUIUtil::formatNiceTimeOffset
QString formatNiceTimeOffset(qint64 secs)
Definition: guiutil.cpp:830
ModalOverlay::ui
Ui::ModalOverlay * ui
Definition: modaloverlay.h:48
ModalOverlay::m_animation
QPropertyAnimation m_animation
Definition: modaloverlay.h:54
guiutil.h
HEADER_HEIGHT_DELTA_SYNC
static constexpr int HEADER_HEIGHT_DELTA_SYNC
The required delta of headers to the estimated number of available headers until we show the IBD prog...
Definition: modaloverlay.h:14
ModalOverlay::eventFilter
bool eventFilter(QObject *obj, QEvent *ev) override
Definition: modaloverlay.cpp:47
ModalOverlay::layerIsVisible
bool layerIsVisible
Definition: modaloverlay.h:52
ModalOverlay::bestHeaderHeight
int bestHeaderHeight
Definition: modaloverlay.h:49
ModalOverlay::toggleVisibility
void toggleVisibility()
Definition: modaloverlay.cpp:180
Ui
Definition: addressbookpage.h:14
ModalOverlay::event
bool event(QEvent *ev) override
Tracks parent widget changes.
Definition: modaloverlay.cpp:67
ModalOverlay::userClosed
bool userClosed
Definition: modaloverlay.h:53
ModalOverlay::setKnownBestHeight
void setKnownBestHeight(int count, const QDateTime &blockDate)
Definition: modaloverlay.cpp:81
ModalOverlay::~ModalOverlay
~ModalOverlay()
Definition: modaloverlay.cpp:43
ModalOverlay::ModalOverlay
ModalOverlay(bool enable_wallet, QWidget *parent)
Definition: modaloverlay.cpp:15
ModalOverlay::showHide
void showHide(bool hide=false, bool userRequested=false)
Definition: modaloverlay.cpp:187
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:508
ModalOverlay::tipUpdate
void tipUpdate(int count, const QDateTime &blockDate, double nVerificationProgress)
Definition: modaloverlay.cpp:89
ModalOverlay::blockProcessTime
QVector< QPair< qint64, double > > blockProcessTime
Definition: modaloverlay.h:51
modaloverlay.h