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 <tapani.mattila@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Aleksei German
2021-08-09 18:58:14 +02:00
parent 1afea827bb
commit 20eaf0bbeb
4 changed files with 63 additions and 19 deletions

View File

@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2020 The Qt Company Ltd. ** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of Qt Creator. ** This file is part of Qt Creator.
@@ -24,18 +24,17 @@
****************************************************************************/ ****************************************************************************/
#include "annotationcommenttab.h" #include "annotationcommenttab.h"
#include "defaultannotations.h"
#include "ui_annotationcommenttab.h" #include "ui_annotationcommenttab.h"
#include "richtexteditor/richtexteditor.h" #include "defaultannotations.h"
#include <qmldesignerplugin.h>
#include <richtexteditor/richtexteditor.h>
#include <projectexplorer/target.h>
#include <qmlprojectmanager/qmlproject.h>
#include <QCryptographicHash> #include <QCryptographicHash>
#include "projectexplorer/session.h"
#include "projectexplorer/target.h"
#include "qmldesignerplugin.h"
#include "qmlprojectmanager/qmlproject.h"
namespace QmlDesigner { namespace QmlDesigner {
AnnotationCommentTab::AnnotationCommentTab(QWidget *parent) AnnotationCommentTab::AnnotationCommentTab(QWidget *parent)
@@ -50,10 +49,27 @@ AnnotationCommentTab::AnnotationCommentTab(QWidget *parent)
filePath = backupFile(filePath); filePath = backupFile(filePath);
}); });
Utils::FilePath projPath = ProjectExplorer::SessionManager::startupProject()->projectFilePath(); m_editor->setImageActionVisible(false);
m_editor->setDocumentBaseUrl(QUrl::fromLocalFile(projPath.toString())); const QmlDesigner::DesignDocument *designDocument = QmlDesigner::QmlDesignerPlugin::instance()
m_editor->setImageActionVisible(true); ->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); ui->formLayout->setWidget(3, QFormLayout::FieldRole, m_editor);
@@ -124,14 +140,35 @@ void AnnotationCommentTab::setDefaultAnnotations(DefaultAnnotationsModel *defaul
QString AnnotationCommentTab::backupFile(const QString &filePath) QString AnnotationCommentTab::backupFile(const QString &filePath)
{ {
const QDir projDir( const QmlDesigner::DesignDocument *designDocument = QmlDesigner::QmlDesignerPlugin::instance()
ProjectExplorer::SessionManager::startupProject()->projectDirectory().toString()); ->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 QString imageSubDir(".AnnotationImages");
const QDir imgDir(projDir.absolutePath() + QDir::separator() + imageSubDir); const QDir imgDir(projDir.absolutePath() + QDir::separator() + imageSubDir);
ensureDir(imgDir); ensureDir(imgDir);
Q_ASSERT(imgDir.exists());
if (!imgDir.exists())
return {};
const QFileInfo oldFile(filePath); const QFileInfo oldFile(filePath);
QFileInfo newFile(imgDir, oldFile.fileName()); QFileInfo newFile(imgDir, oldFile.fileName());

View File

@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2020 The Qt Company Ltd. ** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of Qt Creator. ** This file is part of Qt Creator.
@@ -25,7 +25,7 @@
#pragma once #pragma once
#include "annotation.h" #include <annotation.h>
#include <QWidget> #include <QWidget>
#include <QPointer> #include <QPointer>
@@ -67,16 +67,17 @@ public:
signals: signals:
void titleChanged(const QString &text, QWidget *widget); 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: private:
std::unique_ptr<Ui::AnnotationCommentTab> ui; std::unique_ptr<Ui::AnnotationCommentTab> ui;
RichTextEditor *m_editor; RichTextEditor *m_editor;
Comment m_comment; Comment m_comment;
QPointer<DefaultAnnotationsModel> m_defaults; QPointer<DefaultAnnotationsModel> m_defaults;
QString backupFile(const QString &filePath);
void ensureDir(const QDir &dir);
int compareFileChecksum(const QString &firstFile, const QString &secondFile);
}; };
} //namespace QmlDesigner } //namespace QmlDesigner

View File

@@ -288,6 +288,11 @@ Utils::FilePath DesignDocument::projectFolder() const
return {}; return {};
} }
bool DesignDocument::hasProject() const
{
return ProjectExplorer::SessionManager::projectForFile(fileName());
}
void DesignDocument::changeToInFileComponentModel(ComponentTextModifier *textModifer) void DesignDocument::changeToInFileComponentModel(ComponentTextModifier *textModifer)
{ {
m_inFileComponentTextModifier.reset(textModifer); m_inFileComponentTextModifier.reset(textModifer);

View File

@@ -101,6 +101,7 @@ public:
bool isQtForMCUsProject() const; bool isQtForMCUsProject() const;
Utils::FilePath projectFolder() const; Utils::FilePath projectFolder() const;
bool hasProject() const;
signals: signals:
void displayNameChanged(const QString &newFileName); void displayNameChanged(const QString &newFileName);