Skip to content
Permalink
Browse files

color ramp button widget for gradiant fill and categorized renderer

  • Loading branch information
nirvn committed Nov 30, 2016
1 parent fa16e3e commit deb18c40dbcd89fcebf6f683fb6b694521ad271d
@@ -48,7 +48,7 @@ class QgsColorRamp
*/
virtual QString type() const = 0;

virtual void invert() = 0;
virtual void invert();

/** Creates a clone of the color ramp.
*/
@@ -226,8 +226,6 @@ class QgsLimitedRandomColorRamp : QgsColorRamp

virtual QString type() const;

virtual void invert();

virtual QgsLimitedRandomColorRamp* clone() const /Factory/;

virtual QgsStringMap properties() const;
@@ -287,8 +285,6 @@ class QgsRandomColorRamp : QgsColorRamp

QString type() const;

virtual void invert();

virtual QgsRandomColorRamp* clone() const /Factory/;

QgsStringMap properties() const;
@@ -157,16 +157,11 @@ class QgsCategorizedSymbolRenderer : QgsFeatureRenderer
*/
void setSourceColorRamp( QgsColorRamp* ramp /Transfer/ );

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

/** Update the color ramp used and all symbols colors.
* @param ramp color ramp. Ownership is transferred to the renderer
* @param inverted set to true to invert ramp colors
* @note added in 2.5
*/
void updateColorRamp( QgsColorRamp* ramp /Transfer/, bool inverted = false );
void updateColorRamp( QgsColorRamp* ramp /Transfer/ );

//! items of symbology items in legend should be checkable
//! @note added in 2.5
@@ -41,7 +41,7 @@
%Include qgscharacterselectdialog.sip
%Include qgscolorbrewercolorrampdialog.sip
%Include qgscolorbutton.sip
%Include qgscolorbutton.sip
%Include qgscolorrampbutton.sip
%Include qgscolordialog.sip
%Include qgscolorschemelist.sip
%Include qgscolorswatchgrid.sip
@@ -72,8 +72,6 @@ class QgsCategorizedSymbolRendererWidget : QgsRendererWidget

void changeCategorySymbol();

QgsColorRamp* getColorRamp();

QList<QgsSymbol*> selectedSymbols();
QgsCategoryList selectedCategoryList();
void refreshSymbolView();
@@ -51,7 +51,9 @@ class CORE_EXPORT QgsColorRamp
virtual QString type() const = 0;


virtual void invert() = 0;
/** Inverts the ordering of the color ramp.
*/
virtual void invert() {}

/** Creates a clone of the color ramp.
*/
@@ -259,7 +261,6 @@ class CORE_EXPORT QgsLimitedRandomColorRamp : public QgsColorRamp
virtual double value( int index ) const override;
virtual QColor color( double value ) const override;
virtual QString type() const override { return QStringLiteral( "random" ); }
virtual void invert() override { return; }
virtual QgsLimitedRandomColorRamp* clone() const override;
virtual QgsStringMap properties() const override;
int count() const override { return mCount; }
@@ -375,8 +376,6 @@ class CORE_EXPORT QgsRandomColorRamp: public QgsColorRamp

QString type() const override;

virtual void invert() override { return; }

QgsRandomColorRamp* clone() const override;

QgsStringMap properties() const override;
@@ -152,7 +152,6 @@ void QgsRendererCategory::toSld( QDomDocument &doc, QDomElement &element, QgsStr
QgsCategorizedSymbolRenderer::QgsCategorizedSymbolRenderer( const QString& attrName, const QgsCategoryList& categories )
: QgsFeatureRenderer( QStringLiteral( "categorizedSymbol" ) )
, mAttrName( attrName )
, mInvertedColorRamp( false )
, mAttrNum( -1 )
, mCounting( false )
{
@@ -472,7 +471,6 @@ QgsCategorizedSymbolRenderer* QgsCategorizedSymbolRenderer::clone() const
if ( mSourceColorRamp.data() )
{
r->setSourceColorRamp( mSourceColorRamp->clone() );
r->setInvertedColorRamp( mInvertedColorRamp );
}
r->setUsingSymbolLevels( usingSymbolLevels() );

@@ -631,9 +629,6 @@ QgsFeatureRenderer* QgsCategorizedSymbolRenderer::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" ) );
}

QDomElement rotationElem = element.firstChildElement( QStringLiteral( "rotation" ) );
@@ -721,9 +716,6 @@ QDomElement QgsCategorizedSymbolRenderer::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 );
}

