Designer: Hide plugin class definition in .cpp

Change-Id: I722fa069e5bd00b3f5f2cbfbe38c0a2e0247dcb5
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
hjk
2024-01-12 18:47:55 +01:00
parent 34fd4e8ebf
commit d5938cf2bc
6 changed files with 132 additions and 154 deletions

View File

@@ -46,7 +46,7 @@ add_qtc_plugin(Designer
editordata.h
editorwidget.cpp editorwidget.h
formeditorfactory.cpp formeditorfactory.h
formeditorplugin.cpp formeditorplugin.h
formeditorplugin.cpp
formeditorstack.cpp formeditorstack.h
formeditor.cpp formeditor.h
formtemplatewizardpage.cpp formtemplatewizardpage.h

View File

@@ -53,7 +53,7 @@ QtcPlugin {
"editordata.h",
"editorwidget.cpp", "editorwidget.h",
"formeditorfactory.cpp", "formeditorfactory.h",
"formeditorplugin.cpp", "formeditorplugin.h",
"formeditorplugin.cpp",
"formeditorstack.cpp", "formeditorstack.h",
"formeditor.cpp", "formeditor.h",
"formtemplatewizardpage.cpp", "formtemplatewizardpage.h",

View File

@@ -63,5 +63,7 @@ QList<Core::IOptionsPage *> optionsPages();
void setQtPluginPath(const QString &qtPluginPath);
void addPluginPath(const QString &pluginPath);
QObject *createGoToSlotTest();
} // namespace Internal
} // namespace Designer

View File

