QmlPreview: Avoid processing all editors

Only act if there is a preview running. Explicitly connect and
disconnect to the editor manager if the preview gets triggered.
Fixes displaying message boxes when changing qml documents
without a running preview.
Reverts 66d94a82a32cdfdb344d55ef2c610ee9b3f03f7fi which had been
a hotfix and provides a more intrusive solution to the misbehavior
revealed by 5336fd83a0.

Change-Id: I88be4eca077ac1d56d8e4790a70220e7d3efa960
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Christian Stenger
2023-10-19 10:24:54 +02:00
parent fb2fd513be
commit 5a86b883fa

View File

@@ -114,7 +114,8 @@ public:
void onEditorChanged(Core::IEditor *editor); void onEditorChanged(Core::IEditor *editor);
void onEditorAboutToClose(Core::IEditor *editor); void onEditorAboutToClose(Core::IEditor *editor);
void setDirty(); void setDirty();
void attachToEditor(); void attachToEditorManager();
void detachFromEditorManager();
void checkEditor(); void checkEditor();
void checkFile(const QString &fileName); void checkFile(const QString &fileName);
void triggerPreview(const QString &changedFile, const QByteArray &contents); void triggerPreview(const QString &changedFile, const QByteArray &contents);
@@ -165,7 +166,12 @@ QmlPreviewPluginPrivate::QmlPreviewPluginPrivate(QmlPreviewPlugin *parent)
runPreviewAction->setEnabled(ProjectManager::startupProject() != nullptr); runPreviewAction->setEnabled(ProjectManager::startupProject() != nullptr);
connect(ProjectManager::instance(), &ProjectManager::startupProjectChanged, runPreviewAction, connect(ProjectManager::instance(), &ProjectManager::startupProjectChanged, runPreviewAction,
&QAction::setEnabled); &QAction::setEnabled);
connect(runPreviewAction, &QAction::triggered, this, [this] { connect(runPreviewAction, &QAction::triggered, this, [runPreviewAction, this] {
runPreviewAction->setEnabled(false);
attachToEditorManager();
setDirty();
onEditorChanged(Core::EditorManager::currentEditor());
if (auto multiLanguageAspect = QmlProjectManager::QmlMultiLanguageAspect::current()) if (auto multiLanguageAspect = QmlProjectManager::QmlMultiLanguageAspect::current())
m_localeIsoCode = multiLanguageAspect->currentLocale(); m_localeIsoCode = multiLanguageAspect->currentLocale();
bool skipDeploy = false; bool skipDeploy = false;
@@ -231,8 +237,6 @@ QmlPreviewPluginPrivate::QmlPreviewPluginPrivate(QmlPreviewPlugin *parent)
connect(q, &QmlPreviewPlugin::checkDocument, parser, &QmlPreviewParser::parse); connect(q, &QmlPreviewPlugin::checkDocument, parser, &QmlPreviewParser::parse);
connect(q, &QmlPreviewPlugin::previewedFileChanged, this, &QmlPreviewPluginPrivate::checkFile); connect(q, &QmlPreviewPlugin::previewedFileChanged, this, &QmlPreviewPluginPrivate::checkFile);
connect(parser, &QmlPreviewParser::success, this, &QmlPreviewPluginPrivate::triggerPreview); connect(parser, &QmlPreviewParser::success, this, &QmlPreviewPluginPrivate::triggerPreview);
attachToEditor();
} }
QmlPreviewPlugin::~QmlPreviewPlugin() QmlPreviewPlugin::~QmlPreviewPlugin()
@@ -439,9 +443,14 @@ void QmlPreviewPlugin::removePreview(RunControl *preview)
{ {
d->m_runningPreviews.removeOne(preview); d->m_runningPreviews.removeOne(preview);
emit runningPreviewsChanged(d->m_runningPreviews); emit runningPreviewsChanged(d->m_runningPreviews);
if (d->m_runningPreviews.isEmpty()) {
if (auto cmd = Core::ActionManager::command("QmlPreview.RunPreview"); cmd && cmd->action())
cmd->action()->setEnabled(true);
d->detachFromEditorManager();
}
} }
void QmlPreviewPluginPrivate::attachToEditor() void QmlPreviewPluginPrivate::attachToEditorManager()
{ {
Core::EditorManager *editorManager = Core::EditorManager::instance(); Core::EditorManager *editorManager = Core::EditorManager::instance();
connect(editorManager, &Core::EditorManager::currentEditorChanged, connect(editorManager, &Core::EditorManager::currentEditorChanged,
@@ -450,10 +459,17 @@ void QmlPreviewPluginPrivate::attachToEditor()
this, &QmlPreviewPluginPrivate::onEditorAboutToClose); this, &QmlPreviewPluginPrivate::onEditorAboutToClose);
} }
void QmlPreviewPluginPrivate::detachFromEditorManager()
{
Core::EditorManager *editorManager = Core::EditorManager::instance();
disconnect(editorManager, &Core::EditorManager::currentEditorChanged,
this, &QmlPreviewPluginPrivate::onEditorChanged);
disconnect(editorManager, &Core::EditorManager::editorAboutToClose,
this, &QmlPreviewPluginPrivate::onEditorAboutToClose);
}
void QmlPreviewPluginPrivate::checkEditor() void QmlPreviewPluginPrivate::checkEditor()
{ {
if (m_runningPreviews.isEmpty())
return;
QmlJS::Dialect::Enum dialect = QmlJS::Dialect::AnyLanguage; QmlJS::Dialect::Enum dialect = QmlJS::Dialect::AnyLanguage;
Core::IDocument *doc = m_lastEditor->document(); Core::IDocument *doc = m_lastEditor->document();
using namespace Utils::Constants; using namespace Utils::Constants;