Skip to content
Permalink
Browse files

[symbology] migrate graduated renderer color ramp widget (#3815)

  • Loading branch information
nirvn committed Nov 30, 2016
1 parent 9bb9b58 commit ae75e45d80568f3ca9b708ba9b4d449ca0de3e25
@@ -197,14 +197,23 @@ class QgsGraduatedSymbolRenderer : QgsFeatureRenderer
//! @note Added in 2.6
void calculateLabelPrecision( bool updateRanges = true );

/** Creates a new graduated renderer.
* @param vlayer vector layer
* @param attrName attribute to classify
* @param classes number of classes
* @param mode classification mode
* @param symbol base symbol
* @param ramp color ramp for classes
* @param legendFormat
* @returns new QgsGraduatedSymbolRenderer object
*/
static QgsGraduatedSymbolRenderer* createRenderer(
QgsVectorLayer* vlayer,
const QString& attrName,
int classes,
Mode mode,
QgsSymbol* symbol /Transfer/,
QgsColorRamp* ramp /Transfer/,
bool inverted = false,
const QgsRendererRangeLabelFormat& legendFormat = QgsRendererRangeLabelFormat()
);

@@ -252,16 +261,11 @@ class QgsGraduatedSymbolRenderer : QgsFeatureRenderer
*/
void setSourceColorRamp( QgsColorRamp* ramp /Transfer/ );

//! @note added in 2.1
bool invertedColorRamp();
void setInvertedColorRamp( bool inverted );

/** Update the color ramp used. Also updates all symbols colors.
* Doesn't alter current breaks.
* @param ramp color ramp. Ownership is transferred to the renderer
* @param inverted set to true to invert ramp colors
*/
void updateColorRamp( QgsColorRamp* ramp /Transfer/ = 0, bool inverted = false );
void updateColorRamp( QgsColorRamp* ramp /Transfer/ = 0 );

/** Update all the symbols but leave breaks and colors. This method also sets the source
* symbol for the renderer.
@@ -288,7 +288,6 @@ QgsGraduatedSymbolRenderer::QgsGraduatedSymbolRenderer( const QString& attrName,
: QgsFeatureRenderer( QStringLiteral( "graduatedSymbol" ) )
, mAttrName( attrName )
, mMode( Custom )
, mInvertedColorRamp( false )
, mGraduatedMethod( GraduatedColor )
, mAttrNum( -1 )
, mCounting( false )
@@ -501,7 +500,6 @@ QgsGraduatedSymbolRenderer* QgsGraduatedSymbolRenderer::clone() const
if ( mSourceColorRamp.data() )
{
r->setSourceColorRamp( mSourceColorRamp->clone() );
r->setInvertedColorRamp( mInvertedColorRamp );
}
r->setUsingSymbolLevels( usingSymbolLevels() );
r->setLabelFormat( labelFormat() );
@@ -790,15 +788,13 @@ QgsGraduatedSymbolRenderer* QgsGraduatedSymbolRenderer::createRenderer(
Mode mode,
QgsSymbol* symbol,
QgsColorRamp* ramp,
bool inverted,
const QgsRendererRangeLabelFormat& labelFormat
)
{
QgsRangeList ranges;
QgsGraduatedSymbolRenderer* r = new QgsGraduatedSymbolRenderer( attrName, ranges );
r->setSourceSymbol( symbol->clone() );
r->setSourceColorRamp( ramp->clone() );
r->setInvertedColorRamp( inverted );
r->setMode( mode );
r->setLabelFormat( labelFormat );
r->updateClasses( vlayer, mode, classes );
@@ -916,7 +912,7 @@ void QgsGraduatedSymbolRenderer::updateClasses( QgsVectorLayer* vlayer, Mode mod
QgsSymbol* newSymbol = mSourceSymbol ? mSourceSymbol->clone() : QgsSymbol::defaultSymbol( vlayer->geometryType() );
addClass( QgsRendererRange( lower, upper, newSymbol, label ) );
}
updateColorRamp( nullptr, mInvertedColorRamp );
updateColorRamp( nullptr );
}

QgsFeatureRenderer* QgsGraduatedSymbolRenderer::create( QDomElement& element )
@@ -985,9 +981,6 @@ QgsFeatureRenderer* QgsGraduatedSymbolRenderer::create( QDomElement& element )
if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral( "name" ) ) == QLatin1String( "[source]" ) )
{
r->setSourceColorRamp( QgsSymbolLayerUtils::loadColorRamp( sourceColorRampElem ) );
QDomElement invertedColorRampElem = element.firstChildElement( QStringLiteral( "invertedcolorramp" ) );
if ( !invertedColorRampElem.isNull() )
r->setInvertedColorRamp( invertedColorRampElem.attribute( QStringLiteral( "value" ) ) == QLatin1String( "1" ) );
}

// try to load mode
@@ -1098,9 +1091,6 @@ QDomElement QgsGraduatedSymbolRenderer::save( QDomDocument& doc )
{
QDomElement colorRampElem = QgsSymbolLayerUtils::saveColorRamp( QStringLiteral( "[source]" ), mSourceColorRamp.data(), doc );
rendererElem.appendChild( colorRampElem );
QDomElement invertedElem = doc.createElement( QStringLiteral( "invertedcolorramp" ) );
invertedElem.setAttribute( QStringLiteral( "value" ), mInvertedColorRamp );
rendererElem.appendChild( invertedElem );
}

// save mode
@@ -1306,13 +1296,12 @@ void QgsGraduatedSymbolRenderer::setSymbolSizes( double minSize, double maxSize
}
}

void QgsGraduatedSymbolRenderer::updateColorRamp( QgsColorRamp *ramp, bool inverted )
void QgsGraduatedSymbolRenderer::updateColorRamp( QgsColorRamp *ramp )
{
int i = 0;
if ( ramp )
{
setSourceColorRamp( ramp );
setInvertedColorRamp( inverted );
}

if ( mSourceColorRamp )
@@ -1323,10 +1312,7 @@ void QgsGraduatedSymbolRenderer::updateColorRamp( QgsColorRamp *ramp, bool inver
if ( symbol )
{
double colorValue;
if ( inverted )
colorValue = ( mRanges.count() > 1 ? static_cast< double >( mRanges.count() - i - 1 ) / ( mRanges.count() - 1 ) : 0 );
else
colorValue = ( mRanges.count() > 1 ? static_cast< double >( i ) / ( mRanges.count() - 1 ) : 0 );
colorValue = ( mRanges.count() > 1 ? static_cast< double >( i ) / ( mRanges.count() - 1 ) : 0 );
symbol->setColor( mSourceColorRamp->color( colorValue ) );
}
updateRangeSymbol( i, symbol );
@@ -1441,7 +1427,7 @@ void QgsGraduatedSymbolRenderer::addBreak( double breakValue, bool updateSymbols
switch ( mGraduatedMethod )
{
case GraduatedColor:
updateColorRamp( mSourceColorRamp.data(), mInvertedColorRamp );
updateColorRamp( mSourceColorRamp.data() );
break;
case GraduatedSize:
setSymbolSizes( minSymbolSize(), maxSymbolSize() );
@@ -235,7 +235,6 @@ class CORE_EXPORT QgsGraduatedSymbolRenderer : public QgsFeatureRenderer
* @param mode classification mode
* @param symbol base symbol
* @param ramp color ramp for classes
* @param inverted set to true to invert color ramp
* @param legendFormat
* @returns new QgsGraduatedSymbolRenderer object
*/
@@ -246,7 +245,6 @@ class CORE_EXPORT QgsGraduatedSymbolRenderer : public QgsFeatureRenderer
Mode mode,
QgsSymbol* symbol,
QgsColorRamp* ramp,
bool inverted = false,
const QgsRendererRangeLabelFormat& legendFormat = QgsRendererRangeLabelFormat()
);

