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