~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/widgets/qsplashscreen.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the widgets module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qsplashscreen.h"
 
30
 
 
31
#ifndef QT_NO_SPLASHSCREEN
 
32
 
 
33
#include "qapplication.h"
 
34
#include "qdesktopwidget.h"
 
35
#include "qpainter.h"
 
36
#include "qpixmap.h"
 
37
#include <private/qwidget_p.h>
 
38
 
 
39
class QSplashScreenPrivate : public QWidgetPrivate
 
40
{
 
41
    Q_DECLARE_PUBLIC(QSplashScreen)
 
42
public:
 
43
    QPixmap pixmap;
 
44
    QString currStatus;
 
45
    QColor currColor;
 
46
    int currAlign;
 
47
    void drawContents();
 
48
};
 
49
 
 
50
/*!
 
51
   \class QSplashScreen qsplashscreen.h
 
52
   \brief The QSplashScreen widget provides a splash screen that can
 
53
   be shown during application startup.
 
54
 
 
55
    \ingroup misc
 
56
    \mainclass
 
57
 
 
58
   A splash screen is a widget that is usually displayed when an
 
59
   application is being started. Splash screens are often used for
 
60
   applications that have long start up times (e.g. database or
 
61
   networking applications that take time to establish connections) to
 
62
   provide the user with feedback that the application is loading.
 
63
 
 
64
   The splash screen appears centered on the screen. It may be useful to add
 
65
   the \c Qt::WStyle_StaysOnTop if you desire to keep above all the windows in the
 
66
   GUI.
 
67
 
 
68
   Some X11 window managers do not support the "stays on top" flag. A
 
69
   solution is to set up a timer that periodically calls raise() on
 
70
   the splash screen to simulate the "stays on top" effect.
 
71
 
 
72
   The most common usage is to show a splash screen before the main
 
73
   widget is displayed on the screen. This is illustrated in the
 
74
   following code snippet.
 
75
 
 
76
   \code
 
77
   int main(int argc, char **argv)
 
78
   {
 
79
       QApplication app(argc, argv);
 
80
       QPixmap pixmap("splash.png");
 
81
       QSplashScreen *splash = new QSplashScreen(pixmap);
 
82
       splash->show();
 
83
       QMainWindow *mainWin = new QMainWindow;
 
84
       ...
 
85
       app.setMainWidget(mainWin);
 
86
       mainWin->show();
 
87
       splash->finish(mainWin);
 
88
       delete splash;
 
89
       return app.exec();
 
90
   }
 
91
   \endcode
 
92
 
 
93
   It is sometimes useful to update the splash screen with messages,
 
94
   for example, announcing connections established or modules loaded
 
95
   as the application starts up. QSplashScreen supports this with the
 
96
   showMessage() function. If you wish to do your own drawing you can
 
97
   get a pointer to the pixmap used in the splash screen with pixmap().
 
98
   Alternatively, you can subclass QSplashScreen and reimplement
 
99
   drawContents().
 
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
   \code
 
107
   QSplashScreen *splash = new QSplashScreen("splash.png");
 
108
   splash->show();
 
109
   ... // Loading some items
 
110
   splash->showMessage("Loaded modules");
 
111
   qApp->processEvents();
 
112
   ... // Establishing connections
 
113
   splash->showMessage("Established connections");
 
114
   qApp->processEvents();
 
115
   \endcode
 
116
 
 
117
*/
 
118
 
 
119
/*!
 
120
    Construct a splash screen that will display the \a pixmap.
 
121
 
 
122
    There should be no need to set the widget flags, \a f, except
 
123
    perhaps \c Qt::WStyle_StaysOnTop.
 
124
*/
 
125
QSplashScreen::QSplashScreen(const QPixmap &pixmap, Qt::WFlags f)
 
126
    : QWidget(*(new QSplashScreenPrivate()), 0, Qt::SplashScreen | f)
 
127
{
 
128
    d_func()->pixmap = pixmap;
 
129
    setPixmap(d_func()->pixmap);  // Does an implicit repaint
 
130
}
 
131
 
 
132
/*!
 
133
    \overload
 
134
 
 
135
    This function allows you to specify a parent for your splashscreen. The
 
136
    typical use for this constructor is if you have a multiple screens and
 
137
    prefer to have the splash screen on a different screen than your primary
 
138
    one. In that case pass the proper desktop() as the \a parent.
 
139
*/
 
140
QSplashScreen::QSplashScreen(QWidget *parent, const QPixmap &pixmap, Qt::WFlags f)
 
141
    : QWidget(*new QSplashScreenPrivate, parent, Qt::SplashScreen | f)
 
142
{
 
143
    d_func()->pixmap = pixmap;
 
144
    setPixmap(d_func()->pixmap);  // Does an implicit repaint
 
145
}
 
146
 
 
147
/*!
 
148
  Destructor.
 
149
*/
 
150
QSplashScreen::~QSplashScreen()
 
151
{
 
152
}
 
153
 
 
154
/*!
 
155
    \reimp
 
156
*/
 
157
void QSplashScreen::mousePressEvent(QMouseEvent *)
 