@@ -1,8 +1,6 @@
// 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 "formeditorplugin.h"
#include "designerconstants.h"
#include "designertr.h"
#include "formeditorfactory.h"
@@ -28,6 +26,8 @@
#include <cppeditor/cppeditorconstants.h>
#include <extensionsystem/iplugin.h>
#include <projectexplorer/jsonwizard/jsonwizardfactory.h>
#include <utils/mimeconstants.h>
@@ -45,103 +45,7 @@ using namespace Core;
using namespace Designer::Constants;
using namespace Utils;
namespace Designer {
namespace Internal {
class FormEditorPluginPrivate
{
public:
QAction actionSwitchSource{Tr::tr("Switch Source/Form"), nullptr};
FormEditorFactory formEditorFactory;
SettingsPageProvider settingsPageProvider;
QtDesignerFormClassCodeGenerator formClassCodeGenerator;
FormPageFactory formPageFactory;
};
FormEditorPlugin::~FormEditorPlugin()
{
deleteInstance();
delete d;
}
static void parseArguments(const QStringList &arguments)
{
const auto doWithNext = [arguments](auto it, const std::function<void(QString)> &fun) {
++it;
if (it != arguments.cend())
fun(*it);
};
for (auto it = arguments.cbegin(); it != arguments.cend(); ++it) {
if (*it == "-designer-qt-pluginpath")
doWithNext(it, [](const QString &path) { setQtPluginPath(path); });
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
// -designer-pluginpath is only supported when building with Qt >= 6.7.0, which added the
// required API
else if (*it == "-designer-pluginpath")
doWithNext(it, [](const QString &path) { addPluginPath(path); });
#endif
}
}
bool FormEditorPlugin::initialize([[maybe_unused]] const QStringList &arguments,
[[maybe_unused]] QString *errorString)
{
d = new FormEditorPluginPrivate;
#ifdef CPP_ENABLED
IWizardFactory::registerFactoryCreator([]() -> IWizardFactory * {
IWizardFactory *wizard = new FormClassWizard;
wizard->setCategory(Core::Constants::WIZARD_CATEGORY_QT);
wizard->setDisplayCategory(::Core::Tr::tr(Core::Constants::WIZARD_TR_CATEGORY_QT));
wizard->setDisplayName(Tr::tr("Qt Designer Form Class"));
wizard->setIcon({}, "ui/h");
wizard->setId("C.FormClass");
wizard->setDescription(Tr::tr("Creates a Qt Designer form along with a matching class (C++ header and source file) "
"for implementation purposes. You can add the form and class to an existing Qt Widget Project."));
return wizard;
});
#endif
// Ensure that loading designer translations is done before FormEditorW is instantiated
const QString locale = ICore::userInterfaceLanguage();
if (!locale.isEmpty()) {
auto qtr = new QTranslator(this);
const QString creatorTrPath = ICore::resourcePath("translations").toString();
const QString qtTrPath = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
const QString trFile = "designer_" + locale;
if (qtr->load(trFile, qtTrPath) || qtr->load(trFile, creatorTrPath))
QCoreApplication::installTranslator(qtr);
}
parseArguments(arguments);
return true;
}
void FormEditorPlugin::extensionsInitialized()
{
DesignMode::setDesignModeIsRequired();
// 4) test and make sure everything works (undo, saving, editors, opening/closing multiple files, dirtiness etc)
ActionContainer *mtools = ActionManager::actionContainer(Core::Constants::M_TOOLS);
ActionContainer *mformtools = ActionManager::createMenu(M_FORMEDITOR);
mformtools->menu()->setTitle(Tr::tr("For&m Editor"));
mtools->addMenu(mformtools);
connect(&d->actionSwitchSource, &QAction::triggered, this, &FormEditorPlugin::switchSourceForm);
Context context(C_FORMEDITOR, Core::Constants::C_EDITORMANAGER);
Command *cmd = ActionManager::registerAction(&d->actionSwitchSource,
"FormEditor.FormSwitchSource", context);
cmd->setDefaultKeySequence(Tr::tr("Shift+F4"));
mformtools->addAction(cmd, Core::Constants::G_DEFAULT_THREE);
}
////////////////////////////////////////////////////
//
// PRIVATE functions
//
////////////////////////////////////////////////////
namespace Designer::Internal {
// Find out current existing editor file
static FilePath currentFile()
@@ -185,12 +89,113 @@ static FilePath otherFile()
return {};
}
void FormEditorPlugin::switchSourceForm()
static void parseArguments(const QStringList &arguments)
{
const auto doWithNext = [arguments](auto it, const std::function<void(QString)> &fun) {
++it;
if (it != arguments.cend())
fun(*it);
};
for (auto it = arguments.cbegin(); it != arguments.cend(); ++it) {
if (*it == "-designer-qt-pluginpath")
doWithNext(it, [](const QString &path) { setQtPluginPath(path); });
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
// -designer-pluginpath is only supported when building with Qt >= 6.7.0, which added the
// required API
else if (*it == "-designer-pluginpath")
doWithNext(it, [](const QString &path) { addPluginPath(path); });
#endif
}
}
class FormEditorPluginPrivate
{
public:
QAction actionSwitchSource{Tr::tr("Switch Source/Form"), nullptr};
FormEditorFactory formEditorFactory;
SettingsPageProvider settingsPageProvider;
QtDesignerFormClassCodeGenerator formClassCodeGenerator;
FormPageFactory formPageFactory;
};
class FormEditorPlugin final : public ExtensionSystem::IPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Designer.json")
~FormEditorPlugin() final
{
deleteInstance();
delete d;
}
bool initialize(const QStringList &arguments, QString *) final
{
d = new FormEditorPluginPrivate;
#ifdef CPP_ENABLED
IWizardFactory::registerFactoryCreator([]() -> IWizardFactory * {
IWizardFactory *wizard = new FormClassWizard;
wizard->setCategory(Core::Constants::WIZARD_CATEGORY_QT);
wizard->setDisplayCategory(::Core::Tr::tr(Core::Constants::WIZARD_TR_CATEGORY_QT));
wizard->setDisplayName(Tr::tr("Qt Designer Form Class"));
wizard->setIcon({}, "ui/h");
wizard->setId("C.FormClass");
wizard->setDescription(Tr::tr("Creates a Qt Designer form along with a matching class (C++ header and source file) "
"for implementation purposes. You can add the form and class to an existing Qt Widget Project."));
return wizard;
});
#endif
// Ensure that loading designer translations is done before FormEditorW is instantiated
const QString locale = ICore::userInterfaceLanguage();
if (!locale.isEmpty()) {
auto qtr = new QTranslator(this);
const QString creatorTrPath = ICore::resourcePath("translations").toString();
const QString qtTrPath = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
const QString trFile = "designer_" + locale;
if (qtr->load(trFile, qtTrPath) || qtr->load(trFile, creatorTrPath))
QCoreApplication::installTranslator(qtr);
}
#ifdef WITH_TESTS
addTestCreator(createGoToSlotTest);
#endif
parseArguments(arguments);
return true;
}
void extensionsInitialized() final
{
DesignMode::setDesignModeIsRequired();
// 4) test and make sure everything works (undo, saving, editors, opening/closing multiple files, dirtiness etc)
ActionContainer *mtools = ActionManager::actionContainer(Core::Constants::M_TOOLS);
ActionContainer *mformtools = ActionManager::createMenu(M_FORMEDITOR);
mformtools->menu()->setTitle(Tr::tr("For&m Editor"));
mtools->addMenu(mformtools);
connect(&d->actionSwitchSource, &QAction::triggered, this, &FormEditorPlugin::switchSourceForm);
Context context(C_FORMEDITOR, Core::Constants::C_EDITORMANAGER);
Command *cmd = ActionManager::registerAction(&d->actionSwitchSource,
"FormEditor.FormSwitchSource", context);
cmd->setDefaultKeySequence(Tr::tr("Shift+F4"));
mformtools->addAction(cmd, Core::Constants::G_DEFAULT_THREE);
}
void switchSourceForm()
{
const FilePath fileToOpen = otherFile();
if (!fileToOpen.isEmpty())
EditorManager::openEditor(fileToOpen);
}
}
} // Internal
} // Designer
FormEditorPluginPrivate *d = nullptr;
};
} // Designer::Internal
#include "formeditorplugin.moc"

