Fabcoin Core  0.16.2
P2P Digital Currency
splashscreen.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2017 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 <splashscreen.h>
10 
11 #include <networkstyle.h>
12 
13 #include <clientversion.h>
14 #include <init.h>
15 #include <util.h>
16 #include <ui_interface.h>
17 #include <version.h>
18 
19 #ifdef ENABLE_WALLET
20 #include <wallet/wallet.h>
21 #endif
22 
23 #include <QApplication>
24 #include <QCloseEvent>
25 #include <QDesktopWidget>
26 #include <QPainter>
27 #include <QRadialGradient>
28 
29 SplashScreen::SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle) :
30  QWidget(0, f), curAlignment(0)
31 {
32  // set sizes
33  int versionTextHeight = 30;
34  int statusHeight = 30;
35  int titleAddTextHeight = 20;
36 
37  float fontFactor = 1.0;
38  float devicePixelRatio = 1.0;
39 #if QT_VERSION > 0x050100
40  devicePixelRatio = ((QGuiApplication*)QCoreApplication::instance())->devicePixelRatio();
41 #endif
42 
43  // define text to place
44  QString titleText = tr(PACKAGE_NAME);
45  QString versionText = QString("Version %1").arg(QString::fromStdString(FormatFullVersion()));
46  QString copyrightText = QString::fromUtf8(CopyrightHolders(strprintf("\xc2\xA9 %u ", COPYRIGHT_YEAR)).c_str());
47  QString titleAddText = networkStyle->getTitleAddText();
48 
49  QString font = QApplication::font().toString();
50 
51  // create a bitmap according to device pixelratio
52  QSize splashSize(480,320);
53  pixmap = QPixmap(480*devicePixelRatio,320*devicePixelRatio);
54 
55 #if QT_VERSION > 0x050100
56  // change to HiDPI if it makes sense
57  pixmap.setDevicePixelRatio(devicePixelRatio);
58 #endif
59 
60  QPainter pixPaint(&pixmap);
61  pixPaint.setPen(QColor("#ffffff"));
62 
63  QRect mainRect(QPoint(0,0), splashSize);
64  pixPaint.fillRect(mainRect, QColor("#030509"));
65 
66  // draw background
67  QRect rectBg(QPoint(-50, -50), QSize(splashSize.width() + 50, splashSize.height() + 50));
68  QPixmap bg(":/styles/app-icons/splash_bg");
69  pixPaint.drawPixmap(rectBg, bg);
70 
71  pixPaint.setFont(QFont(font, 32*fontFactor, QFont::Bold));
72  QRect rectTitle(QPoint(0,0), QSize(splashSize.width(), (splashSize.height() / 2)));
73  pixPaint.drawText(rectTitle, Qt::AlignHCenter | Qt::AlignBottom, titleText);
74 
75  QPoint versionPoint(rectTitle.bottomLeft());
76 
77  // draw additional text if special network
78  if(!titleAddText.isEmpty())
79  {
80  QRect titleAddRect(rectTitle.bottomLeft(), QSize(rectTitle.width(), titleAddTextHeight));
81  versionPoint = titleAddRect.bottomLeft();
82  pixPaint.setFont(QFont(font, 8*fontFactor, QFont::Bold));
83  pixPaint.drawText(titleAddRect, Qt::AlignHCenter | Qt::AlignVCenter, titleAddText);
84  }
85 
86  pixPaint.setFont(QFont(font, 11*fontFactor));
87  QRect versionRect(versionPoint, QSize(rectTitle.width(), versionTextHeight));
88  pixPaint.drawText(versionRect, Qt::AlignHCenter | Qt::AlignTop, versionText);
89 
90  // draw copyright stuff
91  QFont statusFont = QApplication::font();
92  statusFont.setPointSizeF(statusFont.pointSizeF() * 0.9);
93  pixPaint.setFont(statusFont);
94  QRect statusRect(mainRect.left(), mainRect.height() - statusHeight, mainRect.width(), statusHeight);
95  QColor statusColor(255, 255, 255);
96  statusColor.setAlphaF(0.1);
97  pixPaint.fillRect(statusRect, statusColor);
98  pixPaint.drawText(statusRect.adjusted(10, 0, -10, 0), Qt::AlignLeft | Qt::AlignVCenter, copyrightText);
99 
100  pixPaint.end();
101 
102  // Set window title
103  setWindowTitle(titleText + " " + titleAddText);
104 
105  // Resize window and move to center of desktop, disallow resizing
106  QRect r(QPoint(), QSize(pixmap.size().width()/devicePixelRatio,pixmap.size().height()/devicePixelRatio));
107  resize(r.size());
108  setFixedSize(r.size());
109  move(QApplication::desktop()->screenGeometry().center() - r.center());
110 
112  installEventFilter(this);
113 }
114 
116 {
118 }
119 
120 bool SplashScreen::eventFilter(QObject * obj, QEvent * ev) {
121  if (ev->type() == QEvent::KeyPress) {
122  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
123  if(keyEvent->text()[0] == 'q' && breakAction != nullptr) {
124  breakAction();
125  }
126  }
127  return QObject::eventFilter(obj, ev);
128 }
129 
130 void SplashScreen::slotFinish(QWidget *mainWin)
131 {
132  Q_UNUSED(mainWin);
133 
134  /* If the window is minimized, hide() will be ignored. */
135  /* Make sure we de-minimize the splashscreen window before hiding */
136  if (isMinimized())
137  showNormal();
138  hide();
139  deleteLater(); // No more need for this
140 }
141 
142 static void InitMessage(SplashScreen *splash, const std::string &message)
143 {
144  QMetaObject::invokeMethod(splash, "showMessage",
145  Qt::QueuedConnection,
146  Q_ARG(QString, QString::fromStdString(message)),
147  Q_ARG(int, Qt::AlignBottom|Qt::AlignRight),
148  Q_ARG(QColor, QColor("#ffffff")));
149 }
150 
151 static void ShowProgress(SplashScreen *splash, const std::string &title, int nProgress)
152 {
153  InitMessage(splash, title + strprintf("%d", nProgress) + "%");
154 }
155 
156 void SplashScreen::setBreakAction(const std::function<void(void)> &action)
157 {
158  breakAction = action;
159 }
160 
161 static void SetProgressBreakAction(SplashScreen *splash, const std::function<void(void)> &action)
162 {
163  QMetaObject::invokeMethod(splash, "setBreakAction",
164  Qt::QueuedConnection,
165  Q_ARG(std::function<void(void)>, action));
166 }
167 
168 #ifdef ENABLE_WALLET
170 {
171  wallet->ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
172  connectedWallets.push_back(wallet);
173 }
174 #endif
175 
177 {
178  // Connect signals to client
179  uiInterface.InitMessage.connect(boost::bind(InitMessage, this, _1));
180  uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
181  uiInterface.SetProgressBreakAction.connect(boost::bind(SetProgressBreakAction, this, _1));
182 #ifdef ENABLE_WALLET
183  uiInterface.LoadWallet.connect(boost::bind(&SplashScreen::ConnectWallet, this, _1));
184 #endif
185 }
186 
188 {
189  // Disconnect signals from client
190  uiInterface.InitMessage.disconnect(boost::bind(InitMessage, this, _1));
191  uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
192 #ifdef ENABLE_WALLET
193  for (CWallet* const & pwallet : connectedWallets) {
194  pwallet->ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
195  }
196 #endif
197 }
198 
199 void SplashScreen::showMessage(const QString &message, int alignment, const QColor &color)
200 {
201  curMessage = message;
202  curAlignment = alignment;
203  curColor = color;
204  update();
205 }
206 
207 void SplashScreen::paintEvent(QPaintEvent *event)
208 {
209  QPainter painter(this);
210  painter.drawPixmap(0, 0, pixmap);
211  QRect r = rect().adjusted(10, 10, -10, -10);
212  painter.setPen(curColor);
213  QFont font = QApplication::font();
214  font.setPointSizeF(font.pointSizeF() * 0.9);
215  painter.setFont(font);
216  painter.drawText(r, curAlignment, curMessage);
217 }
218 
219 void SplashScreen::closeEvent(QCloseEvent *event)
220 {
221  StartShutdown(); // allows an "emergency" shutdown during startup
222  event->ignore();
223 }
bool eventFilter(QObject *obj, QEvent *ev)
void unsubscribeFromCoreSignals()
Disconnect core signals to splash screen.
#define function(a, b, c, d, k, s)
void closeEvent(QCloseEvent *event)
#define COPYRIGHT_YEAR
void ConnectWallet(CWallet *)
Connect wallet signals to splash screen.
#define PACKAGE_NAME
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:1054
void StartShutdown()
Definition: init.cpp:122
void subscribeToCoreSignals()
Connect core signals to splash screen.
boost::signals2::signal< void(CWallet *wallet)> LoadWallet
A wallet has been loaded.
Definition: ui_interface.h:95
QList< CWallet * > connectedWallets
Definition: splashscreen.h:57
void paintEvent(QPaintEvent *event)
void showMessage(const QString &message, int alignment, const QColor &color)
Show message and progress.
QString curMessage
Definition: splashscreen.h:53
boost::signals2::signal< void(const std::string &title, int nProgress)> ShowProgress
Show progress e.g.
Definition: ui_interface.h:98
QPixmap pixmap
Definition: splashscreen.h:52
std::string FormatFullVersion()
std::string CopyrightHolders(const std::string &strPrefix)
Definition: util.cpp:968
std::function< void(void)> breakAction
Definition: splashscreen.h:59
#define f(x)
Definition: gost.cpp:57
void slotFinish(QWidget *mainWin)
Slot to call finish() method as it&#39;s not defined as slot.
boost::signals2::signal< void(std::function< void(void)> action)> SetProgressBreakAction
Set progress break action (possible "cancel button" triggers that action)
Definition: ui_interface.h:101
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:672
boost::signals2::signal< void(const std::string &title, int nProgress)> ShowProgress
Show progress e.g.
Definition: wallet.h:1130
void setBreakAction(const std::function< void(void)> &action)
Sets the break action.
QColor curColor
Definition: splashscreen.h:54
const char * titleAddText
CClientUIInterface uiInterface
Definition: ui_interface.cpp:8
boost::signals2::signal< void(const std::string &message)> InitMessage
Progress message during initialization.
Definition: ui_interface.h:81
const QString & getTitleAddText() const
Definition: networkstyle.h:22