ScxmlEditor: Inline scxmleditorstack.* into its only user

Also de-Q_OBJECT-ify and drop two unnecessary casts.

Change-Id: I8113fc52e96226c3bdd72b36a904775c2fa5f1e6
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
hjk
2024-07-01 12:32:06 +02:00
parent 157d8f2ca2
commit e28e96eae0
5 changed files with 64 additions and 117 deletions

View File

@@ -89,6 +89,5 @@ add_qtc_plugin(ScxmlEditor
scxmleditor.cpp scxmleditor.h
scxmleditordocument.cpp scxmleditordocument.h
scxmleditorplugin.cpp
scxmleditorstack.cpp scxmleditorstack.h
scxmltexteditor.cpp scxmltexteditor.h
)

View File

@@ -4,7 +4,6 @@
#include "mainwidget.h"
#include "scxmleditorconstants.h"
#include "scxmleditordocument.h"
#include "scxmleditorstack.h"
#include "scxmleditortr.h"
#include "scxmltexteditor.h"
@@ -32,9 +31,10 @@
#include <utils/utilsicons.h>
#include <QGuiApplication>
#include <QVBoxLayout>
#include <QStackedWidget>
#include <QToolBar>
#include <QUndoGroup>
#include <QVBoxLayout>
using namespace Core;
using namespace ScxmlEditor::Common;
@@ -43,6 +43,68 @@ using namespace Utils;
namespace ScxmlEditor::Internal {
class ScxmlEditorStack final : public QStackedWidget
{
public:
ScxmlEditorStack() { setObjectName("ScxmlEditorStack"); }
void add(ScxmlTextEditor *editor, QWidget *widget)
{
connect(Core::ModeManager::instance(), &Core::ModeManager::currentModeAboutToChange,
this, &ScxmlEditorStack::modeAboutToChange);
m_editors.append(editor);
addWidget(widget);
connect(editor, &ScxmlTextEditor::destroyed,
this, &ScxmlEditorStack::removeScxmlTextEditor);
}
QWidget *widgetForEditor(ScxmlTextEditor *xmlEditor)
{
const int i = m_editors.indexOf(xmlEditor);
QTC_ASSERT(i >= 0, return nullptr);
return widget(i);
}
void removeScxmlTextEditor(QObject *xmlEditor)
{
const int i = m_editors.indexOf(xmlEditor);
QTC_ASSERT(i >= 0, return);
QWidget *widget = this->widget(i);
if (widget) {
removeWidget(widget);
widget->deleteLater();
}
m_editors.removeAt(i);
}
bool setVisibleEditor(Core::IEditor *xmlEditor)
{
const int i = m_editors.indexOf(xmlEditor);
QTC_ASSERT(i >= 0, return false);
if (i != currentIndex())
setCurrentIndex(i);
return true;
}
private:
void modeAboutToChange(Utils::Id m)
{
// Sync the editor when entering edit mode
if (m == Core::Constants::MODE_EDIT) {
for (auto editor: std::as_const(m_editors))
if (auto document = qobject_cast<ScxmlEditorDocument*>(editor->textDocument()))
document->syncXmlFromDesignWidget();
}
}
QList<ScxmlTextEditor*> m_editors;
};
class ScxmlTextEditorWidget : public TextEditor::TextEditorWidget
{
public:

View File

@@ -24,7 +24,6 @@ QtcPlugin {
"scxmleditor.cpp", "scxmleditor.h",
"scxmleditordocument.cpp", "scxmleditordocument.h",
"scxmleditorplugin.cpp",
"scxmleditorstack.cpp", "scxmleditorstack.h",
"scxmltexteditor.cpp", "scxmltexteditor.h",
]

View File

@@ -1,74 +0,0 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "scxmleditorstack.h"
#include "scxmleditordocument.h"
#include "scxmltexteditor.h"
#include <coreplugin/coreconstants.h>
#include <coreplugin/imode.h>
#include <coreplugin/modemanager.h>
#include <utils/qtcassert.h>
using namespace ScxmlEditor;
using namespace ScxmlEditor::Internal;
ScxmlEditorStack::ScxmlEditorStack(QWidget *parent)
: QStackedWidget(parent)
{
setObjectName("ScxmlEditorStack");
}
void ScxmlEditorStack::add(ScxmlTextEditor *editor, QWidget *w)
{
connect(Core::ModeManager::instance(), &Core::ModeManager::currentModeAboutToChange,
this, &ScxmlEditorStack::modeAboutToChange);
m_editors.append(editor);
addWidget(w);
connect(editor, &ScxmlTextEditor::destroyed,
this, &ScxmlEditorStack::removeScxmlTextEditor);
}
void ScxmlEditorStack::removeScxmlTextEditor(QObject *xmlEditor)
{
const int i = m_editors.indexOf(static_cast<ScxmlTextEditor*>(xmlEditor));
QTC_ASSERT(i >= 0, return);
QWidget *widget = this->widget(i);
if (widget) {
removeWidget(widget);
widget->deleteLater();
}
m_editors.removeAt(i);
}
bool ScxmlEditorStack::setVisibleEditor(Core::IEditor *xmlEditor)
{
const int i = m_editors.indexOf(static_cast<ScxmlTextEditor*>(xmlEditor));
QTC_ASSERT(i >= 0, return false);
if (i != currentIndex())
setCurrentIndex(i);
return true;
}
QWidget *ScxmlEditorStack::widgetForEditor(ScxmlTextEditor *xmlEditor)
{
const int i = m_editors.indexOf(xmlEditor);
QTC_ASSERT(i >= 0, return nullptr);
return widget(i);
}
void ScxmlEditorStack::modeAboutToChange(Utils::Id m)
{
// Sync the editor when entering edit mode
if (m == Core::Constants::MODE_EDIT) {
for (auto editor: std::as_const(m_editors))
if (auto document = qobject_cast<ScxmlEditorDocument*>(editor->textDocument()))
document->syncXmlFromDesignWidget();
}
}

View File

@@ -1,39 +0,0 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include <utils/id.h>
#include <QStackedWidget>
namespace Core {
class IEditor;
class IMode;
}
namespace ScxmlEditor {
class ScxmlTextEditor;
namespace Internal {
class ScxmlEditorStack : public QStackedWidget {
Q_OBJECT
public:
ScxmlEditorStack(QWidget *parent = nullptr);
void add(ScxmlTextEditor *editor, QWidget *widget);
QWidget *widgetForEditor(ScxmlTextEditor *editor);
void removeScxmlTextEditor(QObject*);
bool setVisibleEditor(Core::IEditor *xmlEditor);
private:
void modeAboutToChange(Utils::Id m);
QVector<ScxmlTextEditor*> m_editors;
};
} // namespace Internal
} // namespace ScxmlEditor