tiled icon indicating copy to clipboard operation
tiled copied to clipboard

PNG files cannot be added as tileset (on linux)

Open Ablu opened this issue 10 years ago • 14 comments

If a file has the extension .PNG it cannot be added as tileset image. Neither in the "missing tileset dialog" nor in the usual "add tileset dialog"

Ablu avatar Jan 17 '16 17:01 Ablu

Are you trying to add a .png as if it was a .tsx file?

bjorn avatar Jan 17 '16 19:01 bjorn

No. As normal tileset image.

Ablu avatar Jan 18 '16 12:01 Ablu

Hmm, I can't reproduce that. Maybe a little more information about your environment?

bjorn avatar Jan 18 '16 12:01 bjorn

Hm. You changed the title, but I really meant files with a .PNG so dot, uppercase PNG, not lowercase.

System is Gnome3, Fedora 23.

I can try to look into it somewhen to. but probably not the next days.

Ablu avatar Jan 20 '16 11:01 Ablu

Ah, thanks for clearing that up! I'll try to reproduce it with a file with uppercase PNG as extension as well then.

bjorn avatar Jan 20 '16 21:01 bjorn

Tiled 0.15.0, Xubuntu 14.04.1, XFCE Can't reproduce the problem. mytileset.PNG is added as well as any other images. Maybe this is some specific image or any file which has *.PNG extension is not added?

IMMZ avatar Jan 24 '16 08:01 IMMZ

Hmm, I can also add such tilesets fine, but only when I type in their name, since they are not selectable in the file open dialog. So probably the problem is that it is doing a case-sensitive extension comparison. Is that the problem you mean, @Ablu?

bjorn avatar Jan 24 '16 10:01 bjorn

In my case such files are even selectable from open file dialog with last repo version. tiled

IMMZ avatar Jan 24 '16 10:01 IMMZ

Hmm, interesting. May depend on the GTK or Qt version. I'm using Qt 5.5.1 and GTK 2.24.29.

bjorn avatar Jan 24 '16 12:01 bjorn

May be the file was just renamed and is not a png.

williamespindola avatar Jan 26 '16 23:01 williamespindola

Reproducible on:

Operating System: Linux Mint 22.1                 
Kernel: Linux 6.11.0-29-generic

Only files with lowercase extensions are available in the browse dialog. This is an environment specific issue and is out of the control of Qt.

diff --git a/src/tiled/utils.cpp b/src/tiled/utils.cpp
index 51abd7c6..43d5165a 100644
--- a/src/tiled/utils.cpp
+++ b/src/tiled/utils.cpp
@@ -46,6 +46,8 @@
 #include <QProcess>
 #include <QRegularExpression>
 #include <QScreen>
+#include <QString>
+#include <QRegularExpression>
 
 #if QT_VERSION >= QT_VERSION_CHECK(6, 3, 0) && QT_VERSION < QT_VERSION_CHECK(6, 5, 1)
 #include <QtCore/qapplicationstatic.h>
@@ -70,6 +72,29 @@ static QString toImageFileFilter(const QList<QByteArray> &formats)
 namespace Tiled {
 namespace Utils {
 
+static QString addUpperCaseExtensionsTo(const QString &filter)
+{
+    QString result = filter;
+    QRegularExpression re(QStringLiteral(R"(\*\.(\w+))"));
+    QRegularExpressionMatchIterator i = re.globalMatch(filter);
+
+    while (i.hasNext()) {
+        QRegularExpressionMatch match = i.next();
+        QString ext = match.captured(1);
+        QString extUpper = ext.toUpper();
+
+        const auto starDot = QStringLiteral("*.");
+        const auto space = QStringLiteral(" ");
+
+        if (ext != extUpper) {
+            QString oldPattern = starDot + ext;
+            QString newPattern = starDot + ext + space + starDot + extUpper;
+            result.replace(oldPattern, newPattern);
+        }
+    }
+    return result;
+}
+
 /**
  * Returns a file dialog filter that matches all readable image formats.
  *
@@ -81,7 +106,7 @@ QString readableImageFormatsFilter()
     auto imageFilter = toImageFileFilter(QImageReader::supportedImageFormats());
 
     FormatHelper<MapFormat> helper(FileFormat::Read, imageFilter);
-    return helper.filter();
+    return addUpperCaseExtensionsTo(helper.filter());
 }
 
 /**

cannister-of-sparrows avatar Jul 13 '25 20:07 cannister-of-sparrows

@cannister-of-sparrows Interesting fix, but I'm wondering if we should really resort to such hacks. If the environment explicitly wants to consider "png" and "PNG" as different extensions, users would have this problem with all their files in all their applications and the only real options would be to:

  • Get that file picker to not be case-sensitive, in case it's not intentional.
  • The user needs to learn that file extensions are case-sensitive on their platform.

If this applies to your system, can you verify how other applications behave?

bjorn avatar Jul 14 '25 10:07 bjorn

From the user perspective, they don't know about filter strings. The user only sees "Image files" of which, .PNG certainly is. There is no opportunity, on my system at least, to become aware of any filter string containing *.png.

Image

Qt Creator itself exhibits the same behaviour when browsing for files to open. As do other applications. However, I don't think it is correct to blame the environment or the user.

cannister-of-sparrows avatar Jul 14 '25 10:07 cannister-of-sparrows

Mime types, as here, allow case insensitive file discovery on my system.

cannister-of-sparrows avatar Jul 14 '25 10:07 cannister-of-sparrows