Raven Core  3.0.0
P2P Digital Currency
splashscreen.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven 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/raven-config.h"
8 #endif
9 
10 #include "splashscreen.h"
11 
12 #include "networkstyle.h"
13 
14 #include "clientversion.h"
15 #include "init.h"
16 #include "util.h"
17 #include "ui_interface.h"
18 #include "version.h"
19 
20 #ifdef ENABLE_WALLET
21 #include "wallet/wallet.h"
22 #endif
23 
24 #include <QApplication>
25 #include <QCloseEvent>
26 #include <QDesktopWidget>
27 #include <QPainter>
28 #include <QRadialGradient>
29 
30 SplashScreen::SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle) :
31  QWidget(0, f), curAlignment(0)
32 {
33  // set reference point, paddings
34  int paddingRight = 50;
35  int paddingTop = 50;
36  int titleVersionVSpace = 17;
37  int titleCopyrightVSpace = 40;
38 
39  float fontFactor = 1.0;
40  float devicePixelRatio = 1.0;
41 #if QT_VERSION > 0x050100
42  devicePixelRatio = ((QGuiApplication*)QCoreApplication::instance())->devicePixelRatio();
43 #endif
44 
45  // define text to place
46  QString titleText = tr(PACKAGE_NAME);
47  QString versionText = QString("Version %1").arg(QString::fromStdString(FormatFullVersion()));
48  QString copyrightText = QString::fromUtf8(CopyrightHolders(strprintf("\xc2\xA9 %u-%u ", 2009, COPYRIGHT_YEAR)).c_str());
49  QString titleAddText = networkStyle->getTitleAddText();
50 
51  QString font = QApplication::font().toString();
52 
53  // create a bitmap according to device pixelratio
54  QSize splashSize(480*devicePixelRatio,320*devicePixelRatio);
55  pixmap = QPixmap(splashSize);
56 
57 #if QT_VERSION > 0x050100
58  // change to HiDPI if it makes sense
59  pixmap.setDevicePixelRatio(devicePixelRatio);
60 #endif
61 
62  QPainter pixPaint(&pixmap);
63  pixPaint.setPen(QColor(100,100,100));
64 
65  // draw a slightly radial gradient
66  QRadialGradient gradient(QPoint(0,0), splashSize.width()/devicePixelRatio);
67  gradient.setColorAt(0, Qt::white);
68  gradient.setColorAt(1, QColor(247,247,247));
69  QRect rGradient(QPoint(0,0), splashSize);
70  pixPaint.fillRect(rGradient, gradient);
71 
72  // draw the raven icon, expected size of PNG: 1024x1024
73  QRect rectIcon(QPoint(-40,0), QSize(310,310));
74 
75  const QSize requiredSize(1024,1024);
76  QPixmap icon(networkStyle->getSplashIcon().pixmap(requiredSize));
77 
78  pixPaint.drawPixmap(rectIcon, icon);
79 
80  // check font size and drawing with
81  pixPaint.setFont(QFont(font, 33*fontFactor));
82  QFontMetrics fm = pixPaint.fontMetrics();
83  int titleTextWidth = fm.width(titleText);
84  if (titleTextWidth > 176) {
85  fontFactor = fontFactor * 176 / titleTextWidth;
86  }
87 
88  pixPaint.setFont(QFont(font, 33*fontFactor));
89  fm = pixPaint.fontMetrics();
90  titleTextWidth = fm.width(titleText);
91  pixPaint.drawText(pixmap.width()/devicePixelRatio-titleTextWidth-paddingRight,paddingTop,titleText);
92 
93  pixPaint.setFont(QFont(font, 15*fontFactor));
94 
95  // if the version string is to long, reduce size
96  fm = pixPaint.fontMetrics();
97  int versionTextWidth = fm.width(versionText);
98  if(versionTextWidth > titleTextWidth+paddingRight-10) {
99  pixPaint.setFont(QFont(font, 10*fontFactor));
100  titleVersionVSpace -= 5;
101  }
102  pixPaint.drawText(pixmap.width()/devicePixelRatio-titleTextWidth-paddingRight+2,paddingTop+titleVersionVSpace,versionText);
103 
104  // draw copyright stuff
105  {
106  pixPaint.setFont(QFont(font, 10*fontFactor));
107  const int x = pixmap.width()/devicePixelRatio-titleTextWidth-paddingRight;
108  const int y = paddingTop+titleCopyrightVSpace;
109  QRect copyrightRect(x, y, pixmap.width() - x - paddingRight, pixmap.height() - y);
110  pixPaint.drawText(copyrightRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, copyrightText);
111  }
112 
113  // draw additional text if special network
114  if(!titleAddText.isEmpty()) {
115  QFont boldFont = QFont(font, 10*fontFactor);
116  boldFont.setWeight(QFont::Bold);
117  pixPaint.setFont(boldFont);
118  fm = pixPaint.fontMetrics();
119  int titleAddTextWidth = fm.width(titleAddText);
120  pixPaint.drawText(pixmap.width()/devicePixelRatio-titleAddTextWidth-10,15,titleAddText);
121  }
122 
123  pixPaint.end();
124 
125  // Set window title
126  setWindowTitle(titleText + " " + titleAddText);
127 
128  // Resize window and move to center of desktop, disallow resizing
129  QRect r(QPoint(), QSize(pixmap.size().width()/devicePixelRatio,pixmap.size().height()/devicePixelRatio));
130  resize(r.size());
131  setFixedSize(r.size());
132  move(QApplication::desktop()->screenGeometry().center() - r.center());
133 
135  installEventFilter(this);
136 }
137 
139 {
141 }
142 
143 bool SplashScreen::eventFilter(QObject * obj, QEvent * ev) {
144  if (ev->type() == QEvent::KeyPress) {
145  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
146  if(keyEvent->text()[0] == 'q') {
147  StartShutdown();
148  }
149  }
150  return QObject::eventFilter(obj, ev);
151 }
152 
153 void SplashScreen::slotFinish(QWidget *mainWin)
154 {
155  Q_UNUSED(mainWin);
156 
157  /* If the window is minimized, hide() will be ignored. */
158  /* Make sure we de-minimize the splashscreen window before hiding */
159  if (isMinimized())
160  showNormal();
161  hide();
162  deleteLater(); // No more need for this
163 }
164 
165 static void InitMessage(SplashScreen *splash, const std::string &message)
166 {
167  QMetaObject::invokeMethod(splash, "showMessage",
168  Qt::QueuedConnection,
169  Q_ARG(QString, QString::fromStdString(message)),
170  Q_ARG(int, Qt::AlignBottom|Qt::AlignHCenter),
171  Q_ARG(QColor, QColor(55,55,55)));
172 }
173 
174 static void ShowProgress(SplashScreen *splash, const std::string &title, int nProgress, bool resume_possible)
175 {
176  InitMessage(splash, title + std::string("\n") +
177  (resume_possible ? _("(press q to shutdown and continue later)")
178  : _("press q to shutdown")) +
179  strprintf("\n%d", nProgress) + "%");
180 }
181 
182 #ifdef ENABLE_WALLET
184 {
185  wallet->ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2, false));
186  connectedWallets.push_back(wallet);
187 }
188 #endif
189 
191 {
192  // Connect signals to client
193  uiInterface.InitMessage.connect(boost::bind(InitMessage, this, _1));
194  uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2, _3));
195 #ifdef ENABLE_WALLET
196  uiInterface.LoadWallet.connect(boost::bind(&SplashScreen::ConnectWallet, this, _1));
197 #endif
198 }
199 
201 {
202  // Disconnect signals from client
203  uiInterface.InitMessage.disconnect(boost::bind(InitMessage, this, _1));
204  uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2, _3));
205 #ifdef ENABLE_WALLET
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  painter.drawPixmap(0, 0, pixmap);
224  QRect r = rect().adjusted(5, 5, -5, -5);
225  painter.setPen(curColor);
226  painter.drawText(r, curAlignment, curMessage);
227 }
228 
229 void SplashScreen::closeEvent(QCloseEvent *event)
230 {
231  StartShutdown(); // allows an "emergency" shutdown during startup
232  event->ignore();
233 }
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:21
SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle)
#define strprintf
Definition: tinyformat.h:1054
void StartShutdown()
Definition: init.cpp:128
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:56
#define PACKAGE_NAME
Definition: raven-config.h:350
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.
const QIcon & getSplashIcon() const
Definition: networkstyle.h:22
#define COPYRIGHT_YEAR
Definition: raven-config.h:36
QString curMessage
Definition: splashscreen.h:52
QPixmap pixmap
Definition: splashscreen.h:51
std::string FormatFullVersion()
std::string CopyrightHolders(const std::string &strPrefix)
Definition: util.cpp:917
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:673
boost::signals2::signal< void(const std::string &title, int nProgress)> ShowProgress
Show progress e.g.
Definition: wallet.h:1142
QColor curColor
Definition: splashscreen.h:53
const char * titleAddText
CClientUIInterface uiInterface
Definition: ui_interface.cpp:9
boost::signals2::signal< void(const std::string &title, int nProgress, bool resume_possible)> ShowProgress
Show progress e.g.
Definition: ui_interface.h:102
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...
Definition: util.h:66
boost::signals2::signal< void(const std::string &message)> InitMessage
Progress message during initialization.
Definition: ui_interface.h:82