Skip to content
Permalink
Browse files

[FEATURE][MESH] coloring mesh vector dataset with color ramp shader (#…

…35216)

fixes #33978 Allows user to color the mesh layer vector dataset (arrow, stream lines, traces) with color ramp shader depending on the magnitude of the vector.
  • Loading branch information
vcloarec committed Mar 24, 2020
1 parent 090fdf8 commit 20a7ed44acd68b2a988a4191a175eb64858a7377
@@ -334,7 +334,7 @@ Represents a streamline renderer settings for vector datasets displayed by strea

MeshGridded,

Random,
Random
};

SeedingStartPointsMethod seedingMethod() const;
@@ -448,6 +448,14 @@ Represents a renderer settings for vector datasets
Traces
};

enum ColoringMethod
{
//! Render the vector with a single color
SingleColor,
//! Render the vector with a color ramp
ColorRamp
};


double lineWidth() const;
%Docstring
@@ -534,6 +542,34 @@ Returns the displaying method used to render vector datasets
Sets the displaying method used to render vector datasets

.. versionadded:: 3.12
%End

ColoringMethod coloringMethod() const;
%Docstring
Returns the coloring method used to render vector datasets

.. versionadded:: 3.14
%End

void setColoringMethod( const ColoringMethod &coloringMethod );
%Docstring
Sets the coloring method used to render vector datasets

.. versionadded:: 3.14
%End

QgsColorRampShader colorRampShader() const;
%Docstring
Sets the color ramp shader used to render vector datasets

.. versionadded:: 3.14
%End

void setColorRampShader( const QgsColorRampShader &colorRampShader );
%Docstring
Returns the color ramp shader used to render vector datasets

.. versionadded:: 3.14
%End

QgsMeshRendererVectorArrowSettings arrowSettings() const;
@@ -587,7 +623,6 @@ Writes configuration to a new DOM element
Reads configuration from the given DOM element
%End


};

class QgsMeshRendererSettings
@@ -26,7 +26,19 @@ QgsMeshRendererVectorSettingsWidget::QgsMeshRendererVectorSettingsWidget( QWidge

mShaftLengthComboBox->setCurrentIndex( -1 );

mColoringMethodComboBox->addItem( tr( "Single Color" ), QgsMeshRendererVectorSettings::SingleColor );
mColoringMethodComboBox->addItem( tr( "Color Ramp Shader" ), QgsMeshRendererVectorSettings::ColorRamp );

connect( mColorWidget, &QgsColorButton::colorChanged, this, &QgsMeshRendererVectorSettingsWidget::widgetChanged );
connect( mColoringMethodComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ),
this, &QgsMeshRendererVectorSettingsWidget::onColoringMethodChanged );
connect( mColorRampShaderWidget, &QgsColorRampShaderWidget::widgetChanged,
this, &QgsMeshRendererVectorSettingsWidget::widgetChanged );
connect( mColorRampShaderMinimumEditLine, &QLineEdit::textEdited,
this, &QgsMeshRendererVectorSettingsWidget::onColorRampMinMaxChanged );
connect( mColorRampShaderMaximumEditLine, &QLineEdit::textEdited,
this, &QgsMeshRendererVectorSettingsWidget::onColorRampMinMaxChanged );

connect( mLineWidthSpinBox, qgis::overload<double>::of( &QgsDoubleSpinBox::valueChanged ),
this, &QgsMeshRendererVectorSettingsWidget::widgetChanged );

