88 if (!d->addedPixmaps.isEmpty()) {
89 const auto key = d->hashKey(mode, state);
90 auto it = d->addedPixmaps.constFind(key);
91 while (it != d->addedPixmaps.end() && it.key() == key) {
92 const auto &pm = it.value();
93 if (!pm.isNull() && pm.size() == size)
99 QPixmap pm = pixmap(size, mode, state);
116 const auto key = hashKey(mode, state);
117 QByteArray buf = svgBuffers.value(key);
118 if (!buf.isEmpty()) {
119 if (renderer->load(maybeUncompress(buf)))
121 svgBuffers.remove(key);
123 QString svgFile = svgFiles.value(key);
124 if (!svgFile.isEmpty()) {
125 if (renderer->load(svgFile))
131QIcon::Mode
QSvgIconEnginePrivate::loadDataForModeAndState(QSvgRenderer *renderer, QIcon::Mode mode, QIcon::State state)
133 if (tryLoad(renderer, mode, state))
136 const QIcon::State oppositeState = (state == QIcon::On) ? QIcon::Off : QIcon::On;
137 if (mode == QIcon::Disabled || mode == QIcon::Selected) {
138 const QIcon::Mode oppositeMode = (mode == QIcon::Disabled) ? QIcon::Selected : QIcon::Disabled;
139 if (tryLoad(renderer, QIcon::Normal, state))
140 return QIcon::Normal;
141 if (tryLoad(renderer, QIcon::Active, state))
142 return QIcon::Active;
143 if (tryLoad(renderer, mode, oppositeState))
145 if (tryLoad(renderer, QIcon::Normal, oppositeState))
146 return QIcon::Normal;
147 if (tryLoad(renderer, QIcon::Active, oppositeState))
148 return QIcon::Active;
149 if (tryLoad(renderer, oppositeMode, state))
151 if (tryLoad(renderer, oppositeMode, oppositeState))
154 const QIcon::Mode oppositeMode = (mode == QIcon::Normal) ? QIcon::Active : QIcon::Normal;
155 if (tryLoad(renderer, oppositeMode, state))
157 if (tryLoad(renderer, mode, oppositeState))
159 if (tryLoad(renderer, oppositeMode, oppositeState))
161 if (tryLoad(renderer, QIcon::Disabled, state))
162 return QIcon::Disabled;
163 if (tryLoad(renderer, QIcon::Selected, state))
164 return QIcon::Selected;
165 if (tryLoad(renderer, QIcon::Disabled, oppositeState))
166 return QIcon::Disabled;
167 if (tryLoad(renderer, QIcon::Selected, oppositeState))
168 return QIcon::Selected;
170 return QIcon::Normal;
184 QString pmckey(d->pmcKey(size, mode, state, scale));
185 if (QPixmapCache::find(pmckey, &pm))
188 if (!d->addedPixmaps.isEmpty()) {
189 const auto realSize = size * scale;
190 const auto key = d->hashKey(mode, state);
191 auto it = d->addedPixmaps.constFind(key);
192 while (it != d->addedPixmaps.end() && it.key() == key) {
193 const auto &pm = it.value();
197 if (pm.size() == realSize)
205 const QIcon::Mode loadmode = d->loadDataForModeAndState(&renderer, mode, state);
206 if (!renderer.isValid())
209 QSize actualSize = renderer.defaultSize();
210 if (!actualSize.isNull())
211 actualSize.scale(size * scale, Qt::KeepAspectRatio);
213 if (actualSize.isEmpty())
216 pm = QPixmap(actualSize);
217 pm.fill(Qt::transparent);
221 if (qobject_cast<QGuiApplication *>(QCoreApplication::instance())) {
222 if (loadmode != mode && mode != QIcon::Normal) {
223 const QPixmap generated = QGuiApplicationPrivate::instance()->applyQIconStyleHelper(mode, pm);
224 if (!generated.isNull())
230 pm.setDevicePixelRatio(scale);
231 QPixmapCache::insert(pmckey, pm);
249 const QString &suffix = fi.completeSuffix();
250 if (suffix.endsWith(QLatin1String(
"svg"), Qt::CaseInsensitive))
252 if (suffix.endsWith(QLatin1String(
"svgz"), Qt::CaseInsensitive)
253 || suffix.endsWith(QLatin1String(
"svg.gz"), Qt::CaseInsensitive)) {
256#if QT_CONFIG(mimetype)
257 const QString &mimeTypeName = QMimeDatabase().mimeTypeForFile(fi).name();
258 if (mimeTypeName == QLatin1String(
"image/svg+xml"))
260 if (mimeTypeName == QLatin1String(
"image/svg+xml-compressed"))
261 return CompressedSvgFile;
267 QIcon::Mode mode, QIcon::State state)
269 if (!fileName.isEmpty()) {
270 const QFileInfo fi(fileName);
271 const QString abs = fi.absoluteFilePath();
273#ifndef QT_NO_COMPRESS
276 if (type == SvgFile) {
279 if (renderer.isValid()) {
281 d->svgFiles.insert(d->hashKey(mode, state), abs);
286 addPixmap(pm, mode, state);
292 QIcon::Mode mode, QIcon::State state)
294 QSize pixmapSize = rect.size();
295 if (painter->device())
296 pixmapSize *= painter->device()->devicePixelRatio();
297 painter->drawPixmap(rect, pixmap(pixmapSize, mode, state));
318 d =
new QSvgIconEnginePrivate;
320 if (in.version() >= QDataStream::Qt_4_4) {
322 QHash<
int, QString> fileNames;
323 in >> fileNames >> isCompressed >> d->svgBuffers;
324#ifndef QT_NO_COMPRESS
326 for (
auto &svgBuf : d->svgBuffers)
327 svgBuf = qCompress(svgBuf);
331 qWarning(
"QSvgIconEngine: Can not decompress SVG data");
332 d->svgBuffers.clear();
336 in >> hasAddedPixmaps;
337 if (hasAddedPixmaps) {
338 in >> d->addedPixmaps;
349 if (!data.isEmpty()) {
350#ifndef QT_NO_COMPRESS
351 data = qUncompress(data);
354 d->svgBuffers.insert(d->hashKey(QIcon::Normal, QIcon::Off), data);
357 for (
int i=0; i<num_entries; ++i) {
374 if (out.version() >= QDataStream::Qt_4_4) {
375 int isCompressed = 0;
376#ifndef QT_NO_COMPRESS
379 QHash<
int, QByteArray> svgBuffers = d->svgBuffers;
380 for (
const auto &it : d->svgFiles.asKeyValueRange()) {
383 if (f.open(QIODevice::ReadOnly))
385#ifndef QT_NO_COMPRESS
386 buf = qCompress(buf);
388 svgBuffers.insert(it.first, buf);
390 out << d->svgFiles << isCompressed << svgBuffers;
391 if (d->addedPixmaps.isEmpty())
394 out << 1 << d->addedPixmaps;
397 const auto key = d->hashKey(QIcon::Normal, QIcon::Off);
398 QByteArray buf = d->svgBuffers.value(key);
400 QString svgFile = d->svgFiles.value(key);
401 if (!svgFile.isEmpty()) {
403 if (f.open(QIODevice::ReadOnly))
407#ifndef QT_NO_COMPRESS
408 buf = qCompress(buf);
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override
Returns the icon as a pixmap with the required size, mode, and state.
void addFile(const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state) override
Called by QIcon::addFile().
QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state) override
Returns the actual size of the icon the engine provides for the requested size, mode and state.