changeset 1440:18101be79c9c

Add function to request multiple filenames
author Chris Cannam
date Tue, 23 Apr 2019 16:17:33 +0100
parents aa0616116537
children 8d5bf4ab98ef
files widgets/InteractiveFileFinder.cpp widgets/InteractiveFileFinder.h
diffstat 2 files changed, 99 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/widgets/InteractiveFileFinder.cpp	Thu Mar 21 15:25:05 2019 +0000
+++ b/widgets/InteractiveFileFinder.cpp	Tue Apr 23 16:17:33 2019 +0100
@@ -58,19 +58,52 @@
 }
 
 QString
-InteractiveFileFinder::getOpenFileName(FileType type, QString fallbackLocation)
+InteractiveFileFinder::getOpenFileName(FileType type,
+                                       QString fallbackLocation)
+{
+    QStringList names = getOpenFileNames(type,
+                                         fallbackLocation,
+                                         false);
+    if (names.empty()) return "";
+    else return names[0];
+}
+
+QStringList
+InteractiveFileFinder::getOpenFileNames(FileType type,
+                                        QString fallbackLocation)
+{
+    return getOpenFileNames(type,
+                            fallbackLocation,
+                            true);
+}
+
+QStringList
+InteractiveFileFinder::getOpenFileNames(FileType type,
+                                        QString fallbackLocation,
+                                        bool multiple)
 {
     QString settingsKeyStub;
     QString lastPath = fallbackLocation;
     
-    QString title = tr("Select file");
+    QString title;
+    if (multiple) {
+        title = tr("Select one or more files");
+    } else {
+        title = tr("Select file");
+    }
     QString filter = tr("All files (*.*)");
 
+    QStringList names;
+    
     switch (type) {
 
     case SessionFile:
         settingsKeyStub = "session";
-        title = tr("Select a session file");
+        if (multiple) {
+            title = tr("Select one or more session files");
+        } else {
+            title = tr("Select a session file");
+        }
         filter = tr("%1 session files (*.%2)\nRDF files (%3)\nAll files (*.*)")
             .arg(QApplication::applicationName())
             .arg(m_sessionExtension)
@@ -79,7 +112,11 @@
 
     case AudioFile:
         settingsKeyStub = "audio";
-        title = "Select an audio file";
+        if (multiple) {
+            title = tr("Select one or more audio files");
+        } else {
+            title = tr("Select an audio file");
+        }
         filter = tr("Audio files (%1)\nAll files (*.*)")
             .arg(AudioFileReaderFactory::getKnownExtensions());
         break;
@@ -173,8 +210,6 @@
     settings.beginGroup("FileFinder");
     lastPath = settings.value(settingsKeyStub + "path", lastPath).toString();
 
-    QString path = "";
-
     // Use our own QFileDialog just for symmetry with getSaveFileName below
 
     QFileDialog dialog(m_parent);
@@ -183,52 +218,61 @@
     dialog.setDirectory(lastPath);
 
     dialog.setAcceptMode(QFileDialog::AcceptOpen);
-    dialog.setFileMode(QFileDialog::ExistingFile);
+
+    if (multiple) {
+        dialog.setFileMode(QFileDialog::ExistingFiles);
+    } else {
+        dialog.setFileMode(QFileDialog::ExistingFile);
+    }
+
+    QString testPath = "";
+    QString pathToRemember = "";
     
     if (dialog.exec()) {
-        QStringList files = dialog.selectedFiles();
-        if (!files.empty()) path = *files.begin();
+        names = dialog.selectedFiles();
         
-        QFileInfo fi(path);
+        if (!multiple && !names.empty()) {
+            testPath = *names.begin();
+            QFileInfo fi(testPath);
         
-        if (!fi.exists()) {
+            if (!fi.exists()) {
+                QMessageBox::critical(nullptr, tr("File does not exist"),
+                                      tr("<b>File not found</b><p>File \"%1\" does not exist").arg(testPath));
             
-            QMessageBox::critical(nullptr, tr("File does not exist"),
-                                  tr("<b>File not found</b><p>File \"%1\" does not exist").arg(path));
-            path = "";
+            } else if (!fi.isReadable()) {
             
-        } else if (!fi.isReadable()) {
+                QMessageBox::critical(nullptr, tr("File is not readable"),
+                                      tr("<b>File is not readable</b><p>File \"%1\" can not be read").arg(testPath));
             
-            QMessageBox::critical(nullptr, tr("File is not readable"),
-                                  tr("<b>File is not readable</b><p>File \"%1\" can not be read").arg(path));
-            path = "";
+            } else if (fi.isDir()) {
             
-        } else if (fi.isDir()) {
+                QMessageBox::critical(nullptr, tr("Directory selected"),
+                                      tr("<b>Directory selected</b><p>File \"%1\" is a directory").arg(testPath));
+
+            } else if (!fi.isFile()) {
             
-            QMessageBox::critical(nullptr, tr("Directory selected"),
-                                  tr("<b>Directory selected</b><p>File \"%1\" is a directory").arg(path));
-            path = "";
+                QMessageBox::critical(nullptr, tr("Non-file selected"),
+                                      tr("<b>Not a file</b><p>Path \"%1\" is not a file").arg(testPath));
+            
+            } else if (fi.size() == 0) {
+            
+                QMessageBox::critical(nullptr, tr("File is empty"),
+                                      tr("<b>File is empty</b><p>File \"%1\" is empty").arg(testPath));
 
-        } else if (!fi.isFile()) {
-            
-            QMessageBox::critical(nullptr, tr("Non-file selected"),
-                                  tr("<b>Not a file</b><p>Path \"%1\" is not a file").arg(path));
-            path = "";
-            
-        } else if (fi.size() == 0) {
-            
-            QMessageBox::critical(nullptr, tr("File is empty"),
-                                  tr("<b>File is empty</b><p>File \"%1\" is empty").arg(path));
-            path = "";
-        }                
+            } else {
+                pathToRemember = testPath;
+            }
+        }
     }
 
-    if (path != "") {
+    if (pathToRemember != "") {
         settings.setValue(settingsKeyStub + "path",
-                          QFileInfo(path).absoluteDir().canonicalPath());
+                          QFileInfo(pathToRemember)
+                          .absoluteDir()
+                          .canonicalPath());
     }
     
-    return path;
+    return names;
 }
 
 QString
--- a/widgets/InteractiveFileFinder.h	Thu Mar 21 15:25:05 2019 +0000
+++ b/widgets/InteractiveFileFinder.h	Tue Apr 23 16:17:33 2019 +0100
@@ -38,11 +38,21 @@
         return m_sessionExtension;
     }
 
-    QString getOpenFileName(FileType type, QString fallbackLocation = "") override;
-    QString getSaveFileName(FileType type, QString fallbackLocation = "") override;
-    void registerLastOpenedFilePath(FileType type, QString path) override;
+    QString getOpenFileName(FileType type,
+                            QString fallbackLocation = "") override;
+    
+    QStringList getOpenFileNames(FileType type,
+                                 QString fallbackLocation = "") override;
+    
+    QString getSaveFileName(FileType type,
+                            QString fallbackLocation = "") override;
+    
+    void registerLastOpenedFilePath(FileType type,
+                                    QString path) override;
 
-    QString find(FileType type, QString location, QString lastKnownLocation = "") override;
+    QString find(FileType type,
+                 QString location,
+                 QString lastKnownLocation = "") override;
 
     static void setParentWidget(QWidget *);
 
@@ -55,6 +65,10 @@
     QString findRelative(QString location, QString relativeTo);
     QString locateInteractive(FileType type, QString thing);
 
+    QStringList getOpenFileNames(FileType type,
+                                 QString fallbackLocation,
+                                 bool multiple);
+
     QString m_sessionExtension;
     QString m_lastLocatedLocation;