From 1a6e441f1bfd9c0585970f5bc723e0190cb93603 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Tue, 22 Oct 2019 13:22:06 +0200 Subject: [PATCH] Locator: Ask before creating files With the option to not ask again. It's too easy to create unwanted files when mistyping names in Locator. Fixes: QTCREATORBUG-23078 Change-Id: I082d3e112db404813c7d8f46edb7a16836a98a92 Reviewed-by: Leena Miettinen Reviewed-by: David Schulz --- .../coreplugin/locator/filesystemfilter.cpp | 55 +++++++++++++++---- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/src/plugins/coreplugin/locator/filesystemfilter.cpp b/src/plugins/coreplugin/locator/filesystemfilter.cpp index 5e04215539d..41ad77e5c1e 100644 --- a/src/plugins/coreplugin/locator/filesystemfilter.cpp +++ b/src/plugins/coreplugin/locator/filesystemfilter.cpp @@ -29,13 +29,17 @@ #include #include -#include #include +#include +#include #include +#include #include #include +#include #include +#include using namespace Core; using namespace Core::Internal; @@ -148,8 +152,12 @@ QList FileSystemFilter::matchesFor(QFutureInterface()); } +const char kAlwaysCreate[] = "Locator/FileSystemFilter/AlwaysCreate"; + void FileSystemFilter::accept(LocatorFilterEntry selection, - QString *newText, int *selectionStart, int *selectionLength) const + QString *newText, + int *selectionStart, + int *selectionLength) const { Q_UNUSED(selectionLength) QString fileName = selection.fileName; @@ -159,15 +167,42 @@ void FileSystemFilter::accept(LocatorFilterEntry selection, + QDir::toNativeSeparators(info.absoluteFilePath() + '/'); *newText = value; *selectionStart = value.length(); - return; - } else if (!info.exists()) { - QFile file(selection.internalData.toString()); - file.open(QFile::WriteOnly); - file.close(); + } else { + // Don't block locator filter execution with dialog + QTimer::singleShot(0, EditorManager::instance(), [info, selection] { + const QString targetFile = selection.internalData.toString(); + if (!info.exists()) { + if (Utils::CheckableMessageBox::shouldAskAgain(ICore::settings(), kAlwaysCreate)) { + Utils::CheckableMessageBox messageBox(ICore::dialogParent()); + messageBox.setWindowTitle(tr("Create File")); + messageBox.setIcon(QMessageBox::Question); + messageBox.setText( + tr("Create \"%1\"?") + .arg(Utils::FilePath::fromString(targetFile).shortNativePath())); + messageBox.setCheckBoxVisible(true); + messageBox.setCheckBoxText(tr("Always create")); + messageBox.setChecked(false); + messageBox.setStandardButtons(QDialogButtonBox::Cancel); + QPushButton *createButton = messageBox.addButton(tr("Create"), + QDialogButtonBox::AcceptRole); + messageBox.setDefaultButton(QDialogButtonBox::Cancel); + messageBox.exec(); + if (messageBox.clickedButton() != createButton) + return; + if (messageBox.isChecked()) + Utils::CheckableMessageBox::doNotAskAgain(ICore::settings(), kAlwaysCreate); + } + QFile file(targetFile); + file.open(QFile::WriteOnly); + file.close(); + } + const QFileInfo fileInfo(targetFile); + const QString cleanedFilePath = QDir::cleanPath(fileInfo.absoluteFilePath()); + EditorManager::openEditor(cleanedFilePath, + Id(), + EditorManager::CanContainLineAndColumnNumber); + }); } - const QFileInfo fileInfo(selection.internalData.toString()); - const QString cleanedFilePath = QDir::cleanPath(fileInfo.absoluteFilePath()); - EditorManager::openEditor(cleanedFilePath, Id(), EditorManager::CanContainLineAndColumnNumber); } bool FileSystemFilter::openConfigDialog(QWidget *parent, bool &needsRefresh)