Remove a lot of duplicate hardcoded strings for color ramp types and use static method instead

This commit is contained in:
Nyall Dawson 2020-10-19 08:31:21 +10:00
parent 9cdcef7b95
commit b637b14d07
10 changed files with 272 additions and 98 deletions

View File

@ -20,17 +20,17 @@ Abstract base class for color ramps
#include "qgscolorramp.h"
%End
%ConvertToSubClassCode
if ( sipCpp->type() == "gradient" )
if ( sipCpp->type() == QgsGradientColorRamp::typeString() )
sipType = sipType_QgsGradientColorRamp;
else if ( sipCpp->type() == "random" )
else if ( sipCpp->type() == QgsLimitedRandomColorRamp::typeString() )
sipType = sipType_QgsLimitedRandomColorRamp;
else if ( sipCpp->type() == "randomcolors" )
else if ( sipCpp->type() == QgsRandomColorRamp::typeString() )
sipType = sipType_QgsRandomColorRamp;
else if ( sipCpp->type() == "preset" )
else if ( sipCpp->type() == QgsPresetSchemeColorRamp::typeString() )
sipType = sipType_QgsPresetSchemeColorRamp;
else if ( sipCpp->type() == "colorbrewer" )
else if ( sipCpp->type() == QgsColorBrewerColorRamp::typeString() )
sipType = sipType_QgsColorBrewerColorRamp;
else if ( sipCpp->type() == "cpt-city" )
else if ( sipCpp->type() == QgsCptCityColorRamp::typeString() )
sipType = sipType_QgsCptCityColorRamp;
else
sipType = 0;
@ -77,6 +77,17 @@ Creates a clone of the color ramp.
virtual QgsStringMap properties() const = 0;
%Docstring
Returns a string map containing all the color ramp's properties.
%End
static QList< QPair< QString, QString > > rampTypes();
%Docstring
Returns a list of available ramp types, where the first value in each item is the :py:func:`QgsColorRamp.type()` string
and the second is a user friendly, translated name for the color ramp type.
The ramp types are returned in a order of precedence for exposing in UI, with more commonly used types
listed first.
.. versionadded:: 3.16
%End
};
@ -148,7 +159,16 @@ Creates a new QgsColorRamp from a map of properties
virtual QColor color( double value ) const;
static QString typeString();
%Docstring
Returns the string identifier for QgsGradientColorRamp.
.. versionadded:: 3.16
%End
virtual QString type() const;
virtual void invert();
virtual QgsGradientColorRamp *clone() const /Factory/;
@ -320,7 +340,16 @@ map.
virtual QColor color( double value ) const;
static QString typeString();
%Docstring
Returns the string identifier for QgsLimitedRandomColorRamp.
.. versionadded:: 3.16
%End
virtual QString type() const;
virtual QgsLimitedRandomColorRamp *clone() const /Factory/;
virtual QgsStringMap properties() const;
@ -472,6 +501,13 @@ by subsequent calls to :py:func:`~QgsRandomColorRamp.color`.
:param colorCount: number of unique colors
.. versionadded:: 2.5
%End
static QString typeString();
%Docstring
Returns the string identifier for QgsRandomColorRamp.
.. versionadded:: 3.16
%End
virtual QString type() const;
@ -546,7 +582,16 @@ Returns the list of colors used by the ramp.
virtual QColor color( double value ) const;
static QString typeString();
%Docstring
Returns the string identifier for QgsPresetSchemeColorRamp.
.. versionadded:: 3.16
%End
virtual QString type() const;
virtual void invert();
virtual QgsPresetSchemeColorRamp *clone() const /Factory/;
@ -603,6 +648,14 @@ map.
virtual QColor color( double value ) const;
static QString typeString();
%Docstring
Returns the string identifier for QgsColorBrewerColorRamp.
.. versionadded:: 3.16
%End
virtual QString type() const;
virtual void invert();
@ -711,8 +764,16 @@ Constructor for QgsCptCityColorRamp
static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) /Factory/;
static QString typeString();
%Docstring
Returns the string identifier for QgsCptCityColorRamp.
.. versionadded:: 3.16
%End
virtual QString type() const;
virtual void invert();

View File

