+ move to Qt's built-in search system for icon resources

This commit is contained in:
wmayer 2015-04-14 11:37:24 +02:00
parent 2dc1890fd2
commit 0e91902e74

View File

@ -64,7 +64,6 @@ class BitmapFactoryInstP
public: public:
QMap<std::string, const char**> xpmMap; QMap<std::string, const char**> xpmMap;
QMap<std::string, QPixmap> xpmCache; QMap<std::string, QPixmap> xpmCache;
QStringList paths;
}; };
} }
@ -126,18 +125,22 @@ void BitmapFactoryInst::restoreCustomPaths()
void BitmapFactoryInst::addPath(const QString& path) void BitmapFactoryInst::addPath(const QString& path)
{ {
d->paths.push_back(path); QDir::addSearchPath(QString::fromLatin1("icons"), path);
} }
void BitmapFactoryInst::removePath(const QString& path) void BitmapFactoryInst::removePath(const QString& path)
{ {
int pos = d->paths.indexOf(path); QStringList iconPaths = QDir::searchPaths(QString::fromLatin1("icons"));
if (pos != -1) d->paths.removeAt(pos); int pos = iconPaths.indexOf(path);
if (pos != -1) {
iconPaths.removeAt(pos);
QDir::setSearchPaths(QString::fromLatin1("icons"), iconPaths);
}
} }
QStringList BitmapFactoryInst::getPaths() const QStringList BitmapFactoryInst::getPaths() const
{ {
return d->paths; return QDir::searchPaths(QString::fromLatin1("icons"));
} }
QStringList BitmapFactoryInst::findIconFiles() const QStringList BitmapFactoryInst::findIconFiles() const
@ -147,7 +150,7 @@ QStringList BitmapFactoryInst::findIconFiles() const
for (QList<QByteArray>::iterator it = formats.begin(); it != formats.end(); ++it) for (QList<QByteArray>::iterator it = formats.begin(); it != formats.end(); ++it)
filters << QString::fromAscii("*.%1").arg(QString::fromAscii(*it).toLower()); filters << QString::fromAscii("*.%1").arg(QString::fromAscii(*it).toLower());
QStringList paths = d->paths; QStringList paths = QDir::searchPaths(QString::fromLatin1("icons"));
#if QT_VERSION >= 0x040500 #if QT_VERSION >= 0x040500
paths.removeDuplicates(); paths.removeDuplicates();
#endif #endif
@ -191,7 +194,7 @@ bool BitmapFactoryInst::loadPixmap(const QString& filename, QPixmap& icon) const
if (fi.exists()) { if (fi.exists()) {
// first check if it's an SVG because Qt's qsvg4 module shouldn't be used therefore // first check if it's an SVG because Qt's qsvg4 module shouldn't be used therefore
if (fi.suffix().toLower() == QLatin1String("svg")) { if (fi.suffix().toLower() == QLatin1String("svg")) {
QFile svgFile(filename); QFile svgFile(fi.filePath());
if (svgFile.open(QFile::ReadOnly | QFile::Text)) { if (svgFile.open(QFile::ReadOnly | QFile::Text)) {
QByteArray content = svgFile.readAll(); QByteArray content = svgFile.readAll();
icon = pixmapFromSvg(content, QSize(64,64)); icon = pixmapFromSvg(content, QSize(64,64));
@ -199,7 +202,7 @@ bool BitmapFactoryInst::loadPixmap(const QString& filename, QPixmap& icon) const
} }
else { else {
// try with Qt plugins // try with Qt plugins
icon.load(filename); icon.load(fi.filePath());
} }
} }
@ -227,17 +230,13 @@ QPixmap BitmapFactoryInst::pixmap(const char* name) const
if (icon.isNull()) if (icon.isNull())
loadPixmap(fn, icon); loadPixmap(fn, icon);
// try to find it in the given directories // try to find it in the 'icons' search paths
if (icon.isNull()) { if (icon.isNull()) {
QList<QByteArray> formats = QImageReader::supportedImageFormats(); QList<QByteArray> formats = QImageReader::supportedImageFormats();
formats.prepend("SVG"); // check first for SVG to use special import mechanism formats.prepend("SVG"); // check first for SVG to use special import mechanism
for (QStringList::ConstIterator pt = d->paths.begin(); pt != d->paths.end(); ++pt) {
QDir d(*pt); QString fileName = QString::fromLatin1("icons:") + fn;
QString fileName = d.filePath(fn); if (!loadPixmap(fileName, icon)) {
if (loadPixmap(fileName, icon)) {
break;
}
else {
// Go through supported file formats // Go through supported file formats
for (QList<QByteArray>::iterator fm = formats.begin(); fm != formats.end(); ++fm) { for (QList<QByteArray>::iterator fm = formats.begin(); fm != formats.end(); ++fm) {
QString path = QString::fromAscii("%1.%2").arg(fileName). QString path = QString::fromAscii("%1.%2").arg(fileName).
@ -246,10 +245,6 @@ QPixmap BitmapFactoryInst::pixmap(const char* name) const
break; break;
} }
} }
if (!icon.isNull())
break;
}
} }
} }
@ -271,20 +266,18 @@ QPixmap BitmapFactoryInst::pixmapFromSvg(const char* name, const QSize& size) co
if (QFile(fn).exists()) if (QFile(fn).exists())
iconPath = fn; iconPath = fn;
// try to find it in the given directories // try to find it in the 'icons' search paths
if (iconPath.isEmpty()) { if (iconPath.isEmpty()) {
for (QStringList::ConstIterator pt = d->paths.begin(); pt != d->paths.end(); ++pt) { QString fileName = QString::fromLatin1("icons:") + fn;
QDir d(*pt); QFileInfo fi(fileName);
QString fileName = d.filePath(fn); if (fi.exists()) {
if (QFile(fileName).exists()) { iconPath = fi.filePath();
iconPath = fileName;
break;
} else {
fileName += QLatin1String(".svg");
if (QFile(fileName).exists()) {
iconPath = fileName;
break;
} }
else {
fileName += QLatin1String(".svg");
fi.setFile(fileName);
if (fi.exists()) {
iconPath = fi.filePath();
} }
} }
} }