* This is a convenience static function that will return a file name selected by the user. The file * does not have to exist. */
| 514 | * does not have to exist. |
| 515 | */ |
| 516 | QString FileDialog::getSaveFileName( |
| 517 | QWidget* parent, |
| 518 | const QString& caption, |
| 519 | const QString& startPath, |
| 520 | const FilterList& filters, |
| 521 | qsizetype* selectedFilterIndex, |
| 522 | Options options |
| 523 | ) |
| 524 | { |
| 525 | ActionDisabler actionDisabler {}; |
| 526 | qsizetype actuallySelectedFilterIndex = selectedFilterIndex != nullptr ? *selectedFilterIndex |
| 527 | : -1; |
| 528 | QString suggestedPath = startPath; |
| 529 | bool hasFilename = false; |
| 530 | if (suggestedPath.isEmpty()) { |
| 531 | suggestedPath = getPreferredDialogDirectory(); |
| 532 | } |
| 533 | else { |
| 534 | QFileInfo fi(suggestedPath); |
| 535 | if (fi.isRelative()) { |
| 536 | suggestedPath = getPreferredDialogDirectory(); |
| 537 | suggestedPath += QStringLiteral("/"); |
| 538 | suggestedPath += fi.fileName(); |
| 539 | fi.setFile(suggestedPath); |
| 540 | } |
| 541 | // If the startPath points to a directory (path ends with separator, or points |
| 542 | // to existing dir), don't touch it and don't use QFileDialog::selectFile() later. |
| 543 | if (!(fi.fileName().isEmpty() || fi.isDir())) { |
| 544 | // If there is a file name at the end, make sure it matches one of the patterns |
| 545 | // of the preselected filter, if applicable. |
| 546 | hasFilename = true; |
| 547 | if (actuallySelectedFilterIndex >= 0) { |
| 548 | FileDialogInternal::normalizeSavePath( |
| 549 | suggestedPath, |
| 550 | filters[actuallySelectedFilterIndex] |
| 551 | ); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | QString windowTitle = caption; |
| 557 | if (windowTitle.isEmpty()) { |
| 558 | windowTitle = FileDialog::tr("Save As"); |
| 559 | } |
| 560 | |
| 561 | options |= QFileDialog::HideNameFilterDetails; |
| 562 | |
| 563 | QString file; |
| 564 | if (DialogOptions::dontUseNativeFileDialog()) { |
| 565 | const bool showPatterns = FileDialogInternal::getPreferShowFilterPatterns(); |
| 566 | const auto qtFilterList = toQtFilterList(filters, showPatterns); |
| 567 | QList<QUrl> urls = fetchSidebarUrls(); |
| 568 | |
| 569 | options |= QFileDialog::DontUseNativeDialog; |
| 570 | |
| 571 | FileDialog dlg(parent); |
| 572 | dlg.setOptions(options); |
| 573 | dlg.setWindowTitle(windowTitle); |
no test coverage detected