diff --git a/src/Gui/SoFCColorGradient.cpp b/src/Gui/SoFCColorGradient.cpp index e851706884..5416f5cdbb 100644 --- a/src/Gui/SoFCColorGradient.cpp +++ b/src/Gui/SoFCColorGradient.cpp @@ -48,7 +48,7 @@ SO_NODE_SOURCE(SoFCColorGradient) /*! Constructor. */ -SoFCColorGradient::SoFCColorGradient() : _fMaxX(4.5f), _fMinX(4.0f), _fMaxY(4.0f), _fMinY(-4.0f), _bOutInvisible(false), _precision(3) +SoFCColorGradient::SoFCColorGradient() : _bbox(4.0f, -4.0f, 4.5f, 4.0f), _bOutInvisible(false), _precision(3) { SO_NODE_CONSTRUCTOR(SoFCColorGradient); coords = new SoCoordinate3; @@ -86,24 +86,21 @@ void SoFCColorGradient::setMarkerLabel( const SoMFString& label ) { coinRemoveAllChildren(labels); - float fH=8.0f; int num = label.getNum(); - if ( num > 1 ) - { - float fStep = fH / ((float)num-1); + if (num > 1) { + float fStep = 8.0f / ((float)num-1); SoTransform* trans = new SoTransform; - trans->translation.setValue(_fMaxX+0.1f,_fMaxY-0.05f+fStep,0.0f); + trans->translation.setValue(_bbox.getMax()[0] + 0.1f, _bbox.getMax()[1] - 0.05f + fStep, 0.0f); labels->addChild(trans); - for ( int i=0; itranslation.setValue(0,-fStep,0); - color->rgb.setValue(0,0,0); - text2->string.setValue( label[i] ); + trans->translation.setValue(0, -fStep, 0); + color->rgb.setValue(0, 0, 0); + text2->string.setValue(label[i]); labels->addChild(trans); labels->addChild(color); labels->addChild(text2); @@ -118,61 +115,41 @@ void SoFCColorGradient::setViewportSize( const SbVec2s& size ) float fMinX= 4.0f, fMaxX=4.5f; float fMinY= -4.0f, fMaxY=4.0f; - if ( fRatio > 1.0f ) - { + if (fRatio > 1.0f) { fMinX = 4.0f * fRatio; fMaxX = fMinX+0.5f; } - else if ( fRatio < 1.0f ) - { - fMinY = -4.0f / fRatio; - fMaxY = 4.0f / fRatio; + else if (fRatio < 1.0f) { + fMinY = -4.0f / fRatio; + fMaxY = 4.0f / fRatio; } - _fMaxX = fMaxX; - _fMinX = fMinX; - _fMaxY = fMaxY; - _fMinY = fMinY; - // search for the labels int num=0; - for ( int i=0; igetNumChildren(); i++ ) - { - if ( labels->getChild(i)->getTypeId() == SoTransform::getClassTypeId() ) + for (int i=0; igetNumChildren(); i++) { + if (labels->getChild(i)->getTypeId() == SoTransform::getClassTypeId()) num++; } - if ( num > 2 ) - { + if (num > 2) { bool first=true; float fStep = (fMaxY-fMinY) / ((float)num-2); - for ( int j=0; jgetNumChildren(); j++ ) - { - if ( labels->getChild(j)->getTypeId() == SoTransform::getClassTypeId() ) - { - if ( first ) - { + for (int j=0; jgetNumChildren(); j++) { + if (labels->getChild(j)->getTypeId() == SoTransform::getClassTypeId()) { + if (first) { first = false; static_cast(labels->getChild(j))->translation.setValue(fMaxX+0.1f,fMaxY-0.05f+fStep,0.0f); } - else - { + else { static_cast(labels->getChild(j))->translation.setValue(0,-fStep,0.0f); } } } } - // set the vertices spanning the faces for the color gradient - int ct = coords->point.getNum()/2; - for ( int j=0; jpoint.set1Value(2*j, _fMinX, fPosY, 0.0f); - coords->point.set1Value(2*j+1, _fMaxX, fPosY, 0.0f); - } + _bbox.setBounds(fMinX, fMinY, fMaxX, fMaxY); + modifyPoints(_bbox); } void SoFCColorGradient::setRange(float fMin, float fMax, int prec) @@ -204,27 +181,23 @@ std::vector SoFCColorGradient::getMarkerValues(float fMin, float fMax, in std::vector labels; // the middle of the bar is zero - if ( fMin < 0.0f && fMax > 0.0f && _cColGrad.getStyle() == App::ColorGradient::ZERO_BASED ) - { - if ( count % 2 == 0) count++; + if (fMin < 0.0f && fMax > 0.0f && _cColGrad.getStyle() == App::ColorGradient::ZERO_BASED) { + if (count % 2 == 0) + count++; int half = count / 2; - for (int j=0; j SoFCColorGradient::getMarkerValues(float fMin, float fMax, in return labels; } +void SoFCColorGradient::modifyPoints(const SbBox2f& box) +{ + float fMinX = box.getMin()[0]; + float fMinY = box.getMin()[1]; + float fMaxX = box.getMax()[0]; + float fMaxY = box.getMax()[1]; + + // set the vertices spanning the faces for the color gradient + int intFields = coords->point.getNum() / 2; + for (int i = 0; i < intFields; i++) { + float w = static_cast(i) / (intFields - 1); + float fPosY = (1.0f - w) * fMaxY + w * fMinY; + coords->point.set1Value(2 * i, fMinX, fPosY, 0.0f); + coords->point.set1Value(2 * i + 1, fMaxX, fPosY, 0.0f); + } +} + void SoFCColorGradient::setColorModel(std::size_t index) { _cColGrad.setColorModel(index); @@ -249,23 +239,16 @@ void SoFCColorGradient::setColorStyle (App::ColorGradient::TStyle tStyle) void SoFCColorGradient::rebuildGradient() { App::ColorModel model = _cColGrad.getColorModel(); - int uCtColors = (int)model.getCountColors(); + int uCtColors = static_cast(model.getCountColors()); - coords->point.setNum(2*uCtColors); - for ( int i=0; ipoint.set1Value(2*i, _fMinX, fPosY, 0.0f); - coords->point.set1Value(2*i+1, _fMaxX, fPosY, 0.0f); - } + coords->point.setNum(2 * uCtColors); + modifyPoints(_bbox); // for uCtColors colors we need 2*(uCtColors-1) facets and therefore an array with // 8*(uCtColors-1) face indices SoIndexedFaceSet * faceset = new SoIndexedFaceSet; - faceset->coordIndex.setNum(8*(uCtColors-1)); - for ( int j=0; jcoordIndex.setNum(8 * (uCtColors - 1)); + for (int j = 0; j < uCtColors - 1; j++) { faceset->coordIndex.set1Value(8*j, 2*j); faceset->coordIndex.set1Value(8*j+1, 2*j+3); faceset->coordIndex.set1Value(8*j+2, 2*j+1); @@ -281,9 +264,8 @@ void SoFCColorGradient::rebuildGradient() ttype->value = SoGLRenderAction::DELAYED_BLEND; SoMaterial* mat = new SoMaterial; //mat->transparency = 0.3f; - mat->diffuseColor.setNum(2*uCtColors); - for ( int k=0; kdiffuseColor.setNum(2 * uCtColors); + for (int k = 0; k < uCtColors; k++) { App::Color col = model.colors[uCtColors-k-1]; mat->diffuseColor.set1Value(2*k, col.r, col.g, col.b); mat->diffuseColor.set1Value(2*k+1, col.r, col.g, col.b); @@ -293,7 +275,7 @@ void SoFCColorGradient::rebuildGradient() matBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED; // first clear the children - if ( getNumChildren() > 0 ) + if (getNumChildren() > 0) coinRemoveAllChildren(this); addChild(ttype); addChild(labels); @@ -305,8 +287,7 @@ void SoFCColorGradient::rebuildGradient() bool SoFCColorGradient::isVisible (float fVal) const { - if (_bOutInvisible == true) - { + if (_bOutInvisible) { float fMin, fMax; _cColGrad.getRange(fMin, fMax); if ((fVal > fMax) || (fVal < fMin)) @@ -323,13 +304,13 @@ bool SoFCColorGradient::customize() QWidget* parent = Gui::getMainWindow()->activeWindow(); Gui::Dialog::DlgSettingsColorGradientImp dlg(parent); - dlg.setColorModelNames( _cColGrad.getColorModelNames() ); - dlg.setColorModel( _cColGrad.getColorModelType() ); - dlg.setColorStyle( _cColGrad.getStyle() ); - dlg.setOutGrayed( _cColGrad.isOutsideGrayed() ); - dlg.setOutInvisible( _bOutInvisible ); - dlg.setNumberOfLabels( _cColGrad.getCountColors() ); - dlg.setNumberOfDecimals( _precision ); + dlg.setColorModelNames(_cColGrad.getColorModelNames()); + dlg.setColorModel(_cColGrad.getColorModelType()); + dlg.setColorStyle(_cColGrad.getStyle()); + dlg.setOutGrayed(_cColGrad.isOutsideGrayed()); + dlg.setOutInvisible(_bOutInvisible); + dlg.setNumberOfLabels(_cColGrad.getCountColors()); + dlg.setNumberOfDecimals(_precision); float fMin, fMax; _cColGrad.getRange(fMin, fMax); dlg.setRange(fMin, fMax); @@ -338,17 +319,16 @@ bool SoFCColorGradient::customize() pos += QPoint((int)(-1.1*dlg.width()),(int)(-0.1*dlg.height())); dlg.move( pos ); - if ( dlg.exec() == QDialog::Accepted ) - { - _cColGrad.setColorModel( dlg.colorModel() ); - _cColGrad.setStyle( dlg.colorStyle() ); - _cColGrad.setOutsideGrayed( dlg.isOutGrayed() ); + if (dlg.exec() == QDialog::Accepted) { + _cColGrad.setColorModel(dlg.colorModel()); + _cColGrad.setStyle(dlg.colorStyle()); + _cColGrad.setOutsideGrayed(dlg.isOutGrayed()); _bOutInvisible = dlg.isOutInvisible(); - _cColGrad.setCountColors( dlg.numberOfLabels() ); + _cColGrad.setCountColors(dlg.numberOfLabels()); _precision = dlg.numberOfDecimals(); - dlg.getRange( fMin, fMax ); + dlg.getRange(fMin, fMax); int dec = dlg.numberOfDecimals(); - setRange( fMin, fMax, dec ); + setRange(fMin, fMax, dec); rebuildGradient(); return true; diff --git a/src/Gui/SoFCColorGradient.h b/src/Gui/SoFCColorGradient.h index 514f47e105..60833a823a 100644 --- a/src/Gui/SoFCColorGradient.h +++ b/src/Gui/SoFCColorGradient.h @@ -25,6 +25,7 @@ #define GUI_SOFCCOLORGRADIENT_H #include +#include #include "SoFCColorBar.h" #include #include @@ -97,11 +98,12 @@ protected: private: /** Sets the new labels. */ void setMarkerLabel( const SoMFString& label ); + void modifyPoints(const SbBox2f&); private: SoCoordinate3* coords; SoSeparator* labels; - float _fMaxX, _fMinX, _fMaxY, _fMinY; + SbBox2f _bbox; bool _bOutInvisible; int _precision; App::ColorGradient _cColGrad;