| 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/SoFont.h> |
| 30 |
#include <Inventor/nodes/SoIndexedFaceSet.h> |
| 31 |
#include <Inventor/nodes/SoMaterial.h> |
| 32 |
#include <Inventor/nodes/SoText2.h> |
| 33 |
#include <Inventor/nodes/SoTransform.h> |
| 34 |
#include <Inventor/nodes/SoTransparencyType.h> |
| 35 |
|
| 36 |
|
| 37 |
#include <Base/Parameter.h> |
| 38 |
#include "SoFCColorGradient.h" |
| 39 |
#include "SoTextLabel.h" |
| 40 |
#include "Dialogs/DlgSettingsColorGradientImp.h" |
| 41 |
#include "MainWindow.h" |
| 42 |
#include "MDIView.h" |
| 43 |
#include "ViewProvider.h" |
| 44 |
|
| 45 |
|
| 46 |
using namespace Gui; |
| 47 |
|
| 48 |
constexpr const float xMin = 5.0F; |
| 49 |
constexpr const float xMax = 5.5F; |
| 50 |
constexpr const float yMin = -4.0F; |
| 51 |
constexpr const float yMax = 4.0F; |
| 52 |
constexpr const float spaceX = 0.1F; |
| 53 |
constexpr const float spaceY = 0.05F; |
| 54 |
constexpr const float defaultMin = -0.5F; |
| 55 |
constexpr const float defaultMax = 0.5F; |
| 56 |
constexpr const float upperLimit = 10000.0F; |
| 57 |
|
| 58 |
|
| 59 |
SO_NODE_SOURCE(SoFCColorGradient) |
| 60 |
|
| 61 |
/*! |
| 62 |
Constructor. |
| 63 |
*/ |
| 64 |
SoFCColorGradient::SoFCColorGradient() |
| 65 |
: _bbox(xMin, yMin, xMax, yMax) |
| 66 |
{ |
| 67 |
SO_NODE_CONSTRUCTOR(SoFCColorGradient); |
| 68 |
coords = new SoCoordinate3; |
| 69 |
coords->ref(); |
| 70 |
labels = new SoSeparator; |
| 71 |
labels->ref(); |
| 72 |
|
| 73 |
_cColGrad.setStyle(App::ColorBarStyle::FLOW); |
| 74 |
setColorModel(0); |
| 75 |
SoFCColorGradient::setRange(defaultMin, defaultMax, 1); |
| 76 |
} |
| 77 |
|
| 78 |
/*! |
| 79 |
Destructor. |
| 80 |
*/ |
| 81 |
SoFCColorGradient::~SoFCColorGradient() |
| 82 |
{ |
| 83 |
// delete THIS; |
| 84 |
coords->unref(); |
| 85 |
labels->unref(); |
| 86 |
} |
| 87 |
|
| 88 |
// doc from parent |
| 89 |
void SoFCColorGradient::initClass() |
| 90 |
{ |
| 91 |
SO_NODE_INIT_CLASS(SoFCColorGradient, SoFCColorBarBase, "Separator"); |
| 92 |
} |
| 93 |
|
| 94 |
void SoFCColorGradient::finish() |
| 95 |
{ |
| 96 |
atexit_cleanup(); |
| 97 |
} |
| 98 |
|
| 99 |
const char* SoFCColorGradient::getColorBarName() const |
| 100 |
{ |
| 101 |
return QT_TRANSLATE_NOOP("QObject", "Color Gradient"); |
| 102 |
} |
| 103 |
|
| 104 |
void SoFCColorGradient::applyFormat(const SoLabelTextFormat& fmt) |
| 105 |
{ |
| 106 |
auto textColor = Base::Color(fmt.textColor); |
| 107 |
|
| 108 |
for (int j = 0; j < labels->getNumChildren(); j++) { |
| 109 |
if (labels->getChild(j)->getTypeId() == SoBaseColor::getClassTypeId()) { |
| 110 |
auto baseColor = static_cast<SoBaseColor*>(labels->getChild(j)); // NOLINT |
| 111 |
baseColor->rgb.setValue(textColor.r, textColor.g, textColor.b); |
| 112 |
} |
| 113 |
else if (labels->getChild(j)->getTypeId() == SoFont::getClassTypeId()) { |
| 114 |
auto font = static_cast<SoFont*>(labels->getChild(j)); // NOLINT |
| 115 |
font->size.setValue(static_cast<float>(fmt.textSize)); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
void SoFCColorGradient::setMarkerLabel(const SoMFString& label) |
| 121 |
{ |
| 122 |
coinRemoveAllChildren(labels); |
| 123 |
|
| 124 |
int num = label.getNum(); |
| 125 |
if (num > 1) { |
| 126 |
SbVec2f maxPt = _bbox.getMax(); |
| 127 |
SbVec2f minPt = _bbox.getMin(); |
| 128 |
float fStep = (maxPt[1] - minPt[1]) / ((float)num - 1); |
| 129 |
auto trans = new SoTransform; |
| 130 |
|
| 131 |
SoLabelTextFormat fmt = getFormat(); |
| 132 |
auto textColor = Base::Color(fmt.textColor); |
| 133 |
auto textFont = new SoFont; |
| 134 |
auto color = new SoBaseColor; |
| 135 |
textFont->name.setValue("Helvetica,Arial,Times New Roman"); |
| 136 |
textFont->size.setValue(static_cast<float>(fmt.textSize)); |
| 137 |
trans->translation.setValue(maxPt[0] + spaceX, maxPt[1] - spaceY + fStep, 0.0F); |
| 138 |
color->rgb.setValue(textColor.r, textColor.g, textColor.b); |
| 139 |
labels->addChild(trans); |
| 140 |
labels->addChild(color); |
| 141 |
labels->addChild(textFont); |
| 142 |
|
| 143 |
for (int i = 0; i < num; i++) { |
| 144 |
auto trans = new SoTransform; |
| 145 |
auto text2 = new SoColorBarLabel; |
| 146 |
trans->translation.setValue(0, -fStep, 0); |
| 147 |
text2->string.setValue(label[i]); |
| 148 |
labels->addChild(trans); |
| 149 |
labels->addChild(text2); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
setModified(); |
| 154 |
} |
| 155 |
|
| 156 |
void SoFCColorGradient::setViewportSize(const SbVec2s& size) |
| 157 |
{ |
| 158 |
float fMinX {}; |
| 159 |
float fMinY {}; |
| 160 |
float fMaxX {}; |
| 161 |
float fMaxY {}; |
| 162 |
float boxWidth = getBounds(size, fMinX, fMinY, fMaxX, fMaxY); |
| 163 |
|
| 164 |
// search for the labels |
| 165 |
int num = 0; |
| 166 |
for (int i = 0; i < labels->getNumChildren(); i++) { |
| 167 |
if (labels->getChild(i)->getTypeId() == SoTransform::getClassTypeId()) { |
| 168 |
num++; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if (num > 2) { |
| 173 |
bool first = true; |
| 174 |
float fStep = (fMaxY - fMinY) / ((float)num - 2); |
| 175 |
|
| 176 |
for (int j = 0; j < labels->getNumChildren(); j++) { |
| 177 |
if (labels->getChild(j)->getTypeId() == SoTransform::getClassTypeId()) { |
| 178 |
auto transform = static_cast<SoTransform*>(labels->getChild(j)); // NOLINT |
| 179 |
if (first) { |
| 180 |
first = false; |
| 181 |
transform->translation |
| 182 |
.setValue(fMaxX + spaceX - boxWidth, fMaxY - spaceY + fStep, 0.0F); |
| 183 |
} |
| 184 |
else { |
| 185 |
transform->translation.setValue(0, -fStep, 0.0F); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// gradient bar is shifted to the left by width of the labels to assure that labels are fully visible |
| 192 |
_bbox.setBounds(fMinX - boxWidth, fMinY, fMaxX - boxWidth, fMaxY); |
| 193 |
modifyPoints(_bbox); |
| 194 |
} |
| 195 |
|
| 196 |
namespace |
| 197 |
{ |
| 198 |
bool isScientific(float fMin, float fMax, int prec, int numColors) |
| 199 |
{ |
| 200 |
const float base10 = 10.0F; |
| 201 |
float eps = std::pow(base10, static_cast<float>(-prec)); |
| 202 |
float value_min = std::min<float>(fabs(fMin), fabs(fMax)); |
| 203 |
float value_max = std::max<float>(fabs(fMin), fabs(fMax)); |
| 204 |
|
| 205 |
if (value_min < eps && value_min > 0.0F) { |
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
if ((value_max - value_min) < eps * static_cast<float>(numColors - 1)) { |
| 210 |
return true; |
| 211 |
} |
| 212 |
|
| 213 |
return value_max > upperLimit; |
| 214 |
} |
| 215 |
|
| 216 |
std::ios::fmtflags getFormatFlags(float fMin, float fMax, int prec, int numColors) |
| 217 |
{ |
| 218 |
bool scientific = isScientific(fMin, fMax, prec, numColors); |
| 219 |
std::ios::fmtflags flags = scientific |
| 220 |
? (std::ios::scientific | std::ios::showpoint | std::ios::showpos) |
| 221 |
: (std::ios::fixed | std::ios::showpoint | std::ios::showpos); |
| 222 |
return flags; |
| 223 |
} |
| 224 |
|
| 225 |
std::string getLabelText(float value, int prec, std::ios::fmtflags flags) |
| 226 |
{ |
| 227 |
std::stringstream str; |
| 228 |
str.precision(prec); |
| 229 |
str.setf(flags); |
| 230 |
str << value; |
| 231 |
return str.str(); |
| 232 |
} |
| 233 |
} // namespace |
| 234 |
|
| 235 |
void SoFCColorGradient::setRange(float fMin, float fMax, int prec) |
| 236 |
{ |
| 237 |
_cColGrad.setRange(fMin, fMax); |
| 238 |
int numColors = static_cast<int>(_cColGrad.getCountColors()); |
| 239 |
|
| 240 |
SoMFString label; |
| 241 |
std::ios::fmtflags flags = getFormatFlags(fMin, fMax, prec, numColors); |
| 242 |
|
| 243 |
// write the labels |
| 244 |
int i = 0; |
| 245 |
std::vector<float> marks = getMarkerValues(fMin, fMax, numColors); |
| 246 |
for (auto it : marks) { |
| 247 |
std::string text = getLabelText(it, prec, flags); |
| 248 |
label.set1Value(i++, text.c_str()); |
| 249 |
} |
| 250 |
|
| 251 |
setMarkerLabel(label); |
| 252 |
} |
| 253 |
|
| 254 |
bool SoFCColorGradient::isZeroBased(float fMin, float fMax) const |
| 255 |
{ |
| 256 |
return (fMin < 0.0F && fMax > 0.0F && _cColGrad.getStyle() == App::ColorBarStyle::ZERO_BASED); |
| 257 |
} |
| 258 |
|
| 259 |
std::vector<float> SoFCColorGradient::getMarkerValues(float fMin, float fMax, int count) const |
| 260 |
{ |
| 261 |
std::vector<float> labels; |
| 262 |
|
| 263 |
// the middle of the bar is zero |
| 264 |
if (isZeroBased(fMin, fMax)) { |
| 265 |
if (count % 2 == 0) { |
| 266 |
count++; |
| 267 |
} |
| 268 |
int half = count / 2; |
| 269 |
for (int j = 0; j < half + 1; j++) { |
| 270 |
float w = (float)j / ((float)half); |
| 271 |
float fValue = (1.0F - w) * fMax; |
| 272 |
labels.push_back(fValue); |
| 273 |
} |
| 274 |
for (int k = half + 1; k < count; k++) { |
| 275 |
float w = (float)(k - half + 1) / ((float)(count - half)); |
| 276 |
float fValue = w * fMin; |
| 277 |
labels.push_back(fValue); |
| 278 |
} |
| 279 |
} |
| 280 |
else { // either not zero based or 0 is not in between [fMin,fMax] |
| 281 |
for (int j = 0; j < count; j++) { |
| 282 |
float w = (float)j / ((float)count - 1.0F); |
| 283 |
float fValue = (1.0F - w) * fMax + w * fMin; |
| 284 |
labels.push_back(fValue); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
return labels; |
| 289 |
} |
| 290 |
|
| 291 |
void SoFCColorGradient::modifyPoints(const SbBox2f& box) |
| 292 |
{ |
| 293 |
float fMinX = box.getMin()[0]; |
| 294 |
float fMinY = box.getMin()[1]; |
| 295 |
float fMaxX = box.getMax()[0]; |
| 296 |
float fMaxY = box.getMax()[1]; |
| 297 |
|
| 298 |
// set the vertices spanning the faces for the color gradient |
| 299 |
int intFields = coords->point.getNum() / 2; |
| 300 |
for (int i = 0; i < intFields; i++) { |
| 301 |
float w = static_cast<float>(i) / static_cast<float>(intFields - 1); |
| 302 |
float fPosY = (1.0F - w) * fMaxY + w * fMinY; |
| 303 |
coords->point.set1Value(2 * i, fMinX, fPosY, 0.0F); |
| 304 |
coords->point.set1Value(2 * i + 1, fMaxX, fPosY, 0.0F); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
void SoFCColorGradient::setColorModel(std::size_t index) |
| 309 |
{ |
| 310 |
_cColGrad.setColorModel(index); |
| 311 |
rebuildGradient(); |
| 312 |
} |
| 313 |
|
| 314 |
void SoFCColorGradient::setColorStyle(App::ColorBarStyle tStyle) |
| 315 |
{ |
| 316 |
_cColGrad.setStyle(tStyle); |
| 317 |
rebuildGradient(); |
| 318 |
} |
| 319 |
|
| 320 |
SoIndexedFaceSet* SoFCColorGradient::createFaceSet(int numFaces) const |
| 321 |
{ |
| 322 |
// NOLINTBEGIN |
| 323 |
// for numFaces colors we need 2*(numFaces-1) faces and therefore an array with |
| 324 |
// 8*(numFaces-1) face indices |
| 325 |
auto faceset = new SoIndexedFaceSet; |
| 326 |
faceset->coordIndex.setNum(8 * (numFaces - 1)); |
| 327 |
for (int j = 0; j < numFaces - 1; j++) { |
| 328 |
faceset->coordIndex.set1Value(8 * j, 2 * j); |
| 329 |
faceset->coordIndex.set1Value(8 * j + 1, 2 * j + 3); |
| 330 |
faceset->coordIndex.set1Value(8 * j + 2, 2 * j + 1); |
| 331 |
faceset->coordIndex.set1Value(8 * j + 3, SO_END_FACE_INDEX); |
| 332 |
faceset->coordIndex.set1Value(8 * j + 4, 2 * j); |
| 333 |
faceset->coordIndex.set1Value(8 * j + 5, 2 * j + 2); |
| 334 |
faceset->coordIndex.set1Value(8 * j + 6, 2 * j + 3); |
| 335 |
faceset->coordIndex.set1Value(8 * j + 7, SO_END_FACE_INDEX); |
| 336 |
} |
| 337 |
// NOLINTEND |
| 338 |
|
| 339 |
return faceset; |
| 340 |
} |
| 341 |
|
| 342 |
SoTransparencyType* SoFCColorGradient::createTransparencyType() const |
| 343 |
{ |
| 344 |
auto ttype = new SoTransparencyType; |
| 345 |
ttype->value = SoGLRenderAction::DELAYED_BLEND; |
| 346 |
return ttype; |
| 347 |
} |
| 348 |
|
| 349 |
SoMaterial* SoFCColorGradient::createMaterial() const |
| 350 |
{ |
| 351 |
App::ColorModel model = _cColGrad.getColorModel(); |
| 352 |
int numColors = static_cast<int>(model.getCountColors()); |
| 353 |
|
| 354 |
auto mat = new SoMaterial; |
| 355 |
mat->diffuseColor.setNum(2 * numColors); |
| 356 |
for (int k = 0; k < numColors; k++) { |
| 357 |
Base::Color col = model.colors[numColors - k - 1]; |
| 358 |
mat->diffuseColor.set1Value(2 * k, col.r, col.g, col.b); |
| 359 |
mat->diffuseColor.set1Value(2 * k + 1, col.r, col.g, col.b); |
| 360 |
} |
| 361 |
|
| 362 |
return mat; |
| 363 |
} |
| 364 |
|
| 365 |
SoMaterialBinding* SoFCColorGradient::createMaterialBinding() const |
| 366 |
{ |
| 367 |
auto matBinding = new SoMaterialBinding; |
| 368 |
matBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED; |
| 369 |
return matBinding; |
| 370 |
} |
| 371 |
|
| 372 |
int SoFCColorGradient::getNumColors() const |
| 373 |
{ |
| 374 |
App::ColorModel model = _cColGrad.getColorModel(); |
| 375 |
return static_cast<int>(model.getCountColors()); |
| 376 |
} |
| 377 |
|
| 378 |
void SoFCColorGradient::setCoordSize(int numPoints) |
| 379 |
{ |
| 380 |
coords->point.setNum(numPoints); |
| 381 |
} |
| 382 |
|
| 383 |
void SoFCColorGradient::rebuildGradient() |
| 384 |
{ |
| 385 |
int numColors = getNumColors(); |
| 386 |
|
| 387 |
setCoordSize(2 * numColors); |
| 388 |
modifyPoints(_bbox); |
| 389 |
|
| 390 |
auto faceset = createFaceSet(numColors); |
| 391 |
auto ttype = createTransparencyType(); |
| 392 |
auto mat = createMaterial(); |
| 393 |
auto matBinding = createMaterialBinding(); |
| 394 |
|
| 395 |
// first clear the children |
| 396 |
if (getNumChildren() > 0) { |
| 397 |
coinRemoveAllChildren(this); |
| 398 |
} |
| 399 |
|
| 400 |
addChild(ttype); |
| 401 |
addChild(labels); |
| 402 |
addChild(coords); |
| 403 |
addChild(mat); |
| 404 |
addChild(matBinding); |
| 405 |
addChild(faceset); |
| 406 |
} |
| 407 |
|
| 408 |
bool SoFCColorGradient::isVisible(float fVal) const |
| 409 |
{ |
| 410 |
if (_cColGrad.isOutsideInvisible()) { |
| 411 |
return !_cColGrad.isOutOfRange(fVal); |
| 412 |
} |
| 413 |
|
| 414 |
return true; |
| 415 |
} |
| 416 |
|
| 417 |
void SoFCColorGradient::customize(SoFCColorBarBase* parentNode) |
| 418 |
{ |
| 419 |
QWidget* parent = Gui::getMainWindow()->activeWindow(); |
| 420 |
Gui::Dialog::DlgSettingsColorGradientImp dlg(_cColGrad, parent); |
| 421 |
App::ColorGradientProfile profile = _cColGrad.getProfile(); |
| 422 |
dlg.setNumberOfDecimals(_precision, profile.fMin, profile.fMax); |
| 423 |
|
| 424 |
QPoint pos(QCursor::pos()); |
| 425 |
pos += QPoint(int(-1.1 * dlg.width()), int(-0.1 * dlg.height())); // NOLINT |
| 426 |
dlg.move(pos); |
| 427 |
|
| 428 |
auto applyProfile = [&](const App::ColorGradientProfile& pro, int precision) { |
| 429 |
_cColGrad.setProfile(pro); |
| 430 |
setRange(pro.fMin, pro.fMax, precision); |
| 431 |
rebuildGradient(); |
| 432 |
|
| 433 |
triggerChange(parentNode); |
| 434 |
}; |
| 435 |
QObject::connect(&dlg, &Gui::Dialog::DlgSettingsColorGradientImp::colorModelChanged, [&] { |
| 436 |
try { |
| 437 |
applyProfile(dlg.getProfile(), dlg.numberOfDecimals()); |
| 438 |
} |
| 439 |
catch (const Base::Exception& e) { |
| 440 |
e.reportException(); |
| 441 |
} |
| 442 |
}); |
| 443 |
|
| 444 |
if (dlg.exec() != QDialog::Accepted) { |
| 445 |
int decimals = dlg.numberOfDecimals(); |
| 446 |
if (!profile.isEqual(dlg.getProfile()) || decimals != _precision) { |
| 447 |
applyProfile(profile, _precision); |
| 448 |
} |
| 449 |
} |
| 450 |
else { |
| 451 |
_precision = dlg.numberOfDecimals(); |
| 452 |
} |
| 453 |
} |