comparison widgets/InteractiveFileFinder.cpp @ 778:6d95d87f1654

Remember the last-used suffix in save dialog (#877).
author Chris Cannam
date Fri, 23 May 2014 11:17:52 +0100
parents d0fd7630d32f
children 9c272d39c3e3
comparison
equal deleted inserted replaced
776:fe59d97750ba 778:6d95d87f1654
51 } 51 }
52 52
53 QString 53 QString
54 InteractiveFileFinder::getOpenFileName(FileType type, QString fallbackLocation) 54 InteractiveFileFinder::getOpenFileName(FileType type, QString fallbackLocation)
55 { 55 {
56 QString settingsKey; 56 QString settingsKeyStub;
57 QString lastPath = fallbackLocation; 57 QString lastPath = fallbackLocation;
58 58
59 QString title = tr("Select file"); 59 QString title = tr("Select file");
60 QString filter = tr("All files (*.*)"); 60 QString filter = tr("All files (*.*)");
61 61
62 switch (type) { 62 switch (type) {
63 63
64 case SessionFile: 64 case SessionFile:
65 settingsKey = "sessionpath"; 65 settingsKeyStub = "session";
66 title = tr("Select a session file"); 66 title = tr("Select a session file");
67 filter = tr("%1 session files (*.%1)\nRDF files (%3)\nAll files (*.*)") 67 filter = tr("%1 session files (*.%1)\nRDF files (%3)\nAll files (*.*)")
68 .arg(QApplication::applicationName()) 68 .arg(QApplication::applicationName())
69 .arg(m_sessionExtension) 69 .arg(m_sessionExtension)
70 .arg(RDFImporter::getKnownExtensions()); 70 .arg(RDFImporter::getKnownExtensions());
71 break; 71 break;
72 72
73 case AudioFile: 73 case AudioFile:
74 settingsKey = "audiopath"; 74 settingsKeyStub = "audio";
75 title = "Select an audio file"; 75 title = "Select an audio file";
76 filter = tr("Audio files (%1)\nAll files (*.*)") 76 filter = tr("Audio files (%1)\nAll files (*.*)")
77 .arg(AudioFileReaderFactory::getKnownExtensions()); 77 .arg(AudioFileReaderFactory::getKnownExtensions());
78 break; 78 break;
79 79
80 case LayerFile: 80 case LayerFile:
81 settingsKey = "layerpath"; 81 settingsKeyStub = "layer";
82 filter = tr("All supported files (%1 %2)\nSonic Visualiser Layer XML files (*.svl)\nComma-separated data files (*.csv)\nSpace-separated .lab files (*.lab)\nRDF files (%2)\nMIDI files (*.mid)\nText files (*.txt)\nAll files (*.*)") 82 filter = tr("All supported files (%1 %2)\nSonic Visualiser Layer XML files (*.svl)\nComma-separated data files (*.csv)\nSpace-separated .lab files (*.lab)\nRDF files (%2)\nMIDI files (*.mid)\nText files (*.txt)\nAll files (*.*)")
83 .arg(DataFileReaderFactory::getKnownExtensions()) 83 .arg(DataFileReaderFactory::getKnownExtensions())
84 .arg(RDFImporter::getKnownExtensions()); 84 .arg(RDFImporter::getKnownExtensions());
85 break; 85 break;
86 86
87 case LayerFileNoMidi: 87 case LayerFileNoMidi:
88 settingsKey = "layerpath"; 88 settingsKeyStub = "layer";
89 filter = tr("All supported files (%1 %2)\nSonic Visualiser Layer XML files (*.svl)\nComma-separated data files (*.csv)\nSpace-separated .lab files (*.lab)\nRDF files (%2)\nText files (*.txt)\nAll files (*.*)") 89 filter = tr("All supported files (%1 %2)\nSonic Visualiser Layer XML files (*.svl)\nComma-separated data files (*.csv)\nSpace-separated .lab files (*.lab)\nRDF files (%2)\nText files (*.txt)\nAll files (*.*)")
90 .arg(DataFileReaderFactory::getKnownExtensions()) 90 .arg(DataFileReaderFactory::getKnownExtensions())
91 .arg(RDFImporter::getKnownExtensions()); 91 .arg(RDFImporter::getKnownExtensions());
92 break; 92 break;
93 93
94 case LayerFileNonSV: 94 case LayerFileNonSV:
95 settingsKey = "layerpath"; 95 settingsKeyStub = "layer";
96 filter = tr("All supported files (%1 %2)\nComma-separated data files (*.csv)\nSonic Visualiser Layer XML files (*.svl)\nSpace-separated .lab files (*.lab)\nRDF files (%2)\nMIDI files (*.mid)\nText files (*.txt)\nAll files (*.*)") 96 filter = tr("All supported files (%1 %2)\nComma-separated data files (*.csv)\nSonic Visualiser Layer XML files (*.svl)\nSpace-separated .lab files (*.lab)\nRDF files (%2)\nMIDI files (*.mid)\nText files (*.txt)\nAll files (*.*)")
97 .arg(DataFileReaderFactory::getKnownExtensions()) 97 .arg(DataFileReaderFactory::getKnownExtensions())
98 .arg(RDFImporter::getKnownExtensions()); 98 .arg(RDFImporter::getKnownExtensions());
99 break; 99 break;
100 100
101 case LayerFileNoMidiNonSV: 101 case LayerFileNoMidiNonSV:
102 settingsKey = "layerpath"; 102 settingsKeyStub = "layer";
103 filter = tr("All supported files (%1 %2)\nComma-separated data files (*.csv)\nSonic Visualiser Layer XML files (*.svl)\nSpace-separated .lab files (*.lab)\nRDF files (%2)\nText files (*.txt)\nAll files (*.*)") 103 filter = tr("All supported files (%1 %2)\nComma-separated data files (*.csv)\nSonic Visualiser Layer XML files (*.svl)\nSpace-separated .lab files (*.lab)\nRDF files (%2)\nText files (*.txt)\nAll files (*.*)")
104 .arg(DataFileReaderFactory::getKnownExtensions()) 104 .arg(DataFileReaderFactory::getKnownExtensions())
105 .arg(RDFImporter::getKnownExtensions()); 105 .arg(RDFImporter::getKnownExtensions());
106 break; 106 break;
107 107
108 case SessionOrAudioFile: 108 case SessionOrAudioFile:
109 settingsKey = "lastpath"; 109 settingsKeyStub = "last";
110 filter = tr("All supported files (*.sv %1 %2)\n%3 session files (*.%4)\nAudio files (%2)\nRDF files (%1)\nAll files (*.*)") 110 filter = tr("All supported files (*.sv %1 %2)\n%3 session files (*.%4)\nAudio files (%2)\nRDF files (%1)\nAll files (*.*)")
111 .arg(RDFImporter::getKnownExtensions()) 111 .arg(RDFImporter::getKnownExtensions())
112 .arg(AudioFileReaderFactory::getKnownExtensions()) 112 .arg(AudioFileReaderFactory::getKnownExtensions())
113 .arg(QApplication::applicationName()) 113 .arg(QApplication::applicationName())
114 .arg(m_sessionExtension); 114 .arg(m_sessionExtension);
115 break; 115 break;
116 116
117 case ImageFile: 117 case ImageFile:
118 settingsKey = "imagepath"; 118 settingsKeyStub = "image";
119 { 119 {
120 QStringList fmts; 120 QStringList fmts;
121 QList<QByteArray> formats = QImageReader::supportedImageFormats(); 121 QList<QByteArray> formats = QImageReader::supportedImageFormats();
122 for (QList<QByteArray>::iterator i = formats.begin(); 122 for (QList<QByteArray>::iterator i = formats.begin();
123 i != formats.end(); ++i) { 123 i != formats.end(); ++i) {
127 filter = tr("Image files (%1)\nAll files (*.*)").arg(fmts.join(" ")); 127 filter = tr("Image files (%1)\nAll files (*.*)").arg(fmts.join(" "));
128 } 128 }
129 break; 129 break;
130 130
131 case CSVFile: 131 case CSVFile:
132 settingsKey = "layerpath"; 132 settingsKeyStub = "layer";
133 filter = tr("Comma-separated data files (*.csv)\nSpace-separated .lab files (*.lab)\nText files (*.txt)\nAll files (*.*)"); 133 filter = tr("Comma-separated data files (*.csv)\nSpace-separated .lab files (*.lab)\nText files (*.txt)\nAll files (*.*)");
134 break; 134 break;
135 135
136 case AnyFile: 136 case AnyFile:
137 settingsKey = "lastpath"; 137 settingsKeyStub = "last";
138 filter = tr("All supported files (*.sv %1 %2 %3)\n%4 session files (*.%5)\nAudio files (%1)\nLayer files (%2)\nRDF files (%3)\nAll files (*.*)") 138 filter = tr("All supported files (*.sv %1 %2 %3)\n%4 session files (*.%5)\nAudio files (%1)\nLayer files (%2)\nRDF files (%3)\nAll files (*.*)")
139 .arg(AudioFileReaderFactory::getKnownExtensions()) 139 .arg(AudioFileReaderFactory::getKnownExtensions())
140 .arg(DataFileReaderFactory::getKnownExtensions()) 140 .arg(DataFileReaderFactory::getKnownExtensions())
141 .arg(RDFImporter::getKnownExtensions()) 141 .arg(RDFImporter::getKnownExtensions())
142 .arg(QApplication::applicationName()) 142 .arg(QApplication::applicationName())
154 lastPath = QFileInfo(lastPath).absoluteDir().canonicalPath(); 154 lastPath = QFileInfo(lastPath).absoluteDir().canonicalPath();
155 } 155 }
156 156
157 QSettings settings; 157 QSettings settings;
158 settings.beginGroup("FileFinder"); 158 settings.beginGroup("FileFinder");
159 lastPath = settings.value(settingsKey, lastPath).toString(); 159 lastPath = settings.value(settingsKeyStub + "path", lastPath).toString();
160 160
161 QString path = ""; 161 QString path = "";
162 162
163 // Use our own QFileDialog just for symmetry with getSaveFileName below 163 // Use our own QFileDialog just for symmetry with getSaveFileName below
164 164
207 path = ""; 207 path = "";
208 } 208 }
209 } 209 }
210 210
211 if (path != "") { 211 if (path != "") {
212 settings.setValue(settingsKey, 212 settings.setValue(settingsKeyStub + "path",
213 QFileInfo(path).absoluteDir().canonicalPath()); 213 QFileInfo(path).absoluteDir().canonicalPath());
214 } 214 }
215 215
216 return path; 216 return path;
217 } 217 }
218 218
219 QString 219 QString
220 InteractiveFileFinder::getSaveFileName(FileType type, QString fallbackLocation) 220 InteractiveFileFinder::getSaveFileName(FileType type, QString fallbackLocation)
221 { 221 {
222 QString settingsKey; 222 QString settingsKeyStub;
223 QString lastPath = fallbackLocation; 223 QString lastPath = fallbackLocation;
224 224
225 QString title = tr("Select file"); 225 QString title = tr("Select file");
226 QString filter = tr("All files (*.*)"); 226 QString filter = tr("All files (*.*)");
227 227
228 switch (type) { 228 switch (type) {
229 229
230 case SessionFile: 230 case SessionFile:
231 settingsKey = "savesessionpath"; 231 settingsKeyStub = "savesession";
232 title = tr("Select a session file"); 232 title = tr("Select a session file");
233 filter = tr("%1 session files (*.%2)\nAll files (*.*)") 233 filter = tr("%1 session files (*.%2)\nAll files (*.*)")
234 .arg(QApplication::applicationName()).arg(m_sessionExtension); 234 .arg(QApplication::applicationName()).arg(m_sessionExtension);
235 break; 235 break;
236 236
237 case AudioFile: 237 case AudioFile:
238 settingsKey = "saveaudiopath"; 238 settingsKeyStub = "saveaudio";
239 title = "Select an audio file"; 239 title = "Select an audio file";
240 title = tr("Select a file to export to"); 240 title = tr("Select a file to export to");
241 filter = tr("WAV audio files (*.wav)\nAll files (*.*)"); 241 filter = tr("WAV audio files (*.wav)\nAll files (*.*)");
242 break; 242 break;
243 243
244 case LayerFile: 244 case LayerFile:
245 settingsKey = "savelayerpath"; 245 settingsKeyStub = "savelayer";
246 title = tr("Select a file to export to"); 246 title = tr("Select a file to export to");
247 filter = tr("Sonic Visualiser Layer XML files (*.svl)\nComma-separated data files (*.csv)\nRDF/Turtle files (%1)\nMIDI files (*.mid)\nText files (*.txt)\nAll files (*.*)").arg(RDFExporter::getSupportedExtensions()); 247 filter = tr("Sonic Visualiser Layer XML files (*.svl)\nComma-separated data files (*.csv)\nRDF/Turtle files (%1)\nMIDI files (*.mid)\nText files (*.txt)\nAll files (*.*)").arg(RDFExporter::getSupportedExtensions());
248 break; 248 break;
249 249
250 case LayerFileNoMidi: 250 case LayerFileNoMidi:
251 settingsKey = "savelayerpath"; 251 settingsKeyStub = "savelayer";
252 title = tr("Select a file to export to"); 252 title = tr("Select a file to export to");
253 filter = tr("Sonic Visualiser Layer XML files (*.svl)\nComma-separated data files (*.csv)\nRDF/Turtle files (%1)\nText files (*.txt)\nAll files (*.*)").arg(RDFExporter::getSupportedExtensions()); 253 filter = tr("Sonic Visualiser Layer XML files (*.svl)\nComma-separated data files (*.csv)\nRDF/Turtle files (%1)\nText files (*.txt)\nAll files (*.*)").arg(RDFExporter::getSupportedExtensions());
254 break; 254 break;
255 255
256 case LayerFileNonSV: 256 case LayerFileNonSV:
257 settingsKey = "savelayerpath"; 257 settingsKeyStub = "savelayer";
258 title = tr("Select a file to export to"); 258 title = tr("Select a file to export to");
259 filter = tr("Comma-separated data files (*.csv)\nSonic Visualiser Layer XML files (*.svl)\nRDF/Turtle files (%1)\nMIDI files (*.mid)\nText files (*.txt)\nAll files (*.*)").arg(RDFExporter::getSupportedExtensions()); 259 filter = tr("Comma-separated data files (*.csv)\nSonic Visualiser Layer XML files (*.svl)\nRDF/Turtle files (%1)\nMIDI files (*.mid)\nText files (*.txt)\nAll files (*.*)").arg(RDFExporter::getSupportedExtensions());
260 break; 260 break;
261 261
262 case LayerFileNoMidiNonSV: 262 case LayerFileNoMidiNonSV:
263 settingsKey = "savelayerpath"; 263 settingsKeyStub = "savelayer";
264 title = tr("Select a file to export to"); 264 title = tr("Select a file to export to");
265 filter = tr("Comma-separated data files (*.csv)\nSonic Visualiser Layer XML files (*.svl)\nRDF/Turtle files (%1)\nText files (*.txt)\nAll files (*.*)").arg(RDFExporter::getSupportedExtensions()); 265 filter = tr("Comma-separated data files (*.csv)\nSonic Visualiser Layer XML files (*.svl)\nRDF/Turtle files (%1)\nText files (*.txt)\nAll files (*.*)").arg(RDFExporter::getSupportedExtensions());
266 break; 266 break;
267 267
268 case SessionOrAudioFile: 268 case SessionOrAudioFile:
269 cerr << "ERROR: Internal error: InteractiveFileFinder::getSaveFileName: SessionOrAudioFile cannot be used here" << endl; 269 cerr << "ERROR: Internal error: InteractiveFileFinder::getSaveFileName: SessionOrAudioFile cannot be used here" << endl;
270 abort(); 270 abort();
271 271
272 case ImageFile: 272 case ImageFile:
273 settingsKey = "saveimagepath"; 273 settingsKeyStub = "saveimage";
274 title = tr("Select a file to export to"); 274 title = tr("Select a file to export to");
275 filter = tr("Portable Network Graphics files (*.png)\nAll files (*.*)"); 275 filter = tr("Portable Network Graphics files (*.png)\nAll files (*.*)");
276 break; 276 break;
277 277
278 case CSVFile: 278 case CSVFile:
279 settingsKey = "savelayerpath"; 279 settingsKeyStub = "savelayer";
280 title = tr("Select a file to export to"); 280 title = tr("Select a file to export to");
281 filter = tr("Comma-separated data files (*.csv)\nText files (*.txt)\nAll files (*.*)"); 281 filter = tr("Comma-separated data files (*.csv)\nText files (*.txt)\nAll files (*.*)");
282 break; 282 break;
283 283
284 case AnyFile: 284 case AnyFile:
296 lastPath = QFileInfo(lastPath).absoluteDir().canonicalPath(); 296 lastPath = QFileInfo(lastPath).absoluteDir().canonicalPath();
297 } 297 }
298 298
299 QSettings settings; 299 QSettings settings;
300 settings.beginGroup("FileFinder"); 300 settings.beginGroup("FileFinder");
301 lastPath = settings.value(settingsKey, lastPath).toString(); 301 lastPath = settings.value(settingsKeyStub + "path", lastPath).toString();
302 302
303 QString path = ""; 303 QString path = "";
304 304
305 // Use our own QFileDialog instead of static functions, as we may 305 // Use our own QFileDialog instead of static functions, as we may
306 // need to adjust the file extension based on the selected filter 306 // need to adjust the file extension based on the selected filter
307 307
308 QFileDialog dialog; 308 QFileDialog dialog;
309 dialog.setNameFilters(filter.split('\n')); 309
310 QStringList filters = filter.split('\n');
311
312 dialog.setNameFilters(filters);
310 dialog.setWindowTitle(title); 313 dialog.setWindowTitle(title);
311 dialog.setDirectory(lastPath); 314 dialog.setDirectory(lastPath);
312 315
313 dialog.setAcceptMode(QFileDialog::AcceptSave); 316 dialog.setAcceptMode(QFileDialog::AcceptSave);
314 dialog.setFileMode(QFileDialog::AnyFile); 317 dialog.setFileMode(QFileDialog::AnyFile);
315 dialog.setConfirmOverwrite(false); // we'll do that 318 dialog.setConfirmOverwrite(false); // we'll do that
316 319
320 QString defaultSuffix;
317 if (type == SessionFile) { 321 if (type == SessionFile) {
318 dialog.setDefaultSuffix(m_sessionExtension); 322 defaultSuffix = m_sessionExtension;
319 } else if (type == AudioFile) { 323 } else if (type == AudioFile) {
320 dialog.setDefaultSuffix("wav"); 324 defaultSuffix = "wav";
321 } else if (type == ImageFile) { 325 } else if (type == ImageFile) {
322 dialog.setDefaultSuffix("png"); 326 defaultSuffix = "png";
323 } else if (type == CSVFile) { 327 } else if (type == CSVFile) {
324 dialog.setDefaultSuffix("csv"); 328 defaultSuffix = "csv";
329 }
330
331 defaultSuffix =
332 settings.value(settingsKeyStub + "suffix", defaultSuffix).toString();
333
334 dialog.setDefaultSuffix(defaultSuffix);
335
336 foreach (QString f, filters) {
337 if (f.contains("." + defaultSuffix)) {
338 dialog.selectNameFilter(f);
339 }
325 } 340 }
326 341
327 bool good = false; 342 bool good = false;
328 343
329 while (!good) { 344 while (!good) {
380 395
381 good = true; 396 good = true;
382 } 397 }
383 398
384 if (path != "") { 399 if (path != "") {
385 settings.setValue(settingsKey, 400 settings.setValue(settingsKeyStub + "path",
386 QFileInfo(path).absoluteDir().canonicalPath()); 401 QFileInfo(path).absoluteDir().canonicalPath());
402 settings.setValue(settingsKeyStub + "suffix",
403 QFileInfo(path).suffix());
387 } 404 }
388 405
389 return path; 406 return path;
390 } 407 }
391 408
392 void 409 void
393 InteractiveFileFinder::registerLastOpenedFilePath(FileType type, QString path) 410 InteractiveFileFinder::registerLastOpenedFilePath(FileType type, QString path)
394 { 411 {
395 QString settingsKey; 412 QString settingsKeyStub;
396 413
397 switch (type) { 414 switch (type) {
398 case SessionFile: 415 case SessionFile:
399 settingsKey = "sessionpath"; 416 settingsKeyStub = "session";
400 break; 417 break;
401 418
402 case AudioFile: 419 case AudioFile:
403 settingsKey = "audiopath"; 420 settingsKeyStub = "audio";
404 break; 421 break;
405 422
406 case LayerFile: 423 case LayerFile:
407 settingsKey = "layerpath"; 424 settingsKeyStub = "layer";
408 break; 425 break;
409 426
410 case LayerFileNoMidi: 427 case LayerFileNoMidi:
411 settingsKey = "layerpath"; 428 settingsKeyStub = "layer";
412 break; 429 break;
413 430
414 case LayerFileNonSV: 431 case LayerFileNonSV:
415 settingsKey = "layerpath"; 432 settingsKeyStub = "layer";
416 break; 433 break;
417 434
418 case LayerFileNoMidiNonSV: 435 case LayerFileNoMidiNonSV:
419 settingsKey = "layerpath"; 436 settingsKeyStub = "layer";
420 break; 437 break;
421 438
422 case SessionOrAudioFile: 439 case SessionOrAudioFile:
423 settingsKey = "lastpath"; 440 settingsKeyStub = "last";
424 break; 441 break;
425 442
426 case ImageFile: 443 case ImageFile:
427 settingsKey = "imagepath"; 444 settingsKeyStub = "image";
428 break; 445 break;
429 446
430 case CSVFile: 447 case CSVFile:
431 settingsKey = "layerpath"; 448 settingsKeyStub = "layer";
432 break; 449 break;
433 450
434 case AnyFile: 451 case AnyFile:
435 settingsKey = "lastpath"; 452 settingsKeyStub = "last";
436 break; 453 break;
437 } 454 }
438 455
439 if (path != "") { 456 if (path != "") {
440 QSettings settings; 457 QSettings settings;
441 settings.beginGroup("FileFinder"); 458 settings.beginGroup("FileFinder");
442 path = QFileInfo(path).absoluteDir().canonicalPath(); 459 path = QFileInfo(path).absoluteDir().canonicalPath();
443 settings.setValue(settingsKey, path); 460 QString suffix = QFileInfo(path).suffix();
461 settings.setValue(settingsKeyStub + "path", path);
462 settings.setValue(settingsKeyStub + "suffix", suffix);
444 settings.setValue("lastpath", path); 463 settings.setValue("lastpath", path);
445 } 464 }
446 } 465 }
447 466
448 QString 467 QString