Debian Codegraph @

src/Gui/ViewProviderLine.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2012 Jürgen Riegel <[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/SoText2.h>
26 #include <Inventor/nodes/SoCoordinate3.h>
27 #include <Inventor/nodes/SoIndexedLineSet.h>
28 #include <Inventor/nodes/SoPickStyle.h>
29 #include <Inventor/nodes/SoSeparator.h>
30 #include <Inventor/nodes/SoTranslation.h>
31
32
33 #include <Base/Tools.h>
34 #include <App/Datums.h>
35 #include <Gui/Utilities.h>
36 #include <Gui/ViewParams.h>
37
38 #include "ViewProviderLine.h"
39 #include "ViewProviderCoordinateSystem.h"
40
41 #include <SoTextLabel.h>
42
43
44 using namespace Gui;
45
46 PROPERTY_SOURCE(Gui::ViewProviderLine, Gui::ViewProviderDatum)
47
48
49 ViewProviderLine::ViewProviderLine()
50 {
51 sPixmap = "Std_Axis";
52
53 pLabel = new SoFrameLabel();
54 pLabel->textColor.setValue(1.0, 1.0, 1.0);
55 pLabel->horAlignment = SoImage::CENTER;
56 pLabel->vertAlignment = SoImage::HALF;
57 pLabel->border = false;
58 pLabel->frame = false;
59 pLabel->textUseBaseColor = true;
60 pLabel->size = 8; // NOLINT
61 }
62
63 ViewProviderLine::~ViewProviderLine() = default;
64
65 void ViewProviderLine::attach(App::DocumentObject* obj)
66 {
67 ViewProviderDatum::attach(obj);
68
69 // indexes used to create the edges
70 static const int32_t lines[4] = {0, 1, -1};
71
72 SoSeparator* sep = getDatumRoot();
73
74 pCoords = new SoCoordinate3();
75 sep->addChild(pCoords);
76
77 auto pLines = new SoIndexedLineSet();
78 pLines->coordIndex.setNum(3);
79 pLines->coordIndex.setValues(0, 3, lines);
80 sep->addChild(pLines);
81
82 pLabelTranslation = new SoTranslation();
83 sep->addChild(pLabelTranslation);
84
85 auto ps = new SoPickStyle();
86 ps->style.setValue(SoPickStyle::SHAPE_ON_TOP);
87 sep->addChild(ps);
88
89 sep->addChild(pLabel);
90
91 handlers.addDelayedHandler(
92 ViewParams::instance()->getHandle(),
93 {"DatumLineSize", "DatumScale"},
94 [this](const ParameterGrp::handle&) { updateLineSize(); }
95 );
96
97 updateLineSize();
98 }
99
100 void ViewProviderLine::updateLineSize()
101 {
102 // Setup label text and line colors
103 const char* name = pcObject->getNameInDocument();
104
105 bool noRole = false;
106 auto axisRoles = App::LocalCoordinateSystem::AxisRoles;
107 if (strncmp(name, axisRoles[0], strlen(axisRoles[0])) == 0) {
108 // X-axis: red
109 ShapeAppearance.setDiffuseColor(ViewParams::instance()->getAxisXColor());
110 pLabel->string.setValue("X");
111 }
112 else if (strncmp(name, axisRoles[1], strlen(axisRoles[1])) == 0) {
113 // Y-axis: green
114 ShapeAppearance.setDiffuseColor(ViewParams::instance()->getAxisYColor());
115 pLabel->string.setValue("Y");
116 }
117 else if (strncmp(name, axisRoles[2], strlen(axisRoles[2])) == 0) {
118 // Z-axis: blue
119 ShapeAppearance.setDiffuseColor(ViewParams::instance()->getAxisZColor());
120 pLabel->string.setValue("Z");
121 }
122 else {
123 noRole = true;
124 }
125
126 const auto params = ViewParams::instance();
127 const float size = params->getDatumLineSize() * Base::fromPercent(params->getDatumScale());
128
129 auto line = getObject<App::Line>();
130 Base::Vector3d dir = line->getBaseDirection();
131 SbVec3f verts[2];
132
133 if (noRole) {
134 verts[0] = Base::convertTo<SbVec3f>(dir * 2 * size);
135 verts[1] = SbVec3f(0, 0, 0);
136 }
137 else {
138 verts[0] = Base::convertTo<SbVec3f>(dir * size);
139 verts[1] = Base::convertTo<SbVec3f>(dir * 0.2 * size);
140 }
141
142 pCoords->point.setNum(2);
143 pCoords->point.setValues(0, 2, verts);
144
145 pLabelTranslation->translation.setValue(Base::convertTo<SbVec3f>(dir * 1.2 * size));
146 }