Debian Codegraph @

src/Gui/ViewProviderGroupExtension.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2016 Stefan Tröger <[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 <QMessageBox>
25
26
27 #include <App/Document.h>
28 #include <App/DocumentObject.h>
29 #include <App/DocumentObjectGroup.h>
30 #include <App/GroupExtension.h>
31 #include <Base/Console.h>
32 #include <Base/Tools.h>
33
34 #include "ViewProviderGroupExtension.h"
35 #include "ViewProviderDocumentObject.h"
36 #include "Application.h"
37 #include "Command.h"
38 #include "Document.h"
39 #include "MainWindow.h"
40
41
42 using namespace Gui;
43
44 namespace
45 {
46 // helper function to recursively delete group contents while respecting view provider onDelete methods
47 void deleteGroupContentsRecursively(App::GroupExtension* group)
48 {
49 if (!group) {
50 return;
51 }
52
53 std::vector<App::DocumentObject*> children = group->Group.getValues();
54
55 for (App::DocumentObject* child : children) {
56 if (!child || !child->isAttachedToDocument() || child->isRemoving()) {
57 continue;
58 }
59
60 // if the child is a group, recursively delete its contents first
61 if (child->hasExtension(App::GroupExtension::getExtensionClassTypeId())) {
62 auto* childGroup = child->getExtensionByType<App::GroupExtension>();
63 deleteGroupContentsRecursively(childGroup);
64 }
65
66 Gui::Document* guiDoc = Application::Instance->getDocument(child->getDocument());
67 if (guiDoc) {
68 ViewProvider* vp = guiDoc->getViewProvider(child);
69 if (vp) {
70 // give group_recursive_deletion marker to the VP to mark that the deletion
71 // is supposed to delete all of its children
72 std::vector<std::string> groupDeletionMarker = {"group_recursive_deletion"};
73 bool shouldDelete = vp->onDelete(groupDeletionMarker);
74
75 if (!shouldDelete) {
76 return;
77 }
78 }
79 }
80
81 // if the object still exists and wasn't deleted by its view provider, delete it directly
82 if (child->isAttachedToDocument() && !child->isRemoving()) {
83 child->getDocument()->removeObject(child->getNameInDocument());
84 }
85 }
86 }
87 } // namespace
88
89 EXTENSION_PROPERTY_SOURCE(Gui::ViewProviderGroupExtension, Gui::ViewProviderExtension)
90
91 ViewProviderGroupExtension::ViewProviderGroupExtension()
92 {
93 initExtensionType(ViewProviderGroupExtension::getExtensionClassTypeId());
94 }
95
96 ViewProviderGroupExtension::~ViewProviderGroupExtension() = default;
97
98 bool ViewProviderGroupExtension::extensionCanDragObjects() const
99 {
100 return true;
101 }
102
103 bool ViewProviderGroupExtension::extensionCanDragObject(App::DocumentObject*) const
104 {
105
106 // we can drag anything out
107 return true;
108 }
109
110 void ViewProviderGroupExtension::extensionDragObject(App::DocumentObject* obj)
111 {
112
113 Gui::Command::doCommand(
114 Gui::Command::Doc,
115 "App.getDocument(\"%s\").getObject(\"%s\").removeObject("
116 "App.getDocument(\"%s\").getObject(\"%s\"))",
117 getExtendedViewProvider()->getObject()->getDocument()->getName(),
118 getExtendedViewProvider()->getObject()->getNameInDocument(),
119 obj->getDocument()->getName(),
120 obj->getNameInDocument()
121 );
122 }
123
124 bool ViewProviderGroupExtension::extensionCanDropObjects() const
125 {
126 return true;
127 }
128
129 bool ViewProviderGroupExtension::extensionCanDropObject(App::DocumentObject* obj) const
130 {
131 #ifdef FC_DEBUG
132 Base::Console().log("Check ViewProviderGroupExtension\n");
133 #endif
134
135 auto extobj = getExtendedViewProvider()->getObject();
136 auto group = extobj->getExtensionByType<App::GroupExtension>();
137
138 // we cannot drop thing of this group into it again if it does not allow reorder
139 if (group->hasObject(obj) && !getExtendedViewProvider()->acceptReorderingObjects()) {
140 return false;
141 }
142
143 // Check for possible cyclic dependencies if we allowed to drop the object
144 const auto& list = obj->getOutList();
145 if (std::ranges::find(list, extobj) != list.end()) {
146 Base::Console().warning("Do not add cyclic dependency to %s\n", extobj->Label.getValue());
147 return false;
148 }
149
150 return group->allowObject(obj);
151 }
152
153 void ViewProviderGroupExtension::extensionDropObject(App::DocumentObject* obj)
154 {
155
156 auto grp = getExtendedViewProvider()->getObject();
157 App::Document* doc = grp->getDocument();
158
159 // build Python command for execution
160 QString cmd;
161 cmd = QStringLiteral(
162 "App.getDocument(\"%1\").getObject(\"%2\").addObject("
163 "App.getDocument(\"%1\").getObject(\"%3\"))"
164 )
165 .arg(
166 QString::fromUtf8(doc->getName()),
167 QString::fromUtf8(grp->getNameInDocument()),
168 QString::fromUtf8(obj->getNameInDocument())
169 );
170
171 Gui::Command::doCommand(Gui::Command::App, cmd.toUtf8());
172 }
173
174 std::vector<App::DocumentObject*> ViewProviderGroupExtension::extensionClaimChildren() const
175 {
176
177 auto* obj = getExtendedViewProvider()->getObject();
178 if (!obj) {
179 return {};
180 }
181
182 auto* group = obj->getExtensionByType<App::GroupExtension>();
183 return group->Group.getValues();
184 }
185
186 void ViewProviderGroupExtension::extensionShow()
187 {
188
189 // avoid possible infinite recursion
190 if (guard) {
191 return;
192 }
193 Base::StateLocker lock(guard);
194
195 // when reading the Visibility property from file then do not hide the
196 // objects of this group because they have stored their visibility status, too
197 //
198 // Property::User1 is used by ViewProviderDocumentObject to mark for
199 // temporary visibility changes. Do not propagate the change to children.
200 if (!getExtendedViewProvider()->isRestoring()
201 && !getExtendedViewProvider()->Visibility.testStatus(App::Property::User1)) {
202 auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
203 for (auto obj : group->Group.getValues()) {
204 if (obj && !obj->Visibility.getValue()) {
205 obj->Visibility.setValue(true);
206 }
207 }
208 }
209
210 ViewProviderExtension::extensionShow();
211 }
212
213 void ViewProviderGroupExtension::extensionHide()
214 {
215
216 // avoid possible infinite recursion
217 if (guard) {
218 return;
219 }
220 Base::StateLocker lock(guard);
221
222 // when reading the Visibility property from file then do not hide the
223 // objects of this group because they have stored their visibility status, too
224 //
225 // Property::User1 is used by ViewProviderDocumentObject to mark for
226 // temporary visibility changes. Do not propagate the change to children.
227 if (!getExtendedViewProvider()->isRestoring()
228 && !getExtendedViewProvider()->Visibility.testStatus(App::Property::User1)) {
229 auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
230 for (auto obj : group->Group.getValues()) {
231 if (obj && obj->Visibility.getValue()) {
232 obj->Visibility.setValue(false);
233 }
234 }
235 }
236 ViewProviderExtension::extensionHide();
237 }
238
239 bool ViewProviderGroupExtension::extensionOnDelete(const std::vector<std::string>&)
240 {
241
242 auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
243
244 std::vector<App::DocumentObject*> directChildren = group->Group.getValues();
245
246 // just delete without messagebox if group is empty
247 if (directChildren.empty()) {
248 return true;
249 }
250
251 const auto* docGroup = freecad_cast<App::DocumentObjectGroup*>(
252 getExtendedViewProvider()->getObject()
253 );
254 auto allDescendants = docGroup ? docGroup->getAllChildren() : directChildren;
255
256 QString message;
257 if (allDescendants.size() == directChildren.size()) {
258 message
259 = QObject::tr("The group '%1' contains %2 object(s). Do you want to delete them as well?")
260 .arg(QString::fromUtf8(getExtendedViewProvider()->getObject()->Label.getValue()))
261 .arg(allDescendants.size());
262 }
263 else {
264 // if we have nested groups
265 message = QObject::tr(
266 "The group '%1' contains %2 direct children and %3 total descendants "
267 "(including nested groups). Do you want to delete all of them recursively?"
268 )
269 .arg(QString::fromUtf8(getExtendedViewProvider()->getObject()->Label.getValue()))
270 .arg(directChildren.size())
271 .arg(allDescendants.size());
272 }
273
274 QMessageBox::StandardButton choice = QMessageBox::question(
275 getMainWindow(),
276 QObject::tr("Delete group contents recursively?"),
277 message,
278 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
279 QMessageBox::No
280 );
281
282 if (choice == QMessageBox::Cancel) {
283 // don't delete anything if user has cancelled
284 return false;
285 }
286
287 if (choice == QMessageBox::Yes) {
288 // delete all of the children recursively and call their viewprovider method
289 deleteGroupContentsRecursively(group);
290 }
291 // if user has specified "No" then delete the group but move children to the parent or root
292
293 return true;
294 }
295
296
297 namespace Gui
298 {
299 EXTENSION_PROPERTY_SOURCE_TEMPLATE(Gui::ViewProviderGroupExtensionPython, Gui::ViewProviderGroupExtension)
300
301 // explicit template instantiation
302 template class GuiExport ViewProviderExtensionPythonT<ViewProviderGroupExtension>;
303 } // namespace Gui