You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

splashscreen.cpp 7.7KB

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