View File

@@ -1,37 +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 <extensionsystem/iplugin.h>
namespace Designer {
namespace Internal {
class FormEditorPlugin : public ExtensionSystem::IPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Designer.json")
public:
FormEditorPlugin() = default;
~FormEditorPlugin() override;
#ifdef WITH_TESTS
private slots:
void test_gotoslot();
void test_gotoslot_data();
#endif
private:
bool initialize(const QStringList &arguments, QString *errorString) override;
void extensionsInitialized() override;
void switchSourceForm();
void initializeTemplates();
class FormEditorPluginPrivate *d = nullptr;
};
} // namespace Internal
} // namespace Designer

View File

@@ -1,8 +1,6 @@
// 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 "formeditorplugin.h"
#include "formeditor.h"
#include <coreplugin/editormanager/editormanager.h>
@@ -30,7 +28,7 @@ using namespace Designer;
using namespace Designer::Internal;
using namespace Utils;
namespace {
namespace Designer::Internal {
QTC_DECLARE_MYTESTDATADIR("../../../tests/designer/")
@@ -207,14 +205,18 @@ public:
}
};
} // anonymous namespace
class GoToSlotTest final : public QObject
{
Q_OBJECT
namespace Designer {
namespace Internal {
private slots:
void test_gotoslot();
void test_gotoslot_data();
};
/// Check: Executes "Go To Slot..." on a QPushButton in a *.ui file and checks if the respective
/// header and source files are correctly updated.
void FormEditorPlugin::test_gotoslot()
void GoToSlotTest::test_gotoslot()
{
class SystemSettingsMgr {
public:
@@ -235,7 +237,7 @@ void FormEditorPlugin::test_gotoslot()
(GoToSlotTestCase(Utils::transform(files, FilePath::fromString)));
}
void FormEditorPlugin::test_gotoslot_data()
void GoToSlotTest::test_gotoslot_data()
{
typedef QLatin1String _;
QTest::addColumn<QStringList>("files");
@@ -269,5 +271,11 @@ void FormEditorPlugin::test_gotoslot_data()
testDataDir.file(_("form.ui"))});
}
} // namespace Internal
} // namespace Designer
QObject *createGoToSlotTest()
{
return new GoToSlotTest;
}
} // Designer::Internal
#include "gotoslot_test.moc"