Bitcoin Core  0.18.99
P2P Digital Currency
splashscreen.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2018 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 #if defined(HAVE_CONFIG_H)
7 #endif
8 
9 #include <qt/splashscreen.h>
10 
11 #include <qt/networkstyle.h>
12 
13 #include <clientversion.h>
14 #include <interfaces/handler.h>
15 #include <interfaces/node.h>
16 #include <interfaces/wallet.h>
17 #include <ui_interface.h>
18 #include <util/system.h>
19 #include <version.h>
20 
21 #include <QApplication>
22 #include <QCloseEvent>
23 #include <QDesktopWidget>
24 #include <QPainter>
25 #include <QRadialGradient>
26 
27 
28 SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const NetworkStyle *networkStyle) :
29  QWidget(nullptr, f), curAlignment(0), m_node(node)
30 {
31  // set reference point, paddings
32  int paddingRight = 50;
33  int paddingTop = 50;
34  int titleVersionVSpace = 17;
35  int titleCopyrightVSpace = 40;
36 
37  float fontFactor = 1.0;
38  float devicePixelRatio = 1.0;
39  devicePixelRatio = static_cast<QGuiApplication*>(QCoreApplication::instance())->devicePixelRatio();
40 
41  // define text to place
42  QString titleText = tr(PACKAGE_NAME);
43  QString versionText = QString("Version %1").arg(QString::fromStdString(FormatFullVersion()));
44  QString copyrightText = QString::fromUtf8(CopyrightHolders(strprintf("\xc2\xA9 %u-%u ", 2009, COPYRIGHT_YEAR)).c_str());
45  QString titleAddText = networkStyle->getTitleAddText();
46 
47  QString font = QApplication::font().toString();
48 
49  // create a bitmap according to device pixelratio
50  QSize splashSize(480*devicePixelRatio,320*devicePixelRatio);
51  pixmap = QPixmap(splashSize);
52 
53  // change to HiDPI if it makes sense
54  pixmap.setDevicePixelRatio(devicePixelRatio);
55 
56  QPainter pixPaint(&pixmap);
57  pixPaint.setPen(QColor(100,100,100));
58 
59  // draw a slightly radial gradient
60  QRadialGradient gradient(QPoint(0,0), splashSize.width()/devicePixelRatio);
61  gradient.setColorAt(0, Qt::white);
62  gradient.setColorAt(1, QColor(247,247,247));
63  QRect rGradient(QPoint(0,0), splashSize);
64  pixPaint.fillRect(rGradient, gradient);
65 
66  // draw the bitcoin icon, expected size of PNG: 1024x1024
67  QRect rectIcon(QPoint(-150,-122), QSize(430,430));
68 
69  const QSize requiredSize(1024,1024);
70  QPixmap icon(networkStyle->getAppIcon().pixmap(requiredSize));
71 
72  pixPaint.drawPixmap(rectIcon, icon);
73 
74  // check font size and drawing with
75  pixPaint.setFont(QFont(font, 33*fontFactor));
76  QFontMetrics fm = pixPaint.fontMetrics();
77  int titleTextWidth = fm.width(titleText);
78  if (titleTextWidth > 176) {
79  fontFactor = fontFactor * 176 / titleTextWidth;
80  }
81 
82  pixPaint.setFont(QFont(font, 33*fontFactor));
83  fm = pixPaint.fontMetrics();
84  titleTextWidth = fm.width(titleText);
85  pixPaint.drawText(pixmap.width()/devicePixelRatio-titleTextWidth-paddingRight,paddingTop,titleText);
86 
87  pixPaint.setFont(QFont(font, 15*fontFactor));
88 
89  // if the version string is too long, reduce size
90  fm = pixPaint.fontMetrics();
91  int versionTextWidth = fm.width(versionText);
92  if(versionTextWidth > titleTextWidth+paddingRight-10) {
93  pixPaint.setFont(QFont(font, 10*fontFactor));
94  titleVersionVSpace -= 5;
95  }
96  pixPaint.drawText(pixmap.width()/devicePixelRatio-titleTextWidth-paddingRight+2,paddingTop+titleVersionVSpace,versionText);
97 
98  // draw copyright stuff
99  {
100  pixPaint.setFont(QFont(font, 10*fontFactor));
101  const int x = pixmap.width()/devicePixelRatio-titleTextWidth-paddingRight;
102  const int y = paddingTop+titleCopyrightVSpace;
103  QRect copyrightRect(x, y, pixmap.width() - x - paddingRight, pixmap.height() - y);
104  pixPaint.drawText(copyrightRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, copyrightText);
105  }
106 
107  // draw additional text if special network
108  if(!titleAddText.isEmpty()) {
109  QFont boldFont = QFont(font, 10*fontFactor);
110  boldFont.setWeight(QFont::Bold);
111  pixPaint.setFont(boldFont);
112  fm = pixPaint.fontMetrics();
113  int titleAddTextWidth = fm.width(titleAddText);
114  pixPaint.drawText(pixmap.width()/devicePixelRatio-titleAddTextWidth-10,15,titleAddText);
115  }
116 
117  pixPaint.end();
118 
119  // Set window title
120  setWindowTitle(titleText + " " + titleAddText);
121 
122  // Resize window and move to center of desktop, disallow resizing
123  QRect r(QPoint(), QSize(pixmap.size().width()/devicePixelRatio,pixmap.size().height()/devicePixelRatio));
124  resize(r.size());
125  setFixedSize(r.size());
126  move(QApplication::desktop()->screenGeometry().center() - r.center());
127 
129  installEventFilter(this);
130 }
131 
133 {
135 }
136 
137 bool SplashScreen::eventFilter(QObject * obj, QEvent * ev) {
138  if (ev->type() == QEvent::KeyPress) {
139  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
140  if(keyEvent->text()[0] == 'q') {
142  }
143  }
144  return QObject::eventFilter(obj, ev);
145 }
146 
148 {
149  /* If the window is minimized, hide() will be ignored. */
150  /* Make sure we de-minimize the splashscreen window before hiding */
151  if (isMinimized())
152  showNormal();
153  hide();
154  deleteLater(); // No more need for this
155 }
156 
157 static void InitMessage(SplashScreen *splash, const std::string &message)
158 {
159  QMetaObject::invokeMethod(splash, "showMessage",
160  Qt::QueuedConnection,
161  Q_ARG(QString, QString::fromStdString(message)),
162  Q_ARG(int, Qt::AlignBottom|Qt::AlignHCenter),
163  Q_ARG(QColor, QColor(55,55,55)));
164 }
165 
166 static void ShowProgress(SplashScreen *splash, const std::string &title, int nProgress, bool resume_possible)
167 {
168  InitMessage(splash, title + std::string("\n") +
169  (resume_possible ? _("(press q to shutdown and continue later)")
170  : _("press q to shutdown")) +
171  strprintf("\n%d", nProgress) + "%");
172 }
173 #ifdef ENABLE_WALLET
174 void SplashScreen::ConnectWallet(std::unique_ptr<interfaces::Wallet> wallet)
175 {
176  m_connected_wallet_handlers.emplace_back(wallet->handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2, false)));
177  m_connected_wallets.emplace_back(std::move(wallet));
178 }
179 #endif
180 
182 {
183  // Connect signals to client
184  m_handler_init_message = m_node.handleInitMessage(std::bind(InitMessage, this, std::placeholders::_1));
185  m_handler_show_progress = m_node.handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
186 #ifdef ENABLE_WALLET
187  m_handler_load_wallet = m_node.handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) { ConnectWallet(std::move(wallet)); });
188 #endif
189 }
190 
192 {
193  // Disconnect signals from client
194  m_handler_init_message->disconnect();
195  m_handler_show_progress->disconnect();
196  for (const auto& handler : m_connected_wallet_handlers) {
197  handler->disconnect();
198  }
199  m_connected_wallet_handlers.clear();
200  m_connected_wallets.clear();
201 }
202 
203 void SplashScreen::showMessage(const QString &message, int alignment, const QColor &color)
204 {
205  curMessage = message;
206  curAlignment = alignment;
207  curColor = color;
208  update();
209 }
210 
211 void SplashScreen::paintEvent(QPaintEvent *event)
212 {
213  QPainter painter(this);
214  painter.drawPixmap(0, 0, pixmap);
215  QRect r = rect().adjusted(5, 5, -5, -5);
216  painter.setPen(curColor);
217  painter.drawText(r, curAlignment, curMessage);
218 }
219 
220 void SplashScreen::closeEvent(QCloseEvent *event)
221 {
222  m_node.startShutdown(); // allows an "emergency" shutdown during startup
223  event->ignore();
224 }
bool eventFilter(QObject *obj, QEvent *ev)
#define COPYRIGHT_YEAR
void unsubscribeFromCoreSignals()
Disconnect core signals to splash screen.
bool(* handler)(HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:626
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: splashscreen.h:63
void ConnectWallet(std::unique_ptr< interfaces::Wallet > wallet)
Connect wallet signals to splash screen.
void closeEvent(QCloseEvent *event)
std::unique_ptr< interfaces::Handler > m_handler_load_wallet
Definition: splashscreen.h:64
Class for the splashscreen with information of the running client.
Definition: splashscreen.h:26
std::unique_ptr< interfaces::Handler > m_handler_init_message
Definition: splashscreen.h:62
#define strprintf
Definition: tinyformat.h:1066
virtual void startShutdown()=0
Start shutdown.
std::list< std::unique_ptr< interfaces::Wallet > > m_connected_wallets
Definition: splashscreen.h:65
std::string CopyrightHolders(const std::string &strPrefix)
Definition: system.cpp:1189
const QIcon & getAppIcon() const
Definition: networkstyle.h:20
SplashScreen(interfaces::Node &node, Qt::WindowFlags f, const NetworkStyle *networkStyle)
#define PACKAGE_NAME
void subscribeToCoreSignals()
Connect core signals to splash screen.
const QString & getTitleAddText() const
Definition: networkstyle.h:22
void paintEvent(QPaintEvent *event)
void showMessage(const QString &message, int alignment, const QColor &color)
Show message and progress.
void finish()
Hide the splash screen window and schedule the splash screen object for deletion. ...
std::list< std::unique_ptr< interfaces::Handler > > m_connected_wallet_handlers
Definition: splashscreen.h:66
QString curMessage
Definition: splashscreen.h:57
virtual std::unique_ptr< Handler > handleShowProgress(ShowProgressFn fn)=0
QPixmap pixmap
Definition: splashscreen.h:56
std::string FormatFullVersion()
std::string _(const char *psz)
Translation function.
Definition: system.h:52
virtual std::unique_ptr< Handler > handleLoadWallet(LoadWalletFn fn)=0
QColor curColor
Definition: splashscreen.h:58
const char * titleAddText
interfaces::Node & m_node
Definition: splashscreen.h:61
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:36
virtual std::unique_ptr< Handler > handleInitMessage(InitMessageFn fn)=0