From abe9e23bcd7e6a6ec701c1f648263dc95528a62d Mon Sep 17 00:00:00 2001
From: Raul Radu <raul.radu@mail.polimi.it>
Date: Wed, 24 Jan 2024 22:14:22 +0100
Subject: [PATCH] [Windows] Final Window class design w/ smart ptrs

- Now windows will maintain a shared_ptr of the contained module
- replace child accepts shared_ptr of new modules
- error displayer is a shared pointer
 as it is used out of the window class
- Added window deleter to update the message broker by
   unsubscribing from all filters
   since the module is being deleted
---
 src/shared/Core/Window/Window.cpp | 22 +++++++++++++---------
 src/shared/Core/Window/Window.h   |  4 ++--
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/shared/Core/Window/Window.cpp b/src/shared/Core/Window/Window.cpp
index 87f0f4c7..2d5ebbec 100644
--- a/src/shared/Core/Window/Window.cpp
+++ b/src/shared/Core/Window/Window.cpp
@@ -18,6 +18,7 @@
 
 #include "Window.h"
 
+#include <Core/MessageBroker/MessageBroker.h>
 #include <Modules/Empty/EmptyModule.h>
 #include <Modules/ModulesList.h>
 
@@ -35,23 +36,26 @@ Window::Window(XmlObject configuration)
     }
 }
 
+Window::~Window()
+{
+    MessageBroker::getInstance().unsubscribeFromAll(activeModule);
+}
+
 void Window::setChild(std::shared_ptr<Module> module)
 {
+    Module *rawModule  = module.get();
     this->activeModule = module;
-    setCentralWidget(module.get());
+    setCentralWidget(rawModule);
     module->lower();
-    connect(module.get(), &Module::replaceMe, this, &Window::replaceChild);
-    connect(module.get(), &Module::closeMe, this, &Window::closeChild);
+    connect(rawModule, &Module::replaceMe, this, &Window::replaceChild);
+    connect(rawModule, &Module::closeMe, this, &Window::closeChild);
 }
 
-void Window::replaceChild(std::shared_ptr<Module> _oldModule,
-                          std::shared_ptr<Module> newModule)
+void Window::replaceChild(Module *_oldModule, std::shared_ptr<Module> newModule)
 {
     std::shared_ptr<Module> oldModule = this->activeModule;
-
     setChild(newModule);
-
-    oldModule.reset();
+    MessageBroker::getInstance().unsubscribeFromAll(oldModule);
 }
 
 void Window::closeChild(Module *module) { close(); }
@@ -65,4 +69,4 @@ void Window::closeEvent(QCloseEvent *event)
 {
     emit onWindowClosed();
     event->accept();
-}
+}
\ No newline at end of file
diff --git a/src/shared/Core/Window/Window.h b/src/shared/Core/Window/Window.h
index 66b35021..30bd3b01 100644
--- a/src/shared/Core/Window/Window.h
+++ b/src/shared/Core/Window/Window.h
@@ -34,6 +34,7 @@ class Window : public QMainWindow
 
 public:
     explicit Window(XmlObject configuration);
+    ~Window();
 
     XmlObject toXmlObject();
 
@@ -50,7 +51,6 @@ private:
     void setChild(std::shared_ptr<Module> module);
 
 private slots:
-    void replaceChild(std::shared_ptr<Module> oldModule,
-                      std::shared_ptr<Module> newModule);
+    void replaceChild(Module *oldModule, std::shared_ptr<Module> newModule);
     void closeChild(Module *module);
 };
-- 
GitLab