Skip to content
Snippets Groups Projects
Commit 55f56e44 authored by Raul Radu's avatar Raul Radu
Browse files

[Window] Changed how window handles modules

Now every window will maintain a shared_ptr of a module in order to
prevent it from being deleted
and automatically delete it when the window is destroyed
parent 1488f565
Branches
No related tags found
No related merge requests found
......@@ -23,8 +23,9 @@
Window::Window(XmlObject configuration)
{
errorDisplayer = new ErrorDisplayer(this);
Module *module = ModulesList::getInstance().instantiateModule(
errorDisplayer = std::make_shared<ErrorDisplayer, QWidget *>(this);
std::shared_ptr<Module> module =
ModulesList::getInstance().instantiateModule(
configuration.getObjectName());
if (module)
......@@ -34,22 +35,23 @@ Window::Window(XmlObject configuration)
}
}
void Window::setChild(Module *module)
void Window::setChild(std::shared_ptr<Module> module)
{
setCentralWidget(module);
this->activeModule = module;
setCentralWidget(module.get());
module->lower();
connect(module, &Module::replaceMe, this, &Window::replaceChild);
connect(module, &Module::closeMe, this, &Window::closeChild);
connect(module.get(), &Module::replaceMe, this, &Window::replaceChild);
connect(module.get(), &Module::closeMe, this, &Window::closeChild);
}
void Window::replaceChild(Module *oldModule, Module *newModule)
void Window::replaceChild(std::shared_ptr<Module> _oldModule,
std::shared_ptr<Module> newModule)
{
// Set the new module
std::shared_ptr<Module> oldModule = this->activeModule;
setChild(newModule);
// Delete the old child
delete oldModule;
oldModule.reset();
}
void Window::closeChild(Module *module) { close(); }
......
......@@ -24,6 +24,7 @@
#include <QCloseEvent>
#include <QMainWindow>
#include <QMenu>
#include <memory>
#include "Components/ErrorDisplayer/ErrorDisplayer.h"
......@@ -36,7 +37,7 @@ public:
XmlObject toXmlObject();
ErrorDisplayer *errorDisplayer;
std::shared_ptr<ErrorDisplayer> errorDisplayer;
void closeEvent(QCloseEvent *event) override;
......@@ -44,9 +45,12 @@ signals:
void onWindowClosed();
private:
void setChild(Module *module);
std::shared_ptr<Module> activeModule;
void setChild(std::shared_ptr<Module> module);
private slots:
void replaceChild(Module *oldModule, Module *newModule);
void replaceChild(std::shared_ptr<Module> oldModule,
std::shared_ptr<Module> newModule);
void closeChild(Module *module);
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment