diff --git a/src/app/app_context.cpp b/src/app/app_context.cpp index 36a60afa..8cecc24e 100644 --- a/src/app/app_context.cpp +++ b/src/app/app_context.cpp @@ -26,7 +26,7 @@ AppContext::AppContext(MainWindow* wnd) QObject::connect( m_wnd->widgetPageDocuments(), &WidgetMainControl::currentDocumentIndexChanged, this, &AppContext::onCurrentDocumentIndexChanged - ); + ); } GuiApplication* AppContext::guiApp() const @@ -41,7 +41,8 @@ TaskManager* AppContext::taskMgr() const QWidget* AppContext::pageDocuments_widgetLeftSideBar() const { - return m_wnd->widgetPageDocuments()->widgetLeftSideBar(); + const WidgetMainControl* pageDocs = m_wnd->widgetPageDocuments(); + return pageDocs ? pageDocs->widgetLeftSideBar() : nullptr; } QWidget* AppContext::widgetMain() const diff --git a/src/app/commands_api.cpp b/src/app/commands_api.cpp index 047cf4df..d7c1a500 100644 --- a/src/app/commands_api.cpp +++ b/src/app/commands_api.cpp @@ -76,6 +76,15 @@ QAction* CommandContainer::findCommandAction(std::string_view name) const return cmd ? cmd->action() : nullptr; } +void CommandContainer::clear() +{ + for (auto [name, cmd] : m_mapCommand) { + delete cmd; + } + + m_mapCommand.clear(); +} + void CommandContainer::addCommand_impl(std::string_view name, Command* cmd) { assert(m_appContext != nullptr); diff --git a/src/app/commands_api.h b/src/app/commands_api.h index a7bb956b..1b70fdcd 100644 --- a/src/app/commands_api.h +++ b/src/app/commands_api.h @@ -117,6 +117,8 @@ class CommandContainer { // The command name is implicit and found by assuming the presence of CmdType::Name class member template CmdType* addNamedCommand(Args... p); + void clear(); + private: void addCommand_impl(std::string_view name, Command* cmd); diff --git a/src/app/commands_window.h b/src/app/commands_window.h index bf936f57..1488fdef 100644 --- a/src/app/commands_window.h +++ b/src/app/commands_window.h @@ -26,6 +26,7 @@ class CommandMainWidgetToggleFullscreen : public Command { class CommandLeftSidebarWidgetToggle : public Command { public: CommandLeftSidebarWidgetToggle(IAppContext* context); + void execute() override; bool getEnabledStatus() const override; diff --git a/src/app/mainwindow.cpp b/src/app/mainwindow.cpp index de6233e5..2d8e10f9 100644 --- a/src/app/mainwindow.cpp +++ b/src/app/mainwindow.cpp @@ -66,6 +66,8 @@ MainWindow::MainWindow(GuiApplication* guiApp, QWidget* parent) MainWindow::~MainWindow() { + // Force deletion of Command objects as some of them are event filters of MainWindow widgets + m_cmdContainer.clear(); delete m_ui; } @@ -284,18 +286,20 @@ void MainWindow::updateCurrentPage() m_appContext->setCurrentPage(newPage); } +IWidgetMainPage* MainWindow::widgetMainPage(IAppContext::Page page) const +{ + auto it = m_mapWidgetPage.find(page); + return it != m_mapWidgetPage.cend() ? it->second : nullptr; +} + WidgetMainHome* MainWindow::widgetPageHome() const { - auto it = m_mapWidgetPage.find(IAppContext::Page::Home); - assert(it != m_mapWidgetPage.cend()); - return dynamic_cast(it->second); + return dynamic_cast(this->widgetMainPage(IAppContext::Page::Home)); } WidgetMainControl* MainWindow::widgetPageDocuments() const { - auto it = m_mapWidgetPage.find(IAppContext::Page::Documents); - assert(it != m_mapWidgetPage.cend()); - return dynamic_cast(it->second); + return dynamic_cast(this->widgetMainPage(IAppContext::Page::Documents)); } } // namespace Mayo diff --git a/src/app/mainwindow.h b/src/app/mainwindow.h index 2b8f4b91..c82dd1db 100644 --- a/src/app/mainwindow.h +++ b/src/app/mainwindow.h @@ -54,6 +54,7 @@ class MainWindow : public QMainWindow { void updateControlsActivation(); void updateCurrentPage(); + IWidgetMainPage* widgetMainPage(IAppContext::Page page) const; WidgetMainHome* widgetPageHome() const; WidgetMainControl* widgetPageDocuments() const;