86 if (!d->addedPixmaps.isEmpty()) {
87 const auto key = d->hashKey(mode, state);
88 auto it = d->addedPixmaps.constFind(key);
89 while (it != d->addedPixmaps.end() && it.key() == key) {
90 const auto &pm = it.value();
91 if (!pm.isNull() && pm.size() == size)
97 QPixmap pm = pixmap(size, mode, state);
114 const auto key = hashKey(mode, state);
115 QByteArray buf = svgBuffers.value(key);
116 if (!buf.isEmpty()) {
117 if (renderer->load(maybeUncompress(buf)))
119 svgBuffers.remove(key);
121 QString svgFile = svgFiles.value(key);
122 if (!svgFile.isEmpty()) {
123 if (renderer->load(svgFile))
129QIcon::Mode
QSvgIconEnginePrivate::loadDataForModeAndState(QSvgRenderer *renderer, QIcon::Mode mode, QIcon::State state)
131 if (tryLoad(renderer, mode, state))
134 const QIcon::State oppositeState = (state == QIcon::On) ? QIcon::Off : QIcon::On;
135 if (mode == QIcon::Disabled || mode == QIcon::Selected) {
136 const QIcon::Mode oppositeMode = (mode == QIcon::Disabled) ? QIcon::Selected : QIcon::Disabled;
137 if (tryLoad(renderer, QIcon::Normal, state))
138 return QIcon::Normal;
139 if (tryLoad(renderer, QIcon::Active, state))
140 return QIcon::Active;
141 if (tryLoad(renderer, mode, oppositeState))
143 if (tryLoad(renderer, QIcon::Normal, oppositeState))
144 return QIcon::Normal;
145 if (tryLoad(renderer, QIcon::Active, oppositeState))
146 return QIcon::Active;
147 if (tryLoad(renderer, oppositeMode, state))
149 if (tryLoad(renderer, oppositeMode, oppositeState))
152 const QIcon::Mode oppositeMode = (mode == QIcon::Normal) ? QIcon::Active : QIcon::Normal;
153 if (tryLoad(renderer, oppositeMode, state))
155 if (tryLoad(renderer, mode, oppositeState))
157 if (tryLoad(renderer, oppositeMode, oppositeState))
159 if (tryLoad(renderer, QIcon::Disabled, state))
160 return QIcon::Disabled;
161 if (tryLoad(renderer, QIcon::Selected, state))
162 return QIcon::Selected;
163 if (tryLoad(renderer, QIcon::Disabled, oppositeState))
164 return QIcon::Disabled;
165 if (tryLoad(renderer, QIcon::Selected, oppositeState))
166 return QIcon::Selected;
168 return QIcon::Normal;
182 QString pmckey(d->pmcKey(size, mode, state, scale));
183 if (QPixmapCache::find(pmckey, &pm))
186 if (!d->addedPixmaps.isEmpty()) {
187 const auto realSize = size * scale;
188 const auto key = d->hashKey(mode, state);
189 auto it = d->addedPixmaps.constFind(key);
190 while (it != d->addedPixmaps.end() && it.key() == key) {
191 const auto &pm = it.value();
195 if (pm.size() == realSize)
203 const QIcon::Mode loadmode = d->loadDataForModeAndState(&renderer, mode, state);
204 if (!renderer.isValid())
207 QSize actualSize = renderer.defaultSize();
208 if (!actualSize.isNull())
209 actualSize.scale(size * scale, Qt::KeepAspectRatio);
211 if (actualSize.isEmpty())
214 pm = QPixmap(actualSize);
215 pm.fill(Qt::transparent);
219 if (qobject_cast<QGuiApplication *>(QCoreApplication::instance())) {
220 if (loadmode != mode && mode != QIcon::Normal) {
221 const QPixmap generated = QGuiApplicationPrivate::instance()->applyQIconStyleHelper(mode, pm);
222 if (!generated.isNull())
228 pm.setDevicePixelRatio(scale);
229 QPixmapCache::insert(pmckey, pm);
247 const QString &suffix = fi.completeSuffix();
248 if (suffix.endsWith(QLatin1String(
"svg"), Qt::CaseInsensitive))
250 if (suffix.endsWith(QLatin1String(
"svgz"), Qt::CaseInsensitive)
251 || suffix.endsWith(QLatin1String(
"svg.gz"), Qt::CaseInsensitive)) {
254#if QT_CONFIG(mimetype)
255 const QString &mimeTypeName = QMimeDatabase().mimeTypeForFile(fi).name();
256 if (mimeTypeName == QLatin1String(
"image/svg+xml"))
258 if (mimeTypeName == QLatin1String(
"image/svg+xml-compressed"))
259 return CompressedSvgFile;
265 QIcon::Mode mode, QIcon::State state)
267 if (!fileName.isEmpty()) {
268 const QFileInfo fi(fileName);
269 const QString abs = fi.absoluteFilePath();
271#ifndef QT_NO_COMPRESS
274 if (type == SvgFile) {
277 if (renderer.isValid()) {
279 d->svgFiles.insert(d->hashKey(mode, state), abs);
284 addPixmap(pm, mode, state);
290 QIcon::Mode mode, QIcon::State state)
292 QSize pixmapSize = rect.size();
293 if (painter->device())
294 pixmapSize *= painter->device()->devicePixelRatio();
295 painter->drawPixmap(rect, pixmap(pixmapSize, mode, state));
316 d =
new QSvgIconEnginePrivate;
318 if (in.version() >= QDataStream::Qt_4_4) {
320 QHash<
int, QString> fileNames;
321 in >> fileNames >> isCompressed >> d->svgBuffers;
322#ifndef QT_NO_COMPRESS
324 for (
auto &svgBuf : d->svgBuffers)
325 svgBuf = qCompress(svgBuf);
329 qWarning(
"QSvgIconEngine: Can not decompress SVG data");
330 d->svgBuffers.clear();
334 in >> hasAddedPixmaps;
335 if (hasAddedPixmaps) {
336 in >> d->addedPixmaps;
347 if (!data.isEmpty()) {
348#ifndef QT_NO_COMPRESS
349 data = qUncompress(data);
352 d->svgBuffers.insert(d->hashKey(QIcon::Normal, QIcon::Off), data);
355 for (
int i=0; i<num_entries; ++i) {
372 if (out.version() >= QDataStream::Qt_4_4) {
373 int isCompressed = 0;
374#ifndef QT_NO_COMPRESS
377 QHash<
int, QByteArray> svgBuffers = d->svgBuffers;
378 for (
const auto &it : d->svgFiles.asKeyValueRange()) {
381 if (f.open(QIODevice::ReadOnly))
383#ifndef QT_NO_COMPRESS
384 buf = qCompress(buf);
386 svgBuffers.insert(it.first, buf);
388 out << d->svgFiles << isCompressed << svgBuffers;
389 if (d->addedPixmaps.isEmpty())
392 out << 1 << d->addedPixmaps;
395 const auto key = d->hashKey(QIcon::Normal, QIcon::Off);
396 QByteArray buf = d->svgBuffers.value(key);
398 QString svgFile = d->svgFiles.value(key);
399 if (!svgFile.isEmpty()) {
401 if (f.open(QIODevice::ReadOnly))
405#ifndef QT_NO_COMPRESS
406 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.