LCOV - code coverage report
Current view: top level - src/qt - splashscreen.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 0 99 0.0 %
Date: 2015-10-12 22:39:14 Functions: 0 12 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11