Material: add workbench manipulator

Implement a workbench manipulator to inject Std_Material and Std_Appearance to (context) menu.
This commit is contained in:
wmayer
2024-04-05 08:23:35 +02:00
committed by wwmayer
parent 384902c26c
commit dfc74591cc
6 changed files with 138 additions and 6 deletions

View File

@@ -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"

View File

@@ -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<MatGui::WorkbenchManipulator>();
Gui::WorkbenchManipulator::installManipulator(manip);
// instantiating the commands
CreateMaterialCommands();

View File

@@ -111,6 +111,8 @@ SET(MatGui_SRCS
TextEdit.ui
Workbench.cpp
Workbench.h
WorkbenchManipulator.cpp
WorkbenchManipulator.h
)
if(FREECAD_USE_PCH)

View File

@@ -0,0 +1,77 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2024 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include "PreCompiled.h"
#include "WorkbenchManipulator.h"
#include <Gui/MenuManager.h>
#include <Gui/ToolBarManager.h>
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);
}
}

View File

@@ -0,0 +1,54 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2024 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#ifndef MATGUI_WORKBENCHMANIPULATOR_H
#define MATGUI_WORKBENCHMANIPULATOR_H
#include <Gui/WorkbenchManipulator.h>
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

View File

@@ -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"