Skip to content

Commit

Permalink
Use full featured color ramp shader
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 2, 2020
1 parent af280eb commit 1581bb8
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 138 deletions.
Expand Up @@ -9,7 +9,6 @@




class QgsPointCloudAttributeByRampRenderer : QgsPointCloudRenderer
{
%Docstring
Expand Down Expand Up @@ -62,20 +61,18 @@ Sets the ``attribute`` to use for the renderer.
.. seealso:: :py:func:`attribute`
%End

QgsColorRamp *colorRamp() const;
QgsColorRampShader colorRampShader() const;
%Docstring
Returns the color ramp used for rendering the attribute.
Returns the color ramp shader function used to visualize the attribute.

.. seealso:: :py:func:`setColorRamp`
.. seealso:: :py:func:`setColorRampShader`
%End

void setColorRamp( QgsColorRamp *ramp /Transfer/ );
void setColorRampShader( const QgsColorRampShader &shader );
%Docstring
Sets the color ``ramp`` used for rendering the attribute.

Ownership of ``ramp`` is transferrred to the renderer.
Sets the color ramp ``shader`` function used to visualize the attribute.

.. seealso:: :py:func:`colorRamp`
.. seealso:: :py:func:`colorRampShader`
%End

double min() const;
Expand Down
35 changes: 20 additions & 15 deletions src/core/pointcloud/qgspointcloudattributebyramprenderer.cpp
Expand Up @@ -23,7 +23,7 @@

QgsPointCloudAttributeByRampRenderer::QgsPointCloudAttributeByRampRenderer()
{
mColorRamp.reset( QgsStyle::defaultStyle()->colorRamp( QStringLiteral( "Viridis" ) ) );
mColorRampShader.setSourceColorRamp( QgsStyle::defaultStyle()->colorRamp( QStringLiteral( "Viridis" ) ) );
}

QString QgsPointCloudAttributeByRampRenderer::type() const
Expand All @@ -35,7 +35,7 @@ QgsPointCloudRenderer *QgsPointCloudAttributeByRampRenderer::clone() const
{
std::unique_ptr< QgsPointCloudAttributeByRampRenderer > res = qgis::make_unique< QgsPointCloudAttributeByRampRenderer >();
res->mAttribute = mAttribute;
res->mColorRamp.reset( colorRamp() ? colorRamp()->clone() : QgsStyle::defaultStyle()->colorRamp( QStringLiteral( "Viridis" ) ) );
res->mColorRampShader = mColorRampShader;
res->mMin = mMin;
res->mMax = mMax;

Expand Down Expand Up @@ -77,6 +77,11 @@ void QgsPointCloudAttributeByRampRenderer::renderBlock( const QgsPointCloudBlock
double z = 0;
const QgsCoordinateTransform ct = context.renderContext().coordinateTransform();
const bool reproject = ct.isValid();

int red = 0;
int green = 0;
int blue = 0;
int alpha = 0;
for ( int i = 0; i < count; ++i )
{
if ( considerZ )
Expand Down Expand Up @@ -110,7 +115,7 @@ void QgsPointCloudAttributeByRampRenderer::renderBlock( const QgsPointCloudBlock

mapToPixel.transformInPlace( x, y );

const QColor color = mColorRamp->color( ( attributeValue - mMin ) / ( mMax - mMin ) );
mColorRampShader.shade( attributeValue, &red, &green, &blue, &alpha );
#if 0
pen.setColor( QColor( red, green, blue ) );
context.renderContext().painter()->setPen( pen );
Expand All @@ -119,7 +124,7 @@ void QgsPointCloudAttributeByRampRenderer::renderBlock( const QgsPointCloudBlock

context.renderContext().painter()->fillRect( QRectF( x - mPainterPenWidth * 0.5,
y - mPainterPenWidth * 0.5,
mPainterPenWidth, mPainterPenWidth ), color );
mPainterPenWidth, mPainterPenWidth ), QColor( red, green, blue, alpha ) );
#endif

rendered++;
Expand All @@ -134,11 +139,10 @@ QgsPointCloudRenderer *QgsPointCloudAttributeByRampRenderer::create( QDomElement
std::unique_ptr< QgsPointCloudAttributeByRampRenderer > r = qgis::make_unique< QgsPointCloudAttributeByRampRenderer >();

r->setAttribute( element.attribute( QStringLiteral( "attribute" ), QStringLiteral( "Intensity" ) ) );
QDomElement sourceColorRampElem = element.firstChildElement( QStringLiteral( "colorramp" ) );
if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral( "name" ) ) == QLatin1String( "[source]" ) )
{
r->setColorRamp( QgsSymbolLayerUtils::loadColorRamp( sourceColorRampElem ) );
}

QDomElement elemShader = element.firstChildElement( QStringLiteral( "colorrampshader" ) );
r->mColorRampShader.readXml( elemShader );

r->setMin( element.attribute( QStringLiteral( "min" ), QStringLiteral( "0" ) ).toDouble() );
r->setMax( element.attribute( QStringLiteral( "max" ), QStringLiteral( "100" ) ).toDouble() );

Expand All @@ -156,8 +160,9 @@ QDomElement QgsPointCloudAttributeByRampRenderer::save( QDomDocument &doc, const
rendererElem.setAttribute( QStringLiteral( "max" ), mMax );

rendererElem.setAttribute( QStringLiteral( "attribute" ), mAttribute );
QDomElement colorRampElem = QgsSymbolLayerUtils::saveColorRamp( QStringLiteral( "[source]" ), mColorRamp.get(), doc );
rendererElem.appendChild( colorRampElem );

QDomElement elemShader = mColorRampShader.writeXml( doc );
rendererElem.appendChild( elemShader );

saveCommonProperties( rendererElem, context );

Expand Down Expand Up @@ -193,14 +198,14 @@ void QgsPointCloudAttributeByRampRenderer::setAttribute( const QString &attribut
mAttribute = attribute;
}

QgsColorRamp *QgsPointCloudAttributeByRampRenderer::colorRamp() const
QgsColorRampShader QgsPointCloudAttributeByRampRenderer::colorRampShader() const
{
return mColorRamp.get();
return mColorRampShader;
}

void QgsPointCloudAttributeByRampRenderer::setColorRamp( QgsColorRamp *ramp )
void QgsPointCloudAttributeByRampRenderer::setColorRampShader( const QgsColorRampShader &shader )
{
mColorRamp.reset( ramp );
mColorRampShader = shader;
}

double QgsPointCloudAttributeByRampRenderer::min() const
Expand Down
20 changes: 8 additions & 12 deletions src/core/pointcloud/qgspointcloudattributebyramprenderer.h
Expand Up @@ -21,8 +21,7 @@
#include "qgspointcloudrenderer.h"
#include "qgis_core.h"
#include "qgis_sip.h"

class QgsColorRamp;
#include "qgscolorrampshader.h"

/**
* \ingroup core
Expand Down Expand Up @@ -67,20 +66,18 @@ class CORE_EXPORT QgsPointCloudAttributeByRampRenderer : public QgsPointCloudRen
void setAttribute( const QString &attribute );

/**
* Returns the color ramp used for rendering the attribute.
* Returns the color ramp shader function used to visualize the attribute.
*
* \see setColorRamp()
* \see setColorRampShader()
*/
QgsColorRamp *colorRamp() const;
QgsColorRampShader colorRampShader() const;

/**
* Sets the color \a ramp used for rendering the attribute.
*
* Ownership of \a ramp is transferrred to the renderer.
* Sets the color ramp \a shader function used to visualize the attribute.
*
* \see colorRamp()
* \see colorRampShader()
*/
void setColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
void setColorRampShader( const QgsColorRampShader &shader );

//! Returns min
double min() const;
Expand All @@ -101,8 +98,7 @@ class CORE_EXPORT QgsPointCloudAttributeByRampRenderer : public QgsPointCloudRen
double mMax = 100;

QString mAttribute = QStringLiteral( "Intensity" );

std::unique_ptr<QgsColorRamp> mColorRamp;
QgsColorRampShader mColorRampShader;

};

Expand Down
25 changes: 16 additions & 9 deletions src/gui/pointcloud/qgspointcloudattributebyramprendererwidget.cpp
Expand Up @@ -41,10 +41,11 @@ QgsPointCloudAttributeByRampRendererWidget::QgsPointCloudAttributeByRampRenderer

connect( mAttributeComboBox, &QgsPointCloudAttributeComboBox::attributeChanged,
this, &QgsPointCloudAttributeByRampRendererWidget::emitWidgetChanged );
connect( mBtnColorRamp, &QgsColorRampButton::colorRampChanged,
this, &QgsPointCloudAttributeByRampRendererWidget::emitWidgetChanged );
connect( mMinSpin, qgis::overload<double>::of( &QDoubleSpinBox::valueChanged ), this, &QgsPointCloudAttributeByRampRendererWidget::emitWidgetChanged );
connect( mMaxSpin, qgis::overload<double>::of( &QDoubleSpinBox::valueChanged ), this, &QgsPointCloudAttributeByRampRendererWidget::emitWidgetChanged );
connect( mMinSpin, qgis::overload<double>::of( &QDoubleSpinBox::valueChanged ), this, &QgsPointCloudAttributeByRampRendererWidget::minMaxChanged );
connect( mMaxSpin, qgis::overload<double>::of( &QDoubleSpinBox::valueChanged ), this, &QgsPointCloudAttributeByRampRendererWidget::minMaxChanged );

connect( mScalarColorRampShaderWidget, &QgsColorRampShaderWidget::widgetChanged, this, &QgsPointCloudAttributeByRampRendererWidget::emitWidgetChanged );

}

QgsPointCloudRendererWidget *QgsPointCloudAttributeByRampRendererWidget::create( QgsPointCloudLayer *layer, QgsStyle *style, QgsPointCloudRenderer * )
Expand All @@ -62,9 +63,11 @@ QgsPointCloudRenderer *QgsPointCloudAttributeByRampRendererWidget::renderer()
std::unique_ptr< QgsPointCloudAttributeByRampRenderer > renderer = qgis::make_unique< QgsPointCloudAttributeByRampRenderer >();
renderer->setAttribute( mAttributeComboBox->currentAttribute() );

renderer->setColorRamp( mBtnColorRamp->colorRamp() );
renderer->setMin( mMinSpin->value() );
renderer->setMax( mMaxSpin->value() );

renderer->setColorRampShader( mScalarColorRampShaderWidget->shader() );

return renderer.release();
}

Expand All @@ -74,6 +77,11 @@ void QgsPointCloudAttributeByRampRendererWidget::emitWidgetChanged()
emit widgetChanged();
}