@@ -38,6 +50,10 @@ QgsMeshRendererVectorSettingsWidget::QgsMeshRendererVectorSettingsWidget( QWidge

connect( mDisplayVectorsOnGridGroupBox, &QGroupBox::toggled, this, &QgsMeshRendererVectorSettingsWidget::widgetChanged );

connect( mColorRampShaderLoadButton, &QPushButton::clicked, this, &QgsMeshRendererVectorSettingsWidget::loadColorRampShader );

onColoringMethodChanged();

QVector<QLineEdit *> widgets;
widgets << mMinMagLineEdit << mMaxMagLineEdit
<< mHeadWidthLineEdit << mHeadLengthLineEdit
@@ -102,6 +118,9 @@ QgsMeshRendererVectorSettings QgsMeshRendererVectorSettingsWidget::settings() co
// basic
settings.setColor( mColorWidget->color() );
settings.setLineWidth( mLineWidthSpinBox->value() );
settings.setColoringMethod( static_cast<QgsMeshRendererVectorSettings::ColoringMethod>
( mColoringMethodComboBox->currentData().toInt() ) );
settings.setColorRampShader( mColorRampShaderWidget->shader() );

// filter by magnitude
double val = filterValue( mMinMagLineEdit->text(), -1 );
@@ -179,6 +198,10 @@ void QgsMeshRendererVectorSettingsWidget::syncToLayer( )
// basic
mColorWidget->setColor( settings.color() );
mLineWidthSpinBox->setValue( settings.lineWidth() );
mColoringMethodComboBox->setCurrentIndex( mColoringMethodComboBox->findData( settings.coloringMethod() ) );
mColorRampShaderWidget->setFromShader( settings.colorRampShader() );
mColorRampShaderMinimumEditLine->setText( QString::number( settings.colorRampShader().minimumValue() ) );
mColorRampShaderMaximumEditLine->setText( QString::number( settings.colorRampShader().maximumValue() ) );

// filter by magnitude
if ( settings.filterMin() > 0 )
@@ -246,6 +269,46 @@ void QgsMeshRendererVectorSettingsWidget::onStreamLineSeedingMethodChanged( int
mDisplayVectorsOnGridGroupBox->setEnabled( !enabled );
}

void QgsMeshRendererVectorSettingsWidget::onColoringMethodChanged()
{
mColorRampShaderGroupBox->setVisible( mColoringMethodComboBox->currentData() == QgsMeshRendererVectorSettings::ColorRamp );
mColorWidget->setVisible( mColoringMethodComboBox->currentData() == QgsMeshRendererVectorSettings::SingleColor );
mSimgleColorLabel->setVisible( mColoringMethodComboBox->currentData() == QgsMeshRendererVectorSettings::SingleColor );

if ( mColorRampShaderWidget->shader().colorRampItemList().isEmpty() )
loadColorRampShader();

emit widgetChanged();
}

void QgsMeshRendererVectorSettingsWidget::onColorRampMinMaxChanged()
{
mColorRampShaderWidget->setMinimumMaximumAndClassify(
filterValue( mColorRampShaderMinimumEditLine->text(), 0 ),
filterValue( mColorRampShaderMaximumEditLine->text(), 0 ) );
}

void QgsMeshRendererVectorSettingsWidget::loadColorRampShader()
{
if ( !mMeshLayer )
return;

QgsMeshDataProvider *provider = mMeshLayer->dataProvider();
int currentVectorDataSetGroupIndex = mMeshLayer->rendererSettings().activeVectorDataset().group();
if ( !provider ||
currentVectorDataSetGroupIndex < 0 ||
!provider->datasetGroupMetadata( currentVectorDataSetGroupIndex ).isVector() )
return;

const QgsMeshDatasetGroupMetadata meta = provider->datasetGroupMetadata( currentVectorDataSetGroupIndex );
double min = meta.minimum();
double max = meta.maximum();

mColorRampShaderWidget->setMinimumMaximumAndClassify( min, max );
whileBlocking( mColorRampShaderMinimumEditLine )->setText( QString::number( min ) );
whileBlocking( mColorRampShaderMaximumEditLine )->setText( QString::number( max ) );
}

double QgsMeshRendererVectorSettingsWidget::filterValue( const QString &text, double errVal ) const
{
if ( text.isEmpty() )
@@ -63,6 +63,9 @@ class APP_EXPORT QgsMeshRendererVectorSettingsWidget : public QWidget, private U
private slots:
void onSymbologyChanged( int currentIndex );
void onStreamLineSeedingMethodChanged( int currentIndex );
void onColoringMethodChanged();
void onColorRampMinMaxChanged();
void loadColorRampShader();

private:

@@ -381,6 +381,13 @@ void QgsMeshLayer::assignDefaultStyleToDatasetGroup( int groupIndex )
scalarSettings.setClassificationMinimumMaximum( groupMin, groupMax );
scalarSettings.setColorRampShader( fcn );
mRendererSettings.setScalarSettings( groupIndex, scalarSettings );

if ( metadata.isVector() )
{
QgsMeshRendererVectorSettings vectorSettings;
vectorSettings.setColorRampShader( fcn );
mRendererSettings.setVectorSettings( groupIndex, vectorSettings );
}
}

QgsMapLayerRenderer *QgsMeshLayer::createMapRenderer( QgsRenderContext &rendererContext )
@@ -579,7 +579,10 @@ QDomElement QgsMeshRendererVectorSettings::writeXml( QDomDocument &doc ) const
elem.setAttribute( QStringLiteral( "symbology" ), mDisplayingMethod );

elem.setAttribute( QStringLiteral( "line-width" ), mLineWidth );
elem.setAttribute( QStringLiteral( "coloring-method" ), coloringMethod() );
elem.setAttribute( QStringLiteral( "color" ), QgsSymbolLayerUtils::encodeColor( mColor ) );
QDomElement elemShader = mColorRampShader.writeXml( doc );
elem.appendChild( elemShader );
elem.setAttribute( QStringLiteral( "filter-min" ), mFilterMin );
elem.setAttribute( QStringLiteral( "filter-max" ), mFilterMax );

@@ -600,7 +603,10 @@ void QgsMeshRendererVectorSettings::readXml( const QDomElement &elem )
elem.attribute( QStringLiteral( "symbology" ) ).toInt() );

