text: // Copyright (c) 2009-2017 Satoshi Nakamoto
  // Copyright (c) 2009-2017 The Bitcoin Developers
  // Copyright (c) 2014-2017 The Dash Core Developers
  // Copyright (c) 2017 Credits Developers
  // Distributed under the MIT/X11 software license, see the accompanying
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  
  #include "splashscreen.h"
  
  #include "networkstyle.h"
  
  #include "clientversion.h"
  #include "guiutil.h"
  #include "init.h"
  #include "util.h"
  #include "ui_interface.h"
  #include "version.h"
  #ifdef ENABLE_WALLET
  #include "wallet/wallet.h"
  #endif
  
  #include <QApplication>
  #include <QCloseEvent>
  #include <QDesktopWidget>
  #include <QPainter>
  
  SplashScreen::SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle) :
      QWidget(0, f), curAlignment(0)
  {
  
      // transparent background
      setAttribute(Qt::WA_TranslucentBackground);
      setStyleSheet("background:transparent;");
  
      // no window decorations
      setWindowFlags(Qt::FramelessWindowHint);
  
      QString font            = QApplication::font().toString();
  
      // load the bitmap for writing some text over it
      pixmap     = networkStyle->getSplashImage();
  
      QPainter pixPaint(&pixmap);
      pixPaint.setPen(QColor(100,100,100));
  
      pixPaint.end();
  
      // Resize window and move to center of desktop, disallow resizing
      QRect r(QPoint(), pixmap.size());
      resize(r.size());
      setFixedSize(r.size());
      move(QApplication::desktop()->screenGeometry().center() - r.center());
  
      subscribeToCoreSignals();
  }
  
  SplashScreen::~SplashScreen()
  {
      unsubscribeFromCoreSignals();
  }
  
  void SplashScreen::slotFinish(QWidget *mainWin)
  {
      Q_UNUSED(mainWin);
  
      /* If the window is minimized, hide() will be ignored. */
      /* Make sure we de-minimize the splashscreen window before hiding */
      if (isMinimized())
          showNormal();
      hide();
  }
  
  static void InitMessage(SplashScreen *splash, const std::string &message)
  {
      QMetaObject::invokeMethod(splash, "showMessage",
          Qt::QueuedConnection,
          Q_ARG(QString, QString::fromStdString(message)),
          Q_ARG(int, Qt::AlignBottom|Qt::AlignHCenter),
          Q_ARG(QColor, QColor(75,150,200)));
  }
  
  static void ShowProgress(SplashScreen *splash, const std::string &title, int nProgress)
  {
      InitMessage(splash, title + strprintf("%d", nProgress) + "%");
  }
  
  #ifdef ENABLE_WALLET
  static void ConnectWallet(SplashScreen *splash, CWallet* wallet)
  {
      wallet->ShowProgress.connect(boost::bind(ShowProgress, splash, _1, _2));
  }
  #endif
  
  void SplashScreen::subscribeToCoreSignals()
  {
      // Connect signals to client
      uiInterface.InitMessage.connect(boost::bind(InitMessage, this, _1));
      uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
  #ifdef ENABLE_WALLET
      uiInterface.LoadWallet.connect(boost::bind(ConnectWallet, this, _1));
  #endif
  }
  
  void SplashScreen::unsubscribeFromCoreSignals()
  {
      // Disconnect signals from client
      uiInterface.InitMessage.disconnect(boost::bind(InitMessage, this, _1));
      uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
  #ifdef ENABLE_WALLET
      if(pwalletMain)
          pwalletMain->ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
  #endif
  }
  
  void SplashScreen::showMessage(const QString &message, int alignment, const QColor &color)
  {
      curMessage = message;
      curAlignment = alignment;
      curColor = color;
      update();
  }
  
  void SplashScreen::paintEvent(QPaintEvent *event)
  {
      QPainter painter(this);
      painter.drawPixmap(0, 0, pixmap);
      QRect r = rect().adjusted(5, 5, -5, 0);
      painter.setPen(curColor);
      painter.drawText(r, curAlignment, curMessage);
  }
  
  void SplashScreen::closeEvent(QCloseEvent *event)
  {
      StartShutdown(); // allows an "emergency" shutdown during startup
      event->ignore();
  }
  