comparison widgets/InteractiveFileFinder.cpp @ 1440:18101be79c9c

Add function to request multiple filenames
author Chris Cannam
date Tue, 23 Apr 2019 16:17:33 +0100
parents 70e93f996da6
children c268fe2ad597
comparison
equal deleted inserted replaced
1435:aa0616116537 1440:18101be79c9c
56 { 56 {
57 m_sessionExtension = extension; 57 m_sessionExtension = extension;
58 } 58 }
59 59
60 QString 60 QString
61 InteractiveFileFinder::getOpenFileName(FileType type, QString fallbackLocation) 61 InteractiveFileFinder::getOpenFileName(FileType type,
62 QString fallbackLocation)
63 {
64 QStringList names = getOpenFileNames(type,
65 fallbackLocation,
66 false);
67 if (names.empty()) return "";
68 else return names[0];
69 }
70
71 QStringList
72 InteractiveFileFinder::getOpenFileNames(FileType type,
73 QString fallbackLocation)
74 {
75 return getOpenFileNames(type,
76 fallbackLocation,
77 true);
78 }
79
80 QStringList
81 InteractiveFileFinder::getOpenFileNames(FileType type,
82 QString fallbackLocation,
83 bool multiple)
62 { 84 {
63 QString settingsKeyStub; 85 QString settingsKeyStub;
64 QString lastPath = fallbackLocation; 86 QString lastPath = fallbackLocation;
65 87
66 QString title = tr("Select file"); 88 QString title;
89 if (multiple) {
90 title = tr("Select one or more files");
91 } else {
92 title = tr("Select file");
93 }
67 QString filter = tr("All files (*.*)"); 94 QString filter = tr("All files (*.*)");
68 95
96 QStringList names;
97
69 switch (type) { 98 switch (type) {
70 99
71 case SessionFile: 100 case SessionFile:
72 settingsKeyStub = "session"; 101 settingsKeyStub = "session";
73 title = tr("Select a session file"); 102 if (multiple) {
103 title = tr("Select one or more session files");
104 } else {
105 title = tr("Select a session file");
106 }
74 filter = tr("%1 session files (*.%2)\nRDF files (%3)\nAll files (*.*)") 107 filter = tr("%1 session files (*.%2)\nRDF files (%3)\nAll files (*.*)")
75 .arg(QApplication::applicationName()) 108 .arg(QApplication::applicationName())
76 .arg(m_sessionExtension) 109 .arg(m_sessionExtension)
77 .arg(RDFImporter::getKnownExtensions()); 110 .arg(RDFImporter::getKnownExtensions());
78 break; 111 break;
79 112
80 case AudioFile: 113 case AudioFile:
81 settingsKeyStub = "audio"; 114 settingsKeyStub = "audio";
82 title = "Select an audio file"; 115 if (multiple) {
116 title = tr("Select one or more audio files");
117 } else {
118 title = tr("Select an audio file");
119 }
83 filter = tr("Audio files (%1)\nAll files (*.*)") 120 filter = tr("Audio files (%1)\nAll files (*.*)")
84 .arg(AudioFileReaderFactory::getKnownExtensions()); 121 .arg(AudioFileReaderFactory::getKnownExtensions());
85 break; 122 break;
86 123
87 case LayerFile: 124 case LayerFile:
171 208
172 QSettings settings; 209 QSettings settings;
173 settings.beginGroup("FileFinder"); 210 settings.beginGroup("FileFinder");
174 lastPath = settings.value(settingsKeyStub + "path", lastPath).toString(); 211 lastPath = settings.value(settingsKeyStub + "path", lastPath).toString();
175 212
176 QString path = "";
177
178 // Use our own QFileDialog just for symmetry with getSaveFileName below 213 // Use our own QFileDialog just for symmetry with getSaveFileName below
179 214
180 QFileDialog dialog(m_parent); 215 QFileDialog dialog(m_parent);
181 dialog.setNameFilters(filter.split('\n')); 216 dialog.setNameFilters(filter.split('\n'));
182 dialog.setWindowTitle(title); 217 dialog.setWindowTitle(title);
183 dialog.setDirectory(lastPath); 218 dialog.setDirectory(lastPath);
184 219
185 dialog.setAcceptMode(QFileDialog::AcceptOpen); 220 dialog.setAcceptMode(QFileDialog::AcceptOpen);
186 dialog.setFileMode(QFileDialog::ExistingFile); 221
222 if (multiple) {
223 dialog.setFileMode(QFileDialog::ExistingFiles);
224 } else {
225 dialog.setFileMode(QFileDialog::ExistingFile);
226 }
227
228 QString testPath = "";
229 QString pathToRemember = "";
187 230
188 if (dialog.exec()) { 231 if (dialog.exec()) {
189 QStringList files = dialog.selectedFiles(); 232 names = dialog.selectedFiles();
190 if (!files.empty()) path = *files.begin(); 233
191 234 if (!multiple && !names.empty()) {
192 QFileInfo fi(path); 235 testPath = *names.begin();
193 236 QFileInfo fi(testPath);
194 if (!fi.exists()) { 237
195 238 if (!fi.exists()) {
196 QMessageBox::critical(nullptr, tr("File does not exist"), 239 QMessageBox::critical(nullptr, tr("File does not exist"),
197 tr("<b>File not found</b><p>File \"%1\" does not exist").arg(path)); 240 tr("<b>File not found</b><p>File \"%1\" does not exist").arg(testPath));
198 path = ""; 241
199 242 } else if (!fi.isReadable()) {
200 } else if (!fi.isReadable()) { 243
201 244 QMessageBox::critical(nullptr, tr("File is not readable"),
202 QMessageBox::critical(nullptr, tr("File is not readable"), 245 tr("<b>File is not readable</b><p>File \"%1\" can not be read").arg(testPath));
203 tr("<b>File is not readable</b><p>File \"%1\" can not be read").arg(path)); 246
204 path = ""; 247 } else if (fi.isDir()) {
205 248
206 } else if (fi.isDir()) { 249 QMessageBox::critical(nullptr, tr("Directory selected"),
207 250 tr("<b>Directory selected</b><p>File \"%1\" is a directory").arg(testPath));
208 QMessageBox::critical(nullptr, tr("Directory selected"), 251
209 tr("<b>Directory selected</b><p>File \"%1\" is a directory").arg(path)); 252 } else if (!fi.isFile()) {
210 path = ""; 253
211 254 QMessageBox::critical(nullptr, tr("Non-file selected"),
212 } else if (!fi.isFile()) { 255 tr("<b>Not a file</b><p>Path \"%1\" is not a file").arg(testPath));
213 256
214 QMessageBox::critical(nullptr, tr("Non-file selected"), 257 } else if (fi.size() == 0) {
215 tr("<b>Not a file</b><p>Path \"%1\" is not a file").arg(path)); 258
216 path = ""; 259 QMessageBox::critical(nullptr, tr("File is empty"),
217 260 tr("<b>File is empty</b><p>File \"%1\" is empty").arg(testPath));
218 } else if (fi.size() == 0) { 261
219 262 } else {
220 QMessageBox::critical(nullptr, tr("File is empty"), 263 pathToRemember = testPath;
221 tr("<b>File is empty</b><p>File \"%1\" is empty").arg(path)); 264 }
222 path = ""; 265 }
223 } 266 }
224 } 267
225 268 if (pathToRemember != "") {
226 if (path != "") {
227 settings.setValue(settingsKeyStub + "path", 269 settings.setValue(settingsKeyStub + "path",
228 QFileInfo(path).absoluteDir().canonicalPath()); 270 QFileInfo(pathToRemember)
229 } 271 .absoluteDir()
230 272 .canonicalPath());
231 return path; 273 }
274
275 return names;
232 } 276 }
233 277
234 QString 278 QString
235 InteractiveFileFinder::getSaveFileName(FileType type, 279 InteractiveFileFinder::getSaveFileName(FileType type,
236 QString fallbackLocation) 280 QString fallbackLocation)