summaryrefslogtreecommitdiff
path: root/src/plugins/iconengines
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2016-08-26 11:40:54 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2016-08-30 12:25:48 +0000
commit4469fbecd4e004979dada2da72573c06719fee3c (patch)
tree425c37ad9a151bf6667562e2758077d29eefb7de /src/plugins/iconengines
parent55825068ab0e3102bf5f8843c0cccab1ff120ae1 (diff)
downloadqtsvg-4469fbecd4e004979dada2da72573c06719fee3c.tar.gz
QSvgIconEngine::addFile(): Use QMimeDatabase to determine file type
Introduce an enumeration to describe the file type and use QMimeDatbase to determine the type. This will detect SVG files also in the case resource aliases without suffixes are used. As a drive by, do not pass compressed SVG files to QPixmap when QT_NO_COMPRESS is defined, as this likely results in garbage. Task-number: QTBUG-55388 Change-Id: Ib8bafca60e9238412a5c7edf560d487e512b8fd0 Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Diffstat (limited to 'src/plugins/iconengines')
-rw-r--r--src/plugins/iconengines/svgiconengine/qsvgiconengine.cpp56
1 files changed, 38 insertions, 18 deletions
diff --git a/src/plugins/iconengines/svgiconengine/qsvgiconengine.cpp b/src/plugins/iconengines/svgiconengine/qsvgiconengine.cpp
index 713f2ad..1342c0b 100644
--- a/src/plugins/iconengines/svgiconengine/qsvgiconengine.cpp
+++ b/src/plugins/iconengines/svgiconengine/qsvgiconengine.cpp
@@ -45,6 +45,8 @@
#include "qsvgrenderer.h"
#include "qpixmapcache.h"
#include "qfileinfo.h"
+#include <qmimedatabase.h>
+#include <qmimetype.h>
#include <QAtomicInt>
#include "qdebug.h"
#include <private/qguiapplication_p.h>
@@ -201,31 +203,49 @@ void QSvgIconEngine::addPixmap(const QPixmap &pixmap, QIcon::Mode mode,
d->addedPixmaps->insert(d->hashKey(mode, state), pixmap);
}
+enum FileType { OtherFile, SvgFile, CompressedSvgFile };
+
+static FileType fileType(const QFileInfo &fi)
+{
+ const QString &abs = fi.absoluteFilePath();
+ if (abs.endsWith(QLatin1String(".svg"), Qt::CaseInsensitive))
+ return SvgFile;
+ if (abs.endsWith(QLatin1String(".svgz"), Qt::CaseInsensitive)
+ || abs.endsWith(QLatin1String(".svg.gz"), Qt::CaseInsensitive)) {
+ return CompressedSvgFile;
+ }
+#ifndef QT_NO_MIMETYPE
+ const QString &mimeTypeName = QMimeDatabase().mimeTypeForFile(fi).name();
+ if (mimeTypeName == QLatin1String("image/svg+xml"))
+ return SvgFile;
+ if (mimeTypeName == QLatin1String("image/svg+xml-compressed"))
+ return CompressedSvgFile;
+#endif // !QT_NO_MIMETYPE
+ return OtherFile;
+}
void QSvgIconEngine::addFile(const QString &fileName, const QSize &,
QIcon::Mode mode, QIcon::State state)
{
if (!fileName.isEmpty()) {
- QString abs = fileName;
- if (fileName.at(0) != QLatin1Char(':'))
- abs = QFileInfo(fileName).absoluteFilePath();
- if (abs.endsWith(QLatin1String(".svg"), Qt::CaseInsensitive)
+ const QFileInfo fi(fileName);
+ const QString abs = fi.absoluteFilePath();
+ const FileType type = fileType(fi);
#ifndef QT_NO_COMPRESS
- || abs.endsWith(QLatin1String(".svgz"), Qt::CaseInsensitive)
- || abs.endsWith(QLatin1String(".svg.gz"), Qt::CaseInsensitive)
+ if (type == SvgFile || type == CompressedSvgFile) {
+#else
+ if (type == SvgFile) {
#endif
- )
- {
- QSvgRenderer renderer(abs);
- if (renderer.isValid()) {
- d->stepSerialNum();
- d->svgFiles.insert(d->hashKey(mode, state), abs);
- }
- } else {
- QPixmap pm(abs);
- if (!pm.isNull())
- addPixmap(pm, mode, state);
- }
+ QSvgRenderer renderer(abs);
+ if (renderer.isValid()) {
+ d->stepSerialNum();
+ d->svgFiles.insert(d->hashKey(mode, state), abs);
+ }
+ } else if (type == OtherFile) {
+ QPixmap pm(abs);
+ if (!pm.isNull())
+ addPixmap(pm, mode, state);
+ }
}
}