Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[raster] migrate singleband pseudo-color renderer color ramp widget (#…
  • Loading branch information
nirvn committed Dec 2, 2016
1 parent 1a24452 commit eb85e3f
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 110 deletions.
20 changes: 11 additions & 9 deletions python/core/raster/qgscolorrampshader.sip
Expand Up @@ -7,6 +7,8 @@ class QgsColorRampShader : QgsRasterShaderFunction
public:
QgsColorRampShader( double theMinimumValue = 0.0, double theMaximumValue = 255.0 );

~QgsColorRampShader();

//An entry for classification based upon value.
//Such a classification is typically used for
//single band layers where a pixel value represents
Expand Down Expand Up @@ -40,24 +42,24 @@ class QgsColorRampShader : QgsRasterShaderFunction
/** \brief Get the color ramp type */
QgsColorRampShader::ColorRamp_TYPE colorRampType() const;

/** \brief Get the original color ramp name
/** Get the source color ramp
* @note added in QGIS 3.0
* @see setSourceColorRamp()
*/
QgsColorRamp* sourceColorRamp() const /Factory/;

/** Set the source color ramp
* @note added in QGIS 3.0
* @see setColorRampName()
* @see sourceColorRamp()
*/
QString colorRampName() const;
void setSourceColorRamp( QgsColorRamp* colorramp /Transfer/ );

/** \brief Get the color ramp type as a string */
QString colorRampTypeAsQString();

/** \brief Set custom colormap */
void setColorRampItemList( const QList<QgsColorRampShader::ColorRampItem>& theList ); //TODO: sort on set

/** \brief Set the source color ramp name
* @note added in QGIS 3.0
* @see colorRampName()
*/
void setColorRampName( const QString& theName );

/** \brief Set the color ramp type*/
void setColorRampType( QgsColorRampShader::ColorRamp_TYPE theColorRampType );

Expand Down
11 changes: 11 additions & 0 deletions python/gui/qgscolorrampbutton.sip
Expand Up @@ -147,6 +147,17 @@ class QgsColorRampButton : QToolButton
*/
bool showGradientOnly() const;

/** Sets the name of the current color ramp when it's available in the style manager
* @param name Name of the saved color ramp
* @see colorRampName
*/
void setColorRampName( QString name );

/** Returns the name of the current color ramp when it's available in the style manager
* @see setColorRampName
*/
QString colorRampName() const;

public slots:

/** Sets the current color ramp for the button. Will emit a colorRampChanged() signal if the color ramp is different
Expand Down
4 changes: 4 additions & 0 deletions python/gui/raster/qgssinglebandpseudocolorrendererwidget.sip
Expand Up @@ -22,6 +22,10 @@ class QgsSingleBandPseudoColorRendererWidget : QgsRasterRendererWidget
void setFromRenderer( const QgsRasterRenderer* r );

public slots:

/** Executes the single band pseudo raster classficiation
*/
void classify();
void loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin );

};
Expand Down
37 changes: 34 additions & 3 deletions src/core/raster/qgscolorrampshader.cpp
Expand Up @@ -24,6 +24,7 @@ originally part of the larger QgsRasterLayer class

#include "qgslogger.h"
#include "qgis.h"
#include "qgscolorramp.h"
#include "qgscolorrampshader.h"

#include <cmath>
Expand All @@ -34,12 +35,37 @@ QgsColorRampShader::QgsColorRampShader( double theMinimumValue, double theMaximu
, mLUTOffset( 0.0 )
, mLUTFactor( 1.0 )
, mLUTInitialized( false )
, mColorRampName( QString() )
, mClip( false )
{
QgsDebugMsgLevel( "called.", 4 );
}

QgsColorRampShader::QgsColorRampShader( const QgsColorRampShader& other )
: QgsRasterShaderFunction( other )
, mLUT( other.mLUT )
, mLUTOffset( other.mLUTOffset )
, mLUTFactor( other.mLUTFactor )
, mLUTInitialized( other.mLUTInitialized )
, mClip( other.mClip )
{
mSourceColorRamp.reset( other.sourceColorRamp()->clone() );
}

QgsColorRampShader & QgsColorRampShader::operator=( const QgsColorRampShader & other )
{
mSourceColorRamp.reset( other.sourceColorRamp()->clone() );
mLUT = other.mLUT;
mLUTOffset = other.mLUTOffset;
mLUTFactor = other.mLUTFactor;
mLUTInitialized = other.mLUTInitialized;
mClip = other.mClip;
return *this;
}

QgsColorRampShader::~QgsColorRampShader()
{
}

QString QgsColorRampShader::colorRampTypeAsQString()
{
switch ( mColorRampType )
Expand Down Expand Up @@ -83,9 +109,14 @@ void QgsColorRampShader::setColorRampType( const QString& theType )
}
}

