Debian Codegraph @

src/Gui/ActiveObjectList.cpp

raw
1 /**************************************************************************
2 * Copyright (c) 2014 Jürgen Riegel <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU Library General Public License (LGPL) *
8 * as published by the Free Software Foundation; either version 2 of *
9 * the License, or (at your option) any later version. *
10 * for detail see the LICENCE text file. *
11 * *
12 * FreeCAD is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with FreeCAD; if not, write to the Free Software *
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
20 * USA *
21 * *
22 ***************************************************************************/
23
24 #include <App/Document.h>
25 #include <Gui/Application.h>
26 #include <Gui/Document.h>
27 #include <Gui/Selection/Selection.h>
28 #include <Gui/ViewProviderDocumentObject.h>
29
30 #include "ActiveObjectList.h"
31 #include "TreeParams.h"
32
33
34 FC_LOG_LEVEL_INIT("MDIView", true, true)
35
36 using namespace Gui;
37
38 App::DocumentObject* ActiveObjectList::getObject(
39 const ObjectInfo& info,
40 bool resolve,
41 App::DocumentObject** parent,
42 std::string* subname
43 ) const
44 {
45 if (parent) {
46 *parent = info.obj;
47 }
48 if (subname) {
49 *subname = info.subname;
50 }
51 auto obj = info.obj;
52 if (!obj || !obj->isAttachedToDocument()) {
53 return nullptr;
54 }
55 if (!info.subname.empty()) {
56 obj = obj->getSubObject(info.subname.c_str());
57 if (!obj) {
58 return nullptr;
59 }
60 }
61
62 return resolve ? obj->getLinkedObject(true) : obj;
63 }
64
65 void ActiveObjectList::setHighlight(const ObjectInfo& info, HighlightMode mode, bool enable)
66 {
67 auto obj = getObject(info, false);
68 if (!obj) {
69 return;
70 }
71 auto vp = freecad_cast<ViewProviderDocumentObject*>(Application::Instance->getViewProvider(obj));
72 if (!vp) {
73 return;
74 }
75
76 if (TreeParams::getTreeActiveAutoExpand() && (enable || vp->isAutoCollapseOnDeactivation())) {
77 vp->getDocument()->signalExpandObject(
78 *vp,
79 enable ? TreeItemMode::ExpandPath : TreeItemMode::CollapseItem,
80 info.obj,
81 info.subname.c_str()
82 );
83 }
84
85 vp->getDocument()->signalHighlightObject(*vp, mode, enable, info.obj, info.subname.c_str());
86 }
87
88 Gui::ActiveObjectList::ObjectInfo Gui::ActiveObjectList::getObjectInfo(
89 App::DocumentObject* obj,
90 const char* subname
91 ) const
92 {
93 ObjectInfo info;
94 info.obj = nullptr;
95 if (!obj || !obj->isAttachedToDocument()) {
96 return info;
97 }
98
99 if (subname) {
100 info.obj = obj;
101 info.subname = subname;
102 }
103 else {
104 // If the input object is not from this document, it must be brought in
105 // by some link type object of this document. We only accept the object
106 // if we can find such object in the current selection.
107 auto sels
108 = Gui::Selection().getSelection(_Doc->getDocument()->getName(), ResolveMode::NoResolve);
109 for (auto& sel : sels) {
110 if (sel.pObject == obj || sel.pObject->getLinkedObject(true) == obj) {
111 info.obj = sel.pObject;
112 break;
113 }
114
115 for (auto dot = strchr(sel.SubName, '.'); dot; dot = strchr(dot + 1, '.')) {
116 std::string subname(sel.SubName, dot - sel.SubName + 1);
117 auto sobj = sel.pObject->getSubObject(subname.c_str());
118 if (!sobj) {
119 break;
120 }
121 if (sobj == obj || sobj->getLinkedObject(true) == obj) {
122 info.obj = sel.pObject;
123 info.subname = subname;
124 break;
125 }
126 }
127
128 if (info.obj) {
129 break;
130 }
131 }
132 if (!info.obj) {
133 // No selection is found, try to obtain the object hierarchy using
134 // DocumentObject::getParents()
135 unsigned long count = 0xffffffff;
136 for (auto& v : obj->getParents()) {
137 if (v.first->getDocument() != _Doc->getDocument()) {
138 continue;
139 }
140
141 // We prioritize on non-linked group object having the least
142 // hierarchies.
143 unsigned long cnt = v.first->getSubObjectList(v.second.c_str()).size();
144 if (v.first->getLinkedObject(false) != v.first) {
145 cnt &= 0x8000000;
146 }
147 if (cnt < count) {
148 count = cnt;
149 info.obj = v.first;
150 info.subname = v.second;
151 }
152 }
153
154 if (!info.obj) {
155 if (obj->getDocument() != _Doc->getDocument()) {
156 return info;
157 }
158 info.obj = obj;
159 }
160 }
161 }
162
163 return info;
164 }
165
166 bool Gui::ActiveObjectList::hasObject(App::DocumentObject* obj, const char* name, const char* subname) const
167 {
168 auto it = _ObjectMap.find(name);
169 if (it == _ObjectMap.end()) {
170 return false;
171 }
172 auto info = getObjectInfo(obj, subname);
173 return info.obj == it->second.obj && info.subname == it->second.subname;
174 }
175
176 void Gui::ActiveObjectList::setObject(
177 App::DocumentObject* obj,
178 const char* name,
179 const char* subname,
180 const Gui::HighlightMode& mode
181 )
182 {
183 auto it = _ObjectMap.find(name);
184 if (it != _ObjectMap.end()) {
185 setHighlight(it->second, mode, false);
186 _ObjectMap.erase(it);
187 }
188
189 if (!obj) {
190 if (_Doc) {
191 _Doc->signalActivatedViewProvider(nullptr, name);
192 }
193 return;
194 }
195
196 auto info = getObjectInfo(obj, subname);
197 if (!info.obj) {
198 FC_ERR(
199 "Cannot set active object " << obj->getFullName() << '.' << (subname ? subname : "")
200 << " in document '" << _Doc->getDocument()->getName()
201 << "'. Not found in current selection"
202 );
203 return;
204 }
205
206 _ObjectMap[name] = info;
207 setHighlight(info, mode, true);
208
209 auto vp = freecad_cast<ViewProviderDocumentObject*>(Application::Instance->getViewProvider(obj));
210 if (vp) {
211 vp->getDocument()->signalActivatedViewProvider(vp, name);
212 }
213 }
214
215 bool Gui::ActiveObjectList::hasObject(const char* name) const
216 {
217 return _ObjectMap.find(name) != _ObjectMap.end();
218 }
219
220 void ActiveObjectList::objectDeleted(const ViewProviderDocumentObject& vp)
221 {
222 // Hint: With C++20 std::erase_if for containers can be used
223 auto isEqual = [&vp](const auto& item) {
224 return item.second.obj == vp.getObject();
225 };
226 for (auto it = _ObjectMap.begin(); it != _ObjectMap.end();) {
227 if (isEqual(*it)) {
228 it = _ObjectMap.erase(it);
229 }
230 else {
231 ++it;
232 }
233 }
234 }
235
236 App::DocumentObject* ActiveObjectList::getObjectWithExtension(const Base::Type extensionTypeId) const
237 {
238 for (const auto& pair : _ObjectMap) {
239 App::DocumentObject* obj = getObject(pair.second, true);
240 if (obj && obj->hasExtension(extensionTypeId)) {
241 return obj;
242 }
243 }
244
245 return nullptr;
246 }