diff options
author | Christian Kandeler <[email protected]> | 2025-05-12 16:36:55 +0200 |
---|---|---|
committer | Christian Kandeler <[email protected]> | 2025-05-19 14:25:24 +0000 |
commit | 89e25090e39ff069faf64e43feff8cfb931fbf51 (patch) | |
tree | 547c3b5ca6b664bede9b627ce0af6e00a22552fd /src/plugins | |
parent | f5fd1050252fa85405442b4677b9c2c250d810e5 (diff) |
CppEditor: Let users add a file to a project from warning message
Fixes: QTCREATORBUG-25834
Change-Id: I455d3402da485ef8d6392ab93e9b4c7968c4670e
Reviewed-by: David Schulz <[email protected]>
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/cppeditor/cppeditordocument.cpp | 59 | ||||
-rw-r--r-- | src/plugins/cppeditor/cppeditordocument.h | 1 |
2 files changed, 56 insertions, 4 deletions
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 <coreplugin/editormanager/editormanager.h> #include <coreplugin/session.h> +#include <projectexplorer/projectexplorer.h> +#include <projectexplorer/projectexplorerconstants.h> +#include <projectexplorer/projectmanager.h> +#include <projectexplorer/projectwizardpage.h> + #include <texteditor/icodestylepreferencesfactory.h> #include <texteditor/storagesettings.h> #include <texteditor/textdocumentlayout.h> @@ -28,19 +33,53 @@ #include <utils/minimizableinfobars.h> #include <utils/qtcassert.h> #include <utils/utilsicons.h> +#include <utils/wizard.h> #include <QApplication> #include <QScopeGuard> #include <QTextDocument> +#include <memory> + 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( + "<b>Warning</b>: 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<ProjectWizardPage> wizardPage = std::make_unique<ProjectWizardPage>( + dialogParent()); + wizard.setWindowTitle(Tr::tr("Add File to Project")); + wizard.setProperty( + ProjectExplorer::Constants::PROJECT_POINTER, + QVariant::fromValue(static_cast<void *>(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("<b>Warning</b>: 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); |