forked from FreeCAD/FreeCAD
/
ViewProviderLine.cpp
292 lines (239 loc) · 9.02 KB
/
ViewProviderLine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/***************************************************************************
* Copyright (c) Jürgen Riegel (juergen.riegel@web.de) 2012 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <sstream>
# include <QApplication>
# include <Inventor/SoPickedPoint.h>
# include <Inventor/events/SoMouseButtonEvent.h>
# include <Inventor/nodes/SoSeparator.h>
# include <Inventor/nodes/SoBaseColor.h>
# include <Inventor/nodes/SoFontStyle.h>
# include <Inventor/nodes/SoPickStyle.h>
# include <Inventor/nodes/SoText2.h>
# include <Inventor/nodes/SoTranslation.h>
# include <Inventor/nodes/SoCoordinate3.h>
# include <Inventor/nodes/SoIndexedLineSet.h>
# include <Inventor/nodes/SoMarkerSet.h>
# include <Inventor/nodes/SoDrawStyle.h>
#endif
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoAnnotation.h>
#include <Inventor/details/SoLineDetail.h>
#include <Inventor/nodes/SoAsciiText.h>
#include "ViewProviderLine.h"
#include "SoFCSelection.h"
#include "Application.h"
#include "Document.h"
#include "View3DInventorViewer.h"
#include "Inventor/SoAutoZoomTranslation.h"
#include "SoAxisCrossKit.h"
#include "Window.h"
//#include <SoDepthBuffer.h>
#include <App/PropertyGeo.h>
#include <App/PropertyStandard.h>
#include <App/MeasureDistance.h>
#include <Base/Console.h>
using namespace Gui;
PROPERTY_SOURCE(Gui::ViewProviderLine, Gui::ViewProviderGeometryObject)
ViewProviderLine::ViewProviderLine()
{
ADD_PROPERTY(Size,(1.0));
pMat = new SoMaterial();
pMat->ref();
float size = Size.getValue(); // Note: If you change this, you need to also adapt App/Line.cpp getBoundBox()
SbVec3f verts[4] =
{
SbVec3f(-size,0,0), SbVec3f(size,0,0),
};
// indexes used to create the edges
static const int32_t lines[6] =
{
0,1,-1
};
// Create the selection node
pcHighlight = createFromSettings();
pcHighlight->ref();
if (pcHighlight->selectionMode.getValue() == Gui::SoFCSelection::SEL_OFF)
Selectable.setValue(false);
pMat->diffuseColor.setNum(1);
pMat->diffuseColor.set1Value(0, SbColor(50./255., 150./255., 250./255.));
pCoords = new SoCoordinate3();
pCoords->ref();
pCoords->point.setNum(2);
pCoords->point.setValues(0, 2, verts);
pLines = new SoIndexedLineSet();
pLines->ref();
pLines->coordIndex.setNum(3);
pLines->coordIndex.setValues(0, 3, lines);
pFont = new SoFont();
pFont->size.setValue(Size.getValue()/10.);
pTranslation = new SoTranslation();
pTranslation->ref();
pTranslation->translation.setValue(SbVec3f(-1,0,0));
pText = new SoAsciiText();
pText->ref();
pText->width.setValue(-1);
sPixmap = "view-measurement";
}
ViewProviderLine::~ViewProviderLine()
{
pcHighlight->unref();
pCoords->unref();
pLines->unref();
pMat->unref();
pTranslation->unref();
pText->unref();
}
void ViewProviderLine::onChanged(const App::Property* prop)
{
if (prop == &Size){
float size = Size.getValue(); // Note: If you change this, you need to also adapt App/Line.cpp getBoundBox()
SbVec3f verts[2] =
{
SbVec3f(-size,0,0), SbVec3f(size,0,0),
};
pCoords->point.setValues(0, 2, verts);
pFont->size.setValue(Size.getValue()/10.);
pTranslation->translation.setValue(SbVec3f(-size,0,0));
}
else
ViewProviderGeometryObject::onChanged(prop);
}
std::vector<std::string> ViewProviderLine::getDisplayModes(void) const
{
// add modes
std::vector<std::string> StrList;
StrList.push_back("Base");
return StrList;
}
void ViewProviderLine::setDisplayMode(const char* ModeName)
{
if (strcmp(ModeName, "Base") == 0)
setDisplayMaskMode("Base");
ViewProviderGeometryObject::setDisplayMode(ModeName);
}
void ViewProviderLine::attach(App::DocumentObject* pcObject)
{
ViewProviderGeometryObject::attach(pcObject);
pcHighlight->objectName = pcObject->getNameInDocument();
pcHighlight->documentName = pcObject->getDocument()->getName();
pcHighlight->subElementName = "Main";
SoSeparator *sep = new SoSeparator();
SoAnnotation *lineSep = new SoAnnotation();
SoDrawStyle* style = new SoDrawStyle();
style->lineWidth = 2.0f;
SoMaterialBinding* matBinding = new SoMaterialBinding;
matBinding->value = SoMaterialBinding::OVERALL;
sep->addChild(matBinding);
sep->addChild(pMat);
sep->addChild(pcHighlight);
pcHighlight->addChild(style);
pcHighlight->addChild(pCoords);
pcHighlight->addChild(pLines);
style = new SoDrawStyle();
style->lineWidth = 2.0f;
style->linePattern.setValue(0xF000);
lineSep->addChild(style);
lineSep->addChild(pLines);
lineSep->addChild(pFont);
pText->string.setValue(SbString(pcObject->Label.getValue()));
lineSep->addChild(pTranslation);
lineSep->addChild(pText);
pcHighlight->addChild(lineSep);
pcHighlight->style = SoFCSelection::EMISSIVE_DIFFUSE;
addDisplayMaskMode(sep, "Base");
}
void ViewProviderLine::updateData(const App::Property* prop)
{
pText->string.setValue(SbString(pcObject->Label.getValue()));
ViewProviderGeometryObject::updateData(prop);
}
std::string ViewProviderLine::getElement(const SoDetail* detail) const
{
if (detail) {
if (detail->getTypeId() == SoLineDetail::getClassTypeId()) {
const SoLineDetail* line_detail = static_cast<const SoLineDetail*>(detail);
int edge = line_detail->getLineIndex();
if (edge == 0)
{
return std::string("Main");
}
}
}
return std::string("");
}
SoDetail* ViewProviderLine::getDetail(const char* subelement) const
{
SoLineDetail* detail = 0;
std::string subelem(subelement);
int edge = -1;
if(subelem == "Main") edge = 0;
if(edge >= 0) {
detail = new SoLineDetail();
detail->setPartIndex(edge);
}
return detail;
}
bool ViewProviderLine::isSelectable(void) const
{
return true;
}
bool ViewProviderLine::setEdit(int ModNum)
{
return true;
}
void ViewProviderLine::unsetEdit(int ModNum)
{
}
Gui::SoFCSelection* ViewProviderLine::createFromSettings() const
{
Gui::SoFCSelection* sel = new Gui::SoFCSelection();
float transparency;
ParameterGrp::handle hGrp = Gui::WindowParameter::getDefaultParameter()->GetGroup("View");
bool enablePre = hGrp->GetBool("EnablePreselection", true);
bool enableSel = hGrp->GetBool("EnableSelection", true);
if (!enablePre) {
sel->highlightMode = Gui::SoFCSelection::OFF;
}
else {
// Search for a user defined value with the current color as default
SbColor highlightColor = sel->colorHighlight.getValue();
unsigned long highlight = (unsigned long)(highlightColor.getPackedValue());
highlight = hGrp->GetUnsigned("HighlightColor", highlight);
highlightColor.setPackedValue((uint32_t)highlight, transparency);
sel->colorHighlight.setValue(highlightColor);
}
if (!enableSel || !Selectable.getValue()) {
sel->selectionMode = Gui::SoFCSelection::SEL_OFF;
}
else {
// Do the same with the selection color
SbColor selectionColor = sel->colorSelection.getValue();
unsigned long selection = (unsigned long)(selectionColor.getPackedValue());
selection = hGrp->GetUnsigned("SelectionColor", selection);
selectionColor.setPackedValue((uint32_t)selection, transparency);
sel->colorSelection.setValue(selectionColor);
}
return sel;
}
// ----------------------------------------------------------------------------