From 89e25090e39ff069faf64e43feff8cfb931fbf51 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Mon, 12 May 2025 16:36:55 +0200 Subject: CppEditor: Let users add a file to a project from warning message Fixes: QTCREATORBUG-25834 Change-Id: I455d3402da485ef8d6392ab93e9b4c7968c4670e Reviewed-by: David Schulz --- src/plugins/cppeditor/cppeditordocument.cpp | 59 +++++++++++++++++++++++++++-- src/plugins/cppeditor/cppeditordocument.h | 1 + 2 files changed, 56 insertions(+), 4 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/cppeditor/cppeditordocument.cpp b/src/plugins/cppeditor/cppeditordocument.cpp index f31004a10e4..fa1c6363990 100644 --- a/src/plugins/cppeditor/cppeditordocument.cpp +++ b/src/plugins/cppeditor/cppeditordocument.cpp @@ -17,6 +17,11 @@ #include #include +#include +#include +#include +#include + #include #include #include @@ -28,19 +33,53 @@ #include #include #include +#include #include #include #include +#include + const char NO_PROJECT_CONFIGURATION[] = "NoProject"; +using namespace ProjectExplorer; using namespace TextEditor; using namespace Utils; namespace CppEditor { namespace Internal { +static InfoBarEntry createInfoBarEntry(const FilePath &filePath) +{ + InfoBarEntry infoBarEntry( + NO_PROJECT_CONFIGURATION, + Tr::tr( + "Warning: This file is not part of any project. " + "The code model might have issues parsing this file properly.")); + InfoBarEntry::CallBack addToProject = [filePath] { + Wizard wizard; + const std::unique_ptr wizardPage = std::make_unique( + dialogParent()); + wizard.setWindowTitle(Tr::tr("Add File to Project")); + wizard.setProperty( + ProjectExplorer::Constants::PROJECT_POINTER, + QVariant::fromValue(static_cast(ProjectManager::startupProject()))); + wizard.addPage(wizardPage.get()); + wizardPage->setFiles({filePath}); + wizardPage->initializeVersionControls(); + wizardPage->initializeProjectTree( + nullptr, {}, Core::IWizardFactory::FileWizard, ProjectExplorer::AddExistingFile, false); + if (wizard.exec() == QDialog::Accepted && wizardPage->currentNode()) + ProjectExplorerPlugin::addExistingFiles(wizardPage->currentNode(), {filePath}); + }; + const bool enableAddToProjectButton = !ProjectManager::isAnyProjectParsing() + && !ProjectManager::isKnownFile(filePath); + infoBarEntry.addCustomButton(Tr::tr("Add to project..."), addToProject, {}, {}, + enableAddToProjectButton); + return infoBarEntry; +} + enum { processDocumentIntervalInMs = 150 }; class CppEditorDocumentHandleImpl : public CppEditorDocumentHandle @@ -105,10 +144,15 @@ CppEditorDocument::CppEditorDocument() this, &CppEditorDocument::reparseWithPreferredParseContext); minimizableInfoBars()->setSettingsGroup(Constants::CPPEDITOR_SETTINGSGROUP); - minimizableInfoBars()->setPossibleInfoBarEntries( - {{NO_PROJECT_CONFIGURATION, - Tr::tr("Warning: This file is not part of any project. " - "The code model might have issues parsing this file properly.")}}); + minimizableInfoBars()->setPossibleInfoBarEntries({createInfoBarEntry(filePath())}); + connect(ProjectManager::instance(), &ProjectManager::projectAdded, + this, &CppEditorDocument::updateInfoBarEntryIfVisible); + connect(ProjectManager::instance(), &ProjectManager::projectRemoved, + this, &CppEditorDocument::updateInfoBarEntryIfVisible); + connect(ProjectManager::instance(), &ProjectManager::projectStartedParsing, + this, &CppEditorDocument::updateInfoBarEntryIfVisible); + connect(ProjectManager::instance(), &ProjectManager::projectFinishedParsing, + this, &CppEditorDocument::updateInfoBarEntryIfVisible); // See also onFilePathChanged() for more initialization } @@ -464,6 +508,7 @@ BaseEditorDocumentProcessor *CppEditorDocument::processor() [this](const ProjectPartInfo &info) { const bool hasProjectPart = !(info.hints & ProjectPartInfo::IsFallbackMatch); minimizableInfoBars()->setInfoVisible(NO_PROJECT_CONFIGURATION, !hasProjectPart); + updateInfoBarEntryIfVisible(); m_parseContextModel.update(info); const bool isAmbiguous = info.hints & ProjectPartInfo::IsAmbiguousMatch; const bool isProjectFile = info.hints & ProjectPartInfo::IsFromProjectMatch; @@ -598,5 +643,11 @@ void CppEditorDocument::onDiagnosticsChanged(const FilePath &fileName, const QSt } } +void CppEditorDocument::updateInfoBarEntryIfVisible() +{ + if (minimizableInfoBars()->isShownInInfoBar(NO_PROJECT_CONFIGURATION)) + minimizableInfoBars()->updateEntry(createInfoBarEntry(filePath())); +} + } // namespace Internal } // namespace CppEditor diff --git a/src/plugins/cppeditor/cppeditordocument.h b/src/plugins/cppeditor/cppeditordocument.h index aba526e7872..b913408a46b 100644 --- a/src/plugins/cppeditor/cppeditordocument.h +++ b/src/plugins/cppeditor/cppeditordocument.h @@ -87,6 +87,7 @@ private: void onReloadFinished(); void onDiagnosticsChanged(const Utils::FilePath &fileName, const QString &kind); + void updateInfoBarEntryIfVisible(); void reparseWithPreferredParseContext(const QString &id); -- cgit v1.2.3