widgets/qsplashscreen.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtGui module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qsplashscreen.h" -
43 -
44#ifndef QT_NO_SPLASHSCREEN -
45 -
46#include "qapplication.h" -
47#include "qdesktopwidget.h" -
48#include "qpainter.h" -
49#include "qpixmap.h" -
50#include "qtextdocument.h" -
51#include "qtextcursor.h" -
52#include <QtGui/qwindow.h> -
53#include <QtCore/qdebug.h> -
54#include <QtCore/qelapsedtimer.h> -
55#include <private/qwidget_p.h> -
56 -
57#ifdef Q_OS_WIN -
58# include <QtCore/qt_windows.h> -
59#else -
60# include <time.h> -
61#endif -
62 -
63QT_BEGIN_NAMESPACE -
64 -
65class QSplashScreenPrivate : public QWidgetPrivate -
66{ -
67 Q_DECLARE_PUBLIC(QSplashScreen) -
68public: -
69 QPixmap pixmap; -
70 QString currStatus; -
71 QColor currColor; -
72 int currAlign; -
73 -
74 inline QSplashScreenPrivate(); -
75}; -
76 -
77/*! -
78 \class QSplashScreen -
79 \brief The QSplashScreen widget provides a splash screen that can -
80 be shown during application startup. -
81 -
82 \inmodule QtWidgets -
83 -
84 A splash screen is a widget that is usually displayed when an -
85 application is being started. Splash screens are often used for -
86 applications that have long start up times (e.g. database or -
87 networking applications that take time to establish connections) to -
88 provide the user with feedback that the application is loading. -
89 -
90 The splash screen appears in the center of the screen. It may be -
91 useful to add the Qt::WindowStaysOnTopHint to the splash widget's -
92 window flags if you want to keep it above all the other windows on -
93 the desktop. -
94 -
95 Some X11 window managers do not support the "stays on top" flag. A -
96 solution is to set up a timer that periodically calls raise() on -
97 the splash screen to simulate the "stays on top" effect. -
98 -
99 The most common usage is to show a splash screen before the main -
100 widget is displayed on the screen. This is illustrated in the -
101 following code snippet in which a splash screen is displayed and -
102 some initialization tasks are performed before the application's -
103 main window is shown: -
104 -
105 \snippet qsplashscreen/main.cpp 0 -
106 \dots -
107 \snippet qsplashscreen/main.cpp 1 -
108 -
109 The user can hide the splash screen by clicking on it with the -
110 mouse. Since the splash screen is typically displayed before the -
111 event loop has started running, it is necessary to periodically -
112 call QApplication::processEvents() to receive the mouse clicks. -
113 -
114 It is sometimes useful to update the splash screen with messages, -
115 for example, announcing connections established or modules loaded -
116 as the application starts up: -
117 -
118 \snippet code/src_gui_widgets_qsplashscreen.cpp 0 -
119 -
120 QSplashScreen supports this with the showMessage() function. If you -
121 wish to do your own drawing you can get a pointer to the pixmap -
122 used in the splash screen with pixmap(). Alternatively, you can -
123 subclass QSplashScreen and reimplement drawContents(). -
124*/ -
125 -
126/*! -
127 Construct a splash screen that will display the \a pixmap. -
128 -
129 There should be no need to set the widget flags, \a f, except -
130 perhaps Qt::WindowStaysOnTopHint. -
131*/ -
132QSplashScreen::QSplashScreen(const QPixmap &pixmap, Qt::WindowFlags f) -
133 : QWidget(*(new QSplashScreenPrivate()), 0, Qt::SplashScreen | Qt::FramelessWindowHint | f) -
134{ -
135 setPixmap(pixmap); // Does an implicit repaint
executed (the execution status of this line is deduced): setPixmap(pixmap);
-
136}
executed: }
Execution Count:1
1
137 -
138/*! -
139 \overload -
140 -
141 This function allows you to specify a parent for your splashscreen. The -
142 typical use for this constructor is if you have a multiple screens and -
143 prefer to have the splash screen on a different screen than your primary -
144 one. In that case pass the proper desktop() as the \a parent. -
145*/ -
146QSplashScreen::QSplashScreen(QWidget *parent, const QPixmap &pixmap, Qt::WindowFlags f) -
147 : QWidget(*new QSplashScreenPrivate, parent, Qt::SplashScreen | f) -
148{ -
149 d_func()->pixmap = pixmap;
never executed (the execution status of this line is deduced): d_func()->pixmap = pixmap;
-
150 setPixmap(d_func()->pixmap); // Does an implicit repaint
never executed (the execution status of this line is deduced): setPixmap(d_func()->pixmap);
-
151}
never executed: }
0
152 -
153/*! -
154 Destructor. -
155*/ -
156QSplashScreen::~QSplashScreen() -
157{ -
158} -
159 -
160/*! -
161 \reimp -
162*/ -
163void QSplashScreen::mousePressEvent(QMouseEvent *) -
164{ -
165 hide();
never executed (the execution status of this line is deduced): hide();
-
166}
never executed: }
0
167 -
168/*! -
169 This overrides QWidget::repaint(). It differs from the standard -
170 repaint function in that it also calls QApplication::flush() to -
171 ensure the updates are displayed, even when there is no event loop -
172 present. -
173*/ -
174void QSplashScreen::repaint() -
175{ -
176 QWidget::repaint();
never executed (the execution status of this line is deduced): QWidget::repaint();
-
177 QApplication::flush();
never executed (the execution status of this line is deduced): QApplication::flush();
-
178}
never executed: }
0
179 -
180/*! -
181 \fn QSplashScreen::messageChanged(const QString &message) -
182 -
183 This signal is emitted when the message on the splash screen -
184 changes. \a message is the new message and is a null-string -
185 when the message has been removed. -
186 -
187 \sa showMessage(), clearMessage() -
188*/ -
189 -
190 -
191 -
192/*! -
193 Draws the \a message text onto the splash screen with color \a -
194 color and aligns the text according to the flags in \a alignment. -
195 -
196 To make sure the splash screen is repainted immediately, you can -
197 call \l{QCoreApplication}'s -
198 \l{QCoreApplication::}{processEvents()} after the call to -
199 showMessage(). You usually want this to make sure that the message -
200 is kept up to date with what your application is doing (e.g., -
201 loading files). -
202 -
203 \sa Qt::Alignment, clearMessage() -
204*/ -
205void QSplashScreen::showMessage(const QString &message, int alignment, -
206 const QColor &color) -
207{ -
208 Q_D(QSplashScreen);
never executed (the execution status of this line is deduced): QSplashScreenPrivate * const d = d_func();
-
209 d->currStatus = message;
never executed (the execution status of this line is deduced): d->currStatus = message;
-
210 d->currAlign = alignment;
never executed (the execution status of this line is deduced): d->currAlign = alignment;
-
211 d->currColor = color;
never executed (the execution status of this line is deduced): d->currColor = color;
-
212 emit messageChanged(d->currStatus);
never executed (the execution status of this line is deduced): messageChanged(d->currStatus);
-
213 repaint();
never executed (the execution status of this line is deduced): repaint();
-
214}
never executed: }
0
215 -
216/*! -
217 Removes the message being displayed on the splash screen -
218 -
219 \sa showMessage() -
220 */ -
221void QSplashScreen::clearMessage() -
222{ -
223 d_func()->currStatus.clear();
never executed (the execution status of this line is deduced): d_func()->currStatus.clear();
-
224 emit messageChanged(d_func()->currStatus);
never executed (the execution status of this line is deduced): messageChanged(d_func()->currStatus);
-
225 repaint();
never executed (the execution status of this line is deduced): repaint();
-
226}
never executed: }
0
227 -
228// A copy of Qt Test's qWaitForWindowExposed() and qSleep(). -
229inline static bool waitForWindowExposed(QWindow *window, int timeout = 1000) -
230{ -
231 enum { TimeOutMs = 10 };
never executed (the execution status of this line is deduced): enum { TimeOutMs = 10 };
-
232 QElapsedTimer timer;
never executed (the execution status of this line is deduced): QElapsedTimer timer;
-
233 timer.start();
never executed (the execution status of this line is deduced): timer.start();
-
234 while (!window->isExposed()) {
never evaluated: !window->isExposed()
0
235 const int remaining = timeout - int(timer.elapsed());
never executed (the execution status of this line is deduced): const int remaining = timeout - int(timer.elapsed());
-
236 if (remaining <= 0)
never evaluated: remaining <= 0
0
237 break;
never executed: break;
0
238 QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
never executed (the execution status of this line is deduced): QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
-
239 QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
never executed (the execution status of this line is deduced): QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
-
240#ifdef Q_OS_WIN -
241 Sleep(uint(TimeOutMs)); -
242#else -
243 struct timespec ts = { TimeOutMs / 1000, (TimeOutMs % 1000) * 1000 * 1000 };
never executed (the execution status of this line is deduced): struct timespec ts = { TimeOutMs / 1000, (TimeOutMs % 1000) * 1000 * 1000 };
-
244 nanosleep(&ts, NULL);
never executed (the execution status of this line is deduced): nanosleep(&ts, __null);
-
245#endif -
246 }
never executed: }
0
247 return window->isExposed();
never executed: return window->isExposed();
0
248} -
249 -
250/*! -
251 Makes the splash screen wait until the widget \a mainWin is displayed -
252 before calling close() on itself. -
253*/ -
254 -
255void QSplashScreen::finish(QWidget *mainWin) -
256{ -
257 if (mainWin && mainWin->windowHandle())
never evaluated: mainWin
never evaluated: mainWin->windowHandle()
0
258 waitForWindowExposed(mainWin->windowHandle());
never executed: waitForWindowExposed(mainWin->windowHandle());
0
259 close();
never executed (the execution status of this line is deduced): close();
-
260}
never executed: }
0
261 -
262/*! -
263 Sets the pixmap that will be used as the splash screen's image to -
264 \a pixmap. -
265*/ -
266void QSplashScreen::setPixmap(const QPixmap &pixmap) -
267{ -
268 Q_D(QSplashScreen);
executed (the execution status of this line is deduced): QSplashScreenPrivate * const d = d_func();
-
269 -
270 d->pixmap = pixmap;
executed (the execution status of this line is deduced): d->pixmap = pixmap;
-
271 setAttribute(Qt::WA_TranslucentBackground, pixmap.hasAlpha());
executed (the execution status of this line is deduced): setAttribute(Qt::WA_TranslucentBackground, pixmap.hasAlpha());
-
272 -
273 QRect r(QPoint(), d->pixmap.size());
executed (the execution status of this line is deduced): QRect r(QPoint(), d->pixmap.size());
-
274 resize(r.size());
executed (the execution status of this line is deduced): resize(r.size());
-
275 move(QApplication::desktop()->screenGeometry().center() - r.center());
executed (the execution status of this line is deduced): move(QApplication::desktop()->screenGeometry().center() - r.center());
-
276 if (isVisible())
partially evaluated: isVisible()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
277 repaint();
never executed: repaint();
0
278}
executed: }
Execution Count:1
1
279 -
280/*! -
281 Returns the pixmap that is used in the splash screen. The image -
282 does not have any of the text drawn by showMessage() calls. -
283*/ -
284const QPixmap QSplashScreen::pixmap() const -
285{ -
286 return d_func()->pixmap;
never executed: return d_func()->pixmap;
0
287} -
288 -
289/*! -
290 \internal -
291*/ -
292inline QSplashScreenPrivate::QSplashScreenPrivate() : currAlign(Qt::AlignLeft) -
293{ -
294}
executed: }
Execution Count:1
1
295 -
296/*! -
297 Draw the contents of the splash screen using painter \a painter. -
298 The default implementation draws the message passed by showMessage(). -
299 Reimplement this function if you want to do your own drawing on -
300 the splash screen. -
301*/ -
302void QSplashScreen::drawContents(QPainter *painter) -
303{ -
304 Q_D(QSplashScreen);
never executed (the execution status of this line is deduced): QSplashScreenPrivate * const d = d_func();
-
305 painter->setPen(d->currColor);
never executed (the execution status of this line is deduced): painter->setPen(d->currColor);
-
306 QRect r = rect().adjusted(5, 5, -5, -5);
never executed (the execution status of this line is deduced): QRect r = rect().adjusted(5, 5, -5, -5);
-
307 if (Qt::mightBeRichText(d->currStatus)) {
never evaluated: Qt::mightBeRichText(d->currStatus)
0
308 QTextDocument doc;
never executed (the execution status of this line is deduced): QTextDocument doc;
-
309#ifdef QT_NO_TEXTHTMLPARSER -
310 doc.setPlainText(d->currStatus); -
311#else -
312 doc.setHtml(d->currStatus);
never executed (the execution status of this line is deduced): doc.setHtml(d->currStatus);
-
313#endif -
314 doc.setTextWidth(r.width());
never executed (the execution status of this line is deduced): doc.setTextWidth(r.width());
-
315 QTextCursor cursor(&doc);
never executed (the execution status of this line is deduced): QTextCursor cursor(&doc);
-
316 cursor.select(QTextCursor::Document);
never executed (the execution status of this line is deduced): cursor.select(QTextCursor::Document);
-
317 QTextBlockFormat fmt;
never executed (the execution status of this line is deduced): QTextBlockFormat fmt;
-
318 fmt.setAlignment(Qt::Alignment(d->currAlign));
never executed (the execution status of this line is deduced): fmt.setAlignment(Qt::Alignment(d->currAlign));
-
319 cursor.mergeBlockFormat(fmt);
never executed (the execution status of this line is deduced): cursor.mergeBlockFormat(fmt);
-
320 painter->save();
never executed (the execution status of this line is deduced): painter->save();
-
321 painter->translate(r.topLeft());
never executed (the execution status of this line is deduced): painter->translate(r.topLeft());
-
322 doc.drawContents(painter);
never executed (the execution status of this line is deduced): doc.drawContents(painter);
-
323 painter->restore();
never executed (the execution status of this line is deduced): painter->restore();
-
324 } else {
never executed: }
0
325 painter->drawText(r, d->currAlign, d->currStatus);
never executed (the execution status of this line is deduced): painter->drawText(r, d->currAlign, d->currStatus);
-
326 }
never executed: }
0
327} -
328 -
329/*! \reimp */ -
330bool QSplashScreen::event(QEvent *e) -
331{ -
332 if (e->type() == QEvent::Paint) {
partially evaluated: e->type() == QEvent::Paint
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:13
0-13
333 Q_D(QSplashScreen);
never executed (the execution status of this line is deduced): QSplashScreenPrivate * const d = d_func();
-
334 QPainter painter(this);
never executed (the execution status of this line is deduced): QPainter painter(this);
-
335 if (!d->pixmap.isNull())
never evaluated: !d->pixmap.isNull()
0
336 painter.drawPixmap(QPoint(), d->pixmap);
never executed: painter.drawPixmap(QPoint(), d->pixmap);
0
337 drawContents(&painter);
never executed (the execution status of this line is deduced): drawContents(&painter);
-
338 }
never executed: }
0
339 return QWidget::event(e);
executed: return QWidget::event(e);
Execution Count:13
13
340} -
341 -
342QT_END_NAMESPACE -
343 -
344#endif //QT_NO_SPLASHSCREEN -
345 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial