Skip to content

Commit

Permalink
color ramp button widget for gradiant fill and categorized renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Nov 30, 2016
1 parent fa16e3e commit deb18c4
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 137 deletions.
6 changes: 1 addition & 5 deletions python/core/qgscolorramp.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -226,8 +226,6 @@ class QgsLimitedRandomColorRamp : QgsColorRamp

virtual QString type() const;

virtual void invert();

virtual QgsLimitedRandomColorRamp* clone() const /Factory/;

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

QString type() const;

virtual void invert();

virtual QgsRandomColorRamp* clone() const /Factory/;

QgsStringMap properties() const;
Expand Down
7 changes: 1 addition & 6 deletions python/core/symbology-ng/qgscategorizedsymbolrenderer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python/gui/gui.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ class QgsCategorizedSymbolRendererWidget : QgsRendererWidget

void changeCategorySymbol();

QgsColorRamp* getColorRamp();

QList<QgsSymbol*> selectedSymbols();
QgsCategoryList selectedCategoryList();
void refreshSymbolView();
Expand Down
7 changes: 3 additions & 4 deletions src/core/qgscolorramp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 1 addition & 11 deletions src/core/symbology-ng/qgscategorizedsymbolrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -472,7 +471,6 @@ QgsCategorizedSymbolRenderer* QgsCategorizedSymbolRenderer::clone() const
if ( mSourceColorRamp.data() )
{
r->setSourceColorRamp( mSourceColorRamp->clone() );
r->setInvertedColorRamp( mInvertedColorRamp );
}
r->setUsingSymbolLevels( usingSymbolLevels() );

Expand Down Expand Up @@ -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" ) );
Expand Down Expand Up @@ -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" ) );
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
8 changes: 1 addition & 7 deletions src/core/symbology-ng/qgscategorizedsymbolrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ SET(QGIS_GUI_SRCS
qgscollapsiblegroupbox.cpp
qgscolorbrewercolorrampdialog.cpp
qgscolorbutton.cpp
qgscolorrampbutton.cpp
qgscolordialog.cpp
qgscolorschemelist.cpp
qgscolorswatchgrid.cpp
Expand Down Expand Up @@ -356,6 +357,7 @@ SET(QGIS_GUI_MOC_HDRS
qgscollapsiblegroupbox.h
qgscolorbrewercolorrampdialog.h
qgscolorbutton.h
qgscolorrampbutton.h
qgscolordialog.h
qgscolorschemelist.h
qgscolorswatchgrid.h
Expand Down
58 changes: 12 additions & 46 deletions src/gui/symbology-ng/qgscategorizedsymbolrendererwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "qgssymbol.h"
#include "qgssymbollayerutils.h"
#include "qgscolorramp.h"
#include "qgscolorrampbutton.h"
#include "qgsstyle.h"
#include "qgslogger.h"

Expand Down Expand Up @@ -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() );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() );

Expand All @@ -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();
}
Expand Down
6 changes: 4 additions & 2 deletions src/gui/symbology-ng/qgscategorizedsymbolrendererwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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

void changeCategorySymbol();

QgsColorRamp* getColorRamp();

QList<QgsSymbol*> selectedSymbols() override;
QgsCategoryList selectedCategoryList();
void refreshSymbolView() override;
Expand Down
20 changes: 8 additions & 12 deletions src/gui/symbology-ng/qgssymbollayerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) );
Expand All @@ -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 ) ) );
Expand Down Expand Up @@ -1067,7 +1064,7 @@ void QgsGradientFillSymbolLayerWidget::setSymbolLayer( QgsSymbolLayer* layer )
if ( mLayer->gradientColorType() == QgsGradientFillSymbolLayer::SimpleTwoColor )
{
radioTwoColor->setChecked( true );
cboGradientColorRamp->setEnabled( false );
btnColorRamp->setEnabled( false );
}
else
{
Expand All @@ -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 );
Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 4 additions & 0 deletions src/gui/symbology-ng/qgssymbollayerwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Loading

0 comments on commit deb18c4

Please sign in to comment.