From 690b8925368c337367e23cbdda51eea3a4df3c5f Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 3 Oct 2023 17:24:40 +0200 Subject: [PATCH] Gui: add class WorkbenchManipulator --- src/Gui/CMakeLists.txt | 2 + src/Gui/WorkbenchManipulator.cpp | 88 ++++++++++++++++++++++ src/Gui/WorkbenchManipulator.h | 125 +++++++++++++++++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 src/Gui/WorkbenchManipulator.cpp create mode 100644 src/Gui/WorkbenchManipulator.h diff --git a/src/Gui/CMakeLists.txt b/src/Gui/CMakeLists.txt index 248ea7ac4e..811bb10ab9 100644 --- a/src/Gui/CMakeLists.txt +++ b/src/Gui/CMakeLists.txt @@ -1110,6 +1110,7 @@ SET(Workbench_CPP_SRCS Workbench.cpp WorkbenchFactory.cpp WorkbenchManager.cpp + WorkbenchManipulator.cpp WorkbenchPyImp.cpp ) SET(Workbench_SRCS @@ -1121,6 +1122,7 @@ SET(Workbench_SRCS Workbench.h WorkbenchFactory.h WorkbenchManager.h + WorkbenchManipulator.h ) SOURCE_GROUP("Workbench" FILES ${Workbench_SRCS}) diff --git a/src/Gui/WorkbenchManipulator.cpp b/src/Gui/WorkbenchManipulator.cpp new file mode 100644 index 0000000000..fdddbb878d --- /dev/null +++ b/src/Gui/WorkbenchManipulator.cpp @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + +/*************************************************************************** + * Copyright (c) 2023 Werner Mayer * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + **************************************************************************/ + + +#include "PreCompiled.h" +#include "WorkbenchManipulator.h" + +using namespace Gui; + +std::set WorkbenchManipulator::manipulators; // NOLINT + +void WorkbenchManipulator::installManipulator(const WorkbenchManipulator::Ptr& ptr) +{ + manipulators.insert(ptr); +} + +void WorkbenchManipulator::removeManipulator(const WorkbenchManipulator::Ptr& ptr) +{ + auto it = manipulators.find(ptr); + if (it != manipulators.end()) { + manipulators.erase(it); + } +} + +void WorkbenchManipulator::changeMenuBar(MenuItem* menuBar) +{ + for (auto& it : manipulators) { + it->modifyMenuBar(menuBar); + } +} + +void WorkbenchManipulator::changeContextMenu(const char* recipient, MenuItem* menuBar) +{ + for (auto& it : manipulators) { + it->modifyContextMenu(recipient, menuBar); + } +} + +void WorkbenchManipulator::changeToolBars(ToolBarItem* toolBar) +{ + for (auto& it : manipulators) { + it->modifyToolBars(toolBar); + } +} + +void WorkbenchManipulator::changeDockWindows(DockWindowItems* dockWindow) +{ + for (auto& it : manipulators) { + it->modifyDockWindows(dockWindow); + } +} + +void WorkbenchManipulator::modifyMenuBar([[maybe_unused]] MenuItem* menuBar) +{ +} + +void WorkbenchManipulator::modifyContextMenu([[maybe_unused]] const char* recipient, + [[maybe_unused]] MenuItem* menuBar) +{ +} + +void WorkbenchManipulator::modifyToolBars([[maybe_unused]] ToolBarItem* toolBar) +{ +} + +void WorkbenchManipulator::modifyDockWindows([[maybe_unused]] DockWindowItems* dockWindow) +{ +} diff --git a/src/Gui/WorkbenchManipulator.h b/src/Gui/WorkbenchManipulator.h new file mode 100644 index 0000000000..b4901fba98 --- /dev/null +++ b/src/Gui/WorkbenchManipulator.h @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + +/*************************************************************************** + * Copyright (c) 2023 Werner Mayer * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + **************************************************************************/ + + +#ifndef GUI_WORKBENCHMANIPULATOR_H +#define GUI_WORKBENCHMANIPULATOR_H + +#include +#include +#include +#include +#include + +namespace Gui { + +class DockWindowItems; +class MenuItem; +class ToolBarItem; + +/** + * The WorkbenchManipulator is a class that allows to modify the workbench + * by adding or removing commands. + * WorkbenchManipulator provides methods to manipulate the MenuItem, ToolBarItem or + * DockWindowItems structure before setting up the workbench. + * @author Werner Mayer + */ +class GuiExport WorkbenchManipulator +{ +public: + using Ptr = std::shared_ptr; + /*! + * \brief installManipulator + * Installs a new instance of WorkbenchManipulator + */ + static void installManipulator(const WorkbenchManipulator::Ptr&); + /*! + * \brief removeManipulator + * Removes an installed instance of WorkbenchManipulator + */ + static void removeManipulator(const WorkbenchManipulator::Ptr&); + /*! + * \brief changeMenuBar + * Calls \ref modifyMenuBar for every installed WorkbenchManipulator + */ + static void changeMenuBar(MenuItem* menuBar); + /*! + * \brief changeContextMenu + * Calls \ref modifyContextMenu for every installed WorkbenchManipulator + */ + static void changeContextMenu(const char* recipient, MenuItem* menuBar); + /*! + * \brief changeToolBars + * Calls \ref modifyToolBars for every installed WorkbenchManipulator + */ + static void changeToolBars(ToolBarItem* toolBar); + /*! + * \brief changeDockWindows + * Calls \ref modifyDockWindows for every installed WorkbenchManipulator + */ + static void changeDockWindows(DockWindowItems* dockWindow); + + WorkbenchManipulator() = default; + virtual ~WorkbenchManipulator() = default; + +protected: + /*! + * \brief modifyMenuBar + * Method to manipulate the menu structure of a workbench. + * The default implementation doesn't change anything. + */ + virtual void modifyMenuBar([[maybe_unused]] MenuItem* menuBar); + /*! + * \brief modifyContextMenu + * Method to manipulate the contextmenu structure of a workbench. + * The default implementation doesn't change anything. + */ + virtual void modifyContextMenu([[maybe_unused]] const char* recipient, + [[maybe_unused]] MenuItem* menuBar); + /*! + * \brief modifyToolBars + * Method to manipulate the toolbar structure of a workbench + * The default implementation doesn't change anything. + */ + virtual void modifyToolBars([[maybe_unused]] ToolBarItem* toolBar); + /*! + * \brief modifyDockWindows + * Method to manipulate the dock window structure of a workbench + * The default implementation doesn't change anything. + */ + virtual void modifyDockWindows([[maybe_unused]] DockWindowItems* dockWindow); + +public: + WorkbenchManipulator(const WorkbenchManipulator&) = delete; + WorkbenchManipulator(WorkbenchManipulator&&) = delete; + WorkbenchManipulator& operator = (const WorkbenchManipulator&) = delete; + WorkbenchManipulator& operator = (WorkbenchManipulator&&) = delete; + +private: + static std::set manipulators; // NOLINT +}; + +} // namespace Gui + + +#endif // GUI_WORKBENCHMANIPULATOR_H