Skip to content

Commit

Permalink
Merge pull request #3829 from nirvn/colorrampbutton_save
Browse files Browse the repository at this point in the history
- [FEATURE] save existing color ramp function
- improve new color ramp type naming
  • Loading branch information
nirvn authored Dec 3, 2016
2 parents 1560c7f + d863c44 commit a26434e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
37 changes: 37 additions & 0 deletions src/gui/qgscolorrampbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
#include "qgsstyle.h"

#include "qgsstylemanagerdialog.h"
#include "qgsstylesavedialog.h"
#include "qgsgradientcolorrampdialog.h"
#include "qgslimitedrandomcolorrampdialog.h"
#include "qgscolorbrewercolorrampdialog.h"
#include "qgscptcitycolorrampdialog.h"
#include "qgspresetcolorrampdialog.h"

#include <QAction>
#include <QMessageBox>
#include <QMouseEvent>
#include <QMenu>
#include <QPainter>
Expand Down Expand Up @@ -316,6 +318,11 @@ void QgsColorRampButton::prepareMenu()
editColorRampAction->setEnabled( !isNull() && !isRandomColorRamp() );
connect( editColorRampAction, &QAction::triggered, this, &QgsColorRampButton::showColorRampDialog );
mMenu->addAction( editColorRampAction );

QAction* saveColorRampAction = new QAction( tr( "Save color ramp..." ), this );
saveColorRampAction->setEnabled( !isNull() && !isRandomColorRamp() );
connect( saveColorRampAction, &QAction::triggered, this, &QgsColorRampButton::saveColorRamp );
mMenu->addAction( saveColorRampAction );
}

void QgsColorRampButton::loadColorRamp()
Expand Down Expand Up @@ -350,6 +357,36 @@ void QgsColorRampButton::createColorRamp()
setColorRampFromName( name );
}

void QgsColorRampButton::saveColorRamp()
{
QgsStyleSaveDialog saveDlg( this, QgsStyle::ColorrampEntity );
if ( !saveDlg.exec() || saveDlg.name().isEmpty() )
{
return;
}

// check if there is no symbol with same name
if ( mStyle->symbolNames().contains( saveDlg.name() ) )
{
int res = QMessageBox::warning( this, tr( "Save color ramp" ),
tr( "Color ramp with name '%1' already exists. Overwrite?" )
.arg( saveDlg.name() ),
QMessageBox::Yes | QMessageBox::No );
if ( res != QMessageBox::Yes )
{
return;
}
mStyle->removeColorRamp( saveDlg.name() );
}

QStringList colorRampTags = saveDlg.tags().split( ',' );

// add new symbol to style and re-populate the list
QgsColorRamp* savedColorRamp = colorRamp();
mStyle->addColorRamp( saveDlg.name(), savedColorRamp );
mStyle->saveColorRamp( saveDlg.name(), savedColorRamp, saveDlg.isFavorite(), colorRampTags );
}

void QgsColorRampButton::invertColorRamp()
{
mColorRamp->invert();
Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgscolorrampbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ class GUI_EXPORT QgsColorRampButton : public QToolButton
*/
void createColorRamp();

/** Creates a new color ramp
*/
void saveColorRamp();

/** Inverts the current color ramp
*/
void invertColorRamp();
Expand Down
10 changes: 5 additions & 5 deletions src/gui/symbology-ng/qgsstylemanagerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ QString QgsStyleManagerDialog::addColorRampStatic( QWidget* parent, QgsStyle* st
if ( rampType.isEmpty() )
{
QStringList rampTypes;
rampTypes << tr( "Gradient" ) << tr( "Random" ) << tr( "ColorBrewer" ) << tr( "Preset colors" );
rampTypes << tr( "cpt-city" ); // todo, only for rasters?
rampTypes << tr( "Gradient" ) << tr( "Color presets" ) << tr( "Random" ) << tr( "Catalog: cpt-city" );
rampTypes << tr( "Catalog: ColorBrewer" );
rampType = QInputDialog::getItem( parent, tr( "Color ramp type" ),
tr( "Please select color ramp type:" ), rampTypes, 0, false, &ok );
}
Expand Down Expand Up @@ -499,7 +499,7 @@ QString QgsStyleManagerDialog::addColorRampStatic( QWidget* parent, QgsStyle* st
ramp.reset( dlg.ramp().clone() );
name = tr( "new random ramp" );
}
else if ( rampType == tr( "ColorBrewer" ) )
else if ( rampType == tr( "Catalog: ColorBrewer" ) )
{
QgsColorBrewerColorRampDialog dlg( QgsColorBrewerColorRamp(), parent );
if ( !dlg.exec() )
Expand All @@ -509,7 +509,7 @@ QString QgsStyleManagerDialog::addColorRampStatic( QWidget* parent, QgsStyle* st
ramp.reset( dlg.ramp().clone() );
name = dlg.ramp().schemeName() + QString::number( dlg.ramp().colors() );
}
else if ( rampType == tr( "Preset colors" ) )
else if ( rampType == tr( "Color presets" ) )
{
QgsPresetColorRampDialog dlg( QgsPresetSchemeColorRamp(), parent );
if ( !dlg.exec() )
Expand All @@ -519,7 +519,7 @@ QString QgsStyleManagerDialog::addColorRampStatic( QWidget* parent, QgsStyle* st
ramp.reset( dlg.ramp().clone() );
name = tr( "new preset ramp" );
}
else if ( rampType == tr( "cpt-city" ) )
else if ( rampType == tr( "Catalog: cpt-city" ) )
{
QgsCptCityColorRampDialog dlg( QgsCptCityColorRamp( QLatin1String( "" ), QLatin1String( "" ) ), parent );
if ( !dlg.exec() )
Expand Down

0 comments on commit a26434e

Please sign in to comment.