From 07ac08cda42b70f4dfb44032e97b00dfcb74e134 Mon Sep 17 00:00:00 2001 From: Raul Radu <raul.radu@mail.polimi.it> Date: Wed, 24 Jan 2024 21:21:58 +0100 Subject: [PATCH] [Core] Switched to smart pointers and stl algs - Using smart pointers to contain windows and actions - Added method to add actions to QMenus directly from the Core - rework of addWidowFromXML to work with smart pointers --- src/shared/Core/SkywardHubCore.cpp | 42 ++++++++++++++++++++---------- src/shared/Core/SkywardHubCore.h | 7 ++--- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/shared/Core/SkywardHubCore.cpp b/src/shared/Core/SkywardHubCore.cpp index bccda021..0f0262ed 100644 --- a/src/shared/Core/SkywardHubCore.cpp +++ b/src/shared/Core/SkywardHubCore.cpp @@ -60,7 +60,7 @@ void SkywardHubCore::init() SkywardHubStrings::defaultConfigurationIconPath, ""); } -QList<QAction*> SkywardHubCore::getHubMenuActions() +QList<std::shared_ptr<QAction>> SkywardHubCore::getHubMenuActions() { return hubMenuActionsList; } @@ -85,14 +85,16 @@ void SkywardHubCore::checkDefaultFolders() void SkywardHubCore::buildHubMenuActions() { - QAction* save = new QAction("Save configuration"); + std::shared_ptr<QAction> save = + std::make_shared<QAction>("Save configuration"); hubMenuActionsList.append(save); - connect(save, &QAction::triggered, this, + connect(save.get(), &QAction::triggered, this, [this]() { saveConfigurationPrompt(); }); - QAction* newWindow = new QAction("New Window"); + std::shared_ptr<QAction> newWindow = + std::make_shared<QAction>("New Window"); hubMenuActionsList.append(newWindow); - connect(newWindow, &QAction::triggered, this, + connect(newWindow.get(), &QAction::triggered, this, &SkywardHubCore::addNewWindow); } @@ -109,30 +111,35 @@ void SkywardHubCore::loadConfigurationFromXmlObject(XmlObject configuration) void SkywardHubCore::addNewWindow() { - Module* empty = - ModulesList::getInstance().instantiateModule(ModuleId::EMPTY); + auto empty = ModulesList::getInstance().instantiateModule(ModuleId::EMPTY); return addWindowFromXml(empty->toXmlObject()); } void SkywardHubCore::addWindowFromXml(XmlObject settings) { - Window* newWindow = new Window(settings); + std::unique_ptr<Window> newWindow = std::make_unique<Window>(settings); newWindow->show(); newWindow->activateWindow(); - windowsList.push_back(newWindow); + auto windowIdentifier = newWindow.get(); + windowsList.push_back(std::move(newWindow)); + auto removePredicate = [windowIdentifier](std::unique_ptr<Window> window) + { return window.get() == windowIdentifier; }; - connect(newWindow, &Window::onWindowClosed, this, - [this, newWindow]() + connect(windowIdentifier, &Window::onWindowClosed, this, + [this, removePredicate]() { - windowsList.removeAt(windowsList.indexOf(newWindow)); - delete newWindow; + windowsList.erase( + std::remove_if(windowsList.begin(), windowsList.end(), + removePredicate), + windowsList.end()); }); } void SkywardHubCore::saveConfigurationPrompt() { // Ask configuration data to the user - SaveConfigurationDialog* saveDialog = new SaveConfigurationDialog(); + std::unique_ptr<SaveConfigurationDialog> saveDialog = + std::make_unique<SaveConfigurationDialog>(); saveDialog->setConfigName(SkywardHubStrings::defaultConfigurationFileName); saveDialog->setConfigIconPath( SkywardHubStrings::defaultConfigurationIconPath); @@ -202,3 +209,10 @@ QString SkywardHubCore::saveConfiguration(QString configName, return resultTxt; } + +void SkywardHubCore::addActionsToMenu(QMenu& menu) +{ + std::for_each(hubMenuActionsList.begin(), hubMenuActionsList.end(), + [&menu](std::shared_ptr<QAction> action) + { menu.addAction(action.get()); }); +} \ No newline at end of file diff --git a/src/shared/Core/SkywardHubCore.h b/src/shared/Core/SkywardHubCore.h index d59100e0..b0c99130 100644 --- a/src/shared/Core/SkywardHubCore.h +++ b/src/shared/Core/SkywardHubCore.h @@ -36,7 +36,8 @@ public: void init(); - QList<QAction *> getHubMenuActions(); + QList<std::shared_ptr<QAction>> getHubMenuActions(); + void addActionsToMenu(QMenu &menu); public slots: void addNewWindow(); @@ -53,8 +54,8 @@ private: QString saveConfiguration(QString configName, QString configIconPath, QString description); - QList<Window *> windowsList; - QList<QAction *> hubMenuActionsList; + QList<std::unique_ptr<Window>> windowsList; + QList<std::shared_ptr<QAction>> hubMenuActionsList; public: SkywardHubCore(const SkywardHubCore &) = delete; -- GitLab