diff --git a/src/plugins/collaborators/collaborators.cpp b/src/plugins/collaborators/collaborators.cpp index ae13b4fc1..a2f75b5ce 100644 --- a/src/plugins/collaborators/collaborators.cpp +++ b/src/plugins/collaborators/collaborators.cpp @@ -40,11 +40,10 @@ bool Collaborators::start() windowService->addNavigationItem(new AbstractAction(actionGit), Priority::medium); - AbstractWidget *gitMainWidgetImpl = new AbstractWidget(CVSkeeper::instance()->gitMainWidget()); - - - windowService->registerWidget(MWNA_GIT, gitMainWidgetImpl); - + std::function gitCreator = []()->AbstractWidget* { + return new AbstractWidget(CVSkeeper::instance()->gitMainWidget()); + }; + windowService->registerWidgetCreator(MWNA_GIT, gitCreator); connect(actionGit, &QAction::triggered, this, [=](){ windowService->replaceWidget(MWNA_GIT, diff --git a/src/plugins/core/modules/pluginmanagermodule.cpp b/src/plugins/core/modules/pluginmanagermodule.cpp index 82094c541..242a36548 100644 --- a/src/plugins/core/modules/pluginmanagermodule.cpp +++ b/src/plugins/core/modules/pluginmanagermodule.cpp @@ -9,6 +9,8 @@ #include "base/abstractaction.h" #include "uicontroller/controller.h" #include "gui/pluginsui.h" +#include "gui/plugindetailsview.h" +#include "gui/pluginstorewidget.h" #include @@ -16,7 +18,6 @@ using namespace dpfservice; PluginManagerModule::PluginManagerModule() { - pluginsUi = new PluginsUi(); } PluginManagerModule::~PluginManagerModule() @@ -43,11 +44,23 @@ void PluginManagerModule::initialize(Controller *_uiController) uiController->addAction(MWM_HELP, actionOptionsImpl); uiController->addNavigationItem(actionOptionsImpl, Priority::lowest); - auto detailViewImpl = new AbstractWidget(pluginsUi->getPluginDetailView()); - auto storeWidgetImpl = new AbstractWidget(pluginsUi->getStoreWidget()); + std::function detailWidgetCreator = [this]()->AbstractWidget*{ + if (!pluginsUi) { + pluginsUi = new PluginsUi(); + } + return new AbstractWidget(pluginsUi->getPluginDetailView()); + }; + + std::function storeWidgetCreator = [this]()->AbstractWidget*{ + if (!pluginsUi) { + pluginsUi = new PluginsUi(); + } + return new AbstractWidget(pluginsUi->getStoreWidget()); + }; - uiController->registerWidget("pluginDetail", detailViewImpl); - uiController->registerWidget(MWMTA_PLUGINS, storeWidgetImpl); + uiController->registerWidgetCreator("pluginDetail", detailWidgetCreator); + uiController->registerWidgetCreator(MWMTA_PLUGINS, storeWidgetCreator); + uiController->bindWidgetToNavigation(MWMTA_PLUGINS, actionOptionsImpl); QObject::connect(pluginManagerAction, &QAction::triggered, this, [this]() { diff --git a/src/plugins/core/uicontroller/controller.cpp b/src/plugins/core/uicontroller/controller.cpp index c5c4fb45f..c49e780ec 100644 --- a/src/plugins/core/uicontroller/controller.cpp +++ b/src/plugins/core/uicontroller/controller.cpp @@ -53,7 +53,6 @@ DWIDGET_USE_NAMESPACE struct WidgetInfo { QString name; - DWidget *widget { nullptr }; QDockWidget *dockWidget { nullptr }; QString headerName; QList headerList; @@ -65,6 +64,28 @@ struct WidgetInfo bool defaultVisible { true }; bool created { false }; // has already create dock bool hiddenByManual { false }; + + std::function createDWidgetFunc; + + DWidget* getDWidget() + { + if (!widget) { + if (createDWidgetFunc) { + auto abstractWidget = createDWidgetFunc(); + icon = abstractWidget->getDisplayIcon(); + widget = static_cast(abstractWidget->qWidget()); + Q_ASSERT(widget); + if (!widget->parent()) + widget->setParent(Controller::instance()->mainWindow()); + } + } + return widget; + } + + void setWidget(DWidget *widget) + { + this->widget = widget; + } bool operator==(const WidgetInfo &info) { @@ -72,6 +93,9 @@ struct WidgetInfo return true; return false; }; + +private: + DWidget *widget { nullptr }; }; class DocksManagerButton : public DToolButton @@ -196,6 +220,9 @@ void Controller::registerService() if (!windowService->registerWidget) { windowService->registerWidget = std::bind(&Controller::registerWidget, this, _1, _2); } + if (!windowService->registerWidgetCreator) { + windowService->registerWidgetCreator = std::bind(&Controller::registerWidgetCreator, this, _1, _2); + } if (!windowService->showWidgetAtPosition) { windowService->showWidgetAtPosition = std::bind(&Controller::showWidgetAtPosition, this, _1, _2, _3); } @@ -329,7 +356,7 @@ Controller::~Controller() void Controller::createDockWidget(WidgetInfo &info) { - auto dock = d->mainWindow->addWidget(info.name, info.widget, info.defaultPos); + auto dock = d->mainWindow->addWidget(info.name, info.getDWidget(), info.defaultPos); info.dockWidget = dock; info.created = true; @@ -362,7 +389,7 @@ void Controller::raiseMode(const QString &mode) if (!widgetInfo.hiddenByManual) d->mainWindow->showWidget(widgetInfo.name); // widget in mainWindow is Dock(widget), show dock and hide widget - widgetInfo.widget->setVisible(widgetInfo.defaultVisible); + widgetInfo.getDWidget()->setVisible(widgetInfo.defaultVisible); if (widgetInfo.dockWidget) d->currentDocks.append(widgetInfo.name); } @@ -401,7 +428,7 @@ void Controller::insertWidget(const QString &name, Position pos, Qt::Orientation auto &info = d->allWidgets[name]; if (!info.created) { - auto dock = d->mainWindow->addWidget(name, info.widget, pos, orientation); + auto dock = d->mainWindow->addWidget(name, info.getDWidget(), pos, orientation); info.dockWidget = dock; info.created = true; info.replace = false; @@ -442,7 +469,7 @@ void Controller::registerWidgetToMode(const QString &name, AbstractWidget *abstr widgetInfo.name = name; widgetInfo.defaultPos = pos; widgetInfo.replace = replace; - widgetInfo.widget = qWidget; + widgetInfo.setWidget(qWidget); widgetInfo.defaultVisible = isVisible; widgetInfo.icon = abstractWidget->getDisplayIcon(); @@ -464,12 +491,24 @@ void Controller::registerWidget(const QString &name, AbstractWidget *abstractWid WidgetInfo widgetInfo; widgetInfo.name = name; - widgetInfo.widget = widget; + widgetInfo.setWidget(widget); widgetInfo.icon = abstractWidget->getDisplayIcon(); d->allWidgets.insert(name, widgetInfo); } +void Controller::registerWidgetCreator(const QString &name, std::function &widgetCreateFunc) +{ + if (d->allWidgets.contains(name)) + return; + + WidgetInfo widgetInfo; + widgetInfo.name = name; + widgetInfo.createDWidgetFunc = widgetCreateFunc; + + d->allWidgets.insert(name, widgetInfo); +} + void Controller::showWidgetAtPosition(const QString &name, Position pos, bool replace) { if (replace) @@ -1096,7 +1135,7 @@ void Controller::initContextWidget() // add contextWidget after add centralWidget or it`s height is incorrect WidgetInfo info; info.name = WN_CONTEXTWIDGET; - info.widget = d->contextWidget; + info.setWidget(d->contextWidget); info.defaultPos = Position::Bottom; info.icon = QIcon::fromTheme("context_widget"); @@ -1127,7 +1166,7 @@ void Controller::initWorkspaceWidget() WidgetInfo info; info.name = WN_WORKSPACE; - info.widget = d->workspace; + info.setWidget(d->workspace); info.defaultPos = Position::Left; info.replace = true; d->allWidgets.insert(WN_WORKSPACE, info); diff --git a/src/plugins/core/uicontroller/controller.h b/src/plugins/core/uicontroller/controller.h index 5be4dd565..e3e3d2684 100644 --- a/src/plugins/core/uicontroller/controller.h +++ b/src/plugins/core/uicontroller/controller.h @@ -37,6 +37,7 @@ public slots: void setCurrentPlugin(const QString &plugin); void registerWidgetToMode(const QString &name, AbstractWidget *abstractWidget, const QString &mode, Position pos, bool replace, bool isVisible); void registerWidget(const QString &name, AbstractWidget *abstractWidget); + void registerWidgetCreator(const QString &name, std::function &widgetCreateFunc); void replaceWidget(const QString &name, Position pos = Position::FullWindow); void insertWidget(const QString &name, Position pos, Qt::Orientation orientation); void hideWidget(const QString &name); diff --git a/src/plugins/find/findplugin.cpp b/src/plugins/find/findplugin.cpp index 225edbf4b..b44b8666b 100644 --- a/src/plugins/find/findplugin.cpp +++ b/src/plugins/find/findplugin.cpp @@ -74,11 +74,16 @@ void FindPlugin::registerToSidebar() auto actionImpl = new AbstractAction(action); windowService->addNavigationItem(actionImpl, Priority::highest); - advSearchWidget = new AdvancedSearchWidget; - windowService->registerWidget(MWNA_ADVANCEDSEARCH, new AbstractWidget(advSearchWidget)); + std::function findCreator = []()->AbstractWidget * { + auto advancedSearchWidget = new AdvancedSearchWidget(); + advancedSearchWidget->initOperator(); + return new AbstractWidget(advancedSearchWidget); + }; + + windowService->registerWidgetCreator(MWNA_ADVANCEDSEARCH, findCreator); + windowService->setDockHeaderName(MWNA_ADVANCEDSEARCH, tr("ADVANCED SEARCH")); windowService->bindWidgetToNavigation(MWNA_ADVANCEDSEARCH, actionImpl); - advSearchWidget->initOperator(); connect(action, &QAction::triggered, this, &FindPlugin::switchToSearch, Qt::DirectConnection); } diff --git a/src/plugins/linglong/linglongplugin.cpp b/src/plugins/linglong/linglongplugin.cpp index df4e44c5b..27154fa57 100644 --- a/src/plugins/linglong/linglongplugin.cpp +++ b/src/plugins/linglong/linglongplugin.cpp @@ -65,16 +65,18 @@ bool LinglongPlugin::start() return false; windowService->addNavigationItem(new AbstractAction(action), Priority::low); - auto mainFrame = new AbstractWidget(new MainFrame()); + std::function findCreator = []() -> AbstractWidget * { + return new AbstractWidget(new MainFrame()); + }; + // mainFrame will setParent when register to windowService - windowService->registerWidget(LL_NAME, mainFrame); + windowService->registerWidgetCreator(LL_NAME, findCreator); connect(action, &QAction::triggered, this, [=]() { windowService->showWidgetAtPosition(LL_NAME, Position::FullWindow, true); windowService->showContextWidget(); MainFrame::checkToolInstalled("ll-cli"); - }, - Qt::DirectConnection); + }, Qt::DirectConnection); return true; } diff --git a/src/services/window/windowservice.h b/src/services/window/windowservice.h index 5ef23c13d..9e13737c6 100644 --- a/src/services/window/windowservice.h +++ b/src/services/window/windowservice.h @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -92,6 +93,7 @@ class WindowService final : public dpf::PluginService, dpf::AutoServiceRegister< * \param abstractWidget */ DPF_INTERFACE(void, registerWidget, const QString &name, AbstractWidget *abstractWidget); + DPF_INTERFACE(void, registerWidgetCreator, const QString &name, std::function &widgetCreateFunc); DPF_INTERFACE(void, showWidgetAtPosition, const QString &name, Position pos, bool replace); DPF_INTERFACE(QString, getCentralWidgetName); DPF_INTERFACE(QStringList, getCurrentDockName, Position pos);