1
/***************************************************************************
2
* Copyright (c) 2015 Stefan Tröger <stefantroeger@gmx.net> *
3
* Copyright (c) 2015 Alexander Golubev (Fat-Zer) <fatzer2@gmail.com> *
5
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
22
***************************************************************************/
24
#include "PreCompiled.h"
27
# include <Inventor/nodes/SoLightModel.h>
28
# include <Inventor/nodes/SoSeparator.h>
31
#include <App/Document.h>
32
#include <App/Origin.h>
33
#include "Base/Console.h"
34
#include <Base/Vector3D.h>
36
#include "ViewProviderOrigin.h"
37
#include "Application.h"
40
#include "ViewProviderLine.h"
41
#include "ViewProviderPlane.h"
47
PROPERTY_SOURCE(Gui::ViewProviderOrigin, Gui::ViewProviderDocumentObject)
50
* Creates the view provider for an object group.
52
ViewProviderOrigin::ViewProviderOrigin()
54
ADD_PROPERTY_TYPE ( Size, (Base::Vector3d(10,10,10)), 0, App::Prop_None,
55
QT_TRANSLATE_NOOP("App::Property", "The displayed size of the origin"));
56
Size.setStatus(App::Property::ReadOnly, true);
58
sPixmap = "Std_CoordinateSystem";
59
Visibility.setValue(false);
61
pcGroupChildren = new SoGroup();
62
pcGroupChildren->ref();
64
auto lm = new SoLightModel();
65
lm->model = SoLightModel::BASE_COLOR;
66
pcRoot->insertChild(lm, 0);
69
ViewProviderOrigin::~ViewProviderOrigin() {
70
pcGroupChildren->unref();
71
pcGroupChildren = nullptr;
74
std::vector<App::DocumentObject*> ViewProviderOrigin::claimChildren() const {
75
return static_cast<App::Origin*>( getObject() )->OriginFeatures.getValues ();
78
std::vector<App::DocumentObject*> ViewProviderOrigin::claimChildren3D() const {
79
return claimChildren ();
82
void ViewProviderOrigin::attach(App::DocumentObject* pcObject)
84
Gui::ViewProviderDocumentObject::attach(pcObject);
85
addDisplayMaskMode(pcGroupChildren, "Base");
88
std::vector<std::string> ViewProviderOrigin::getDisplayModes() const
93
void ViewProviderOrigin::setDisplayMode(const char* ModeName)
95
if (strcmp(ModeName, "Base") == 0)
96
setDisplayMaskMode("Base");
97
ViewProviderDocumentObject::setDisplayMode(ModeName);
100
void ViewProviderOrigin::setTemporaryVisibility(bool axis, bool plane) {
101
auto origin = static_cast<App::Origin*>( getObject() );
103
bool saveState = tempVisMap.empty();
106
// Remember & Set axis visibility
107
for(App::DocumentObject* obj : origin->axes()) {
109
Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(obj);
112
tempVisMap[vp] = vp->isVisible();
114
vp->setVisible(axis);
119
// Remember & Set plane visibility
120
for(App::DocumentObject* obj : origin->planes()) {
122
Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(obj);
125
tempVisMap[vp] = vp->isVisible();
127
vp->setVisible(plane);
131
} catch (const Base::Exception &ex) {
132
Base::Console().Error ("%s\n", ex.what() );
135
// Remember & Set self visibility
136
tempVisMap[this] = isVisible();
141
void ViewProviderOrigin::resetTemporaryVisibility() {
142
for(std::pair<Gui::ViewProvider*, bool> pair : tempVisMap) {
143
pair.first->setVisible(pair.second);
148
double ViewProviderOrigin::defaultSize()
150
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
151
return 0.25 * hGrp->GetFloat("NewDocumentCameraScale",100.0);
154
bool ViewProviderOrigin::isTemporaryVisibility() {
155
return !tempVisMap.empty();
158
void ViewProviderOrigin::onChanged(const App::Property* prop) {
161
Gui::Application *app = Gui::Application::Instance;
162
Base::Vector3d sz = Size.getValue ();
163
auto origin = static_cast<App::Origin*> ( getObject() );
165
// Calculate axes and planes sizes
166
double szXY = std::max ( sz.x, sz.y );
167
double szXZ = std::max ( sz.x, sz.z );
168
double szYZ = std::max ( sz.y, sz.z );
170
double szX = std::min ( szXY, szXZ );
171
double szY = std::min ( szXY, szYZ );
172
double szZ = std::min ( szXZ, szYZ );
174
// Find view providers
175
Gui::ViewProviderPlane* vpPlaneXY, *vpPlaneXZ, *vpPlaneYZ;
176
Gui::ViewProviderLine* vpLineX, *vpLineY, *vpLineZ;
178
vpPlaneXY = static_cast<Gui::ViewProviderPlane *> ( app->getViewProvider ( origin->getXY () ) );
179
vpPlaneXZ = static_cast<Gui::ViewProviderPlane *> ( app->getViewProvider ( origin->getXZ () ) );
180
vpPlaneYZ = static_cast<Gui::ViewProviderPlane *> ( app->getViewProvider ( origin->getYZ () ) );
182
vpLineX = static_cast<Gui::ViewProviderLine *> ( app->getViewProvider ( origin->getX () ) );
183
vpLineY = static_cast<Gui::ViewProviderLine *> ( app->getViewProvider ( origin->getY () ) );
184
vpLineZ = static_cast<Gui::ViewProviderLine *> ( app->getViewProvider ( origin->getZ () ) );
187
if (vpPlaneXY) { vpPlaneXY->Size.setValue ( szXY ); }
188
if (vpPlaneXZ) { vpPlaneXZ->Size.setValue ( szXZ ); }
189
if (vpPlaneYZ) { vpPlaneYZ->Size.setValue ( szYZ ); }
190
if (vpLineX) { vpLineX->Size.setValue ( szX * axesScaling ); }
191
if (vpLineY) { vpLineY->Size.setValue ( szY * axesScaling ); }
192
if (vpLineZ) { vpLineZ->Size.setValue ( szZ * axesScaling ); }
194
} catch (const Base::Exception &ex) {
195
// While restoring a document don't report errors if one of the lines or planes
197
App::Document* doc = getObject()->getDocument();
198
if (!doc->testStatus(App::Document::Restoring))
199
Base::Console().Error ("%s\n", ex.what() );
203
ViewProviderDocumentObject::onChanged ( prop );
206
bool ViewProviderOrigin::onDelete(const std::vector<std::string> &) {
207
auto origin = static_cast<App::Origin*>( getObject() );
209
if ( !origin->getInList().empty() ) {
213
auto objs = origin->OriginFeatures.getValues();
214
origin->OriginFeatures.setValues({});
216
for (auto obj: objs ) {
217
Gui::Command::doCommand( Gui::Command::Doc, "App.getDocument(\"%s\").removeObject(\"%s\")",
218
obj->getDocument()->getName(), obj->getNameInDocument() );