QDomElement rotationElem = doc.createElement( QStringLiteral( "rotation" ) );
@@ -864,10 +856,9 @@ void QgsCategorizedSymbolRenderer::setSourceColorRamp( QgsColorRamp* ramp )
mSourceColorRamp.reset( ramp );
}

void QgsCategorizedSymbolRenderer::updateColorRamp( QgsColorRamp* ramp, bool inverted )
void QgsCategorizedSymbolRenderer::updateColorRamp( QgsColorRamp* ramp )
{
setSourceColorRamp( ramp );
setInvertedColorRamp( inverted );
double num = mCategories.count() - 1;
double count = 0;

@@ -882,7 +873,6 @@ void QgsCategorizedSymbolRenderer::updateColorRamp( QgsColorRamp* ramp, bool inv
Q_FOREACH ( const QgsRendererCategory &cat, mCategories )
{
double value = count / num;
if ( mInvertedColorRamp ) value = 1.0 - value;
cat.symbol()->setColor( mSourceColorRamp->color( value ) );
count += 1;
}
@@ -167,16 +167,11 @@ class CORE_EXPORT QgsCategorizedSymbolRenderer : 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 and all symbols colors.
* @param ramp color ramp. Ownership is transferred to the renderer
* @param inverted set to true to invert ramp colors
* @note added in 2.5
*/
void updateColorRamp( QgsColorRamp* ramp, bool inverted = false );
void updateColorRamp( QgsColorRamp* ramp );

virtual bool legendSymbolItemsCheckable() const override;
virtual bool legendSymbolItemChecked( const QString& key ) override;
@@ -194,7 +189,6 @@ class CORE_EXPORT QgsCategorizedSymbolRenderer : public QgsFeatureRenderer
QgsCategoryList mCategories;
QScopedPointer<QgsSymbol> mSourceSymbol;
QScopedPointer<QgsColorRamp> mSourceColorRamp;
bool mInvertedColorRamp;
QScopedPointer<QgsExpression> mExpression;

//! attribute index (derived from attribute name in startRender)
@@ -188,6 +188,7 @@ SET(QGIS_GUI_SRCS
qgscollapsiblegroupbox.cpp
qgscolorbrewercolorrampdialog.cpp
qgscolorbutton.cpp
qgscolorrampbutton.cpp
qgscolordialog.cpp
qgscolorschemelist.cpp
qgscolorswatchgrid.cpp
@@ -356,6 +357,7 @@ SET(QGIS_GUI_MOC_HDRS
qgscollapsiblegroupbox.h
qgscolorbrewercolorrampdialog.h
qgscolorbutton.h
qgscolorrampbutton.h
qgscolordialog.h
qgscolorschemelist.h
qgscolorswatchgrid.h
@@ -21,6 +21,7 @@
#include "qgssymbol.h"
#include "qgssymbollayerutils.h"
#include "qgscolorramp.h"
#include "qgscolorrampbutton.h"
#include "qgsstyle.h"
#include "qgslogger.h"

@@ -410,21 +411,15 @@ QgsCategorizedSymbolRendererWidget::QgsCategorizedSymbolRendererWidget( QgsVecto

mExpressionWidget->setLayer( mLayer );

cboCategorizedColorRamp->populate( mStyle );
int randomIndex = cboCategorizedColorRamp->findText( tr( "Random colors" ) );
if ( randomIndex != -1 )
{
cboCategorizedColorRamp->setCurrentIndex( randomIndex );
mButtonEditRamp->setEnabled( false );
}
// initiate color ramp button to random
btnColorRamp->setShowRandomColorRamp( true );
btnColorRamp->setRandomColorRamp();

// set project default color ramp
QString defaultColorRamp = QgsProject::instance()->readEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/ColorRamp" ), QLatin1String( "" ) );
if ( defaultColorRamp != QLatin1String( "" ) )
{
int index = cboCategorizedColorRamp->findText( defaultColorRamp, Qt::MatchCaseSensitive );
if ( index >= 0 )
cboCategorizedColorRamp->setCurrentIndex( index );
btnColorRamp->setColorRampFromName( defaultColorRamp );
}

mCategorizedSymbol = QgsSymbol::defaultSymbol( mLayer->geometryType() );
@@ -455,10 +450,8 @@ QgsCategorizedSymbolRendererWidget::QgsCategorizedSymbolRendererWidget( QgsVecto
connect( btnDeleteCategories, SIGNAL( clicked() ), this, SLOT( deleteCategories() ) );
connect( btnDeleteAllCategories, SIGNAL( clicked() ), this, SLOT( deleteAllCategories() ) );
connect( btnAddCategory, SIGNAL( clicked() ), this, SLOT( addCategory() ) );
connect( cbxInvertedColorRamp, SIGNAL( toggled( bool ) ), this, SLOT( applyColorRamp() ) );
connect( cboCategorizedColorRamp, SIGNAL( currentIndexChanged( int ) ), this, SLOT( applyColorRamp() ) );
connect( cboCategorizedColorRamp, SIGNAL( sourceRampEdited() ), this, SLOT( applyColorRamp() ) );
connect( mButtonEditRamp, SIGNAL( clicked() ), cboCategorizedColorRamp, SLOT( editSourceRamp() ) );

connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsCategorizedSymbolRendererWidget::applyColorRamp );

// menus for data-defined rotation/size
QMenu* advMenu = new QMenu;
@@ -501,17 +494,11 @@ void QgsCategorizedSymbolRendererWidget::updateUiFromRenderer()
updateCategorizedSymbolIcon();
}

// set source color ramp
// if a color ramp attached to the renderer, enable the color ramp button
if ( mRenderer->sourceColorRamp() )
{
cboCategorizedColorRamp->setSourceColorRamp( mRenderer->sourceColorRamp() );
cbxInvertedColorRamp->setChecked( mRenderer->invertedColorRamp() );
btnColorRamp->setColorRamp( mRenderer->sourceColorRamp() );
}

if ( cboCategorizedColorRamp->currentText() == tr( "Random colors" ) )
mButtonEditRamp->setEnabled( false );
else
mButtonEditRamp->setEnabled( true );
}

QgsFeatureRenderer* QgsCategorizedSymbolRendererWidget::renderer()
@@ -628,20 +615,6 @@ static void _createCategories( QgsCategoryList& cats, QList<QVariant>& values, Q
}
}

QgsColorRamp* QgsCategorizedSymbolRendererWidget::getColorRamp()
{
QgsColorRamp* ramp = cboCategorizedColorRamp->currentColorRamp();
if ( !ramp )
{
if ( cboCategorizedColorRamp->count() == 0 )
QMessageBox::critical( this, tr( "Error" ), tr( "There are no available color ramps. You can add them in Style Manager." ) );
else if ( !cboCategorizedColorRamp->createNewColorRampSelected() )
QMessageBox::critical( this, tr( "Error" ), tr( "The selected color ramp is not available." ) );
}
return ramp;
}


void QgsCategorizedSymbolRendererWidget::addCategories()
{
QString attrName = mExpressionWidget->currentField();
@@ -759,8 +732,7 @@ void QgsCategorizedSymbolRendererWidget::addCategories()
// recreate renderer
QgsCategorizedSymbolRenderer *r = new QgsCategorizedSymbolRenderer( attrName, cats );
r->setSourceSymbol( mCategorizedSymbol->clone() );
r->setInvertedColorRamp( cbxInvertedColorRamp->isChecked() );
QScopedPointer< QgsColorRamp > ramp( getColorRamp() );
QScopedPointer< QgsColorRamp > ramp( btnColorRamp->colorRamp() );
if ( ramp )
r->setSourceColorRamp( ramp->clone() );

@@ -777,15 +749,9 @@ void QgsCategorizedSymbolRendererWidget::addCategories()

void QgsCategorizedSymbolRendererWidget::applyColorRamp()
{
if ( cboCategorizedColorRamp->currentText() == tr( "Random colors" ) )
mButtonEditRamp->setEnabled( false );
else
mButtonEditRamp->setEnabled( true );

QgsColorRamp* ramp = getColorRamp();
if ( ramp )
if ( !btnColorRamp->isNull() )
{
mRenderer->updateColorRamp( ramp, cbxInvertedColorRamp->isChecked() );
mRenderer->updateColorRamp( btnColorRamp->colorRamp()->clone() );
}
mModel->updateSymbology();
}
@@ -107,7 +107,11 @@ class GUI_EXPORT QgsCategorizedSymbolRendererWidget : public QgsRendererWidget,
void categoriesDoubleClicked( const QModelIndex & idx );
void addCategory();
void addCategories();

/** Applies the color ramp passed on by the color ramp button
*/
void applyColorRamp();

void deleteCategories();
void deleteAllCategories();

@@ -156,8 +160,6 @@ class GUI_EXPORT QgsCategorizedSymbolRendererWidget : public QgsRendererWidget,

void changeCategorySymbol();

QgsColorRamp* getColorRamp();

QList<QgsSymbol*> selectedSymbols() override;
QgsCategoryList selectedCategoryList();
void refreshSymbolView() override;
@@ -1012,8 +1012,7 @@ QgsGradientFillSymbolLayerWidget::QgsGradientFillSymbolLayerWidget( const QgsVec
setupUi( this );
mOffsetUnitWidget->setUnits( QgsUnitTypes::RenderUnitList() << QgsUnitTypes::RenderMillimeters << QgsUnitTypes::RenderMapUnits << QgsUnitTypes::RenderPixels );

cboGradientColorRamp->setShowGradientOnly( true );
cboGradientColorRamp->populate( QgsStyle::defaultStyle() );
btnColorRamp->setShowGradientOnly( true );

btnChangeColor->setAllowAlpha( true );
btnChangeColor->setColorDialogTitle( tr( "Select gradient color" ) );
@@ -1031,9 +1030,7 @@ QgsGradientFillSymbolLayerWidget::QgsGradientFillSymbolLayerWidget( const QgsVec

connect( btnChangeColor, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setColor( const QColor& ) ) );
connect( btnChangeColor2, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setColor2( const QColor& ) ) );
connect( cboGradientColorRamp, SIGNAL( currentIndexChanged( int ) ), this, SLOT( applyColorRamp() ) );
connect( cboGradientColorRamp, SIGNAL( sourceRampEdited() ), this, SLOT( applyColorRamp() ) );
connect( mButtonEditRamp, SIGNAL( clicked() ), cboGradientColorRamp, SLOT( editSourceRamp() ) );
connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsGradientFillSymbolLayerWidget::applyColorRamp );
connect( cboGradientType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setGradientType( int ) ) );
connect( cboCoordinateMode, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setCoordinateMode( int ) ) );
connect( cboGradientSpread, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setGradientSpread( int ) ) );
@@ -1067,7 +1064,7 @@ void QgsGradientFillSymbolLayerWidget::setSymbolLayer( QgsSymbolLayer* layer )
if ( mLayer->gradientColorType() == QgsGradientFillSymbolLayer::SimpleTwoColor )
{
radioTwoColor->setChecked( true );
cboGradientColorRamp->setEnabled( false );
btnColorRamp->setEnabled( false );
}
else
{
@@ -1079,9 +1076,9 @@ void QgsGradientFillSymbolLayerWidget::setSymbolLayer( QgsSymbolLayer* layer )
// set source color ramp
if ( mLayer->colorRamp() )
{
cboGradientColorRamp->blockSignals( true );
cboGradientColorRamp->setSourceColorRamp( mLayer->colorRamp() );
cboGradientColorRamp->blockSignals( false );
btnColorRamp->blockSignals( true );
btnColorRamp->setColorRamp( mLayer->colorRamp() );
btnColorRamp->blockSignals( false );
}

cboGradientType->blockSignals( true );
@@ -1219,11 +1216,10 @@ void QgsGradientFillSymbolLayerWidget::colorModeChanged()

void QgsGradientFillSymbolLayerWidget::applyColorRamp()
{
QgsColorRamp* ramp = cboGradientColorRamp->currentColorRamp();
if ( !ramp )
if ( btnColorRamp->isNull() )
return;

mLayer->setColorRamp( ramp );
mLayer->setColorRamp( btnColorRamp->colorRamp()->clone() );
emit changed();
}

@@ -314,7 +314,11 @@ class GUI_EXPORT QgsGradientFillSymbolLayerWidget : public QgsSymbolLayerWidget,
public slots:
void setColor( const QColor& color );
void setColor2( const QColor& color );

/** Applies the color ramp passed on by the color ramp button
*/
void applyColorRamp();

void setGradientType( int index );
void setCoordinateMode( int index );
void setGradientSpread( int index );

0 comments on commit deb18c4

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