From 08359cafde68103aa5f99e259633d30374e97f5a Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 9 May 2022 14:55:18 +0200 Subject: [PATCH] Warn when editing generated files We already warn when editing a file that is not corresponding to a node in the project and not under some project directory and not under some project's version control. Generated files are part of the project tree (not shown by default, if "Hide Generated Files" is not turned off in the filter settings). So, add another warning that the file is generated when editing a file that is part of the project tree, but has the isGenerated flag set. Task-number: QTCREATORBUG-27173 Change-Id: Id554d0e97bd5e033e957e7b3a863897845b7b654 Reviewed-by: Reviewed-by: Qt CI Bot Reviewed-by: Christian Kandeler --- src/plugins/projectexplorer/projecttree.cpp | 42 ++++++++++++++------- src/plugins/projectexplorer/projecttree.h | 2 + 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/plugins/projectexplorer/projecttree.cpp b/src/plugins/projectexplorer/projecttree.cpp index ee040d5a37f..7941ab56607 100644 --- a/src/plugins/projectexplorer/projecttree.cpp +++ b/src/plugins/projectexplorer/projecttree.cpp @@ -52,7 +52,9 @@ #include #include -namespace { const char EXTERNAL_FILE_WARNING[] = "ExternalFile"; } +namespace { +const char EXTERNAL_OR_GENERATED_FILE_WARNING[] = "ExternalOrGeneratedFile"; +} using namespace Utils; @@ -214,14 +216,20 @@ void ProjectTree::setCurrent(Node *node, Project *project) } if (Core::IDocument *document = Core::EditorManager::currentDocument()) { - if (node) { - disconnect(document, &Core::IDocument::changed, - this, &ProjectTree::updateExternalFileWarning); - document->infoBar()->removeInfo(EXTERNAL_FILE_WARNING); - } else { + if (!node) { connect(document, &Core::IDocument::changed, this, &ProjectTree::updateExternalFileWarning, Qt::UniqueConnection); + } else if (node->isGenerated()) { + connect(document, &Core::IDocument::changed, + this, &ProjectTree::updateGeneratedFileWarning, + Qt::UniqueConnection); + } else { + disconnect(document, &Core::IDocument::changed, + this, &ProjectTree::updateExternalFileWarning); + disconnect(document, &Core::IDocument::changed, + this, &ProjectTree::updateGeneratedFileWarning); + document->infoBar()->removeInfo(EXTERNAL_OR_GENERATED_FILE_WARNING); } } @@ -304,18 +312,18 @@ void ProjectTree::changeProjectRootDirectory() m_currentProject->changeRootProjectDirectory(); } -void ProjectTree::updateExternalFileWarning() +void ProjectTree::updateFileWarning(const QString &text) { auto document = qobject_cast(sender()); if (!document || document->filePath().isEmpty()) return; Utils::InfoBar *infoBar = document->infoBar(); - Utils::Id externalFileId(EXTERNAL_FILE_WARNING); + Utils::Id infoId(EXTERNAL_OR_GENERATED_FILE_WARNING); if (!document->isModified()) { - infoBar->removeInfo(externalFileId); + infoBar->removeInfo(infoId); return; } - if (!infoBar->canInfoBeAdded(externalFileId)) + if (!infoBar->canInfoBeAdded(infoId)) return; const FilePath fileName = document->filePath(); const QList projects = SessionManager::projects(); @@ -335,9 +343,17 @@ void ProjectTree::updateExternalFileWarning() } } infoBar->addInfo( - Utils::InfoBarEntry(externalFileId, - tr("Warning: This file is outside the project directory."), - Utils::InfoBarEntry::GlobalSuppression::Enabled)); + Utils::InfoBarEntry(infoId, text, Utils::InfoBarEntry::GlobalSuppression::Enabled)); +} + +void ProjectTree::updateExternalFileWarning() +{ + updateFileWarning(tr("Warning: This file is outside the project directory.")); +} + +void ProjectTree::updateGeneratedFileWarning() +{ + updateFileWarning(tr("Warning: This file is generated.")); } bool ProjectTree::hasFocus(ProjectTreeWidget *widget) diff --git a/src/plugins/projectexplorer/projecttree.h b/src/plugins/projectexplorer/projecttree.h index 3947666d3a6..c09d3591561 100644 --- a/src/plugins/projectexplorer/projecttree.h +++ b/src/plugins/projectexplorer/projecttree.h @@ -132,7 +132,9 @@ private: void updateFromFocus(); + void updateFileWarning(const QString &text); void updateExternalFileWarning(); + void updateGeneratedFileWarning(); static bool hasFocus(Internal::ProjectTreeWidget *widget); Internal::ProjectTreeWidget *currentWidget() const; void hideContextMenu();