1 /*************************************************************************
2              Splash.cpp  -  splash screen for Kwave
3                              -------------------
4     begin                : Tue Jun 24 2003
5     copyright            : Copyright (C) 2003 Gilles CAULIER
6     email                : Gilles CAULIER <caulier.gilles@free.fr>
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <QApplication>
21 #include <QDesktopWidget>
22 #include <QFont>
23 #include <QLabel>
24 #include <QPainter>
25 #include <QPixmap>
26 #include <QRect>
27 #include <QScreen>
28 #include <QString>
29 #include <QThread>
30 
31 #include <KAboutData>
32 #include <KLocalizedString>
33 
34 #include <QStandardPaths>
35 
36 #include "libkwave/String.h"
37 
38 #include "Splash.h"
39 
40 // static pointer to the current instance
41 QPointer<Kwave::Splash> Kwave::Splash::m_splash = Q_NULLPTR;
42 
43 //***************************************************************************
Splash(const QString & PNGFile)44 Kwave::Splash::Splash(const QString &PNGFile)
45     :QFrame(Q_NULLPTR, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint),
46      m_font(),
47      m_pixmap(QStandardPaths::locate(QStandardPaths::AppDataLocation, PNGFile)),
48      m_message(_("   "))
49 {
50     const int w = m_pixmap.width();
51     const int h = m_pixmap.height();
52     setFixedSize(w, h);
53 
54     QPainter p;
55     p.begin(&m_pixmap);
56 
57     // get all the strings that we should display
58     const KAboutData about_data = KAboutData::applicationData();
59     QString version = i18nc("%1=Version number", "v%1", about_data.version());
60 
61     m_font.setBold(true);
62     m_font.setStyleHint(QFont::Decorative);
63     m_font.setWeight(QFont::Black);
64     p.setFont(m_font);
65 
66     QFontMetrics fm(m_font);
67     QRect rect = fm.boundingRect(version);
68 
69     // version
70     const int r  = 5;
71     const int th = rect.height();
72     const int tw = rect.width();
73     const int x  = w - 10 - tw;
74     const int y  = h - 10 - th;
75 
76     const QBrush brush(palette().window().color());
77     p.setBrush(brush);
78     p.setOpacity(0.50);
79     p.setPen(Qt::NoPen);
80     p.drawRoundedRect(
81 	x - r, y - r,
82 	tw + 2 * r, th + (2 * r),
83 	(200 * r) / th, (200 * r) / th,
84 	Qt::RelativeSize
85     );
86 
87     p.setOpacity(1.0);
88     p.setPen(palette().buttonText().color());
89     p.drawText(x, y, tw, th, Qt::AlignCenter, version);
90 
91     p.end();
92 
93     // center on the screen
94     rect = QRect(QPoint(), m_pixmap.size());
95     resize(rect.size());
96 
97     QScreen *screen = qApp->primaryScreen();
98     Q_ASSERT(screen);
99     QRect screen_rect;
100     screen_rect.setSize(screen->availableSize());
101     move(screen_rect.center() - rect.center());
102 
103     m_splash = this;
104 }
105 
106 //***************************************************************************
done()107 void Kwave::Splash::done()
108 {
109     // check: start() must be called from the GUI thread only!
110     Q_ASSERT(this->thread() == QThread::currentThread());
111     Q_ASSERT(this->thread() == qApp->thread());
112     m_splash = Q_NULLPTR;
113     hide();
114 }
115 
116 //***************************************************************************
~Splash()117 Kwave::Splash::~Splash()
118 {
119     m_splash = Q_NULLPTR;
120 }
121 
122 //***************************************************************************
showMessage(const QString & message)123 void Kwave::Splash::showMessage(const QString &message)
124 {
125     if (!m_splash) return;
126     m_splash->m_message = message;
127     m_splash->repaint();
128     QApplication::processEvents(QEventLoop::AllEvents);
129 }
130 
131 //***************************************************************************
paintEvent(QPaintEvent *)132 void Kwave::Splash::paintEvent(QPaintEvent *)
133 {
134     // special handling: a null message tells us to hide
135     if (!m_message.length()) {
136         m_splash = Q_NULLPTR;
137 	hide();
138 	return;
139     }
140 
141     QRect rect(this->rect());
142     const int border = 5;
143     rect.setRect(
144 	rect.x() + border,
145 	rect.y() + border,
146 	rect.width()  - (2 * border),
147 	rect.height() - (2 * border)
148     );
149 
150     QPainter p(this);
151     p.drawPixmap(this->rect(), m_pixmap);
152     p.setFont(m_font);
153     p.setPen(palette().buttonText().color());
154     p.drawText(rect, Qt::AlignLeft | Qt::AlignTop, m_message);
155 }
156 
157 //***************************************************************************
mousePressEvent(QMouseEvent *)158 void Kwave::Splash::mousePressEvent(QMouseEvent *)
159 {
160     done();
161 }
162 
163 //***************************************************************************
164 //***************************************************************************
165