Debian Codegraph @

src/Gui/ViewProviderCoordinateSystem.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2015 Stefan Tröger <[email protected]> *
3 * Copyright (c) 2015 Alexander Golubev (Fat-Zer) <[email protected]> *
4 * *
5 * This file is part of the FreeCAD CAx development system. *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Library General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2 of the License, or (at your option) any later version. *
11 * *
12 * This library 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 this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ***************************************************************************/
23
24
25 #include <Inventor/nodes/SoLightModel.h>
26 #include <Inventor/nodes/SoSeparator.h>
27 #include <QMessageBox>
28 #include <QCheckBox>
29
30
31 #include <App/Document.h>
32 #include <App/Origin.h>
33 #include "Base/Console.h"
34 #include <Base/Vector3D.h>
35
36 #include "ViewProviderCoordinateSystem.h"
37 #include "Application.h"
38 #include "Command.h"
39 #include "Document.h"
40 #include "ViewProviderLine.h"
41 #include "ViewProviderPlane.h"
42 #include "ViewProviderPoint.h"
43
44
45 using namespace Gui;
46
47
48 PROPERTY_SOURCE(Gui::ViewProviderCoordinateSystem, Gui::ViewProviderGeoFeatureGroup)
49
50 /**
51 * Creates the view provider for an object group.
52 */
53 ViewProviderCoordinateSystem::ViewProviderCoordinateSystem()
54 {
55 sPixmap = "Std_CoordinateSystem";
56 Visibility.setValue(false);
57
58 pcGroupChildren = new SoGroup();
59 pcGroupChildren->ref();
60
61 auto lm = new SoLightModel();
62 lm->model = SoLightModel::BASE_COLOR;
63 pcRoot->insertChild(lm, 0);
64 }
65
66 ViewProviderCoordinateSystem::~ViewProviderCoordinateSystem()
67 {
68 pcGroupChildren->unref();
69 pcGroupChildren = nullptr;
70 }
71
72 std::vector<App::DocumentObject*> ViewProviderCoordinateSystem::claimChildren() const
73 {
74 auto* lcs = getObject<App::LocalCoordinateSystem>();
75 if (!lcs) {
76 return {};
77 }
78
79 std::vector<App::DocumentObject*> childs = lcs->OriginFeatures.getValues();
80 auto it = std::find(childs.begin(), childs.end(), lcs);
81 if (it != childs.end()) {
82 childs.erase(it);
83 }
84 return childs;
85 }
86
87 std::vector<App::DocumentObject*> ViewProviderCoordinateSystem::claimChildren3D() const
88 {
89 return claimChildren();
90 }
91
92 void ViewProviderCoordinateSystem::attach(App::DocumentObject* pcObject)
93 {
94 Gui::ViewProviderDocumentObject::attach(pcObject);
95 addDisplayMaskMode(pcGroupChildren, "Base");
96 }
97
98 std::vector<std::string> ViewProviderCoordinateSystem::getDisplayModes() const
99 {
100 return {"Base"};
101 }
102
103 void ViewProviderCoordinateSystem::setDisplayMode(const char* ModeName)
104 {
105 if (strcmp(ModeName, "Base") == 0) {
106 setDisplayMaskMode("Base");
107 }
108 ViewProviderDocumentObject::setDisplayMode(ModeName);
109 }
110
111 void ViewProviderCoordinateSystem::setTemporaryVisibility(DatumElements elements)
112 {
113 auto* lcs = getObject<App::LocalCoordinateSystem>();
114 if (!lcs) {
115 return;
116 }
117
118 bool saveState = tempVisMap.empty();
119
120 try {
121 // Remember & Set axis visibility
122 for (App::DocumentObject* obj : lcs->axes()) {
123 if (auto vp = Gui::Application::Instance->getViewProvider(obj)) {
124 if (saveState) {
125 tempVisMap[vp] = vp->isVisible();
126 }
127 vp->setVisible(elements.testFlag(DatumElement::Axes));
128 }
129 }
130
131 // Remember & Set plane visibility
132 for (App::DocumentObject* obj : lcs->planes()) {
133 if (auto vp = Gui::Application::Instance->getViewProvider(obj)) {
134 if (saveState) {
135 tempVisMap[vp] = vp->isVisible();
136 }
137 vp->setVisible(elements.testFlag(DatumElement::Planes));
138 }
139 }
140
141 // Remember & Set origin point visibility
142 App::DocumentObject* obj = lcs->getOrigin();
143 if (auto vp = Gui::Application::Instance->getViewProvider(obj)) {
144 if (saveState) {
145 tempVisMap[vp] = vp->isVisible();
146 }
147 vp->setVisible(elements.testFlag(DatumElement::Origin));
148 }
149 }
150 catch (const Base::Exception& ex) {
151 Base::Console().error("%s\n", ex.what());
152 }
153
154 // Remember & Set self visibility
155 tempVisMap[this] = isVisible();
156 setVisible(true);
157 }
158
159 void ViewProviderCoordinateSystem::resetTemporaryVisibility()
160 {
161 for (std::pair<Gui::ViewProvider*, bool> pair : tempVisMap) {
162 pair.first->setVisible(pair.second);
163 }
164 tempVisMap.clear();
165 }
166
167 double ViewProviderCoordinateSystem::defaultSize()
168 {
169 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
170 "User parameter:BaseApp/Preferences/View"
171 );
172 return hGrp->GetFloat("DatumsSize", 25);
173 }
174
175 bool ViewProviderCoordinateSystem::isTemporaryVisibility()
176 {
177 return !tempVisMap.empty();
178 }
179
180 void ViewProviderCoordinateSystem::setPlaneLabelVisibility(bool val)
181 {
182 auto* lcs = getObject<App::LocalCoordinateSystem>();
183 if (!lcs) {
184 return;
185 }
186 for (auto* plane : lcs->planes()) {
187 auto* vp = dynamic_cast<Gui::ViewProviderPlane*>(
188 Gui::Application::Instance->getViewProvider(plane)
189 );
190 if (vp) {
191 vp->setLabelVisibility(val);
192 }
193 }
194 }
195
196 void ViewProviderCoordinateSystem::applyDatumObjects(const DatumObjectFunc& func)
197 {
198 auto* lcs = getObject<App::LocalCoordinateSystem>();
199 if (!lcs) {
200 return;
201 }
202 const auto& objs = lcs->OriginFeatures.getValues();
203 for (auto* obj : objs) {
204 auto* vp = dynamic_cast<Gui::ViewProviderDatum*>(
205 Gui::Application::Instance->getViewProvider(obj)
206 );
207 if (vp) {
208 func(vp);
209 }
210 }
211 }
212
213 void ViewProviderCoordinateSystem::setTemporaryScale(double factor)
214 {
215 applyDatumObjects([factor](ViewProviderDatum* vp) { vp->setTemporaryScale(factor); });
216 }
217
218 void ViewProviderCoordinateSystem::resetTemporarySize()
219 {
220 applyDatumObjects([](ViewProviderDatum* vp) { vp->resetTemporarySize(); });
221 }
222
223 bool ViewProviderCoordinateSystem::onDelete(const std::vector<std::string>&)
224 {
225 auto* lcs = getObject<App::LocalCoordinateSystem>();
226 if (!lcs) {
227 return false;
228 }
229
230 if (lcs->is<App::Origin>() && !lcs->getInList().empty()) {
231 // Do not allow deletion of origin objects that are not lost.
232 return false;
233 }
234
235 auto objs = lcs->OriginFeatures.getValues();
236 lcs->OriginFeatures.setValues({});
237
238 for (auto obj : objs) {
239 Gui::Command::doCommand(
240 Gui::Command::Doc,
241 "App.getDocument(\"%s\").removeObject(\"%s\")",
242 obj->getDocument()->getName(),
243 obj->getNameInDocument()
244 );
245 }
246
247 return true;
248 }