qsplashscreen.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9