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