diff options
author | Mitch Curtis <[email protected]> | 2021-10-01 10:34:29 +0200 |
---|---|---|
committer | Mitch Curtis <[email protected]> | 2021-10-06 11:37:03 +0200 |
commit | d000649cb705ea1239023c3d3d27a781137c57b4 (patch) | |
tree | 0fb9a63118aaa654f2a4f84e9a638be1664b8231 | |
parent | 3e3d759b6ec343a5a8edde66a86e1ccf8e4f8326 (diff) |
QQuickFileDialogImpl: fix issues with breadcrumb bar text edit
- Always set the text edit's text to the current directory, not the
selected file. This matches the expected behavior of a file dialog.
- Fix an issue where the text edit's visibility was toggled twice,
resulting in it showing up on top of the breadcrumbs when it
shouldn't.
The latter issue was found while fixing the former, and they are
both tested by the same auto test.
Change-Id: I8f1420ec9edaba1e33f1d1a268567a45d63d76b5
Pick-to: 6.2
Reviewed-by: Oliver Eftevaag <[email protected]>
Reviewed-by: Shawn Rutledge <[email protected]>
-rw-r--r-- | src/quickdialogs2/quickdialogs2quickimpl/qquickfolderbreadcrumbbar.cpp | 18 | ||||
-rw-r--r-- | tests/auto/quickcontrols2/dialogs/qquickfiledialogimpl/tst_qquickfiledialogimpl.cpp | 71 |
2 files changed, 84 insertions, 5 deletions
diff --git a/src/quickdialogs2/quickdialogs2quickimpl/qquickfolderbreadcrumbbar.cpp b/src/quickdialogs2/quickdialogs2quickimpl/qquickfolderbreadcrumbbar.cpp index c204d8fa29..337d25a6b1 100644 --- a/src/quickdialogs2/quickdialogs2quickimpl/qquickfolderbreadcrumbbar.cpp +++ b/src/quickdialogs2/quickdialogs2quickimpl/qquickfolderbreadcrumbbar.cpp @@ -258,11 +258,11 @@ void QQuickFolderBreadcrumbBarPrivate::executeTextField(bool complete) void QQuickFolderBreadcrumbBarPrivate::toggleTextFieldVisibility() { - const QUrl url = !fileDialog->selectedFile().isEmpty() ? fileDialog->selectedFile() : fileDialog->currentFolder(); - textField->setText(QQmlFile::urlToLocalFileOrQrc(url)); - textField->setVisible(!textField->isVisible()); + textField->setText(QQmlFile::urlToLocalFileOrQrc(fileDialog->currentFolder())); - qCDebug(lcTextInput) << "text field visibility changed to" << textField->isVisible(); + qCDebug(lcTextInput).nospace() << "text field visibility was " << textField->isVisible() + << "; setting it to " << !textField->isVisible(); + textField->setVisible(!textField->isVisible()); if (textField->isVisible()) { // The text field is now visible, so give it focus, @@ -319,7 +319,13 @@ void QQuickFolderBreadcrumbBarPrivate::textFieldAccepted() qCDebug(lcTextInput) << "path entered is not valid; not setting file/folder"; } - toggleTextFieldVisibility(); + // If the enter key was pressed and the dialog closed, the text input will lose + // active focus, and textFieldActiveFocusChanged() will toggle its visibility. + // We should only toggle visibility if the dialog is actually closed, otherwise + // we'll end up toggling twice, and the text input will be visible the next time + // the dialog is opened. + if (fileDialog->isVisible()) + toggleTextFieldVisibility(); } void QQuickFolderBreadcrumbBarPrivate::textFieldVisibleChanged() @@ -334,6 +340,8 @@ void QQuickFolderBreadcrumbBarPrivate::textFieldVisibleChanged() void QQuickFolderBreadcrumbBarPrivate::textFieldActiveFocusChanged() { + qCDebug(lcTextInput) << "text field activeFocus changed to" << textField->hasActiveFocus(); + // When the text field loses focus, it should be hidden. if (!textField->hasActiveFocus() && textField->isVisible()) toggleTextFieldVisibility(); diff --git a/tests/auto/quickcontrols2/dialogs/qquickfiledialogimpl/tst_qquickfiledialogimpl.cpp b/tests/auto/quickcontrols2/dialogs/qquickfiledialogimpl/tst_qquickfiledialogimpl.cpp index bee0f7a65d..643e4f3431 100644 --- a/tests/auto/quickcontrols2/dialogs/qquickfiledialogimpl/tst_qquickfiledialogimpl.cpp +++ b/tests/auto/quickcontrols2/dialogs/qquickfiledialogimpl/tst_qquickfiledialogimpl.cpp @@ -90,6 +90,7 @@ private slots: void changeFolderViaDoubleClick(); void chooseFolderViaTextEdit(); void chooseFolderViaEnter(); + void chooseFileAndThenFolderViaTextEdit(); void cancelDialogWhileTextEditHasFocus(); void goUp(); void goUpWhileTextEditHasFocus(); @@ -644,6 +645,76 @@ void tst_QQuickFileDialogImpl::chooseFolderViaEnter() QTRY_VERIFY(!dialogHelper.quickDialog->isVisible()); } +void tst_QQuickFileDialogImpl::chooseFileAndThenFolderViaTextEdit() +{ + // Open the dialog. + DialogTestHelper<QQuickFileDialog, QQuickFileDialogImpl> dialogHelper(this, "fileDialog.qml"); + QVERIFY2(dialogHelper.isWindowInitialized(), dialogHelper.failureMessage()); + QVERIFY(dialogHelper.waitForWindowActive()); + QVERIFY(dialogHelper.openDialog()); + QTRY_VERIFY(dialogHelper.isQuickDialogOpen()); + + // Get the text edit visible with Ctrl+L. + const auto editPathKeySequence = QKeySequence(Qt::CTRL | Qt::Key_L); + QTest::keySequence(dialogHelper.window(), editPathKeySequence); + auto breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar*>(); + QVERIFY(breadcrumbBar); + QVERIFY(breadcrumbBar->textField()->isVisible()); + QCOMPARE(breadcrumbBar->textField()->text(), dialogHelper.dialog->currentFolder().toLocalFile()); + QCOMPARE(breadcrumbBar->textField()->selectedText(), breadcrumbBar->textField()->text()); + + // Enter the path to the file in the text edit. + enterText(dialogHelper.window(), tempFile2->fileName()); + QCOMPARE(breadcrumbBar->textField()->text(), tempFile2->fileName()); + + // Hit enter to accept. + QTest::keyClick(dialogHelper.window(), Qt::Key_Return); + COMPARE_URL(dialogHelper.quickDialog->selectedFile(), QUrl::fromLocalFile(tempFile2->fileName())); + COMPARE_URL(dialogHelper.dialog->selectedFile(), QUrl::fromLocalFile(tempFile2->fileName())); + COMPARE_URLS(dialogHelper.dialog->selectedFiles(), { QUrl::fromLocalFile(tempFile2->fileName()) }); + QVERIFY(!dialogHelper.dialog->isVisible()); + QTRY_VERIFY(!dialogHelper.quickDialog->isVisible()); + // Check that the text edit is hidden and breadcrumbs are shown instead. + QVERIFY(!breadcrumbBar->textField()->isVisible()); + + // Re-open the dialog. + QVERIFY(dialogHelper.openDialog()); + QTRY_VERIFY(dialogHelper.isQuickDialogOpen()); + // The breadcrumbs should be visible after opening, not the text edit. + QVERIFY(!breadcrumbBar->textField()->isVisible()); + + // Get the text edit visible with Ctrl+L. + QTest::keySequence(dialogHelper.window(), editPathKeySequence); + QVERIFY(breadcrumbBar->textField()->isVisible()); + // The text edit should show the directory that contains the last file that was selected. + QCOMPARE(breadcrumbBar->textField()->text(), tempDir.path()); + QCOMPARE(breadcrumbBar->textField()->selectedText(), breadcrumbBar->textField()->text()); + + // Enter the path to the folder in the text edit. + enterText(dialogHelper.window(), tempSubDir.path()); + QCOMPARE(breadcrumbBar->textField()->text(), tempSubDir.path()); + + // Hit enter to accept. + QTest::keyClick(dialogHelper.window(), Qt::Key_Return); + // The first file in the directory should be selected, which is "sub-sub-dir". + COMPARE_URL(dialogHelper.dialog->currentFile(), QUrl::fromLocalFile(tempSubSubDir.path())); + auto fileDialogListView = dialogHelper.quickDialog->findChild<QQuickListView*>("fileDialogListView"); + QVERIFY(fileDialogListView); + QQuickFileDialogDelegate *subSubDirDelegate = nullptr; + QTRY_VERIFY(findViewDelegateItem(fileDialogListView, 0, subSubDirDelegate)); + QCOMPARE(subSubDirDelegate->isHighlighted(), true); + // We just changed directories, so the actual selected file shouldn't change. + COMPARE_URL(dialogHelper.dialog->selectedFile(), QUrl::fromLocalFile(tempFile2->fileName())); + COMPARE_URLS(dialogHelper.dialog->selectedFiles(), { QUrl::fromLocalFile(tempFile2->fileName()) }); + COMPARE_URL(dialogHelper.dialog->currentFolder(), QUrl::fromLocalFile(tempSubDir.path())); + QTRY_VERIFY(dialogHelper.dialog->isVisible()); + + // Close the dialog. + dialogHelper.dialog->close(); + QVERIFY(!dialogHelper.dialog->isVisible()); + QTRY_VERIFY(!dialogHelper.quickDialog->isVisible()); +} + void tst_QQuickFileDialogImpl::cancelDialogWhileTextEditHasFocus() { // Open the dialog. |