diff options
author | Eike Ziller <eike.ziller@qt.io> | 2019-10-22 13:22:06 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2019-10-23 06:11:24 +0000 |
commit | 1a6e441f1bfd9c0585970f5bc723e0190cb93603 (patch) | |
tree | c11ac7ced91dca9cc9aa141c29c64e0378be7bd7 /src/plugins | |
parent | f68588a58581a3169375b998ec6cf57509388986 (diff) | |
download | qt-creator-1a6e441f1bfd9c0585970f5bc723e0190cb93603.tar.gz |
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 <riitta-leena.miettinen@qt.io>
Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/coreplugin/locator/filesystemfilter.cpp | 55 |
1 files changed, 45 insertions, 10 deletions
diff --git a/src/plugins/coreplugin/locator/filesystemfilter.cpp b/src/plugins/coreplugin/locator/filesystemfilter.cpp index 5e04215539..41ad77e5c1 100644 --- a/src/plugins/coreplugin/locator/filesystemfilter.cpp +++ b/src/plugins/coreplugin/locator/filesystemfilter.cpp @@ -29,13 +29,17 @@ #include <coreplugin/coreconstants.h> #include <coreplugin/documentmanager.h> -#include <coreplugin/editormanager/ieditor.h> #include <coreplugin/editormanager/editormanager.h> +#include <coreplugin/editormanager/ieditor.h> +#include <coreplugin/icore.h> #include <coreplugin/idocument.h> +#include <utils/checkablemessagebox.h> #include <utils/fileutils.h> #include <QDir> +#include <QPushButton> #include <QRegularExpression> +#include <QTimer> using namespace Core; using namespace Core::Internal; @@ -148,8 +152,12 @@ QList<LocatorFilterEntry> FileSystemFilter::matchesFor(QFutureInterface<LocatorF return std::accumulate(std::begin(entries), std::end(entries), QList<LocatorFilterEntry>()); } +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) |