158
{
 
159
    hide();
 
160
}
 
161
 
 
162
/*!
 
163
    This overrides QWidget::repaint(). It differs from the standard
 
164
    repaint function in that it also calls QApplication::flush() to
 
165
    ensure the updates are displayed, even when there is no event loop
 
166
    present.
 
167
*/
 
168
void QSplashScreen::repaint()
 
169
{
 
170
    d_func()->drawContents();
 
171
    QWidget::repaint();
 
172
    QApplication::flush();
 
173
}
 
174
 
 
175
/*!
 
176
    \fn QSplashScreen::messageChanged(const QString &message)
 
177
 
 
178
    This signal is emitted when the message on the splash screen
 
179
    changes. \a message is the new message and is a null-string
 
180
    when the message has been removed.
 
181
 
 
182
    \sa showMessage(), clearMessage()
 
183
*/
 
184
 
 
185
 
 
186
 
 
187
/*!
 
188
    Draws the \a message text onto the splash screen with color \a
 
189
    color and aligns the text according to the flags in \a alignment.
 
190
 
 
191
    \sa Qt::Alignment, clearMessage()
 
192
*/
 
193
void QSplashScreen::showMessage(const QString &message, int alignment,
 
194
                             const QColor &color)
 
195
{
 
196
    Q_D(QSplashScreen);
 
197
    d->currStatus = message;
 
198
    d->currAlign = alignment;
 
199
    d->currColor = color;
 
200
    emit messageChanged(d->currStatus);
 
201
    repaint();
 
202
}
 
203
 
 
204
/*!
 
205
    Removes the message being displayed on the splash screen
 
206
 
 
207
    \sa showMessage()
 
208
 */
 
209
void QSplashScreen::clearMessage()
 
210
{
 
211
    d_func()->currStatus.clear();
 
212
    emit messageChanged(d_func()->currStatus);
 
213
    repaint();
 
214
}
 
215
 
 
216
/*!
 
217
    Makes the splash screen wait until the widget \a mainWin is displayed
 
218
    before calling close() on itself.
 
219
*/
 
220
void QSplashScreen::finish(QWidget *mainWin)
 
221
{
 
222
    if (mainWin) {
 
223
#if defined(Q_WS_X11)
 
224
        extern void qt_x11_wait_for_window_manager(QWidget *mainWin);
 
225
        qt_x11_wait_for_window_manager(mainWin);
 
226
#endif
 
227
    }
 
228
    close();
 
229
}
 
230
 
 
231
/*!
 
232
    Sets the pixmap that will be used as the splash screen's image to
 
233
    \a pixmap.
 
234
*/
 
235
void QSplashScreen::setPixmap(const QPixmap &pixmap)
 
236
{
 
237
    Q_D(QSplashScreen);
 
238
    d->pixmap = pixmap;
 
239
    QRect r(0, 0, d->pixmap.size().width(), d->pixmap.size().height());
 
240
    resize(d->pixmap.size());
 
241
    move(QApplication::desktop()->screenGeometry().center() - r.center());
 
242
    if (!isVisible())
 
243
        d->drawContents();
 
244
    else
 
245
        repaint();
 
246
}
 
247
 
 
248
/*!
 
249
    Returns the pixmap that is used in the splash screen. The image
 
250
    does not have any of the text drawn by showMessage() calls.
 
251
*/
 
252
const QPixmap QSplashScreen::pixmap() const
 
253
{
 
254
    return d_func()->pixmap;
 
255
}
 
256
 
 
257
/*!
 
258
  \internal
 
259
*/
 
260
void QSplashScreenPrivate::drawContents()
 
261
{
 
262
    Q_Q(QSplashScreen);
 
263
    QPixmap textPix = pixmap;
 
264
    QPainter painter(&textPix);
 
265
    q->drawContents(&painter);
 
266
    QPalette p = q->palette();
 
267
    p.setBrush(q->backgroundRole(), QBrush(textPix));
 
268
    q->setPalette(p);
 
269
}
 
270
 
 
271
/*!
 
272
    Draw the contents of the splash screen using painter \a painter.
 
273
    The default implementation draws the message passed by showMessage().
 
274
    Reimplement this function if you want to do your own drawing on
 
275
    the splash screen.
 
276
*/
 
277
void QSplashScreen::drawContents(QPainter *painter)
 
278
{
 
279
    painter->setPen(d_func()->currColor);
 
280
    QRect r = rect();
 
281
    r.setRect(r.x() + 5, r.y() + 5, r.width() - 10, r.height() - 10);
 
282
    painter->drawText(r, d_func()->currAlign, d_func()->currStatus);
 
283
}
 
284
 
 
285
/*!
 
286
    \fn void QSplashScreen::message(const QString &message, int alignment,
 
287
                                    const QColor &color)
 
288
    \compat
 
289
 
 
290
    Use showMessage() instead.
 
291
*/
 
292
 
 
293
/*!
 
294
    \fn void QSplashScreen::clear()
 
295
    \compat
 
296
 
 
297
    Use clearMessage() instead.
 
298
*/
 
299
 
 
300
#endif //QT_NO_SPLASHSCREEN