Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] save existing color ramp function
  • Loading branch information
nirvn committed Dec 3, 2016
1 parent 37c43df commit 04c6007
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/gui/qgscolorrampbutton.cpp
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
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
2 changes: 1 addition & 1 deletion src/gui/symbology-ng/qgsstylemanagerdialog.cpp
Expand Up @@ -469,7 +469,7 @@ QString QgsStyleManagerDialog::addColorRampStatic( QWidget* parent, QgsStyle* st
{
QStringList rampTypes;
rampTypes << tr( "Gradient" ) << tr( "Random" ) << tr( "ColorBrewer" ) << tr( "Preset colors" );
rampTypes << tr( "cpt-city" ); // todo, only for rasters?
rampTypes << tr( "cpt-city" );
rampType = QInputDialog::getItem( parent, tr( "Color ramp type" ),
tr( "Please select color ramp type:" ), rampTypes, 0, false, &ok );
}
Expand Down

0 comments on commit 04c6007

Please sign in to comment.