Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor regitster function #889

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/plugins/collaborators/collaborators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbstractWidget *()> gitCreator = []()->AbstractWidget* {
return new AbstractWidget(CVSkeeper::instance()->gitMainWidget());
};
windowService->registerWidgetCreator(MWNA_GIT, gitCreator);

connect(actionGit, &QAction::triggered, this, [=](){
windowService->replaceWidget(MWNA_GIT,
Expand Down
23 changes: 18 additions & 5 deletions src/plugins/core/modules/pluginmanagermodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
#include "base/abstractaction.h"
#include "uicontroller/controller.h"
#include "gui/pluginsui.h"
#include "gui/plugindetailsview.h"
#include "gui/pluginstorewidget.h"

#include <QAction>

using namespace dpfservice;

PluginManagerModule::PluginManagerModule()
{
pluginsUi = new PluginsUi();
}

PluginManagerModule::~PluginManagerModule()
Expand All @@ -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<AbstractWidget*()> detailWidgetCreator = [this]()->AbstractWidget*{
if (!pluginsUi) {
pluginsUi = new PluginsUi();
}
return new AbstractWidget(pluginsUi->getPluginDetailView());
};

std::function<AbstractWidget*()> 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]() {
Expand Down
55 changes: 47 additions & 8 deletions src/plugins/core/uicontroller/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
struct WidgetInfo
{
QString name;
DWidget *widget { nullptr };
QDockWidget *dockWidget { nullptr };
QString headerName;
QList<QAction *> headerList;
Expand All @@ -65,13 +64,38 @@
bool defaultVisible { true };
bool created { false }; // has already create dock
bool hiddenByManual { false };

std::function<AbstractWidget*()> createDWidgetFunc;

DWidget* getDWidget()
{
if (!widget) {
if (createDWidgetFunc) {
auto abstractWidget = createDWidgetFunc();
icon = abstractWidget->getDisplayIcon();
widget = static_cast<DWidget *>(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)
{
if (name == info.name && widget == info.widget)
return true;
return false;
};

private:
DWidget *widget { nullptr };
};

class DocksManagerButton : public DToolButton
Expand Down Expand Up @@ -196,6 +220,9 @@
if (!windowService->registerWidget) {
windowService->registerWidget = std::bind(&Controller::registerWidget, this, _1, _2);
}
if (!windowService->registerWidgetCreator) {
windowService->registerWidgetCreator = std::bind(&Controller::registerWidgetCreator, this, _1, _2);

Check warning on line 224 in src/plugins/core/uicontroller/controller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'widgetCreateFunc' can be declared with const. However it seems that 'registerWidgetCreator' is a callback function, if 'widgetCreateFunc' is declared with const you might also need to cast function pointer(s).
}
if (!windowService->showWidgetAtPosition) {
windowService->showWidgetAtPosition = std::bind(&Controller::showWidgetAtPosition, this, _1, _2, _3);
}
Expand Down Expand Up @@ -329,7 +356,7 @@

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;

Expand Down Expand Up @@ -362,7 +389,7 @@
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);
}
Expand Down Expand Up @@ -401,7 +428,7 @@

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;
Expand Down Expand Up @@ -442,7 +469,7 @@
widgetInfo.name = name;
widgetInfo.defaultPos = pos;
widgetInfo.replace = replace;
widgetInfo.widget = qWidget;
widgetInfo.setWidget(qWidget);
widgetInfo.defaultVisible = isVisible;
widgetInfo.icon = abstractWidget->getDisplayIcon();

Expand All @@ -464,13 +491,25 @@

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<AbstractWidget*()> &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)

Check warning on line 512 in src/plugins/core/uicontroller/controller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Local variable 'name' shadows outer argument
{
if (replace)
d->mainWindow->hideWidget(pos);
Expand Down Expand Up @@ -1096,7 +1135,7 @@
// 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");

Expand Down Expand Up @@ -1127,7 +1166,7 @@

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);
Expand Down
1 change: 1 addition & 0 deletions src/plugins/core/uicontroller/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbstractWidget*()> &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);
Expand Down
11 changes: 8 additions & 3 deletions src/plugins/find/findplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbstractWidget *()> 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);
}
10 changes: 6 additions & 4 deletions src/plugins/linglong/linglongplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,18 @@ bool LinglongPlugin::start()
return false;

windowService->addNavigationItem(new AbstractAction(action), Priority::low);
auto mainFrame = new AbstractWidget(new MainFrame());
std::function<AbstractWidget *()> 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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/services/window/windowservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <framework/framework.h>

#include <DToolButton>
#include <DWidget>

#include <QMap>
#include <QDockWidget>
Expand Down Expand Up @@ -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<AbstractWidget*()> &widgetCreateFunc);
DPF_INTERFACE(void, showWidgetAtPosition, const QString &name, Position pos, bool replace);
DPF_INTERFACE(QString, getCentralWidgetName);
DPF_INTERFACE(QStringList, getCurrentDockName, Position pos);
Expand Down
Loading