From 20eaf0bbeb6674d69d5a32eee0428418ea51923f Mon Sep 17 00:00:00 2001 From: Aleksei German Date: Mon, 9 Aug 2021 18:58:14 +0200 Subject: [PATCH] QmlDesigner: Fix for Annotation images crash - removed direct usage of SessionManager - replaced it with DesignDocument usage - deactivated add image button for files without projects - add hasProject method to DesignDocument Task-number: QDS-4810 Change-Id: I77c74f3c6e9b5b9dcbda68f7937cfeda531d0309 Reviewed-by: Tapani Mattila Reviewed-by: Qt CI Bot Reviewed-by: Thomas Hartmann --- .../annotationeditor/annotationcommenttab.cpp | 63 +++++++++++++++---- .../annotationeditor/annotationcommenttab.h | 13 ++-- .../components/integration/designdocument.cpp | 5 ++ .../components/integration/designdocument.h | 1 + 4 files changed, 63 insertions(+), 19 deletions(-) diff --git a/src/plugins/qmldesigner/components/annotationeditor/annotationcommenttab.cpp b/src/plugins/qmldesigner/components/annotationeditor/annotationcommenttab.cpp index 9a930747215..17d3cdd957f 100644 --- a/src/plugins/qmldesigner/components/annotationeditor/annotationcommenttab.cpp +++ b/src/plugins/qmldesigner/components/annotationeditor/annotationcommenttab.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2020 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of Qt Creator. @@ -24,18 +24,17 @@ ****************************************************************************/ #include "annotationcommenttab.h" -#include "defaultannotations.h" #include "ui_annotationcommenttab.h" -#include "richtexteditor/richtexteditor.h" +#include "defaultannotations.h" + +#include +#include +#include +#include #include -#include "projectexplorer/session.h" -#include "projectexplorer/target.h" -#include "qmldesignerplugin.h" -#include "qmlprojectmanager/qmlproject.h" - namespace QmlDesigner { AnnotationCommentTab::AnnotationCommentTab(QWidget *parent) @@ -50,10 +49,27 @@ AnnotationCommentTab::AnnotationCommentTab(QWidget *parent) filePath = backupFile(filePath); }); - Utils::FilePath projPath = ProjectExplorer::SessionManager::startupProject()->projectFilePath(); + m_editor->setImageActionVisible(false); - m_editor->setDocumentBaseUrl(QUrl::fromLocalFile(projPath.toString())); - m_editor->setImageActionVisible(true); + const QmlDesigner::DesignDocument *designDocument = QmlDesigner::QmlDesignerPlugin::instance() + ->documentManager().currentDesignDocument(); + Utils::FilePath projectPath; + + Q_ASSERT(designDocument); + + if (designDocument) { + if (designDocument->currentTarget() && designDocument->currentTarget()->project()) { + projectPath = designDocument->currentTarget()->project()->projectFilePath(); + m_editor->setImageActionVisible(true); + } + + if (projectPath.isEmpty()) { + projectPath = designDocument->fileName(); + m_editor->setImageActionVisible(false); + } + + m_editor->setDocumentBaseUrl(QUrl::fromLocalFile(projectPath.toString())); + } ui->formLayout->setWidget(3, QFormLayout::FieldRole, m_editor); @@ -124,14 +140,35 @@ void AnnotationCommentTab::setDefaultAnnotations(DefaultAnnotationsModel *defaul QString AnnotationCommentTab::backupFile(const QString &filePath) { - const QDir projDir( - ProjectExplorer::SessionManager::startupProject()->projectDirectory().toString()); + const QmlDesigner::DesignDocument *designDocument = QmlDesigner::QmlDesignerPlugin::instance() + ->documentManager().currentDesignDocument(); + Utils::FilePath projectFolderPath; + + Q_ASSERT(designDocument); + + if (designDocument) { + if (designDocument->hasProject()) + projectFolderPath = designDocument->projectFolder(); + if (projectFolderPath.isEmpty()) + projectFolderPath = designDocument->fileName().parentDir(); + } + else + return {}; + + const QDir projDir(projectFolderPath.toDir()); + + if (!projDir.exists()) + return {}; const QString imageSubDir(".AnnotationImages"); const QDir imgDir(projDir.absolutePath() + QDir::separator() + imageSubDir); ensureDir(imgDir); + Q_ASSERT(imgDir.exists()); + if (!imgDir.exists()) + return {}; + const QFileInfo oldFile(filePath); QFileInfo newFile(imgDir, oldFile.fileName()); diff --git a/src/plugins/qmldesigner/components/annotationeditor/annotationcommenttab.h b/src/plugins/qmldesigner/components/annotationeditor/annotationcommenttab.h index beb2e4c3774..a1e2509590d 100644 --- a/src/plugins/qmldesigner/components/annotationeditor/annotationcommenttab.h +++ b/src/plugins/qmldesigner/components/annotationeditor/annotationcommenttab.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2020 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of Qt Creator. @@ -25,7 +25,7 @@ #pragma once -#include "annotation.h" +#include #include #include @@ -67,16 +67,17 @@ public: signals: void titleChanged(const QString &text, QWidget *widget); +private: + QString backupFile(const QString &filePath); + void ensureDir(const QDir &dir); + int compareFileChecksum(const QString &firstFile, const QString &secondFile); + private: std::unique_ptr ui; RichTextEditor *m_editor; Comment m_comment; QPointer m_defaults; - - QString backupFile(const QString &filePath); - void ensureDir(const QDir &dir); - int compareFileChecksum(const QString &firstFile, const QString &secondFile); }; } //namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/integration/designdocument.cpp b/src/plugins/qmldesigner/components/integration/designdocument.cpp index d5e5757f9e7..9374e61776b 100644 --- a/src/plugins/qmldesigner/components/integration/designdocument.cpp +++ b/src/plugins/qmldesigner/components/integration/designdocument.cpp @@ -288,6 +288,11 @@ Utils::FilePath DesignDocument::projectFolder() const return {}; } +bool DesignDocument::hasProject() const +{ + return ProjectExplorer::SessionManager::projectForFile(fileName()); +} + void DesignDocument::changeToInFileComponentModel(ComponentTextModifier *textModifer) { m_inFileComponentTextModifier.reset(textModifer); diff --git a/src/plugins/qmldesigner/components/integration/designdocument.h b/src/plugins/qmldesigner/components/integration/designdocument.h index 3b0ed8c9edb..0e73c13e31c 100644 --- a/src/plugins/qmldesigner/components/integration/designdocument.h +++ b/src/plugins/qmldesigner/components/integration/designdocument.h @@ -101,6 +101,7 @@ public: bool isQtForMCUsProject() const; Utils::FilePath projectFolder() const; + bool hasProject() const; signals: void displayNameChanged(const QString &newFileName);