QtCanpool 3.3.0
载入中...
搜索中...
未找到
qxsplashscreen.cpp
浏览该文件的文档.
1
5#include "qxsplashscreen.h"
6
7#include <QtCore/QCoreApplication>
8#include <QtCore/QEventLoop>
9#include <QtGui/QColor>
10#include <QtGui/QFont>
11#include <QtGui/QPainter>
12
14
15namespace
16{
17
19const int kBandHeight = 46;
21const int kBandMargin = 14;
23const int kLineHeight = 18;
24
29QPixmap prepareLogo(const QPixmap &logo)
30{
31 if (!logo.isNull() && !logo.size().isEmpty()) {
32 return logo;
33 }
34 QPixmap fallback(480, 300);
35 fallback.fill(QColor(0x30, 0x30, 0x30));
36 return fallback;
37}
38
39} // namespace
40
41class QxSplashScreenPrivate
42{
43 QX_DECLARE_PUBLIC(QxSplashScreen)
44public:
45 QxSplashScreenPrivate();
46public:
47 QString m_applicationName;
48 QString m_version;
49 QString m_message;
50 int m_progress;
51};
52
53QxSplashScreenPrivate::QxSplashScreenPrivate()
54 : m_progress(-1)
55{
56}
57
58QxSplashScreen::QxSplashScreen(const QPixmap &logo, const QString &applicationName, const QString &version,
59 Qt::WindowFlags flags)
60 : QSplashScreen(prepareLogo(logo), flags)
61 , d_ptr(new QxSplashScreenPrivate())
62{
63 d_ptr->setPublic(this);
64 Q_D(QxSplashScreen);
65 d->m_applicationName = applicationName;
66 d->m_version = version;
67}
68
73
75{
76 Q_D(const QxSplashScreen);
77 return d->m_applicationName;
78}
79
81{
82 Q_D(const QxSplashScreen);
83 return d->m_version;
84}
85
87{
88 Q_D(const QxSplashScreen);
89 return d->m_message;
90}
91
93{
94 Q_D(const QxSplashScreen);
95 return d->m_progress;
96}
97
98void QxSplashScreen::setApplicationName(const QString &applicationName)
99{
100 Q_D(QxSplashScreen);
101 if (d->m_applicationName == applicationName) {
102 return;
103 }
104 d->m_applicationName = applicationName;
105 repaint();
106}
107
108void QxSplashScreen::setVersion(const QString &version)
109{
110 Q_D(QxSplashScreen);
111 if (d->m_version == version) {
112 return;
113 }
114 d->m_version = version;
115 repaint();
116}
117
118void QxSplashScreen::setMessage(const QString &message)
119{
120 Q_D(QxSplashScreen);
121 if (d->m_message == message) {
122 return;
123 }
124 d->m_message = message;
125 repaint();
126}
127
129{
130 Q_D(QxSplashScreen);
131 const int bounded = progress < 0 ? -1 : qBound(0, progress, 100);
132 if (d->m_progress == bounded) {
133 return;
134 }
135 d->m_progress = bounded;
136 repaint();
137}
138
139void QxSplashScreen::step(int progress, const QString &message)
140{
141 if (!message.isEmpty()) {
143 }
145
146 // The caller is blocking the event loop with start-up work, so the splash
147 // has to be driven from here. User input stays queued: the window is not
148 // ready to react yet.
149 if (QCoreApplication::instance()) {
150 QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
151 }
152}
153
154void QxSplashScreen::drawContents(QPainter *painter)
155{
156 Q_D(QxSplashScreen);
157
158 const QRect area = rect();
159 if (area.isEmpty()) {
160 return;
161 }
162
163 const int bandHeight = qMin(kBandHeight, area.height());
164 const QRect band(0, area.height() - bandHeight, area.width(), bandHeight);
165 painter->fillRect(band, QColor(0, 0, 0, 150));
166
167 const int margin = qMin(kBandMargin, area.width() / 4);
168 const QRect line(band.left() + margin, band.top() + 4, qMax(0, band.width() - 2 * margin), kLineHeight);
169
170 QFont font = painter->font();
171 font.setBold(true);
172 painter->setFont(font);
173 painter->setPen(QColor(0xff, 0xff, 0xff));
174
175 QString title = d->m_applicationName;
176 if (!d->m_version.isEmpty()) {
177 title += QStringLiteral(" ");
178 title += d->m_version;
179 }
180 painter->drawText(line, Qt::AlignLeft | Qt::AlignVCenter, title);
181
182 font.setBold(false);
183 painter->setFont(font);
184 painter->setPen(QColor(0xd0, 0xd0, 0xd0));
185
186 const QRect messageLine(line.left(), line.top() + kLineHeight, line.width(), kLineHeight);
187 painter->drawText(messageLine, Qt::AlignLeft | Qt::AlignVCenter, d->m_message);
188
189 if (d->m_progress < 0) {
190 return;
191 }
192
193 painter->setPen(QColor(0xff, 0xff, 0xff));
194 painter->drawText(messageLine, Qt::AlignRight | Qt::AlignVCenter, QStringLiteral("%1%").arg(d->m_progress));
195
196 const QRect track(band.left() + margin, band.bottom() - 3, qMax(0, band.width() - 2 * margin), 2);
197 painter->fillRect(track, QColor(0xff, 0xff, 0xff, 60));
198 QRect fill = track;
199 fill.setWidth(track.width() * d->m_progress / 100);
200 painter->fillRect(fill, QColor(0x2f, 0x7c, 0xd1));
201}
202
A splash screen that reports how far the start-up has come.
QString applicationName() const
int progress() const
Progress in percent, or -1 while it is unknown.
void drawContents(QPainter *painter) override
void setApplicationName(const QString &applicationName)
void setMessage(const QString &message)
void step(int progress, const QString &message=QString())
Sets the progress and the message in one call, then processes pending events so that the splash reall...
QString version() const
QString message() const
void setProgress(int progress)
Sets the progress to 0..100 percent; a negative value means "unknown".
void setVersion(const QString &version)
#define QX_APP_END_NAMESPACE
#define QX_DECLARE_PUBLIC(Class)
#define QX_APP_BEGIN_NAMESPACE
#define QX_FINI_PRIVATE()