source: trunk/src/gui/widgets/qsplashscreen.cpp@ 138

Last change on this file since 138 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 10.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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 <QtCore/qdebug.h>
53#include <private/qwidget_p.h>
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 void drawContents();
68};
69
70/*!
71 \class QSplashScreen
72 \brief The QSplashScreen widget provides a splash screen that can
73 be shown during application startup.
74
75 \ingroup misc
76 \mainclass
77
78 A splash screen is a widget that is usually displayed when an
79 application is being started. Splash screens are often used for
80 applications that have long start up times (e.g. database or
81 networking applications that take time to establish connections) to
82 provide the user with feedback that the application is loading.
83
84 The splash screen appears in the center of the screen. It may be
85 useful to add the Qt::WindowStaysOnTopHint to the splash widget's
86 window flags if you want to keep it above all the other windows on
87 the desktop.
88
89 Some X11 window managers do not support the "stays on top" flag. A
90 solution is to set up a timer that periodically calls raise() on
91 the splash screen to simulate the "stays on top" effect.
92
93 The most common usage is to show a splash screen before the main
94 widget is displayed on the screen. This is illustrated in the
95 following code snippet in which a splash screen is displayed and
96 some initialization tasks are performed before the application's
97 main window is shown:
98
99 \snippet doc/src/snippets/qsplashscreen/main.cpp 0
100 \dots
101 \snippet doc/src/snippets/qsplashscreen/main.cpp 1
102
103 The user can hide the splash screen by clicking on it with the
104 mouse. Since the splash screen is typically displayed before the
105 event loop has started running, it is necessary to periodically
106 call QApplication::processEvents() to receive the mouse clicks.
107
108 It is sometimes useful to update the splash screen with messages,
109 for example, announcing connections established or modules loaded
110 as the application starts up:
111
112 \snippet doc/src/snippets/code/src_gui_widgets_qsplashscreen.cpp 0
113
114 QSplashScreen supports this with the showMessage() function. If you
115 wish to do your own drawing you can get a pointer to the pixmap
116 used in the splash screen with pixmap(). Alternatively, you can
117 subclass QSplashScreen and reimplement drawContents().
118*/
119
120/*!
121 Construct a splash screen that will display the \a pixmap.
122
123 There should be no need to set the widget flags, \a f, except
124 perhaps Qt::WindowStaysOnTopHint.
125*/
126QSplashScreen::QSplashScreen(const QPixmap &pixmap, Qt::WindowFlags f)
127 : QWidget(*(new QSplashScreenPrivate()), 0, Qt::SplashScreen | f)
128{
129 d_func()->pixmap = pixmap;
130 setPixmap(d_func()->pixmap); // Does an implicit repaint
131}
132
133/*!
134 \overload
135
136 This function allows you to specify a parent for your splashscreen. The
137 typical use for this constructor is if you have a multiple screens and
138 prefer to have the splash screen on a different screen than your primary
139 one. In that case pass the proper desktop() as the \a parent.
140*/
141QSplashScreen::QSplashScreen(QWidget *parent, const QPixmap &pixmap, Qt::WindowFlags f)
142 : QWidget(*new QSplashScreenPrivate, parent, Qt::SplashScreen | f)
143{
144 d_func()->pixmap = pixmap;
145 setPixmap(d_func()->pixmap); // Does an implicit repaint
146}
147
148/*!
149 Destructor.
150*/
151QSplashScreen::~QSplashScreen()
152{
153}
154
155/*!
156 \reimp
157*/
158void QSplashScreen::mousePressEvent(QMouseEvent *)
159{
160 hide();
161}
162
163/*!
164 This overrides QWidget::repaint(). It differs from the standard
165 repaint function in that it also calls QApplication::flush() to
166 ensure the updates are displayed, even when there is no event loop
167 present.
168*/
169void QSplashScreen::repaint()
170{
171 d_func()->drawContents();
172 QWidget::repaint();
173 QApplication::flush();
174}
175
176/*!
177 \fn QSplashScreen::messageChanged(const QString &message)
178
179 This signal is emitted when the message on the splash screen
180 changes. \a message is the new message and is a null-string
181 when the message has been removed.
182
183 \sa showMessage(), clearMessage()
184*/
185
186
187
188/*!
189 Draws the \a message text onto the splash screen with color \a
190 color and aligns the text according to the flags in \a alignment.
191
192 \sa Qt::Alignment, clearMessage()
193*/
194void QSplashScreen::showMessage(const QString &message, int alignment,
195 const QColor &color)
196{
197 Q_D(QSplashScreen);
198 d->currStatus = message;
199 d->currAlign = alignment;
200 d->currColor = color;
201 emit messageChanged(d->currStatus);
202 repaint();
203}
204
205/*!
206 Removes the message being displayed on the splash screen
207
208 \sa showMessage()
209 */
210void QSplashScreen::clearMessage()
211{
212 d_func()->currStatus.clear();
213 emit messageChanged(d_func()->currStatus);
214 repaint();
215}
216
217/*!
218 Makes the splash screen wait until the widget \a mainWin is displayed
219 before calling close() on itself.
220*/
221void QSplashScreen::finish(QWidget *mainWin)
222{
223 if (mainWin) {
224#if defined(Q_WS_X11)
225 extern void qt_x11_wait_for_window_manager(QWidget *mainWin);
226 qt_x11_wait_for_window_manager(mainWin);
227#endif
228 }
229 close();
230}
231
232/*!
233 Sets the pixmap that will be used as the splash screen's image to
234 \a pixmap.
235*/
236void QSplashScreen::setPixmap(const QPixmap &pixmap)
237{
238 Q_D(QSplashScreen);
239
240 if (pixmap.hasAlpha()) {
241 QPixmap opaque(pixmap.size());
242 QPainter p(&opaque);
243 p.fillRect(0, 0, pixmap.width(), pixmap.height(), palette().background());
244 p.drawPixmap(0, 0, pixmap);
245 p.end();
246 d->pixmap = opaque;
247 } else {
248 d->pixmap = pixmap;
249 }
250
251 QRect r(0, 0, d->pixmap.size().width(), d->pixmap.size().height());
252 resize(d->pixmap.size());
253 move(QApplication::desktop()->screenGeometry().center() - r.center());
254 if (!isVisible())
255 d->drawContents();
256 else
257 repaint();
258}
259
260/*!
261 Returns the pixmap that is used in the splash screen. The image
262 does not have any of the text drawn by showMessage() calls.
263*/
264const QPixmap QSplashScreen::pixmap() const
265{
266 return d_func()->pixmap;
267}
268
269/*!
270 \internal
271*/
272void QSplashScreenPrivate::drawContents()
273{
274 Q_Q(QSplashScreen);
275 QPixmap textPix = pixmap;
276 if (!textPix.isNull()) {
277 QPainter painter(&textPix);
278 painter.initFrom(q);
279 q->drawContents(&painter);
280 QPalette p = q->palette();
281 p.setBrush(q->backgroundRole(), QBrush(textPix));
282 q->setPalette(p);
283 }
284}
285
286/*!
287 \internal
288*/
289inline QSplashScreenPrivate::QSplashScreenPrivate() : currAlign(Qt::AlignLeft)
290{
291}
292
293/*!
294 Draw the contents of the splash screen using painter \a painter.
295 The default implementation draws the message passed by showMessage().
296 Reimplement this function if you want to do your own drawing on
297 the splash screen.
298*/
299void QSplashScreen::drawContents(QPainter *painter)
300{
301 Q_D(QSplashScreen);
302 painter->setPen(d->currColor);
303 QRect r = rect();
304 r.setRect(r.x() + 5, r.y() + 5, r.width() - 10, r.height() - 10);
305 if (Qt::mightBeRichText(d->currStatus)) {
306 QTextDocument doc;
307#ifdef QT_NO_TEXTHTMLPARSER
308 doc.setPlainText(d->currStatus);
309#else
310 doc.setHtml(d->currStatus);
311#endif
312 doc.setTextWidth(r.width());
313 QTextCursor cursor(&doc);
314 cursor.select(QTextCursor::Document);
315 QTextBlockFormat fmt;
316 fmt.setAlignment(Qt::Alignment(d->currAlign));
317 cursor.mergeBlockFormat(fmt);
318 painter->save();
319 painter->translate(r.topLeft());
320 doc.drawContents(painter);
321 painter->restore();
322 } else {
323 painter->drawText(r, d->currAlign, d->currStatus);
324 }
325}
326
327/*!
328 \fn void QSplashScreen::message(const QString &message, int alignment,
329 const QColor &color)
330 \compat
331
332 Use showMessage() instead.
333*/
334
335/*!
336 \fn void QSplashScreen::clear()
337 \compat
338
339 Use clearMessage() instead.
340*/
341
342/*! \reimp */
343bool QSplashScreen::event(QEvent *e)
344{
345 return QWidget::event(e);
346}
347
348QT_END_NAMESPACE
349
350#endif //QT_NO_SPLASHSCREEN
Note: See TracBrowser for help on using the repository browser.