Files
thehub/hub-qt/splashscreen.cpp
T

211 lines
6.8 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2011-2015 The Bitcoin Core developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2013-11-04 16:20:43 +01:00
2013-04-14 11:35:37 +02:00
#include "splashscreen.h"
2013-04-13 00:13:08 -05:00
2015-01-30 10:08:46 +01:00
#include "networkstyle.h"
2013-04-14 11:35:37 +02:00
#include "clientversion.h"
2014-03-19 00:26:14 +01:00
#include "init.h"
2014-01-22 09:46:15 +01:00
#include "util.h"
2018-02-12 21:19:28 +01:00
#include "UiInterface.h"
2014-09-05 13:18:35 +02:00
#include "version.h"
2014-03-19 00:26:14 +01:00
#ifdef ENABLE_WALLET
#include "wallet/wallet.h"
2014-03-19 00:26:14 +01:00
#endif
2013-04-14 11:35:37 +02:00
#include <QApplication>
#include <QCloseEvent>
2014-09-18 13:14:38 +02:00
#include <QDesktopWidget>
#include <QPainter>
#include <QRadialGradient>
2013-04-14 11:35:37 +02:00
2017-01-30 12:17:25 +01:00
SplashScreen::SplashScreen(const NetworkStyle &networkStyle, Qt::WindowFlags f) :
2014-09-18 13:14:38 +02:00
QWidget(0, f), curAlignment(0)
2013-04-14 11:35:37 +02:00
{
// set reference point, paddings
2018-01-16 12:41:03 +00:00
int paddingRight = 30;
int paddingTop = 38;
2013-04-14 11:35:37 +02:00
// define text to place
2018-01-16 12:41:03 +00:00
QString titleText = "Flowee the Hub";
QString versionText = QString("%1").arg(QString::fromStdString(FormatFullVersion()));
2017-01-30 12:17:25 +01:00
QString titleAddText = networkStyle.getTitleAddText();
2015-01-16 16:27:12 -05:00
QString font = QApplication::font().toString();
2013-04-14 11:35:37 +02:00
// create a bitmap according to device pixelratio
2017-02-05 18:48:01 +01:00
float devicePixelRatio;
bool useMorePixels = false;
#if QT_VERSION > 0x050100
devicePixelRatio = qobject_cast<QGuiApplication*>(QCoreApplication::instance())->devicePixelRatio();
if (qFuzzyCompare(devicePixelRatio, (float) 1)) {
useMorePixels = true;
devicePixelRatio = logicalDpiX() / (float) 96;
}
#else
devicePixelRatio = 1.0;
#endif
2018-01-16 12:41:03 +00:00
QSize splashSize(350 * devicePixelRatio, 250 * devicePixelRatio);
pixmap = QPixmap(splashSize);
#if QT_VERSION > 0x050100
2017-02-05 18:48:01 +01:00
if (!useMorePixels) // change to HiDPI if it makes sense
pixmap.setDevicePixelRatio(devicePixelRatio);
#endif
2013-04-14 11:35:37 +02:00
2014-09-18 13:14:38 +02:00
QPainter pixPaint(&pixmap);
2018-01-16 12:41:03 +00:00
pixPaint.setPen(QColor(220,220,220));
pixPaint.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
2017-02-05 18:48:01 +01:00
if (useMorePixels) // change to HiDPI if it makes sense
pixPaint.scale(devicePixelRatio, devicePixelRatio);
2013-04-14 11:35:37 +02:00
2015-08-09 00:17:27 +01:00
// draw a slightly radial gradient
QRadialGradient gradient(QPoint(0,0), splashSize.width()/devicePixelRatio);
gradient.setColorAt(0, Qt::white);
gradient.setColorAt(1, QColor(247,247,247));
QRect rGradient(QPoint(0,0), splashSize);
pixPaint.fillRect(rGradient, gradient);
2018-01-16 12:41:03 +00:00
// draw the bitcoin icon, expected size of PNG: 1000x655
QRect rectIcon(QPoint(8, 5), QSize(333, 218));
2017-01-30 12:17:25 +01:00
QImage icon = networkStyle.getAppIcon();
2018-01-16 12:41:03 +00:00
Q_ASSERT(icon.width() == 1000);
Q_ASSERT(icon.height() == 655);
2017-01-30 12:17:25 +01:00
pixPaint.drawImage(rectIcon, icon);
2017-02-05 18:48:01 +01:00
// check font size and drawing width
float fontFactor = 1.0;
if (useMorePixels) // fonts are set in Point, but we used painter::scale(), so we have to counter that.
fontFactor /= devicePixelRatio;
2013-04-14 11:35:37 +02:00
pixPaint.setFont(QFont(font, 15*fontFactor));
2018-01-16 12:41:03 +00:00
QFontMetrics fm(pixPaint.fontMetrics());
2013-04-14 11:35:37 +02:00
// if the version string is to long, reduce size
int versionTextWidth = fm.width(versionText);
2018-01-16 12:41:03 +00:00
if (versionTextWidth > paddingRight-10) {
2013-04-14 11:35:37 +02:00
pixPaint.setFont(QFont(font, 10*fontFactor));
2018-01-16 12:41:03 +00:00
versionTextWidth = pixPaint.fontMetrics().width(versionText);
2013-04-14 11:35:37 +02:00
}
2018-01-16 12:41:03 +00:00
pixPaint.drawText(pixmap.width() / devicePixelRatio - versionTextWidth - paddingRight, paddingTop, versionText);
2013-04-14 11:35:37 +02:00
2014-10-09 11:04:49 +02:00
// draw additional text if special network
2018-01-16 12:41:03 +00:00
if (!titleAddText.isEmpty()) {
2013-04-14 11:35:37 +02:00
QFont boldFont = QFont(font, 10*fontFactor);
boldFont.setWeight(QFont::Bold);
pixPaint.setFont(boldFont);
fm = pixPaint.fontMetrics();
2014-10-09 11:04:49 +02:00
int titleAddTextWidth = fm.width(titleAddText);
2018-01-16 12:41:03 +00:00
pixPaint.drawText(pixmap.width()/devicePixelRatio-titleAddTextWidth-paddingRight, 22 ,titleAddText);
2013-04-14 11:35:37 +02:00
}
pixPaint.end();
2014-09-18 13:14:38 +02:00
// Set window title
2014-10-09 11:04:49 +02:00
setWindowTitle(titleText + " " + titleAddText);
2014-09-18 13:14:38 +02:00
2017-02-05 18:48:01 +01:00
if (useMorePixels) // if scaling uses more pixels, actually allow the windows to have larger pixelsize
devicePixelRatio = 1;
2014-09-18 13:14:38 +02:00
// Resize window and move to center of desktop, disallow resizing
QRect r(QPoint(), QSize(pixmap.size().width()/devicePixelRatio,pixmap.size().height()/devicePixelRatio));
2014-09-18 13:14:38 +02:00
resize(r.size());
setFixedSize(r.size());
move(QApplication::desktop()->screenGeometry().center() - r.center());
2014-01-08 08:59:24 +01:00
subscribeToCoreSignals();
}
SplashScreen::~SplashScreen()
{
unsubscribeFromCoreSignals();
}
void SplashScreen::slotFinish(QWidget *mainWin)
{
2014-09-22 15:48:34 +02:00
Q_UNUSED(mainWin);
2014-09-18 13:14:38 +02:00
hide();
2014-01-08 08:59:24 +01:00
}
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(55,55,55)));
}
2014-03-19 00:26:14 +01:00
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
2014-01-08 08:59:24 +01:00
void SplashScreen::subscribeToCoreSignals()
{
// Connect signals to client
uiInterface.InitMessage.connect(boost::bind(InitMessage, this, _1));
2014-05-23 18:04:09 +02:00
uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
2014-03-19 00:26:14 +01:00
#ifdef ENABLE_WALLET
uiInterface.LoadWallet.connect(boost::bind(ConnectWallet, this, _1));
#endif
2014-01-08 08:59:24 +01:00
}
void SplashScreen::unsubscribeFromCoreSignals()
{
// Disconnect signals from client
uiInterface.InitMessage.disconnect(boost::bind(InitMessage, this, _1));
2014-05-23 18:04:09 +02:00
uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
2014-03-19 00:26:14 +01:00
#ifdef ENABLE_WALLET
if(pwalletMain)
pwalletMain->ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
#endif
2013-04-14 11:35:37 +02:00
}
2014-09-18 13:14:38 +02:00
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, -5);
painter.setPen(curColor);
painter.drawText(r, curAlignment, curMessage);
}
void SplashScreen::closeEvent(QCloseEvent *event)
{
StartShutdown(); // allows an "emergency" shutdown during startup
event->ignore();
}