@@ -285,16 +283,11 @@ class CORE_EXPORT QgsGraduatedSymbolRenderer : public QgsFeatureRenderer
*/
void setSourceColorRamp( QgsColorRamp* ramp );

//! @note added in 2.1
bool invertedColorRamp() { return mInvertedColorRamp; }
void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }

/** Update the color ramp used. Also updates all symbols colors.
* Doesn't alter current breaks.
* @param ramp color ramp. Ownership is transferred to the renderer
* @param inverted set to true to invert ramp colors
*/
void updateColorRamp( QgsColorRamp* ramp = nullptr, bool inverted = false );
void updateColorRamp( QgsColorRamp* ramp = nullptr );

/** Update all the symbols but leave breaks and colors. This method also sets the source
* symbol for the renderer.
@@ -343,7 +336,6 @@ class CORE_EXPORT QgsGraduatedSymbolRenderer : public QgsFeatureRenderer
Mode mMode;
QScopedPointer<QgsSymbol> mSourceSymbol;
QScopedPointer<QgsColorRamp> mSourceColorRamp;
bool mInvertedColorRamp;
QgsRendererRangeLabelFormat mLabelFormat;

QScopedPointer<QgsExpression> mExpression;
@@ -226,7 +226,7 @@ QPixmap QgsColorRampButton::createMenuIcon( QgsColorRamp* colorramp )

void QgsColorRampButton::buttonClicked()
{
if ( !isRandomColorRamp() )
if ( !isRandomColorRamp() && !isNull() )
{
showColorRampDialog();
}
@@ -241,7 +241,7 @@ void QgsColorRampButton::prepareMenu()
mMenu->clear();

QAction* invertAction = new QAction( tr( "Invert color ramp" ), this );
invertAction->setEnabled( !isRandomColorRamp() );
invertAction->setEnabled( !isNull() && !isRandomColorRamp() );
mMenu->addAction( invertAction );
connect( invertAction, &QAction::triggered, this, &QgsColorRampButton::invertColorRamp );

@@ -317,6 +317,7 @@ void QgsColorRampButton::prepareMenu()
mMenu->addAction( newColorRampAction );

QAction* editColorRampAction = new QAction( tr( "Edit color ramp..." ), this );
editColorRampAction->setEnabled( !isNull() && !isRandomColorRamp() );
connect( editColorRampAction, &QAction::triggered, this, &QgsColorRampButton::showColorRampDialog );
mMenu->addAction( editColorRampAction );
}
@@ -421,6 +421,12 @@ QgsCategorizedSymbolRendererWidget::QgsCategorizedSymbolRendererWidget( QgsVecto
{
btnColorRamp->setColorRampFromName( defaultColorRamp );
}
else
{
QgsColorRamp* ramp = new QgsGradientColorRamp( QColor( 255, 255, 255 ), QColor( 255, 0, 0 ) );
btnColorRamp->setColorRamp( ramp );
delete ramp;
}

mCategorizedSymbol = QgsSymbol::defaultSymbol( mLayer->geometryType() );

0 comments on commit ae75e45

Please sign in to comment.
You can’t perform that action at this time.