void QgsColorRampShader::setColorRampName( const QString& theName )
QgsColorRamp* QgsColorRampShader::sourceColorRamp() const
{
return mSourceColorRamp.data();
}

void QgsColorRampShader::setSourceColorRamp( QgsColorRamp* colorramp )
{
mColorRampName = theName;
mSourceColorRamp.reset( colorramp );
}


Expand Down
40 changes: 28 additions & 12 deletions src/core/raster/qgscolorrampshader.h
Expand Up @@ -24,6 +24,7 @@ originally part of the larger QgsRasterLayer class
#include <QColor>
#include <QVector>

#include "qgscolorramp.h"
#include "qgsrastershaderfunction.h"

/** \ingroup core
Expand All @@ -33,8 +34,21 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
{

public:

QgsColorRampShader( double theMinimumValue = 0.0, double theMaximumValue = 255.0 );

/** Destructor
*/
virtual ~QgsColorRampShader();

/** Copy constructor
*/
QgsColorRampShader( const QgsColorRampShader& other );

/** Assignment operator
*/
QgsColorRampShader& operator=( const QgsColorRampShader& other );

//An entry for classification based upon value.
//Such a classification is typically used for
//single band layers where a pixel value represents
Expand Down Expand Up @@ -75,23 +89,23 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
//! \brief Get the color ramp type as a string
QString colorRampTypeAsQString();

/** Get the original color ramp name
* @note added in QGIS 3.0
* @see setColorRampName()
*/
QString colorRampName() const { return mColorRampName; }

//! \brief Set custom colormap
void setColorRampItemList( const QList<QgsColorRampShader::ColorRampItem>& theList ); //TODO: sort on set

//! \brief Set the color ramp type
void setColorRampType( QgsColorRampShader::ColorRamp_TYPE theColorRampType );

/** Sets the source color ramp name
/** Get the source color ramp
* @note added in QGIS 3.0
* @see setSourceColorRamp()
*/
QgsColorRamp* sourceColorRamp() const;

/** Set the source color ramp. Ownership is transferred to the renderer.
* @note added in QGIS 3.0
* @see colorRampName()
* @see sourceColorRamp()
*/
void setColorRampName( const QString& theName );
void setSourceColorRamp( QgsColorRamp* colorramp );

//! \brief Set the color ramp type
void setColorRampType( const QString& theType );
Expand All @@ -116,6 +130,11 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
*/
bool clip() const { return mClip; }

protected:

//! Source color ramp
QScopedPointer<QgsColorRamp> mSourceColorRamp;

private:

