Debian Codegraph @

src/Gui/CommandFeat.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2002 Jürgen Riegel <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <App/DocumentObjectGroup.h>
25 #include <App/GroupExtension.h>
26 #include <App/Part.h>
27 #include "Application.h"
28 #include "Action.h"
29 #include "cet_lut.hpp"
30 #include "CommandT.h"
31 #include "DockWindowManager.h"
32 #include "Document.h"
33 #include "PythonConsole.h"
34 #include "Selection.h"
35 #include "SelectionObject.h"
36 #include "ViewProvider.h"
37 #include "ViewProviderDocumentObject.h"
38 #include "ViewProviderLink.h"
39
40 using namespace Gui;
41
42
43 //===========================================================================
44 // Std_Recompute
45 //===========================================================================
46
47 DEF_STD_CMD(StdCmdFeatRecompute)
48
49 StdCmdFeatRecompute::StdCmdFeatRecompute()
50 : Command("Std_Recompute")
51 {
52 // setting the
53 sGroup = "File";
54 sMenuText = QT_TR_NOOP("&Recompute");
55 sToolTipText = QT_TR_NOOP("Recomputes a feature or document");
56 sWhatsThis = "Std_Recompute";
57 sStatusTip = sToolTipText;
58 sPixmap = "view-refresh";
59 sAccel = "Ctrl+R";
60 }
61
62 void StdCmdFeatRecompute::activated(int iMsg)
63 {
64 Q_UNUSED(iMsg);
65 }
66
67 //===========================================================================
68 // Std_RandomColor
69 //===========================================================================
70
71 DEF_STD_CMD_A(StdCmdRandomColor)
72
73 StdCmdRandomColor::StdCmdRandomColor()
74 : Command("Std_RandomColor")
75 {
76 sGroup = "File";
77 sMenuText = QT_TR_NOOP("Random &Color");
78 sToolTipText = QT_TR_NOOP("Assigns random diffuse colors for the selected objects");
79 sWhatsThis = "Std_RandomColor";
80 sStatusTip = sToolTipText;
81 sPixmap = "Std_RandomColor";
82 }
83
84 void StdCmdRandomColor::activated(int iMsg)
85 {
86 Q_UNUSED(iMsg);
87
88 auto setRandomColor = [](ViewProvider* view) {
89 // NOLINTBEGIN
90 int colIndex = rand() % (CET::R1.size() / 3) * 3;
91 float fRed = CET::R1[colIndex] / 255.0F;
92 float fGrn = CET::R1[colIndex + 1] / 255.0F;
93 float fBlu = CET::R1[colIndex + 2] / 255.0F;
94 // NOLINTEND
95 auto objColor = Base::Color(fRed, fGrn, fBlu);
96
97 auto vpLink = freecad_cast<ViewProviderLink*>(view);
98 if (vpLink) {
99 if (!vpLink->OverrideMaterial.getValue()) {
100 vpLink->OverrideMaterial.setValue(true);
101 }
102 vpLink->ShapeMaterial.setDiffuseColor(objColor);
103 }
104 else if (view) {
105 // clang-format off
106 // get the view provider of the selected object and set the shape color
107 if (auto prop = dynamic_cast<App::PropertyMaterialList*>(view->getPropertyByName("ShapeAppearance"))) {
108 prop->setDiffuseColor(objColor);
109 }
110 else if (auto prop = dynamic_cast<App::PropertyMaterial*>(view->getPropertyByName("ShapeAppearance"))) {
111 prop->setDiffuseColor(objColor);
112 }
113 // clang-format on
114 }
115 };
116
117 auto allowToChangeColor = [](const App::DocumentObject* obj) {
118 return (obj->isDerivedFrom<App::Part>() || obj->isDerivedFrom<App::DocumentObjectGroup>());
119 };
120
121 // get the complete selection
122 std::vector<SelectionSingleton::SelObj> sel = Selection().getCompleteSelection();
123
124 Command::openCommand(QT_TRANSLATE_NOOP("Command", "Set Random Color"));
125 for (const auto& it : sel) {
126 ViewProvider* view = Application::Instance->getViewProvider(it.pObject);
127 setRandomColor(view);
128
129 if (auto grp = it.pObject->getExtension<App::GroupExtension>()) {
130 if (allowToChangeColor(it.pObject)) {
131 std::vector<App::DocumentObject*> objs = grp->getObjects();
132 for (auto obj : objs) {
133 ViewProvider* view = Application::Instance->getViewProvider(obj);
134 setRandomColor(view);
135 }
136 }
137 }
138 }
139
140 Command::commitCommand();
141 }
142
143 bool StdCmdRandomColor::isActive()
144 {
145 return (Gui::Selection().size() != 0);
146 }
147
148 //===========================================================================
149 // Std_ToggleFreeze
150 //===========================================================================
151 DEF_STD_CMD_A(StdCmdToggleFreeze)
152
153 StdCmdToggleFreeze::StdCmdToggleFreeze()
154 : Command("Std_ToggleFreeze")
155 {
156 sGroup = "File";
157 sMenuText = QT_TR_NOOP("Toggle Freeze");
158 static std::string toolTip
159 = std::string("<p>")
160 + QT_TR_NOOP("Toggles freeze state of the selected objects. A frozen object is not recomputed when its parents change.")
161 + "</p>";
162 sToolTipText = toolTip.c_str();
163 sStatusTip = sToolTipText;
164 sWhatsThis = "Std_ToggleFreeze";
165 sPixmap = "Std_ToggleFreeze";
166 sAccel = "";
167 eType = AlterDoc;
168 }
169
170 void StdCmdToggleFreeze::activated(int iMsg)
171 {
172 Q_UNUSED(iMsg);
173
174 std::vector<Gui::SelectionSingleton::SelObj> sels = Gui::Selection().getCompleteSelection();
175
176 Command::openCommand(QT_TRANSLATE_NOOP("Command", "Toggle freeze"));
177 for (Gui::SelectionSingleton::SelObj& sel : sels) {
178 App::DocumentObject* obj = sel.pObject;
179 if (!obj) {
180 continue;
181 }
182
183 if (obj->isFreezed()) {
184 obj->unfreeze();
185 }
186 else {
187 obj->freeze();
188 }
189 }
190 Command::commitCommand();
191 }
192
193 bool StdCmdToggleFreeze::isActive()
194 {
195 return (Gui::Selection().size() != 0);
196 }
197
198
199 //===========================================================================
200 // Std_SendToPythonConsole
201 //===========================================================================
202
203 DEF_STD_CMD_A(StdCmdSendToPythonConsole)
204
205 StdCmdSendToPythonConsole::StdCmdSendToPythonConsole()
206 : Command("Std_SendToPythonConsole")
207 {
208 // setting the
209 sGroup = "Edit";
210 sMenuText = QT_TR_NOOP("&Send to Python Console");
211 sToolTipText = QT_TR_NOOP("Sends the selected object to the Python console");
212 sWhatsThis = "Std_SendToPythonConsole";
213 sStatusTip = sToolTipText;
214 sPixmap = "applications-python";
215 sAccel = "Ctrl+Shift+P";
216 }
217
218 bool StdCmdSendToPythonConsole::isActive()
219 {
220 // active only if either 1 object is selected or multiple subobjects from the same object
221 return Gui::Selection().getSelectionEx().size() == 1;
222 }
223
224 void StdCmdSendToPythonConsole::activated(int iMsg)
225 {
226 Q_UNUSED(iMsg);
227 const std::vector<Gui::SelectionObject>& sels = Gui::Selection().getSelectionEx(
228 "*",
229 App::DocumentObject::getClassTypeId(),
230 ResolveMode::OldStyleElement,
231 false
232 );
233 if (sels.empty()) {
234 return;
235 }
236 const App::DocumentObject* obj = sels[0].getObject();
237 if (!obj) {
238 return;
239 }
240 QString docname = QString::fromLatin1(obj->getDocument()->getName());
241 QString objname = QString::fromLatin1(obj->getNameInDocument());
242 try {
243 // clear variables from previous run, if any
244 QString cmd = QLatin1String(
245 "try:\n del(doc,lnk,obj,shp,sub,subs)\nexcept Exception:\n pass\n"
246 );
247 Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
248 cmd = QStringLiteral("doc = App.getDocument(\"%1\")").arg(docname);
249 Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
250 // support links
251 if (obj->isDerivedFrom<App::Link>()) {
252 cmd = QStringLiteral("lnk = doc.getObject(\"%1\")").arg(objname);
253 Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
254 cmd = QStringLiteral("obj = lnk.getLinkedObject()");
255 Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
256 const auto link = static_cast<const App::Link*>(obj);
257 obj = link->getLinkedObject();
258 }
259 else {
260 cmd = QStringLiteral("obj = doc.getObject(\"%1\")").arg(objname);
261 Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
262 }
263 if (obj->isDerivedFrom<App::GeoFeature>()) {
264 const auto geoObj = static_cast<const App::GeoFeature*>(obj);
265 const App::PropertyGeometry* geo = geoObj->getPropertyOfGeometry();
266 if (geo) {
267 cmd = QStringLiteral("shp = obj.")
268 + QLatin1String(geo->getName()); //"Shape", "Mesh", "Points", etc.
269 Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
270 if (sels[0].hasSubNames()) {
271 std::vector<std::string> subnames = sels[0].getSubNames();
272 QString subname = QString::fromLatin1(subnames[0].c_str());
273 cmd = QStringLiteral("sub = obj.getSubObject(\"%1\")").arg(subname);
274 Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
275 if (subnames.size() > 1) {
276 std::ostringstream strm;
277 strm << "subs = [";
278 for (const auto& subname : subnames) {
279 strm << "obj.getSubObject(\"" << subname << "\"),";
280 }
281 strm << "]";
282 Gui::Command::runCommand(Gui::Command::Gui, strm.str().c_str());
283 }
284 }
285 }
286 }
287 // show the python console if it's not already visible, and set the keyboard focus to it
288 QWidget* pc = DockWindowManager::instance()->getDockWindow("Python console");
289 auto pcPython = qobject_cast<PythonConsole*>(pc);
290 if (pcPython) {
291 DockWindowManager::instance()->activate(pcPython);
292 pcPython->setFocus();
293 }
294 }
295 catch (const Base::Exception& e) {
296 e.reportException();
297 }
298 }
299
300 //===========================================================================
301 // Std_ToggleSkipRecompute
302 //===========================================================================
303
304 DEF_STD_CMD_AC(StdCmdToggleSkipRecompute)
305
306 StdCmdToggleSkipRecompute::StdCmdToggleSkipRecompute()
307 : Command("Std_ToggleSkipRecompute")
308 {
309 sGroup = "File";
310 sMenuText = QT_TR_NOOP("Skip Recomputes");
311
312 static std::string toolTip = QT_TR_NOOP("Enables or disables the recomputations of the document");
313
314 sToolTipText = toolTip.c_str();
315 sStatusTip = sToolTipText;
316 sWhatsThis = "Std_ToggleSkipRecompute";
317 eType = AlterDoc;
318 }
319
320 Gui::Action* StdCmdToggleSkipRecompute::createAction()
321 {
322 Action* pcAction = Command::createAction();
323 pcAction->setCheckable(true);
324 pcAction->setIcon(QIcon());
325 _pcAction = pcAction;
326 isActive();
327 return pcAction;
328 }
329
330 void StdCmdToggleSkipRecompute::activated(int iMsg)
331 {
332 const auto doc = this->getDocument();
333 if (doc == nullptr) {
334 return;
335 }
336
337 Command::openCommand(QT_TRANSLATE_NOOP("Command", "Skip recomputes"));
338 doc->setStatus(App::Document::SkipRecompute, (bool)iMsg);
339 if (_pcAction) {
340 _pcAction->setChecked((bool)iMsg);
341 }
342 Command::commitCommand();
343 }
344
345 bool StdCmdToggleSkipRecompute::isActive()
346 {
347 const auto doc = this->getDocument();
348 if (doc == nullptr) {
349 return false;
350 }
351
352 const bool skipRecomputeStatus = doc->testStatus(App::Document::SkipRecompute);
353 if (_pcAction && _pcAction->isChecked() != skipRecomputeStatus) {
354 _pcAction->setChecked(skipRecomputeStatus);
355 }
356 return true;
357 }
358
359 namespace Gui
360 {
361
362 void CreateFeatCommands()
363 {
364 CommandManager& rcCmdMgr = Application::Instance->commandManager();
365
366 rcCmdMgr.addCommand(new StdCmdFeatRecompute());
367 rcCmdMgr.addCommand(new StdCmdToggleFreeze());
368 rcCmdMgr.addCommand(new StdCmdRandomColor());
369 rcCmdMgr.addCommand(new StdCmdSendToPythonConsole());
370 rcCmdMgr.addCommand(new StdCmdToggleSkipRecompute());
371 }
372
373 } // namespace Gui