Debian Codegraph @

src/Gui/SoFCColorLegend.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <sstream>
25 #include <QCoreApplication>
26 #include <Inventor/fields/SoMFString.h>
27 #include <Inventor/nodes/SoBaseColor.h>
28 #include <Inventor/nodes/SoCoordinate3.h>
29 #include <Inventor/nodes/SoIndexedFaceSet.h>
30 #include <Inventor/nodes/SoMaterial.h>
31 #include <Inventor/nodes/SoText2.h>
32 #include <Inventor/nodes/SoTransform.h>
33
34
35 #include "SoFCColorLegend.h"
36 #include "ViewProvider.h"
37
38
39 using namespace Gui;
40
41 SO_NODE_SOURCE(SoFCColorLegend)
42
43 /*!
44 Constructor.
45 */
46 SoFCColorLegend::SoFCColorLegend()
47 : _bbox(4.0f, -4.0f, 4.5f, 4.0f)
48 {
49 SO_NODE_CONSTRUCTOR(SoFCColorLegend);
50 coords = new SoCoordinate3;
51 coords->ref();
52 labelGroup = new SoSeparator;
53 labelGroup->ref();
54
55 valueGroup = new SoSeparator;
56 valueGroup->ref();
57
58 setColorLegend(_currentLegend);
59 setLegendLabels(_currentLegend, 3);
60 }
61
62 /*!
63 Destructor.
64 */
65 SoFCColorLegend::~SoFCColorLegend()
66 {
67 // delete THIS;
68 coords->unref();
69 labelGroup->unref();
70 valueGroup->unref();
71 }
72
73 // doc from parent
74 void SoFCColorLegend::initClass()
75 {
76 SO_NODE_INIT_CLASS(SoFCColorLegend, SoFCColorBarBase, "Separator");
77 }
78
79 void SoFCColorLegend::finish()
80 {
81 atexit_cleanup();
82 }
83
84 const char* SoFCColorLegend::getColorBarName() const
85 {
86 return QT_TRANSLATE_NOOP("QObject", "Color Legend");
87 }
88
89 namespace
90 {
91 std::vector<SbVec3f> getLabelPositions(int num, const SbBox2f& bbox)
92 {
93 std::vector<SbVec3f> pos;
94 float fMinY = bbox.getMin()[1];
95 float fMaxX = bbox.getMax()[0];
96 float fMaxY = bbox.getMax()[1] - 0.5f;
97
98 if (num > 1) {
99 float fStep = (fMaxY - fMinY) / static_cast<float>(num - 1);
100 pos.emplace_back(fMaxX + 0.1f, fMaxY + 0.20f + fStep, 0.0f);
101 for (int i = 0; i < num; i++) {
102 pos.emplace_back(0.0f, -fStep, 0.0f);
103 }
104 }
105
106 return pos;
107 }
108
109 std::vector<SbVec3f> getValuePositions(int num, const SbBox2f& bbox)
110 {
111 std::vector<SbVec3f> pos;
112 float fMinY = bbox.getMin()[1];
113 float fMaxX = bbox.getMax()[0];
114 float fMaxY = bbox.getMax()[1] - 0.5f;
115
116 if (num > 2) {
117 float fStep = (fMaxY - fMinY) / static_cast<float>(num - 2);
118 float eps = fStep / 4.0f;
119
120 pos.emplace_back(fMaxX + 0.1f, fMaxY + 0.25f + 1.5f * fStep, 0.0f);
121 for (int i = 0; i < num; i++) {
122 pos.emplace_back(0.0f, -fStep, 0.0f);
123 }
124
125 SbVec3f v;
126 v = pos[1];
127 v[1] -= eps;
128 pos[1] = v;
129
130 v = pos[2];
131 v[1] += eps;
132 pos[2] = v;
133
134 v = pos.back();
135 v[1] += eps;
136 pos.back() = v;
137 }
138
139 return pos;
140 }
141 } // namespace
142
143 void SoFCColorLegend::setMarkerLabel(const SoMFString& label)
144 {
145 coinRemoveAllChildren(labelGroup);
146
147 int num = label.getNum();
148 if (num > 1) {
149 std::vector<SbVec3f> pos = getLabelPositions(num, _bbox);
150
151 auto trans = new SoTransform;
152 trans->translation.setValue(pos[0]);
153 labelGroup->addChild(trans);
154
155 for (int i = 0; i < num; i++) {
156 auto trans = new SoTransform;
157 auto color = new SoBaseColor;
158 auto text2 = new SoText2;
159
160 trans->translation.setValue(pos[i + 1]);
161 color->rgb.setValue(0, 0, 0);
162 text2->string.setValue(label[i]);
163 labelGroup->addChild(trans);
164 labelGroup->addChild(color);
165 labelGroup->addChild(text2);
166 }
167 }
168 }
169
170 void SoFCColorLegend::setMarkerValue(const SoMFString& value)
171 {
172 coinRemoveAllChildren(valueGroup);
173
174 int num = value.getNum();
175 if (num > 1) {
176 std::vector<SbVec3f> pos = getValuePositions(num, _bbox);
177
178 auto trans = new SoTransform;
179 trans->translation.setValue(pos[0]);
180 valueGroup->addChild(trans);
181
182 for (int i = 0; i < num; i++) {
183 auto trans = new SoTransform;
184 auto color = new SoBaseColor;
185 auto text2 = new SoText2;
186
187 trans->translation.setValue(pos[i + 1]);
188 color->rgb.setValue(0, 0, 0);
189 text2->string.setValue(value[i]);
190 valueGroup->addChild(trans);
191 valueGroup->addChild(color);
192 valueGroup->addChild(text2);
193 }
194 }
195 }
196
197 void SoFCColorLegend::setViewportSize(const SbVec2s& size)
198 {
199 float fMinX, fMinY, fMaxX, fMaxY;
200 float boxWidth = getBounds(size, fMinX, fMinY, fMaxX, fMaxY);
201
202 // legend bar is shifted to the left by width of the labels to assure that labels are fully visible
203 _bbox.setBounds(fMinX - boxWidth, fMinY, fMaxX - boxWidth, fMaxY);
204
205 arrangeLabels(_bbox);
206 arrangeValues(_bbox);
207 modifyPoints(_bbox);
208 }
209
210 void SoFCColorLegend::setRange(float fMin, float fMax, int prec)
211 {
212 std::size_t numFields = _currentLegend.hasNumberOfFields();
213 for (std::size_t i = 0; i <= numFields; i++) {
214 float factor = static_cast<float>(i) / numFields;
215 float value = (1 - factor) * fMin + factor * fMax;
216 _currentLegend.setValue(i, value);
217 }
218
219 setColorLegend(_currentLegend);
220 setLegendLabels(_currentLegend, prec);
221 }
222
223 void SoFCColorLegend::setLegendLabels(const App::ColorLegend& legend, int prec)
224 {
225 float fMin = legend.getMinValue();
226 float fMax = legend.getMaxValue();
227
228 std::size_t numFields = legend.hasNumberOfFields();
229
230 SoMFString labels, values;
231
232 float eps = std::pow(10.0f, static_cast<float>(-prec));
233 float value = std::min<float>(fabs(fMin), fabs(fMax));
234 std::ios::fmtflags flags = value < eps
235 ? (std::ios::scientific | std::ios::showpoint | std::ios::showpos)
236 : (std::ios::fixed | std::ios::showpoint | std::ios::showpos);
237
238 for (std::size_t i = 0; i < numFields; i++) {
239 std::stringstream s;
240 s << legend.getText(numFields - 1 - i);
241 labels.set1Value(i, s.str().c_str());
242 }
243
244 for (std::size_t i = 0; i <= numFields; i++) {
245 std::stringstream s;
246 s.precision(prec);
247 s.setf(flags);
248 float fValue = legend.getValue(numFields - i);
249 s << fValue;
250 values.set1Value(i, s.str().c_str());
251 }
252
253 setMarkerLabel(labels);
254 setMarkerValue(values);
255
256 setModified();
257 }
258
259 void SoFCColorLegend::modifyPoints(const SbBox2f& box)
260 {
261 float fMinX = box.getMin()[0];
262 float fMinY = box.getMin()[1];
263 float fMaxX = box.getMax()[0];
264 float fMaxY = box.getMax()[1] - 0.5f;
265
266 // set the vertices spanning the faces for the color legend
267 int intFields = coords->point.getNum() / 4;
268 for (int i = 0; i < intFields; i++) {
269 float w = static_cast<float>(i) / (intFields - 1);
270 float fPosY1 = w * fMaxY + (1.0f - w) * fMinY;
271 float fPosY2 = fPosY1 + 0.5f;
272 coords->point.set1Value(4 * i, fMinX, fPosY1, 0.0f);
273 coords->point.set1Value(4 * i + 1, fMaxX, fPosY1, 0.0f);
274 coords->point.set1Value(4 * i + 2, fMaxX, fPosY2, 0.0f);
275 coords->point.set1Value(4 * i + 3, fMinX, fPosY2, 0.0f);
276 }
277 }
278
279 void SoFCColorLegend::arrangeLabels(const SbBox2f& box)
280 {
281 // search for the labels
282 int num = 0;
283 for (int i = 0; i < labelGroup->getNumChildren(); i++) {
284 if (labelGroup->getChild(i)->getTypeId() == SoTransform::getClassTypeId()) {
285 num++;
286 }
287 }
288
289 if (num > 2) {
290 std::vector<SbVec3f> pos = getLabelPositions(num - 1, box);
291
292 int index = 0;
293 for (int j = 0; j < labelGroup->getNumChildren(); j++) {
294 if (labelGroup->getChild(j)->getTypeId() == SoTransform::getClassTypeId()) {
295 static_cast<SoTransform*>(labelGroup->getChild(j))->translation.setValue(pos[index++]);
296 }
297 }
298 }
299 }
300
301 void SoFCColorLegend::arrangeValues(const SbBox2f& box)
302 {
303 // search for the labels
304 int num = 0;
305 for (int i = 0; i < valueGroup->getNumChildren(); i++) {
306 if (valueGroup->getChild(i)->getTypeId() == SoTransform::getClassTypeId()) {
307 num++;
308 }
309 }
310
311 if (num > 3) {
312 std::vector<SbVec3f> pos = getValuePositions(num - 1, box);
313
314 int index = 0;
315 for (int j = 0; j < valueGroup->getNumChildren(); j++) {
316 if (valueGroup->getChild(j)->getTypeId() == SoTransform::getClassTypeId()) {
317 static_cast<SoTransform*>(valueGroup->getChild(j))->translation.setValue(pos[index++]);
318 }
319 }
320 }
321 }
322
323 void SoFCColorLegend::setColorLegend(const App::ColorLegend& legend)
324 {
325 // create top value field
326 std::size_t numFields = legend.hasNumberOfFields();
327 int intFields = static_cast<int>(numFields);
328 coords->point.setNum(4 * intFields);
329 modifyPoints(_bbox);
330
331 // for numFields colors we need numFields quads
332 auto faceset = new SoIndexedFaceSet;
333 faceset->coordIndex.setNum(5 * intFields);
334 for (int j = 0; j < intFields; j++) {
335 faceset->coordIndex.set1Value(5 * j, 4 * j);
336 faceset->coordIndex.set1Value(5 * j + 1, 4 * j + 1);
337 faceset->coordIndex.set1Value(5 * j + 2, 4 * j + 2);
338 faceset->coordIndex.set1Value(5 * j + 3, 4 * j + 3);
339 faceset->coordIndex.set1Value(5 * j + 4, SO_END_FACE_INDEX);
340 }
341
342 auto mat = new SoMaterial;
343 mat->diffuseColor.setNum(intFields);
344 for (std::size_t k = 0; k < numFields; k++) {
345 Base::Color col = legend.getColor(k);
346 mat->diffuseColor.set1Value(k, col.r, col.g, col.b);
347 }
348
349 auto matBinding = new SoMaterialBinding;
350 matBinding->value = SoMaterialBinding::PER_FACE;
351
352 // first clear the children
353 if (getNumChildren() > 0) {
354 coinRemoveAllChildren(this);
355 }
356 addChild(labelGroup);
357 addChild(valueGroup);
358 addChild(coords);
359 addChild(mat);
360 addChild(matBinding);
361 addChild(faceset);
362 }