void QgsPointCloudAttributeByRampRendererWidget::minMaxChanged()
{
mScalarColorRampShaderWidget->setMinimumMaximumAndClassify( mMinSpin->value(), mMaxSpin->value() );
}

void QgsPointCloudAttributeByRampRendererWidget::setFromRenderer( const QgsPointCloudRenderer *r )
{
mBlockChangedSignal = true;
Expand All @@ -82,10 +90,11 @@ void QgsPointCloudAttributeByRampRendererWidget::setFromRenderer( const QgsPoint
{
mAttributeComboBox->setAttribute( mbcr->attribute() );

mBtnColorRamp->setColorRamp( mbcr->colorRamp() );

mMinSpin->setValue( mbcr->min() );
mMaxSpin->setValue( mbcr->max() );

whileBlocking( mScalarColorRampShaderWidget )->setFromShader( mbcr->colorRampShader() );
whileBlocking( mScalarColorRampShaderWidget )->setMinimumMaximum( mbcr->min(), mbcr->max() );
}
else
{
Expand All @@ -97,8 +106,6 @@ void QgsPointCloudAttributeByRampRendererWidget::setFromRenderer( const QgsPoint
{
mAttributeComboBox->setCurrentIndex( mAttributeComboBox->count() > 1 ? 1 : 0 );
}

mBtnColorRamp->setColorRamp( QgsStyle::defaultStyle()->colorRamp( QStringLiteral( "Viridis" ) ) );
}
mBlockChangedSignal = false;
}
Expand Down
Expand Up @@ -43,6 +43,7 @@ class GUI_EXPORT QgsPointCloudAttributeByRampRendererWidget: public QgsPointClou
private slots:

void emitWidgetChanged();
void minMaxChanged();

private:
void setFromRenderer( const QgsPointCloudRenderer *r );
Expand Down
16 changes: 9 additions & 7 deletions src/ui/mesh/qgsrenderermeshpropswidgetbase.ui
Expand Up @@ -41,7 +41,7 @@
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<property name="iconSize">
<size>
Expand All @@ -60,7 +60,7 @@
</property>
<widget class="QWidget" name="mActiveDatasetTab">
<attribute name="icon">
<iconset>
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/propertyicons/general.svg</normaloff>:/images/themes/default/propertyicons/general.svg</iconset>
</attribute>
<attribute name="title">
Expand Down Expand Up @@ -132,7 +132,7 @@
</widget>
<widget class="QWidget" name="mContoursTab">
<attribute name="icon">
<iconset>
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/propertyicons/meshcontours.svg</normaloff>:/images/themes/default/propertyicons/meshcontours.svg</iconset>
</attribute>
<attribute name="title">
Expand Down Expand Up @@ -195,7 +195,7 @@
</widget>
<widget class="QWidget" name="mVectorsTab">
<attribute name="icon">
<iconset>
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/propertyicons/meshvectors.svg</normaloff>:/images/themes/default/propertyicons/meshvectors.svg</iconset>
</attribute>
<attribute name="title">
Expand Down Expand Up @@ -253,7 +253,7 @@
</widget>
<widget class="QWidget" name="mMeshTab">
<attribute name="icon">
<iconset>
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/propertyicons/meshframe.svg</normaloff>:/images/themes/default/propertyicons/meshframe.svg</iconset>
</attribute>
<attribute name="title">
Expand Down Expand Up @@ -391,7 +391,7 @@
</widget>
<widget class="QWidget" name="mAveragingTab">
<attribute name="icon">
<iconset>
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/propertyicons/meshaveraging.svg</normaloff>:/images/themes/default/propertyicons/meshaveraging.svg</iconset>
</attribute>
<attribute name="title">
Expand Down Expand Up @@ -481,6 +481,8 @@
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<resources>
<include location="../../../images/images.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit 1581bb8

Please sign in to comment.