Qt 4.8
qsplashscreen.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
56 
58 {
60 public:
64  int currAlign;
65 
66  inline QSplashScreenPrivate();
67 };
68 
125 QSplashScreen::QSplashScreen(const QPixmap &pixmap, Qt::WindowFlags f)
127 {
128  setPixmap(pixmap); // Does an implicit repaint
129 }
130 
143  : QWidget(*new QSplashScreenPrivate, parent, Qt::SplashScreen | f)
144 {
145  d_func()->pixmap = pixmap;
146  setPixmap(d_func()->pixmap); // Does an implicit repaint
147 }
148 
153 {
154 }
155 
160 {
161  hide();
162 }
163 
171 {
174 }
175 
204 void QSplashScreen::showMessage(const QString &message, int alignment,
205  const QColor &color)
206 {
208  d->currStatus = message;
209  d->currAlign = alignment;
210  d->currColor = color;
211  emit messageChanged(d->currStatus);
212  repaint();
213 }
214 
221 {
222  d_func()->currStatus.clear();
223  emit messageChanged(d_func()->currStatus);
224  repaint();
225 }
226 
232 {
233  if (mainWin) {
234 #if defined(Q_WS_X11)
235  extern void qt_x11_wait_for_window_manager(QWidget *mainWin, bool);
236  qt_x11_wait_for_window_manager(mainWin, false);
237 #endif
238  }
239  close();
240 }
241 
247 {
249 
250  d->pixmap = pixmap;
252 
253  QRect r(QPoint(), d->pixmap.size());
254  resize(r.size());
255  move(QApplication::desktop()->screenGeometry().center() - r.center());
256  if (isVisible())
257  repaint();
258 }
259 
265 {
266  return d_func()->pixmap;
267 }
268 
273 {
274 }
275 
283 {
285  painter->setPen(d->currColor);
286  QRect r = rect().adjusted(5, 5, -5, -5);
287  if (Qt::mightBeRichText(d->currStatus)) {
288  QTextDocument doc;
289 #ifdef QT_NO_TEXTHTMLPARSER
290  doc.setPlainText(d->currStatus);
291 #else
292  doc.setHtml(d->currStatus);
293 #endif
294  doc.setTextWidth(r.width());
295  QTextCursor cursor(&doc);
297  QTextBlockFormat fmt;
298  fmt.setAlignment(Qt::Alignment(d->currAlign));
299  cursor.mergeBlockFormat(fmt);
300  painter->save();
301  painter->translate(r.topLeft());
302  doc.drawContents(painter);
303  painter->restore();
304  } else {
305  painter->drawText(r, d->currAlign, d->currStatus);
306  }
307 }
308 
332 {
333  if (e->type() == QEvent::Paint) {
335  QPainter painter(this);
336  if (!d->pixmap.isNull())
337  painter.drawPixmap(QPoint(), d->pixmap);
338  drawContents(&painter);
339  }
340  return QWidget::event(e);
341 }
342 
344 
345 #endif //QT_NO_SPLASHSCREEN
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
double d
Definition: qnumeric_p.h:62
virtual ~QSplashScreen()
Destructor.
QRect adjusted(int x1, int y1, int x2, int y2) const
Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of ...
Definition: qrect.h:431
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool event(QEvent *e)
Reimplemented Function
bool isVisible() const
Definition: qwidget.h:1005
void restore()
Restores the current painter state (pops a saved state off the stack).
Definition: qpainter.cpp:1620
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
Q_GUI_EXPORT bool mightBeRichText(const QString &)
Returns true if the string text is likely to be rich text; otherwise returns false.
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
The QString class provides a Unicode character string.
Definition: qstring.h:83
void mergeBlockFormat(const QTextBlockFormat &modifier)
Modifies the block format of the current block (or all blocks that are contained in the selection) wi...
#define Q_D(Class)
Definition: qglobal.h:2482
void save()
Saves the current painter state (pushes the state onto a stack).
Definition: qpainter.cpp:1590
const QPixmap pixmap() const
Returns the pixmap that is used in the splash screen.
QTextStream & center(QTextStream &stream)
Calls QTextStream::setFieldAlignment(QTextStream::AlignCenter) on stream and returns stream...
void setPlainText(const QString &text)
Replaces the entire contents of the document with the given plain text.
void drawText(const QPointF &p, const QString &s)
Draws the given text with the currently defined text direction, beginning at the given position...
Definition: qpainter.cpp:6231
void finish(QWidget *w)
Makes the splash screen wait until the widget mainWin is displayed before calling close() on itself...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QTextCursor class offers an API to access and modify QTextDocuments.
Definition: qtextcursor.h:70
QSplashScreen(const QPixmap &pixmap=QPixmap(), Qt::WindowFlags f=0)
Construct a splash screen that will display the pixmap.
#define emit
Definition: qobjectdefs.h:76
void setHtml(const QString &html)
Replaces the entire contents of the document with the given HTML-formatted text in the html string...
void select(SelectionType selection)
Selects text in the document according to the given selection.
void hide()
Hides the widget.
Definition: qwidget.h:501
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
Q_CORE_EXPORT QTextStream & center(QTextStream &s)
static QDesktopWidget * desktop()
Returns the desktop widget (also called the root window).
void repaint()
Repaints the widget directly by calling paintEvent() immediately, unless updates are disabled or the ...
Definition: qwidget.cpp:10761
The QSplashScreen widget provides a splash screen that can be shown during application startup...
Definition: qsplashscreen.h:57
void drawContents(QPainter *painter, const QRectF &rect=QRectF())
Draws the content of the document with painter p, clipped to rect.
void setAlignment(Qt::Alignment alignment)
Sets the paragraph&#39;s alignment.
Definition: qtextformat.h:619
void setPixmap(const QPixmap &pixmap)
Sets the pixmap that will be used as the splash screen&#39;s image to pixmap.
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
void resize(int w, int h)
This corresponds to resize(QSize(w, h)).
Definition: qwidget.h:1014
void setTextWidth(qreal width)
bool hasAlpha() const
Returns true if this pixmap has an alpha channel, or has a mask, otherwise returns false...
Definition: qpixmap.cpp:1938
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void clearMessage()
Removes the message being displayed on the splash screen.
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QTextBlockFormat class provides formatting information for blocks of text in a QTextDocument...
Definition: qtextformat.h:545
void setPen(const QColor &color)
Sets the painter&#39;s pen to have style Qt::SolidLine, width 0 and the specified color.
Definition: qpainter.cpp:4047
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
void qt_x11_wait_for_window_manager(QWidget *w, bool sendPostedEvents)
Definition: qnamespace.h:54
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
virtual void drawContents(QPainter *painter)
Draw the contents of the splash screen using painter painter.
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
The QTextDocument class holds formatted text that can be viewed and edited using a QTextEdit...
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device...
Definition: qpainter.cpp:5619
bool close()
Closes this widget.
Definition: qwidget.cpp:8305
bool event(QEvent *)
This is the main event handler; it handles event event.
Definition: qwidget.cpp:8636
void mousePressEvent(QMouseEvent *)
Reimplemented Function
void messageChanged(const QString &message)
This signal is emitted when the message on the splash screen changes.
void showMessage(const QString &message, int alignment=Qt::AlignLeft, const QColor &color=Qt::black)
Draws the message text onto the splash screen with color color and aligns the text according to the f...
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
static void flush()
Flushes the platform specific event queues.
void move(int x, int y)
This corresponds to move(QPoint(x, y)).
Definition: qwidget.h:1011
QPoint topLeft() const
Returns the position of the rectangle&#39;s top-left corner.
Definition: qrect.h:288
void repaint()
This overrides QWidget::repaint().
void translate(const QPointF &offset)
Translates the coordinate system by the given offset; i.e.
Definition: qpainter.cpp:3311