MainBar: fixup file extension filter in "Import File" dialog
authorGerhard Sittig <gerhard.sittig@gmx.net>
Sun, 4 Feb 2018 13:54:32 +0000 (14:54 +0100)
committerUwe Hermann <uwe@hermann-uwe.de>
Sat, 10 Feb 2018 19:35:32 +0000 (20:35 +0100)
The previous implementation used a "*" filter when a file of any other
format than srzip got imported. This happened to be a duplicate of
"All files", and ignored the list of filename extensions provided by
the input formats.

This change does respect the input format's file extensions, and copes
with the lack of such a list (raw binary), as well as lists that have
one (most formats), or multiple extensions (raw analog).

This fixes bug #1039.

Another byproduct of the change is that extensions and their decoration
(separators, parentheses) move outside of tr() calls. These technical
details shall not concern translators, and translations for human
languages shall not break the filter mechanism.

This implementation might be "too complex, computationally expensive".
But it works, and fixes an issue, and the code path executes seldom and
waits for user interaction anyway. Cost reduction can get applied later.

pv/toolbars/mainbar.cpp

index c7b373341041f0bb76491b4ac70709e65da3e03c..1879693d57a31c01bc87458f556d67dd01db7506 100644 (file)
@@ -668,16 +668,19 @@ void MainBar::import_file(shared_ptr<InputFormat> format)
 
        // Construct the filter
        const vector<string> exts = format->extensions();
-       const QString filter = exts.empty() ? "" :
-               tr("%1 files (*.%2)").arg(
-                       QString::fromStdString(format->description()),
-                       QString::fromStdString(join(exts, ", *.")));
+       const QString filter_exts = exts.empty() ? "" : QString::fromStdString("%1 (%2)").arg(
+               tr("%1 files").arg(QString::fromStdString(format->description())),
+               QString::fromStdString("*.%1").arg(QString::fromStdString(join(exts, " *."))));
+       const QString filter_all = QString::fromStdString("%1 (%2)").arg(
+               tr("All Files"), QString::fromStdString("*"));
+       const QString filter = QString::fromStdString("%1%2%3").arg(
+               exts.empty() ? "" : filter_exts,
+               exts.empty() ? "" : ";;",
+               filter_all);
 
        // Show the file dialog
        const QString file_name = QFileDialog::getOpenFileName(
-               this, tr("Import File"), dir, tr(
-                       "%1 files (*);;All Files (*)").arg(
-                       QString::fromStdString(format->description())));
+               this, tr("Import File"), dir, filter);
 
        if (file_name.isEmpty())
                return;