@ -33,10 +33,13 @@ this style will be shown in the dialog, and changes made within the dialog will
The ``style`` object must last for the lifetime of the dialog.
%End
static QString addColorRampStatic( QWidget *parent, QgsStyle *style,
QString RampType = QString() );
const QString &RampType = QString() );
%Docstring
Opens the add color ramp dialog, returning the new color ramp's name if the ramp has been added.
The ``RampType`` argument should be set to a string representing a valid :py:func:`QgsColorRamp.type()` value.
%End
void setFavoritesGroupVisible( bool show );
@ -226,7 +229,16 @@ Context menu for the listItems ( symbols list )
%End
protected slots:
bool addColorRamp( const QString &type = QString() );
%Docstring
Triggers adding a new color ramp.
If ``type`` is set to a string representing a valid :py:func:`QgsColorRamp.type()` value then a new ramp
of that type will be created. If ``type`` is empty, then the user will be prompted to select
the color ramp type.
%End
void addFavoriteSelectedSymbols();
%Docstring
Add selected symbols to favorites

View File

@ -180,9 +180,9 @@ void QgsGlowEffect::readProperties( const QgsStringMap &props )
mColor = QgsSymbolLayerUtils::decodeColor( props.value( QStringLiteral( "single_color" ) ) );
}
//attempt to create color ramp from props
//attempt to create color ramp from props
delete mRamp;
if ( props.contains( QStringLiteral( "rampType" ) ) && props[QStringLiteral( "rampType" )] == QLatin1String( "cpt-city" ) )
if ( props.contains( QStringLiteral( "rampType" ) ) && props[QStringLiteral( "rampType" )] == QgsCptCityColorRamp::typeString() )
{
mRamp = QgsCptCityColorRamp::create( props );
}

View File

