Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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/modaloverlay.h>
6 #include <qt/forms/ui_modaloverlay.h>
7 
8 #include <qt/guiutil.h>
9 
10 #include <chainparams.h>
11 
12 #include <QResizeEvent>
13 #include <QPropertyAnimation>
14 
15 ModalOverlay::ModalOverlay(QWidget *parent) :
16 QWidget(parent),
17 ui(new Ui::ModalOverlay),
18 bestHeaderHeight(0),
19 bestHeaderDate(QDateTime()),
20 layerIsVisible(false),
21 userClosed(false),
22 foreverHidden(false)
23 {
24  ui->setupUi(this);
25 
26  GUIUtil::setFont({ui->infoTextStrong,
27  ui->labelNumberOfBlocksLeft,
28  ui->labelLastBlockTime,
29  ui->labelSyncDone,
30  ui->labelProgressIncrease,
31  ui->labelEstimatedTimeLeft,
33 
34  ui->warningIcon->setPixmap(GUIUtil::getIcon("warning", GUIUtil::ThemedColor::ORANGE).pixmap(48, 48));
35 
36  connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(closeClicked()));
37  if (parent) {
38  parent->installEventFilter(this);
39  raise();
40  }
41 
42  blockProcessTime.clear();
43  setVisible(false);
44 
46 }
47 
49 {
50  delete ui;
51 }
52 
53 bool ModalOverlay::eventFilter(QObject * obj, QEvent * ev) {
54  if (obj == parent()) {
55  if (ev->type() == QEvent::Resize) {
56  QResizeEvent * rev = static_cast<QResizeEvent*>(ev);
57  resize(rev->size());
58  if (!layerIsVisible)
59  setGeometry(0, height(), width(), height());
60 
61  }
62  else if (ev->type() == QEvent::ChildAdded) {
63  raise();
64  }
65  }
66  return QWidget::eventFilter(obj, ev);
67 }
68 
70 bool ModalOverlay::event(QEvent* ev) {
71  if (ev->type() == QEvent::ParentAboutToChange) {
72  if (parent()) parent()->removeEventFilter(this);
73  }
74  else if (ev->type() == QEvent::ParentChange) {
75  if (parent()) {
76  parent()->installEventFilter(this);
77  raise();
78  }
79  }
80  return QWidget::event(ev);
81 }
82 
83 void ModalOverlay::setKnownBestHeight(int count, const QDateTime& blockDate)
84 {
85  if (count > bestHeaderHeight) {
87  bestHeaderDate = blockDate;
88  }
89 }
90 
91 void ModalOverlay::tipUpdate(int count, const QDateTime& blockDate, double nVerificationProgress)
92 {
93  QDateTime currentDate = QDateTime::currentDateTime();
94 
95  // keep a vector of samples of verification progress at height
96  blockProcessTime.push_front(qMakePair(currentDate.toMSecsSinceEpoch(), nVerificationProgress));
97 
98  // show progress speed if we have more then one sample
99  if (blockProcessTime.size() >= 2) {
100  double progressDelta = 0;
101  double progressPerHour = 0;
102  qint64 timeDelta = 0;
103  qint64 remainingMSecs = 0;
104  double remainingProgress = 1.0 - nVerificationProgress;
105  for (int i = 1; i < blockProcessTime.size(); i++) {
106  QPair<qint64, double> sample = blockProcessTime[i];
107 
108  // take first sample after 500 seconds or last available one
109  if (sample.first < (currentDate.toMSecsSinceEpoch() - 500 * 1000) || i == blockProcessTime.size() - 1) {
110  progressDelta = blockProcessTime[0].second - sample.second;
111  timeDelta = blockProcessTime[0].first - sample.first;
112  progressPerHour = progressDelta / (double) timeDelta * 1000 * 3600;
113  remainingMSecs = (progressDelta > 0) ? remainingProgress / progressDelta * timeDelta : -1;
114  break;
115  }
116  }
117  // show progress increase per hour
118  ui->progressIncreasePerH->setText(QString::number(progressPerHour * 100, 'f', 2)+"%");
119 
120  // show expected remaining time
121  if(remainingMSecs >= 0) {
122  ui->expectedTimeLeft->setText(GUIUtil::formatNiceTimeOffset(remainingMSecs / 1000.0));
123  } else {
124  ui->expectedTimeLeft->setText(QObject::tr("unknown"));
125  }
126 
127  static const int MAX_SAMPLES = 5000;
128  if (blockProcessTime.count() > MAX_SAMPLES) {
129  blockProcessTime.remove(MAX_SAMPLES, blockProcessTime.count() - MAX_SAMPLES);
130  }
131  }
132 
133  // show the last block date
134  ui->newestBlockDate->setText(blockDate.toString());
135 
136  // show the percentage done according to nVerificationProgress
137  ui->percentageProgress->setText(QString::number(nVerificationProgress*100, 'f', 2)+"%");
138  ui->progressBar->setValue(nVerificationProgress*100);
139 
140  if (!bestHeaderDate.isValid())
141  // not syncing
142  return;
143 
144  // estimate the number of headers left based on nPowTargetSpacing
145  // and check if the gui is not aware of the best header (happens rarely)
146  int estimateNumHeadersLeft = bestHeaderDate.secsTo(currentDate) / Params().GetConsensus().nPowTargetSpacing;
147  bool hasBestHeader = bestHeaderHeight >= count;
148 
149  // show remaining number of blocks
150  if (estimateNumHeadersLeft < HEADER_HEIGHT_DELTA_SYNC && hasBestHeader) {
151  ui->numberOfBlocksLeft->setText(QString::number(bestHeaderHeight - count));
152  } else {
153  ui->numberOfBlocksLeft->setText(tr("Unknown. Syncing Headers (%1)...").arg(bestHeaderHeight));
154  ui->expectedTimeLeft->setText(tr("Unknown..."));
155  }
156 }
157 
159 {
160  showHide(layerIsVisible, true);
161  if (!layerIsVisible)
162  userClosed = true;
163 }
164 
165 void ModalOverlay::showHide(bool hide, bool userRequested)
166 {
167  if ( (layerIsVisible && !hide) || (!layerIsVisible && hide) || (!hide && userClosed && !userRequested))
168  return;
169 
170  if (!hide && foreverHidden)
171  return;
172 
173  if (!isVisible() && !hide)
174  setVisible(true);
175 
176  setGeometry(0, hide ? 0 : height(), width(), height());
177 
178  QPropertyAnimation* animation = new QPropertyAnimation(this, "pos");
179  animation->setDuration(300);
180  animation->setStartValue(QPoint(0, hide ? 0 : this->height()));
181  animation->setEndValue(QPoint(0, hide ? this->height() : 0));
182  animation->setEasingCurve(QEasingCurve::OutQuad);
183  animation->start(QAbstractAnimation::DeleteWhenStopped);
184  layerIsVisible = !hide;
185 }
186 
188 {
189  showHide(true);
190  userClosed = true;
191 }
192 
194 {
195  foreverHidden = true;
196 }
ModalOverlay(QWidget *parent)
void setFont(const std::vector< QWidget *> &vecWidgets, FontWeight weight, int nPointSize, bool fItalic)
Workaround to set correct font styles in all themes since there is a bug in macOS which leads to issu...
Definition: guiutil.cpp:1552
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:12
bool event(QEvent *ev)
Tracks parent widget changes.
bool eventFilter(QObject *obj, QEvent *ev)
void tipUpdate(int count, const QDateTime &blockDate, double nVerificationProgress)
Modal overlay to display information about the chain-sync state.
Definition: modaloverlay.h:19
Ui::ModalOverlay * ui
Definition: modaloverlay.h:43
bool foreverHidden
Definition: modaloverlay.h:49
void updateFonts()
Update the font of all widgets where a custom font has been set with GUIUtil::setFont.
Definition: guiutil.cpp:1563
false
Definition: bls_dkg.cpp:168
void setKnownBestHeight(int count, const QDateTime &blockDate)
int64_t nPowTargetSpacing
Definition: params.h:176
void toggleVisibility()
QIcon getIcon(const QString &strIcon, const ThemedColor color, const ThemedColor colorAlternative, const QString &strIconPath)
Helper to get an icon colorized with the given color (replaces black) and colorAlternative (replaces ...
Definition: guiutil.cpp:216
QDateTime bestHeaderDate
Definition: modaloverlay.h:45
bool layerIsVisible
Definition: modaloverlay.h:47
int bestHeaderHeight
Definition: modaloverlay.h:44
void showHide(bool hide=false, bool userRequested=false)
void hideForever()
const CChainParams & Params()
Return the currently selected parameters.
static int count
Definition: tests.c:45
QVector< QPair< qint64, double > > blockProcessTime
Definition: modaloverlay.h:46
QString formatNiceTimeOffset(qint64 secs)
Definition: guiutil.cpp:1898
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:54
void closeClicked()
Released under the MIT license