mLineWidth = elem.attribute( QStringLiteral( "line-width" ) ).toDouble();
mColoringMethod = static_cast<QgsMeshRendererVectorSettings::ColoringMethod>(
elem.attribute( QStringLiteral( "coloring-method" ) ).toInt() );
mColor = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "color" ) ) );
mColorRampShader.readXml( elem.firstChildElement( "colorrampshader" ) );
mFilterMin = elem.attribute( QStringLiteral( "filter-min" ) ).toDouble();
mFilterMax = elem.attribute( QStringLiteral( "filter-max" ) ).toDouble();

@@ -621,6 +627,26 @@ void QgsMeshRendererVectorSettings::readXml( const QDomElement &elem )
mTracesSettings.readXml( elemTraces );
}

QgsMeshRendererVectorSettings::ColoringMethod QgsMeshRendererVectorSettings::coloringMethod() const
{
return mColoringMethod;
}

void QgsMeshRendererVectorSettings::setColoringMethod( const QgsMeshRendererVectorSettings::ColoringMethod &coloringMethod )
{
mColoringMethod = coloringMethod;
}

QgsColorRampShader QgsMeshRendererVectorSettings::colorRampShader() const
{
return mColorRampShader;
}

void QgsMeshRendererVectorSettings::setColorRampShader( const QgsColorRampShader &colorRampShader )
{
mColorRampShader = colorRampShader;
}

QgsMeshRendererVectorTracesSettings QgsMeshRendererVectorSettings::tracesSettings() const
{
return mTracesSettings;
@@ -329,7 +329,7 @@ class CORE_EXPORT QgsMeshRendererVectorStreamlineSettings
/**
* Seeds start points randomly on the mesh
*/
Random,
Random
};

//! Returns the method used for seeding start points of strealines
@@ -418,6 +418,18 @@ class CORE_EXPORT QgsMeshRendererVectorSettings
Traces
};

/**
* Defines the how the color of vector is defined
* \since QGIS 3.14
*/
enum ColoringMethod
{
//! Render the vector with a single color
SingleColor = 0,
//! Render the vector with a color ramp
ColorRamp
};


//! Returns line width of the arrow (in millimeters)
double lineWidth() const;
@@ -482,6 +494,30 @@ class CORE_EXPORT QgsMeshRendererVectorSettings
*/
void setSymbology( const Symbology &symbology );

/**
* Returns the coloring method used to render vector datasets
* \since QGIS 3.14
*/
ColoringMethod coloringMethod() const;

/**
* Sets the coloring method used to render vector datasets
* \since QGIS 3.14
*/
void setColoringMethod( const ColoringMethod &coloringMethod );

/**
* Sets the color ramp shader used to render vector datasets
* \since QGIS 3.14
*/
QgsColorRampShader colorRampShader() const;

/**
* Returns the color ramp shader used to render vector datasets
* \since QGIS 3.14
*/
void setColorRampShader( const QgsColorRampShader &colorRampShader );

/**
* Returns settings for vector rendered with arrows
* \since QGIS 3.12
@@ -523,13 +559,14 @@ class CORE_EXPORT QgsMeshRendererVectorSettings
//! Reads configuration from the given DOM element
void readXml( const QDomElement &elem );


private:

Symbology mDisplayingMethod = Arrows;

double mLineWidth = DEFAULT_LINE_WIDTH; //in millimeters
QgsColorRampShader mColorRampShader;
QColor mColor = Qt::black;
ColoringMethod mColoringMethod = SingleColor;
double mFilterMin = -1; //disabled
double mFilterMax = -1; //disabled
int mUserGridCellWidth = 10; // in pixels

0 comments on commit 20a7ed4

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