Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

splashscreen.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 The Bitcoin Core developers
2 // Copyright (c) 2014-2019 The Dash Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #if defined(HAVE_CONFIG_H)
7 #include <config/dash-config.h>
8 #endif
9 
10 #include <qt/splashscreen.h>
11 
12 #include <qt/guiutil.h>
13 #include <qt/networkstyle.h>
14 
15 #include <chainparams.h>
16 #include <clientversion.h>
17 #include <init.h>
18 #include <util.h>
19 #include <ui_interface.h>
20 #include <version.h>
21 #ifdef ENABLE_WALLET
22 #include <wallet/wallet.h>
23 #endif
24 
25 #include <QApplication>
26 #include <QCloseEvent>
27 #include <QDesktopWidget>
28 #include <QPainter>
29 
30 SplashScreen::SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle) :
31  QWidget(0, f), curAlignment(0)
32 {
33 
34  // transparent background
35  setAttribute(Qt::WA_TranslucentBackground);
36  setStyleSheet("background:transparent;");
37 
38  // no window decorations
39  setWindowFlags(Qt::FramelessWindowHint);
40 
41  // Geometries of splashscreen
42  int width = 380;
43  int height = 460;
44  int logoWidth = 270;
45  int logoHeight = 270;
46 
47  // set reference point, paddings
48  int paddingTop = 10;
49  int titleVersionVSpace = 25;
50 
51  float fontFactor = 1.0;
52  float scale = qApp->devicePixelRatio();
53 
54  // define text to place
55  QString titleText = tr(PACKAGE_NAME);
56  QString versionText = QString::fromStdString(FormatFullVersion()).remove(0, 1);
57  QString titleAddText = networkStyle->getTitleAddText();
58 
59  QFont fontNormal = GUIUtil::getFontNormal();
60  QFont fontBold = GUIUtil::getFontBold();
61 
62  QPixmap pixmapLogo = networkStyle->getSplashImage();
63  pixmapLogo.setDevicePixelRatio(scale);
64 
65  // Adjust logo color based on the current theme
66  QImage imgLogo = pixmapLogo.toImage().convertToFormat(QImage::Format_ARGB32);
68  for (int x = 0; x < imgLogo.width(); ++x) {
69  for (int y = 0; y < imgLogo.height(); ++y) {
70  const QRgb rgb = imgLogo.pixel(x, y);
71  imgLogo.setPixel(x, y, qRgba(logoColor.red(), logoColor.green(), logoColor.blue(), qAlpha(rgb)));
72  }
73  }
74  pixmapLogo.convertFromImage(imgLogo);
75 
76  pixmap = QPixmap(width * scale, height * scale);
77  pixmap.setDevicePixelRatio(scale);
79 
80  QPainter pixPaint(&pixmap);
81 
82  QRect rect = QRect(1, 1, width - 2, height - 2);
84 
85  pixPaint.drawPixmap((width / 2) - (logoWidth / 2), (height / 2) - (logoHeight / 2) + 20, pixmapLogo.scaled(logoWidth * scale, logoHeight * scale, Qt::KeepAspectRatio, Qt::SmoothTransformation));
87 
88  // check font size and drawing with
89  fontBold.setPointSize(50 * fontFactor);
90  pixPaint.setFont(fontBold);
91  QFontMetrics fm = pixPaint.fontMetrics();
92  int titleTextWidth = fm.width(titleText);
93  if (titleTextWidth > width * 0.8) {
94  fontFactor = 0.75;
95  }
96 
97  fontBold.setPointSize(50 * fontFactor);
98  pixPaint.setFont(fontBold);
99  fm = pixPaint.fontMetrics();
100  titleTextWidth = fm.width(titleText);
101  int titleTextHeight = fm.height();
102  pixPaint.drawText((width / 2) - (titleTextWidth / 2), titleTextHeight + paddingTop, titleText);
103 
104  fontNormal.setPointSize(16 * fontFactor);
105  pixPaint.setFont(fontNormal);
106  fm = pixPaint.fontMetrics();
107  int versionTextWidth = fm.width(versionText);
108  pixPaint.drawText((width / 2) - (versionTextWidth / 2), titleTextHeight + paddingTop + titleVersionVSpace, versionText);
109 
110  // draw additional text if special network
111  if(!titleAddText.isEmpty()) {
112  fontBold.setPointSize(10 * fontFactor);
113  pixPaint.setFont(fontBold);
114  fm = pixPaint.fontMetrics();
115  int titleAddTextWidth = fm.width(titleAddText);
116  // Draw the badge backround with the network-specific color
117  QRect badgeRect = QRect(width - titleAddTextWidth - 20, 5, width, fm.height() + 10);
118  QColor badgeColor = networkStyle->getBadgeColor();
119  pixPaint.fillRect(badgeRect, badgeColor);
120  // Draw the text itself using white color, regardless of the current theme
121  pixPaint.setPen(QColor(255, 255, 255));
122  pixPaint.drawText(width - titleAddTextWidth - 10, paddingTop + 10, titleAddText);
123  }
124 
125  pixPaint.end();
126 
127  // Resize window and move to center of desktop, disallow resizing
128  QRect r(QPoint(), QSize(width, height));
129  resize(r.size());
130  setFixedSize(r.size());
131  move(QApplication::desktop()->screenGeometry().center() - r.center());
132 
134  installEventFilter(this);
135 }
136 
138 {
140 }
141 
142 bool SplashScreen::eventFilter(QObject * obj, QEvent * ev) {
143  if (ev->type() == QEvent::KeyPress) {
144  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
145  if(keyEvent->text()[0] == 'q') {
146  StartShutdown();
147  }
148  }
149  return QObject::eventFilter(obj, ev);
150 }
151 
152 void SplashScreen::slotFinish(QWidget *mainWin)
153 {
154  Q_UNUSED(mainWin);
155 
156  /* If the window is minimized, hide() will be ignored. */
157  /* Make sure we de-minimize the splashscreen window before hiding */
158  if (isMinimized())
159  showNormal();
160  hide();
161  deleteLater(); // No more need for this
162 }
163 
164 static void InitMessage(SplashScreen *splash, const std::string &message)
165 {
166  QMetaObject::invokeMethod(splash, "showMessage",
167  Qt::QueuedConnection,
168  Q_ARG(QString, QString::fromStdString(message)),
169  Q_ARG(int, Qt::AlignBottom | Qt::AlignHCenter),
171 }
172 
173 static void ShowProgress(SplashScreen *splash, const std::string &title, int nProgress, bool resume_possible)
174 {
175  InitMessage(splash, title + std::string("\n") +
176  (resume_possible ? _("(press q to shutdown and continue later)")
177  : _("press q to shutdown")) +
178  strprintf("\n%d", nProgress) + "%");
179 }
180 
181 #ifdef ENABLE_WALLET
183 {
184  wallet->ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2, false));
185  connectedWallets.push_back(wallet);
186 }
187 #endif
188 
190 {
191  // Connect signals to client
192  uiInterface.InitMessage.connect(boost::bind(InitMessage, this, _1));
193  uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2, _3));
194 #ifdef ENABLE_WALLET
195  uiInterface.LoadWallet.connect(boost::bind(&SplashScreen::ConnectWallet, this, _1));
196 #endif
197 }
198 
200 {
201  // Disconnect signals from client
202  uiInterface.InitMessage.disconnect(boost::bind(InitMessage, this, _1));
203  uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2, _3));
204 #ifdef ENABLE_WALLET
205  uiInterface.LoadWallet.disconnect(boost::bind(&SplashScreen::ConnectWallet, this, _1));
206  for (CWallet* const & pwallet : connectedWallets) {
207  pwallet->ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2, false));
208  }
209 #endif
210 }
211 
212 void SplashScreen::showMessage(const QString &message, int alignment, const QColor &color)
213 {
214  curMessage = message;
215  curAlignment = alignment;
216  curColor = color;
217  update();
218 }
219 
220 void SplashScreen::paintEvent(QPaintEvent *event)
221 {
222  QPainter painter(this);
223  QFont messageFont = GUIUtil::getFontNormal();
224  messageFont.setPointSize(14);
225  painter.setFont(messageFont);
226  painter.drawPixmap(0, 0, pixmap);
227  QRect r = rect().adjusted(5, 5, -5, -15);
228  painter.setPen(curColor);
229  painter.drawText(r, curAlignment, curMessage);
230 }
231 
232 void SplashScreen::closeEvent(QCloseEvent *event)
233 {
234  StartShutdown(); // allows an "emergency" shutdown during startup
235  event->ignore();
236 }
bool eventFilter(QObject *obj, QEvent *ev)
void unsubscribeFromCoreSignals()
Disconnect core signals to splash screen.
void closeEvent(QCloseEvent *event)
void ConnectWallet(CWallet *)
Connect wallet signals to splash screen.
Class for the splashscreen with information of the running client.
Definition: splashscreen.h:20
SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle)
#define strprintf
Definition: tinyformat.h:1066
QColor getThemedQColor(ThemedColor color)
Helper to get colors for various themes which can&#39;t be applied via css for some reason.
Definition: guiutil.cpp:204
void StartShutdown()
Definition: init.cpp:171
QFont getFontNormal()
Get the default normal QFont.
Definition: guiutil.cpp:1735
void subscribeToCoreSignals()
Connect core signals to splash screen.
boost::signals2::signal< void(CWallet *wallet)> LoadWallet
A wallet has been loaded.
Definition: ui_interface.h:96
QList< CWallet * > connectedWallets
Definition: splashscreen.h:55
const QString & getTitleAddText() const
Definition: networkstyle.h:24
void paintEvent(QPaintEvent *event)
void showMessage(const QString &message, int alignment, const QColor &color)
Show message and progress.
static void InitMessage(SplashScreen *splash, const std::string &message)
QString curMessage
Definition: splashscreen.h:51
static void ShowProgress(SplashScreen *splash, const std::string &title, int nProgress, bool resume_possible)
QPixmap pixmap
Definition: splashscreen.h:50
std::string FormatFullVersion()
QFont getFontBold()
Get the default bold QFont.
Definition: guiutil.cpp:1740
void slotFinish(QWidget *mainWin)
Slot to call finish() method as it&#39;s not defined as slot.
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:715
boost::signals2::signal< void(const std::string &title, int nProgress)> ShowProgress
Show progress e.g.
Definition: wallet.h:1200
QColor curColor
Definition: splashscreen.h:52
const std::string titleAddText
CClientUIInterface uiInterface
Definition: ui_interface.cpp:8
const QColor & getBadgeColor() const
Definition: networkstyle.h:25
boost::signals2::signal< void(const std::string &title, int nProgress, bool resume_possible)> ShowProgress
Show progress e.g.
Definition: ui_interface.h:102
const QPixmap & getSplashImage() const
Definition: networkstyle.h:22
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...
Definition: util.h:92
boost::signals2::signal< void(const std::string &message)> InitMessage
Progress message during initialization.
Definition: ui_interface.h:82
Released under the MIT license