@ -160,6 +160,11 @@ QColor QgsGradientColorRamp::color( double value ) const
}
}
QString QgsGradientColorRamp::type() const
{
return QgsGradientColorRamp::typeString();
}
void QgsGradientColorRamp::invert()
{
QgsGradientStopsList newStops;
@ -354,6 +359,11 @@ QColor QgsLimitedRandomColorRamp::color( double value ) const
return QColor();
}
QString QgsLimitedRandomColorRamp::type() const
{
return QgsLimitedRandomColorRamp::typeString();
}
QgsLimitedRandomColorRamp *QgsLimitedRandomColorRamp::clone() const
{
return new QgsLimitedRandomColorRamp( mCount, mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax );
@ -478,7 +488,7 @@ void QgsRandomColorRamp::setTotalColorCount( const int colorCount )
QString QgsRandomColorRamp::type() const
{
return QStringLiteral( "randomcolors" );
return QgsRandomColorRamp::typeString();
}
QgsRandomColorRamp *QgsRandomColorRamp::clone() const
@ -631,6 +641,11 @@ QgsColorRamp *QgsCptCityColorRamp::create( const QgsStringMap &props )
return new QgsCptCityColorRamp( schemeName, variantName, inverted );
}
QString QgsCptCityColorRamp::type() const
{
return QgsCptCityColorRamp::typeString();
}
void QgsCptCityColorRamp::invert()
{
mInverted = !mInverted;
@ -878,6 +893,11 @@ QColor QgsPresetSchemeColorRamp::color( double value ) const
return QColor();
}
QString QgsPresetSchemeColorRamp::type() const
{
return QgsPresetSchemeColorRamp::typeString();
}
void QgsPresetSchemeColorRamp::invert()
{
QgsNamedColorList tmpColors;
@ -915,3 +935,15 @@ QgsNamedColorList QgsPresetSchemeColorRamp::fetchColors( const QString &, const
{
return mColors;
}
QList<QPair<QString, QString> > QgsColorRamp::rampTypes()
{
return QList<QPair<QString, QString> >
{
qMakePair( QgsGradientColorRamp::typeString(), QObject::tr( "Gradient" ) ),
qMakePair( QgsPresetSchemeColorRamp::typeString(), QObject::tr( "Color Presets" ) ),
qMakePair( QgsLimitedRandomColorRamp::typeString(), QObject::tr( "Random" ) ),
qMakePair( QgsCptCityColorRamp::typeString(), QObject::tr( "Catalog: cpt-city" ) ),
qMakePair( QgsColorBrewerColorRamp::typeString(), QObject::tr( "Catalog: ColorBrewer" ) )
};
}

View File

@ -33,17 +33,17 @@ class CORE_EXPORT QgsColorRamp
#ifdef SIP_RUN
SIP_CONVERT_TO_SUBCLASS_CODE
if ( sipCpp->type() == "gradient" )
if ( sipCpp->type() == QgsGradientColorRamp::typeString() )
sipType = sipType_QgsGradientColorRamp;
else if ( sipCpp->type() == "random" )
else if ( sipCpp->type() == QgsLimitedRandomColorRamp::typeString() )
sipType = sipType_QgsLimitedRandomColorRamp;
else if ( sipCpp->type() == "randomcolors" )
else if ( sipCpp->type() == QgsRandomColorRamp::typeString() )
sipType = sipType_QgsRandomColorRamp;
else if ( sipCpp->type() == "preset" )
else if ( sipCpp->type() == QgsPresetSchemeColorRamp::typeString() )
sipType = sipType_QgsPresetSchemeColorRamp;
else if ( sipCpp->type() == "colorbrewer" )
else if ( sipCpp->type() == QgsColorBrewerColorRamp::typeString() )
sipType = sipType_QgsColorBrewerColorRamp;
else if ( sipCpp->type() == "cpt-city" )
else if ( sipCpp->type() == QgsCptCityColorRamp::typeString() )
sipType = sipType_QgsCptCityColorRamp;
else
sipType = 0;
@ -90,6 +90,17 @@ class CORE_EXPORT QgsColorRamp
* Returns a string map containing all the color ramp's properties.
*/
virtual QgsStringMap properties() const = 0;
/**
* Returns a list of available ramp types, where the first value in each item is the QgsColorRamp::type() string
* and the second is a user friendly, translated name for the color ramp type.
*
* The ramp types are returned in a order of precedence for exposing in UI, with more commonly used types
* listed first.
*
* \since QGIS 3.16
*/
static QList< QPair< QString, QString > > rampTypes();
};
/**
@ -159,7 +170,15 @@ class CORE_EXPORT QgsGradientColorRamp : public QgsColorRamp
int count() const override { return mStops.count() + 2; }
double value( int index ) const override;
QColor color( double value ) const override;
QString type() const override { return QStringLiteral( "gradient" ); }
/**
* Returns the string identifier for QgsGradientColorRamp.
*
* \since QGIS 3.16
*/
static QString typeString() { return QStringLiteral( "gradient" ); }
QString type() const override;
void invert() override;
QgsGradientColorRamp *clone() const override SIP_FACTORY;
QgsStringMap properties() const override;
@ -309,7 +328,15 @@ class CORE_EXPORT QgsLimitedRandomColorRamp : public QgsColorRamp
double value( int index ) const override;
QColor color( double value ) const override;
QString type() const override { return QStringLiteral( "random" ); }
/**
* Returns the string identifier for QgsLimitedRandomColorRamp.
*
* \since QGIS 3.16
*/
static QString typeString() { return QStringLiteral( "random" ); }
QString type() const override;
QgsLimitedRandomColorRamp *clone() const override SIP_FACTORY;
QgsStringMap properties() const override;
int count() const override { return mCount; }
@ -448,6 +475,13 @@ class CORE_EXPORT QgsRandomColorRamp: public QgsColorRamp
*/
virtual void setTotalColorCount( int colorCount );
/**
* Returns the string identifier for QgsRandomColorRamp.
*
* \since QGIS 3.16
*/
static QString typeString() { return QStringLiteral( "randomcolors" ); }
QString type() const override;
QgsRandomColorRamp *clone() const override SIP_FACTORY;
@ -507,7 +541,15 @@ class CORE_EXPORT QgsPresetSchemeColorRamp : public QgsColorRamp, public QgsColo
double value( int index ) const override;
QColor color( double value ) const override;
QString type() const override { return QStringLiteral( "preset" ); }
/**
* Returns the string identifier for QgsPresetSchemeColorRamp.
*
* \since QGIS 3.16
*/
static QString typeString() { return QStringLiteral( "preset" ); }
QString type() const override;
void invert() override;
QgsPresetSchemeColorRamp *clone() const override SIP_FACTORY;
QgsStringMap properties() const override;
@ -556,7 +598,15 @@ class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
double value( int index ) const override;
QColor color( double value ) const override;
QString type() const override { return QStringLiteral( "colorbrewer" ); }
/**
* Returns the string identifier for QgsColorBrewerColorRamp.
*
* \since QGIS 3.16
*/
static QString typeString() { return QStringLiteral( "colorbrewer" ); }
QString type() const override { return QgsColorBrewerColorRamp::typeString(); }
void invert() override;
QgsColorBrewerColorRamp *clone() const override SIP_FACTORY;
QgsStringMap properties() const override;
@ -653,7 +703,14 @@ class CORE_EXPORT QgsCptCityColorRamp : public QgsGradientColorRamp
static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
QString type() const override { return QStringLiteral( "cpt-city" ); }
/**
* Returns the string identifier for QgsCptCityColorRamp.
*
* \since QGIS 3.16
*/
static QString typeString() { return QStringLiteral( "cpt-city" ); }
QString type() const override;
void invert() override;

View File

@ -567,7 +567,7 @@ QgsSymbolLayer *QgsGradientFillSymbolLayer::create( const QgsStringMap &props )
//attempt to create color ramp from props
QgsColorRamp *gradientRamp = nullptr;
if ( props.contains( QStringLiteral( "rampType" ) ) && props[QStringLiteral( "rampType" )] == QLatin1String( "cpt-city" ) )
if ( props.contains( QStringLiteral( "rampType" ) ) && props[QStringLiteral( "rampType" )] == QgsCptCityColorRamp::typeString() )
{
gradientRamp = QgsCptCityColorRamp::create( props );
}
@ -848,7 +848,7 @@ void QgsGradientFillSymbolLayer::applyGradient( const QgsSymbolRenderContext &co
//add stops to gradient
if ( gradientColorType == QgsGradientFillSymbolLayer::ColorRamp && gradientRamp &&
( gradientRamp->type() == QLatin1String( "gradient" ) || gradientRamp->type() == QLatin1String( "cpt-city" ) ) )
( gradientRamp->type() == QgsGradientColorRamp::typeString() || gradientRamp->type() == QgsCptCityColorRamp::typeString() ) )
{
//color ramp gradient
QgsGradientColorRamp *gradRamp = static_cast<QgsGradientColorRamp *>( gradientRamp );
@ -1057,7 +1057,7 @@ QgsSymbolLayer *QgsShapeburstFillSymbolLayer::create( const QgsStringMap &props
//attempt to create color ramp from props
QgsColorRamp *gradientRamp = nullptr;
if ( props.contains( QStringLiteral( "rampType" ) ) && props[QStringLiteral( "rampType" )] == QLatin1String( "cpt-city" ) )
if ( props.contains( QStringLiteral( "rampType" ) ) && props[QStringLiteral( "rampType" )] == QgsCptCityColorRamp::typeString() )
{
gradientRamp = QgsCptCityColorRamp::create( props );
}

View File

@ -3091,15 +3091,15 @@ QgsColorRamp *QgsSymbolLayerUtils::loadColorRamp( QDomElement &element )
// parse properties
QgsStringMap props = QgsSymbolLayerUtils::parseProperties( element );
if ( rampType == QLatin1String( "gradient" ) )
if ( rampType == QgsGradientColorRamp::typeString() )
return QgsGradientColorRamp::create( props );
else if ( rampType == QLatin1String( "random" ) )
else if ( rampType == QgsLimitedRandomColorRamp::typeString() )
return QgsLimitedRandomColorRamp::create( props );
else if ( rampType == QLatin1String( "colorbrewer" ) )
else if ( rampType == QgsColorBrewerColorRamp::typeString() )
return QgsColorBrewerColorRamp::create( props );
else if ( rampType == QLatin1String( "cpt-city" ) )
else if ( rampType == QgsCptCityColorRamp::typeString() )
return QgsCptCityColorRamp::create( props );
else if ( rampType == QLatin1String( "preset" ) )
else if ( rampType == QgsPresetSchemeColorRamp::typeString() )
return QgsPresetSchemeColorRamp::create( props );
else
{
@ -3153,15 +3153,15 @@ QgsColorRamp *QgsSymbolLayerUtils::loadColorRamp( const QVariant &value )
props.insert( property.key(), property.value().toString() );
}
if ( rampType == QLatin1String( "gradient" ) )
if ( rampType == QgsGradientColorRamp::typeString() )
return QgsGradientColorRamp::create( props );
else if ( rampType == QLatin1String( "random" ) )
else if ( rampType == QgsLimitedRandomColorRamp::typeString() )
return QgsLimitedRandomColorRamp::create( props );
else if ( rampType == QLatin1String( "colorbrewer" ) )
else if ( rampType == QgsColorBrewerColorRamp::typeString() )
return QgsColorBrewerColorRamp::create( props );
else if ( rampType == QLatin1String( "cpt-city" ) )
else if ( rampType == QgsCptCityColorRamp::typeString() )
return QgsCptCityColorRamp::create( props );
else if ( rampType == QLatin1String( "preset" ) )
else if ( rampType == QgsPresetSchemeColorRamp::typeString() )
return QgsPresetSchemeColorRamp::create( props );
else
{

View File

@ -80,7 +80,7 @@ void QgsColorRampButton::showColorRampDialog()
setColorRampName( QString() );
if ( currentRamp->type() == QLatin1String( "gradient" ) )
if ( currentRamp->type() == QgsGradientColorRamp::typeString() )
{
QgsGradientColorRamp *gradRamp = static_cast<QgsGradientColorRamp *>( currentRamp.get() );
QgsGradientColorRampDialog dlg( *gradRamp, this );
@ -90,7 +90,7 @@ void QgsColorRampButton::showColorRampDialog()
setColorRamp( dlg.ramp().clone() );
}
}
else if ( currentRamp->type() == QLatin1String( "random" ) )
else if ( currentRamp->type() == QgsLimitedRandomColorRamp::typeString() )
{
QgsLimitedRandomColorRamp *randRamp = static_cast<QgsLimitedRandomColorRamp *>( currentRamp.get() );
if ( panelMode )
@ -109,7 +109,7 @@ void QgsColorRampButton::showColorRampDialog()
}
}
}
else if ( currentRamp->type() == QLatin1String( "preset" ) )
else if ( currentRamp->type() == QgsPresetSchemeColorRamp::typeString() )
{
QgsPresetSchemeColorRamp *presetRamp = static_cast<QgsPresetSchemeColorRamp *>( currentRamp.get() );
if ( panelMode )
@ -128,7 +128,7 @@ void QgsColorRampButton::showColorRampDialog()
}
}
}
else if ( currentRamp->type() == QLatin1String( "colorbrewer" ) )
else if ( currentRamp->type() == QgsColorBrewerColorRamp::typeString() )
{
QgsColorBrewerColorRamp *brewerRamp = static_cast<QgsColorBrewerColorRamp *>( currentRamp.get() );
if ( panelMode )
@ -147,7 +147,7 @@ void QgsColorRampButton::showColorRampDialog()
}
}
}
else if ( currentRamp->type() == QLatin1String( "cpt-city" ) )
else if ( currentRamp->type() == QgsCptCityColorRamp::typeString() )
{
QgsCptCityColorRamp *cptCityRamp = static_cast<QgsCptCityColorRamp *>( currentRamp.get() );
QgsCptCityColorRampDialog dlg( *cptCityRamp, this );
@ -274,7 +274,7 @@ void QgsColorRampButton::prepareMenu()
{
std::unique_ptr< QgsColorRamp > ramp( mStyle->colorRamp( *it ) );
if ( !mShowGradientOnly || ( ramp->type() == QLatin1String( "gradient" ) || ramp->type() == QLatin1String( "cpt-city" ) ) )
if ( !mShowGradientOnly || ( ramp->type() == QgsGradientColorRamp::typeString() || ramp->type() == QgsCptCityColorRamp::typeString() ) )
{
QIcon icon = QgsSymbolLayerUtils::colorRampPreviewIcon( ramp.get(), QSize( iconSize, iconSize ) );
QAction *ra = new QAction( *it, this );
@ -294,7 +294,7 @@ void QgsColorRampButton::prepareMenu()
{
std::unique_ptr< QgsColorRamp > ramp( mStyle->colorRamp( *it ) );
if ( !mShowGradientOnly || ( ramp->type() == QLatin1String( "gradient" ) || ramp->type() == QLatin1String( "cpt-city" ) ) )
if ( !mShowGradientOnly || ( ramp->type() == QgsGradientColorRamp::typeString() || ramp->type() == QgsCptCityColorRamp::typeString() ) )
{
QIcon icon = QgsSymbolLayerUtils::colorRampPreviewIcon( ramp.get(), QSize( iconSize, iconSize ) );
QAction *ra = new QAction( *it, this );
@ -334,43 +334,49 @@ void QgsColorRampButton::loadColorRamp()
void QgsColorRampButton::createColorRamp()
{
QStringList rampTypes;
QString rampType;
bool ok = true;
QList< QPair< QString, QString > > rampTypes = QgsColorRamp::rampTypes();
QStringList rampTypeNames;
rampTypeNames.reserve( rampTypes.size() );
if ( mShowGradientOnly )
{
rampTypes << tr( "Gradient" ) << tr( "Catalog: cpt-city" );
rampTypes.erase( std::remove_if( rampTypes.begin(), rampTypes.end(), []( const QPair< QString, QString > &type )
{
return type.first != QgsGradientColorRamp::typeString() && type.first != QgsCptCityColorRamp::typeString();
} ), rampTypes.end() );
}
else
{
rampTypes << tr( "Gradient" ) << tr( "Color presets" ) << tr( "Random" ) << tr( "Catalog: cpt-city" );
rampTypes << tr( "Catalog: ColorBrewer" );
}
rampType = QInputDialog::getItem( this, tr( "Color ramp type" ),
tr( "Please select color ramp type:" ), rampTypes, 0, false, &ok );
if ( !ok || rampType.isEmpty() )
for ( const QPair< QString, QString > &type : rampTypes )
rampTypeNames << type.second;
const QString selectedRampTypeName = QInputDialog::getItem( this, tr( "Color ramp type" ),
tr( "Please select color ramp type:" ), rampTypeNames, 0, false, &ok );
if ( !ok || selectedRampTypeName.isEmpty() )
return;
QgsColorRamp *ramp = nullptr;
if ( rampType == tr( "Gradient" ) )
const QString rampType = rampTypes.value( rampTypeNames.indexOf( selectedRampTypeName ) ).first;
QgsColorRamp *ramp = nullptr;
if ( rampType == QgsGradientColorRamp::typeString() )
{
ramp = new QgsGradientColorRamp();
}
else if ( rampType == tr( "Random" ) )
else if ( rampType == QgsLimitedRandomColorRamp::typeString() )
{
ramp = new QgsLimitedRandomColorRamp();
}
else if ( rampType == tr( "Catalog: ColorBrewer" ) )
else if ( rampType == QgsColorBrewerColorRamp::typeString() )
{
ramp = new QgsColorBrewerColorRamp();
}
else if ( rampType == tr( "Color presets" ) )
else if ( rampType == QgsPresetSchemeColorRamp::typeString() )
{
ramp = new QgsPresetSchemeColorRamp();
}
else if ( rampType == tr( "Catalog: cpt-city" ) )
else if ( rampType == QgsCptCityColorRamp::typeString() )
{
ramp = new QgsCptCityColorRamp( QString(), QString() );
}

View File

@ -325,11 +325,6 @@ QgsStyleManagerDialog::QgsStyleManagerDialog( QgsStyle *style, QWidget *parent,
if ( !mReadOnly )
{
// Menu for the "Add item" toolbutton when in colorramp mode
QStringList rampTypes;
rampTypes << tr( "Gradient…" ) << tr( "Color presets…" ) << tr( "Random…" ) << tr( "Catalog: cpt-city…" );
rampTypes << tr( "Catalog: ColorBrewer…" );
mMenuBtnAddItemAll = new QMenu( this );
mMenuBtnAddItemColorRamp = new QMenu( this );
mMenuBtnAddItemLabelSettings = new QMenu( this );
@ -346,19 +341,14 @@ QgsStyleManagerDialog::QgsStyleManagerDialog( QgsStyle *style, QWidget *parent,
connect( item, &QAction::triggered, this, [ = ]( bool ) { addSymbol( QgsSymbol::Fill ); } );
mMenuBtnAddItemAll->addAction( item );
mMenuBtnAddItemAll->addSeparator();
for ( const std::pair< QString, QString > &rampType :
{
std::pair< QString, QString > { tr( "Gradient…" ), tr( "Gradient" ) },
std::pair< QString, QString > { tr( "Color presets…" ), tr( "Color presets" ) },
std::pair< QString, QString > { tr( "Random…" ), tr( "Random" ) },
std::pair< QString, QString > { tr( "Catalog: cpt-city…" ), tr( "Catalog: cpt-city" ) },
std::pair< QString, QString > { tr( "Catalog: ColorBrewer…" ), tr( "Catalog: ColorBrewer" ) }
} )
const QList< QPair< QString, QString > > rampTypes = QgsColorRamp::rampTypes();
for ( const QPair< QString, QString > &rampType : rampTypes )
{
item = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "styleicons/color.svg" ) ), rampType.first, this );
connect( item, &QAction::triggered, this, [ = ]( bool ) { addColorRamp( rampType.second ); } );
item = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "styleicons/color.svg" ) ), tr( "%1…" ).arg( rampType.second ), this );
connect( item, &QAction::triggered, this, [ = ]( bool ) { addColorRamp( rampType.first ); } );
mMenuBtnAddItemAll->addAction( item );
mMenuBtnAddItemColorRamp->addAction( new QAction( rampType.first, this ) );
mMenuBtnAddItemColorRamp->addAction( item );
}
mMenuBtnAddItemAll->addSeparator();
item = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "mIconFieldText.svg" ) ), tr( "Text Format…" ), this );
@ -405,9 +395,6 @@ QgsStyleManagerDialog::QgsStyleManagerDialog( QgsStyle *style, QWidget *parent,
connect( item, &QAction::triggered, this, [ = ]( bool ) { addSymbol3D( QStringLiteral( "polygon" ) ); } );
mMenuBtnAddItemAll->addAction( item );
mMenuBtnAddItemSymbol3D->addAction( item );
connect( mMenuBtnAddItemColorRamp, &QMenu::triggered,
this, [ = ]( QAction * action ) { addColorRamp( action->data().toString() ); } );
}
// Context menu for symbols/colorramps. The menu entries for every group are created when displaying the menu.
@ -1361,25 +1348,31 @@ bool QgsStyleManagerDialog::addSymbol( int symbolType )
}
QString QgsStyleManagerDialog::addColorRampStatic( QWidget *parent, QgsStyle *style, QString rampType )
QString QgsStyleManagerDialog::addColorRampStatic( QWidget *parent, QgsStyle *style, const QString &type )
{
// let the user choose the color ramp type if rampType is not given
bool ok = true;
QString rampType = type;
if ( rampType.isEmpty() )
{
QStringList rampTypes;
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 );
// let the user choose the color ramp type if rampType is not given
bool ok = true;
const QList< QPair< QString, QString > > rampTypes = QgsColorRamp::rampTypes();
QStringList rampTypeNames;
rampTypeNames.reserve( rampTypes.size() );
for ( const QPair< QString, QString > &type : rampTypes )
rampTypeNames << type.second;
const QString selectedRampTypeName = QInputDialog::getItem( parent, tr( "Color Ramp Type" ),
tr( "Please select color ramp type:" ), rampTypeNames, 0, false, &ok );
if ( !ok || selectedRampTypeName.isEmpty() )
return QString();
rampType = rampTypes.value( rampTypeNames.indexOf( selectedRampTypeName ) ).first;
}
if ( !ok || rampType.isEmpty() )
return QString();
QString name = tr( "new ramp" );
std::unique_ptr< QgsColorRamp > ramp;
if ( rampType == tr( "Gradient" ) )
if ( rampType == QgsGradientColorRamp::typeString() )
{
QgsGradientColorRampDialog dlg( QgsGradientColorRamp(), parent );
if ( !dlg.exec() )
@ -1389,7 +1382,7 @@ QString QgsStyleManagerDialog::addColorRampStatic( QWidget *parent, QgsStyle *st
ramp.reset( dlg.ramp().clone() );
name = tr( "new gradient ramp" );
}
else if ( rampType == tr( "Random" ) )
else if ( rampType == QgsLimitedRandomColorRamp::typeString() )
{
QgsLimitedRandomColorRampDialog dlg( QgsLimitedRandomColorRamp(), parent );
if ( !dlg.exec() )
@ -1399,7 +1392,7 @@ QString QgsStyleManagerDialog::addColorRampStatic( QWidget *parent, QgsStyle *st
ramp.reset( dlg.ramp().clone() );
name = tr( "new random ramp" );
}
else if ( rampType == tr( "Catalog: ColorBrewer" ) )
else if ( rampType == QgsColorBrewerColorRamp::typeString() )
{
QgsColorBrewerColorRampDialog dlg( QgsColorBrewerColorRamp(), parent );
if ( !dlg.exec() )
@ -1409,7 +1402,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( "Color presets" ) )
else if ( rampType == QgsPresetSchemeColorRamp::typeString() )
{
QgsPresetColorRampDialog dlg( QgsPresetSchemeColorRamp(), parent );
if ( !dlg.exec() )
@ -1419,7 +1412,7 @@ QString QgsStyleManagerDialog::addColorRampStatic( QWidget *parent, QgsStyle *st
ramp.reset( dlg.ramp().clone() );
name = tr( "new preset ramp" );
}
else if ( rampType == tr( "Catalog: cpt-city" ) )
else if ( rampType == QgsCptCityColorRamp::typeString() )
{
QgsCptCityColorRampDialog dlg( QgsCptCityColorRamp( QString(), QString() ), parent );
if ( !dlg.exec() )
@ -1602,7 +1595,7 @@ bool QgsStyleManagerDialog::editColorRamp()
std::unique_ptr< QgsColorRamp > ramp( mStyle->colorRamp( name ) );
if ( ramp->type() == QLatin1String( "gradient" ) )
if ( ramp->type() == QgsGradientColorRamp::typeString() )
{
QgsGradientColorRamp *gradRamp = static_cast<QgsGradientColorRamp *>( ramp.get() );
QgsGradientColorRampDialog dlg( *gradRamp, this );
@ -1615,7 +1608,7 @@ bool QgsStyleManagerDialog::editColorRamp()
}
ramp.reset( dlg.ramp().clone() );
}
else if ( ramp->type() == QLatin1String( "random" ) )
else if ( ramp->type() == QgsLimitedRandomColorRamp::typeString() )
{
QgsLimitedRandomColorRamp *randRamp = static_cast<QgsLimitedRandomColorRamp *>( ramp.get() );
QgsLimitedRandomColorRampDialog dlg( *randRamp, this );
@ -1628,7 +1621,7 @@ bool QgsStyleManagerDialog::editColorRamp()
}
ramp.reset( dlg.ramp().clone() );
}
else if ( ramp->type() == QLatin1String( "colorbrewer" ) )
else if ( ramp->type() == QgsColorBrewerColorRamp::typeString() )
{
QgsColorBrewerColorRamp *brewerRamp = static_cast<QgsColorBrewerColorRamp *>( ramp.get() );
QgsColorBrewerColorRampDialog dlg( *brewerRamp, this );
@ -1641,7 +1634,7 @@ bool QgsStyleManagerDialog::editColorRamp()
}
ramp.reset( dlg.ramp().clone() );
}
else if ( ramp->type() == QLatin1String( "preset" ) )
else if ( ramp->type() == QgsPresetSchemeColorRamp::typeString() )
{
QgsPresetSchemeColorRamp *presetRamp = static_cast<QgsPresetSchemeColorRamp *>( ramp.get() );
QgsPresetColorRampDialog dlg( *presetRamp, this );
@ -1654,7 +1647,7 @@ bool QgsStyleManagerDialog::editColorRamp()
}
ramp.reset( dlg.ramp().clone() );
}
else if ( ramp->type() == QLatin1String( "cpt-city" ) )
else if ( ramp->type() == QgsCptCityColorRamp::typeString() )
{
QgsCptCityColorRamp *cptCityRamp = static_cast<QgsCptCityColorRamp *>( ramp.get() );
QgsCptCityColorRampDialog dlg( *cptCityRamp, this );

View File

@ -80,11 +80,15 @@ class GUI_EXPORT QgsStyleManagerDialog : public QDialog, private Ui::QgsStyleMan
QgsStyleManagerDialog( QgsStyle *style, QWidget *parent SIP_TRANSFERTHIS = nullptr, Qt::WindowFlags flags = Qt::WindowFlags(),
bool readOnly = false );
// TODO QGIS 4.0 -- rename "RampType" to "rampType".
/**
* Opens the add color ramp dialog, returning the new color ramp's name if the ramp has been added.
*
* The \a RampType argument should be set to a string representing a valid QgsColorRamp::type() value.
*/
static QString addColorRampStatic( QWidget *parent, QgsStyle *style,
QString RampType = QString() );
const QString &RampType = QString() );
/**
* Sets whether the favorites group should be shown. The default is to show the group.
@ -254,7 +258,16 @@ class GUI_EXPORT QgsStyleManagerDialog : public QDialog, private Ui::QgsStyleMan
void listitemsContextMenu( QPoint );
protected slots:
/**
* Triggers adding a new color ramp.
*
* If \a type is set to a string representing a valid QgsColorRamp::type() value then a new ramp
* of that type will be created. If \a type is empty, then the user will be prompted to select
* the color ramp type.
*/
bool addColorRamp( const QString &type = QString() );
//! Add selected symbols to favorites
void addFavoriteSelectedSymbols();
//! Remove selected symbols from favorites