From dfc74591cc66e736b76db5ed159b43cb4e24f520 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 5 Apr 2024 08:23:35 +0200 Subject: [PATCH] Material: add workbench manipulator Implement a workbench manipulator to inject Std_Material and Std_Appearance to (context) menu. --- src/Gui/Workbench.cpp | 6 +- src/Mod/Material/Gui/AppMatGui.cpp | 3 + src/Mod/Material/Gui/CMakeLists.txt | 2 + src/Mod/Material/Gui/WorkbenchManipulator.cpp | 77 +++++++++++++++++++ src/Mod/Material/Gui/WorkbenchManipulator.h | 54 +++++++++++++ src/Mod/PartDesign/Gui/Workbench.cpp | 2 - 6 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 src/Mod/Material/Gui/WorkbenchManipulator.cpp create mode 100644 src/Mod/Material/Gui/WorkbenchManipulator.h diff --git a/src/Gui/Workbench.cpp b/src/Gui/Workbench.cpp index 45e61e2ddb..09e54d4e4b 100644 --- a/src/Gui/Workbench.cpp +++ b/src/Gui/Workbench.cpp @@ -601,7 +601,7 @@ void StdWorkbench::setupContextMenu(const char* recipient, MenuItem* item) const << "Std_ViewDockUndockFullscreen"; if (Gui::Selection().countObjectsOfType(App::DocumentObject::getClassTypeId()) > 0) { - *item << "Separator" << "Std_SetMaterial" << "Std_SetAppearance" << "Std_ToggleVisibility" + *item << "Separator" << "Std_ToggleVisibility" << "Std_ToggleSelectability" << "Std_TreeSelection" << "Std_RandomColor" << "Std_ToggleTransparency" << "Separator" << "Std_Delete" << "Std_SendToPythonConsole" << "Std_TransformManip" << "Std_Placement"; @@ -613,7 +613,7 @@ void StdWorkbench::setupContextMenu(const char* recipient, MenuItem* item) const *item << "Std_ToggleFreeze" << "Separator" << "Std_Placement" << "Std_ToggleVisibility" << "Std_ShowSelection" << "Std_HideSelection" << "Std_ToggleSelectability" << "Std_TreeSelectAllInstances" << "Separator" - << "Std_SetMaterial" << "Std_SetAppearance" << "Std_RandomColor" << "Std_ToggleTransparency" << "Separator" + << "Std_RandomColor" << "Std_ToggleTransparency" << "Separator" << "Std_Cut" << "Std_Copy" << "Std_Paste" << "Std_Delete" << "Std_SendToPythonConsole" << "Separator"; } @@ -701,8 +701,6 @@ MenuItem* StdWorkbench::setupMenuBar() const #endif << "Separator" << visu << "Std_ToggleNavigation" - << "Std_SetMaterial" - << "Std_SetAppearance" << "Std_RandomColor" << "Std_ToggleTransparency" << "Separator" diff --git a/src/Mod/Material/Gui/AppMatGui.cpp b/src/Mod/Material/Gui/AppMatGui.cpp index ee972b41f9..0c5f673cf1 100644 --- a/src/Mod/Material/Gui/AppMatGui.cpp +++ b/src/Mod/Material/Gui/AppMatGui.cpp @@ -33,6 +33,7 @@ #include "DlgSettingsMaterial.h" #include "Workbench.h" +#include "WorkbenchManipulator.h" // use a different name to CreateCommand() void CreateMaterialCommands(); @@ -89,6 +90,8 @@ PyMOD_INIT_FUNC(MatGui) Base::Console().Log("Loading GUI of Material module... done\n"); MatGui::Workbench ::init(); + auto manip = std::make_shared(); + Gui::WorkbenchManipulator::installManipulator(manip); // instantiating the commands CreateMaterialCommands(); diff --git a/src/Mod/Material/Gui/CMakeLists.txt b/src/Mod/Material/Gui/CMakeLists.txt index 751cf58356..3e9dee2510 100644 --- a/src/Mod/Material/Gui/CMakeLists.txt +++ b/src/Mod/Material/Gui/CMakeLists.txt @@ -111,6 +111,8 @@ SET(MatGui_SRCS TextEdit.ui Workbench.cpp Workbench.h + WorkbenchManipulator.cpp + WorkbenchManipulator.h ) if(FREECAD_USE_PCH) diff --git a/src/Mod/Material/Gui/WorkbenchManipulator.cpp b/src/Mod/Material/Gui/WorkbenchManipulator.cpp new file mode 100644 index 0000000000..1fcdfc443f --- /dev/null +++ b/src/Mod/Material/Gui/WorkbenchManipulator.cpp @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + +/*************************************************************************** + * Copyright (c) 2024 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" +#include +#include + +using namespace MatGui; + +void WorkbenchManipulator::modifyMenuBar([[maybe_unused]] Gui::MenuItem* menuBar) +{ + addCommands(menuBar, "Std_ToggleNavigation"); +} + +void WorkbenchManipulator::modifyContextMenu(const char* recipient, Gui::MenuItem* menuBar) +{ + if (strcmp(recipient, "View") == 0) { + addCommands(menuBar, "Std_TreeSelection"); + } + else if (strcmp(recipient, "Tree") == 0) { + addCommandsToTree(menuBar); + } +} + +void WorkbenchManipulator::addCommands(Gui::MenuItem* menuBar, const char* reference) +{ + auto par = menuBar->findParentOf(reference); + if (par) { + auto item = par->findItem(reference); + item = par->afterItem(item); + + auto cmd1 = new Gui::MenuItem(); + cmd1->setCommand("Std_SetMaterial"); + par->insertItem(item, cmd1); + auto cmd2 = new Gui::MenuItem(); + cmd2->setCommand("Std_SetAppearance"); + par->insertItem(item, cmd2); + } +} + +void WorkbenchManipulator::addCommandsToTree(Gui::MenuItem* menuBar) +{ + const char* randomColor = "Std_RandomColor"; + auto par = menuBar->findParentOf(randomColor); + if (par) { + auto item = par->findItem(randomColor); + + auto cmd1 = new Gui::MenuItem(); + cmd1->setCommand("Std_SetMaterial"); + par->insertItem(item, cmd1); + auto cmd2 = new Gui::MenuItem(); + cmd2->setCommand("Std_SetAppearance"); + par->insertItem(item, cmd2); + } +} diff --git a/src/Mod/Material/Gui/WorkbenchManipulator.h b/src/Mod/Material/Gui/WorkbenchManipulator.h new file mode 100644 index 0000000000..0ca5e7fd76 --- /dev/null +++ b/src/Mod/Material/Gui/WorkbenchManipulator.h @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + +/*************************************************************************** + * Copyright (c) 2024 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 MATGUI_WORKBENCHMANIPULATOR_H +#define MATGUI_WORKBENCHMANIPULATOR_H + +#include + +namespace MatGui { + +class WorkbenchManipulator: public Gui::WorkbenchManipulator +{ +protected: + /*! + * \brief modifyMenuBar + * Adds the commands Std_SetMaterial and Std_SetAppearance to the View menu + */ + void modifyMenuBar(Gui::MenuItem* menuBar) override; + /*! + * \brief modifyContextMenu + * Adds the commands Std_SetMaterial and Std_SetAppearance to the contex-menu + */ + void modifyContextMenu(const char* recipient, Gui::MenuItem* menuBar) override; + +private: + static void addCommands(Gui::MenuItem* menuBar, const char* reference); + static void addCommandsToTree(Gui::MenuItem* menuBar); +}; + +} // namespace MatGui + + +#endif // MATGUI_WORKBENCHMANIPULATOR_H diff --git a/src/Mod/PartDesign/Gui/Workbench.cpp b/src/Mod/PartDesign/Gui/Workbench.cpp index 3ac5c9422f..408d5b9172 100644 --- a/src/Mod/PartDesign/Gui/Workbench.cpp +++ b/src/Mod/PartDesign/Gui/Workbench.cpp @@ -237,14 +237,12 @@ void Workbench::setupContextMenu(const char* recipient, Gui::MenuItem* item) con if (Gui::Selection().countObjectsOfType(App::DocumentObject::getClassTypeId()) > 0) { *item << "Std_Placement" - << "Std_SetMaterial" << "Std_ToggleVisibility" << "Std_ShowSelection" << "Std_HideSelection" << "Std_ToggleSelectability" << "Std_TreeSelectAllInstances" << "Separator" - << "Std_SetAppearance" << "Std_RandomColor" << "Std_ToggleTransparency" << "Std_Cut"