QgsSingleBandPseudoColorRenderer* QgsSingleBandPseudoColorRenderer::clone() const { QgsRasterShader *shader = nullptr; if ( mShader ) { shader = new QgsRasterShader( mShader->minimumValue(), mShader->maximumValue() ); // Shader function const QgsColorRampShader* origColorRampShader = dynamic_cast<const QgsColorRampShader*>( mShader->rasterShaderFunction() ); if ( origColorRampShader ) { QgsColorRampShader * colorRampShader = new QgsColorRampShader( mShader->minimumValue(), mShader->maximumValue() ); if ( origColorRampShader->sourceColorRamp() ) { colorRampShader->setSourceColorRamp( origColorRampShader->sourceColorRamp()->clone() ); } colorRampShader->setColorRampType( origColorRampShader->colorRampType() ); colorRampShader->setClip( origColorRampShader->clip() ); colorRampShader->setColorRampItemList( origColorRampShader->colorRampItemList() ); shader->setRasterShaderFunction( colorRampShader ); } } QgsSingleBandPseudoColorRenderer * renderer = new QgsSingleBandPseudoColorRenderer( nullptr, mBand, shader ); renderer->copyCommonProperties( this ); return renderer; }
void QgsRasterShader::readXML( const QDomElement& elem ) { //only colorrampshader QDomElement colorRampShaderElem = elem.firstChildElement( "colorrampshader" ); if ( !colorRampShaderElem.isNull() ) { QgsColorRampShader* colorRampShader = new QgsColorRampShader(); colorRampShader->setColorRampType( colorRampShaderElem.attribute( "colorRampType", "INTERPOLATED" ) ); colorRampShader->setClip( colorRampShaderElem.attribute( "clip", "0" ) == "1" ); QList<QgsColorRampShader::ColorRampItem> itemList; QDomElement itemElem; QString itemLabel; double itemValue; QColor itemColor; QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( "item" ); for ( int i = 0; i < itemNodeList.size(); ++i ) { itemElem = itemNodeList.at( i ).toElement(); itemValue = itemElem.attribute( "value" ).toDouble(); itemLabel = itemElem.attribute( "label" ); itemColor.setNamedColor( itemElem.attribute( "color" ) ); itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) ); } colorRampShader->setColorRampItemList( itemList ); setRasterShaderFunction( colorRampShader ); } }
QgsRasterRenderer* QgsSingleBandPseudoColorRendererWidget::renderer() { QgsRasterShader* rasterShader = new QgsRasterShader(); QgsColorRampShader* colorRampShader = new QgsColorRampShader(); colorRampShader->setClip( mClipCheckBox->isChecked() ); //iterate through mColormapTreeWidget and set colormap info of layer QList<QgsColorRampShader::ColorRampItem> colorRampItems; int topLevelItemCount = mColormapTreeWidget->topLevelItemCount(); QTreeWidgetItem* currentItem; for ( int i = 0; i < topLevelItemCount; ++i ) { currentItem = mColormapTreeWidget->topLevelItem( i ); if ( !currentItem ) { continue; } QgsColorRampShader::ColorRampItem newColorRampItem; newColorRampItem.value = currentItem->text( 0 ).toDouble(); newColorRampItem.color = currentItem->background( 1 ).color(); newColorRampItem.label = currentItem->text( 2 ); colorRampItems.append( newColorRampItem ); } // sort the shader items qSort( colorRampItems ); colorRampShader->setColorRampItemList( colorRampItems ); if ( mColorInterpolationComboBox->currentText() == tr( "Linear" ) ) { colorRampShader->setColorRampType( QgsColorRampShader::INTERPOLATED ); } else if ( mColorInterpolationComboBox->currentText() == tr( "Discrete" ) ) { colorRampShader->setColorRampType( QgsColorRampShader::DISCRETE ); } else { colorRampShader->setColorRampType( QgsColorRampShader::EXACT ); } rasterShader->setRasterShaderFunction( colorRampShader ); int bandNumber = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt(); QgsSingleBandPseudoColorRenderer *renderer = new QgsSingleBandPseudoColorRenderer( mRasterLayer->dataProvider(), bandNumber, rasterShader ); renderer->setClassificationMin( lineEditValue( mMinLineEdit ) ); renderer->setClassificationMax( lineEditValue( mMaxLineEdit ) ); renderer->setClassificationMinMaxOrigin( mMinMaxOrigin ); return renderer; }
void QgsRasterShader::readXml( const QDomElement& elem ) { //only colorrampshader QDomElement colorRampShaderElem = elem.firstChildElement( QStringLiteral( "colorrampshader" ) ); if ( !colorRampShaderElem.isNull() ) { QgsColorRampShader* colorRampShader = new QgsColorRampShader(); // try to load color ramp (optional) QDomElement sourceColorRampElem = colorRampShaderElem.firstChildElement( QStringLiteral( "colorramp" ) ); if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral( "name" ) ) == QLatin1String( "[source]" ) ) { colorRampShader->setSourceColorRamp( QgsSymbolLayerUtils::loadColorRamp( sourceColorRampElem ) ); } colorRampShader->setColorRampType( colorRampShaderElem.attribute( QStringLiteral( "colorRampType" ), QStringLiteral( "INTERPOLATED" ) ) ); colorRampShader->setClassificationMode( static_cast< QgsColorRampShader::ClassificationMode >( colorRampShaderElem.attribute( QStringLiteral( "classificationMode" ), QStringLiteral( "1" ) ).toInt() ) ); colorRampShader->setClip( colorRampShaderElem.attribute( QStringLiteral( "clip" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) ); QList<QgsColorRampShader::ColorRampItem> itemList; QDomElement itemElem; QString itemLabel; double itemValue; QColor itemColor; QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( QStringLiteral( "item" ) ); itemList.reserve( itemNodeList.size() ); for ( int i = 0; i < itemNodeList.size(); ++i ) { itemElem = itemNodeList.at( i ).toElement(); itemValue = itemElem.attribute( QStringLiteral( "value" ) ).toDouble(); itemLabel = itemElem.attribute( QStringLiteral( "label" ) ); itemColor.setNamedColor( itemElem.attribute( QStringLiteral( "color" ) ) ); itemColor.setAlpha( itemElem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "255" ) ).toInt() ); itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) ); } colorRampShader->setColorRampItemList( itemList ); setRasterShaderFunction( colorRampShader ); } }