/** This vector holds the information for classification based on values.
Expand All @@ -135,9 +154,6 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
double mLUTFactor;
bool mLUTInitialized;

//! Color ramp name
QString mColorRampName;

//! Do not render values out of range
bool mClip;
};
Expand Down
20 changes: 18 additions & 2 deletions src/core/raster/qgsrastershader.cpp
Expand Up @@ -20,6 +20,8 @@ email : ersts@amnh.org
#include "qgscolorrampshader.h"
#include "qgsrastershader.h"
#include "qgsrasterblock.h"
#include "qgssymbollayerutils.h"

#include <QDomDocument>
#include <QDomElement>

Expand Down Expand Up @@ -144,9 +146,16 @@ void QgsRasterShader::writeXml( QDomDocument& doc, QDomElement& parent ) const
if ( colorRampShader )
{
QDomElement colorRampShaderElem = doc.createElement( QStringLiteral( "colorrampshader" ) );
colorRampShaderElem.setAttribute( "colorRampName", colorRampShader->colorRampName() );
colorRampShaderElem.setAttribute( "colorRampType", colorRampShader->colorRampTypeAsQString() );
colorRampShaderElem.setAttribute( QStringLiteral( "clip" ), colorRampShader->clip() );

// save source color ramp
if ( colorRampShader->sourceColorRamp() )
{
QDomElement colorRampElem = QgsSymbolLayerUtils::saveColorRamp( QStringLiteral( "[source]" ), colorRampShader->sourceColorRamp(), doc );
colorRampShaderElem.appendChild( colorRampElem );
}

//items
QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->colorRampItemList();
QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
Expand All @@ -171,7 +180,14 @@ void QgsRasterShader::readXml( const QDomElement& elem )
if ( !colorRampShaderElem.isNull() )
{
QgsColorRampShader* colorRampShader = new QgsColorRampShader();
colorRampShader->setColorRampName( colorRampShaderElem.attribute( "colorRampName" ) );

// 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->setClip( colorRampShaderElem.attribute( QStringLiteral( "clip" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );

Expand Down
5 changes: 4 additions & 1 deletion src/core/raster/qgssinglebandpseudocolorrenderer.cpp
Expand Up @@ -63,7 +63,10 @@ QgsSingleBandPseudoColorRenderer* QgsSingleBandPseudoColorRenderer::clone() cons
{
QgsColorRampShader * colorRampShader = new QgsColorRampShader( mShader->minimumValue(), mShader->maximumValue() );

colorRampShader->setColorRampName( origColorRampShader->colorRampName() );
if ( origColorRampShader->sourceColorRamp() )
{
colorRampShader->setSourceColorRamp( origColorRampShader->sourceColorRamp()->clone() );
}
colorRampShader->setColorRampType( origColorRampShader->colorRampType() );
colorRampShader->setClip( origColorRampShader->clip() );
colorRampShader->setColorRampItemList( origColorRampShader->colorRampItemList() );
Expand Down
28 changes: 13 additions & 15 deletions src/gui/qgscolorrampbutton.cpp
Expand Up @@ -89,6 +89,8 @@ void QgsColorRampButton::showColorRampDialog()
if ( !currentRamp )
return;

setColorRampName( QString() );

if ( currentRamp->type() == QLatin1String( "gradient" ) )
{
QgsGradientColorRamp* gradRamp = static_cast<QgsGradientColorRamp*>( currentRamp.data() );
Expand Down Expand Up @@ -193,16 +195,10 @@ bool QgsColorRampButton::event( QEvent *e )
{
if ( e->type() == QEvent::ToolTip )
{
//QString name = this->colorRamp().name();
/*int hue = this->color().hue();
//int value = this->color().value();
//int saturation = this->color().saturation();
QString info = QString( "HEX: %1 \n"
"RGB: %2 \n"
"HSV: %3,%4,%5" ).arg( name,
QgsSymbolLayerUtils::encodeColor( this->color() ) )
.arg( hue ).arg( saturation ).arg( value );*/
setToolTip( QString( "fix" ) );
if ( !colorRampName().isEmpty() )
{
setToolTip( colorRampName() );
}
}
return QToolButton::event( e );
}
Expand Down Expand Up @@ -328,28 +324,30 @@ void QgsColorRampButton::loadColorRamp()
if ( selectedItem )
{
QString name = selectedItem->text();
setColorRampName( name );
setColorRampFromName( name );
}
}

void QgsColorRampButton::createColorRamp()
{
QString rampName;
QString name;
if ( !mShowGradientOnly )
{
rampName = QgsStyleManagerDialog::addColorRampStatic( this, mStyle );
name = QgsStyleManagerDialog::addColorRampStatic( this, mStyle );
}
else
{
rampName = QgsStyleManagerDialog::addColorRampStatic( this, mStyle, QStringLiteral( "Gradient" ) );
name = QgsStyleManagerDialog::addColorRampStatic( this, mStyle, QStringLiteral( "Gradient" ) );
}
if ( rampName.isEmpty() )
if ( name.isEmpty() )
return;

// make sure the color ramp is stored
mStyle->save();

setColorRampFromName( rampName );
setColorRampName( name );
setColorRampFromName( name );
}

void QgsColorRampButton::invertColorRamp()
Expand Down
12 changes: 12 additions & 0 deletions src/gui/qgscolorrampbutton.h
Expand Up @@ -175,6 +175,17 @@ class GUI_EXPORT QgsColorRampButton : public QToolButton
*/
bool showGradientOnly() const { return mShowGradientOnly; }

/** Sets the name of the current color ramp when it's available in the style manager
* @param name Name of the saved color ramp
* @see colorRampName
*/
void setColorRampName( const QString& name ) { mColorRampName = name; }

/** Returns the name of the current color ramp when it's available in the style manager
* @see setColorRampName
*/
QString colorRampName() const { return mColorRampName; }

public slots:

/** Sets the current color ramp for the button. Will emit a colorRampChanged() signal if the color ramp is different
Expand Down Expand Up @@ -243,6 +254,7 @@ class GUI_EXPORT QgsColorRampButton : public QToolButton
QString mColorRampDialogTitle;
bool mShowGradientOnly;
QgsColorRamp* mColorRamp;
QString mColorRampName;
QgsStyle* mStyle;

QgsColorRamp* mDefaultColorRamp;
Expand Down

0 comments on commit eb85e3f

Please sign in to comment.