Skip to content

Commit

Permalink
[FEATURE] Show recent colors in layer right click menus
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 8, 2016
1 parent 87c58f4 commit 38baaaf
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 33 deletions.
6 changes: 6 additions & 0 deletions python/core/qgscolorscheme.sip
Expand Up @@ -194,6 +194,12 @@ class QgsRecentColorScheme : QgsColorScheme
const QColor &baseColor = QColor() );

virtual QgsRecentColorScheme* clone() const /Factory/;

/** Adds a color to the list of recent colors.
* @param color color to add
* @note added in QGIS 2.14
*/
static void addRecentColor( const QColor& color );
};

/** \ingroup core
Expand Down
17 changes: 17 additions & 0 deletions python/gui/qgscolorswatchgrid.sip
Expand Up @@ -139,6 +139,23 @@ class QgsColorSwatchGridAction: QWidgetAction
*/
void setContext( const QString &context );

/** Sets whether the parent menu should be dismissed and closed when a color is selected
* from the action's color widget.
* @param dismiss set to true (default) to immediately close the menu when a color is selected
* from the widget. If set to false, the colorChanged signal will be emitted but the menu will
* stay open.
* @see dismissOnColorSelection()
* @note added in QGIS 2.14
*/
void setDismissOnColorSelection( bool dismiss );

/** Returns whether the parent menu will be dismissed after a color is selected from the
* action's color widget.
* @see setDismissOnColorSelection
* @note added in QGIS 2.14
*/
bool dismissOnColorSelection() const;

public slots:

/** Reload colors from scheme and redraws the widget
Expand Down
27 changes: 27 additions & 0 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -142,6 +142,19 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
//store the layer id in action, so we can later retrieve the corresponding layer
colorAction->setProperty( "layerId", vlayer->id() );
menuStyleManager->addAction( colorAction );

//add recent colors action
QList<QgsRecentColorScheme *> recentSchemes;
QgsColorSchemeRegistry::instance()->schemes( recentSchemes );
if ( !recentSchemes.isEmpty() )
{
QgsColorSwatchGridAction* recentColorAction = new QgsColorSwatchGridAction( recentSchemes.at( 0 ), menuStyleManager, "symbology", menuStyleManager );
recentColorAction->setProperty( "layerId", vlayer->id() );
recentColorAction->setDismissOnColorSelection( false );
menuStyleManager->addAction( recentColorAction );
connect( recentColorAction, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setVectorSymbolColor( const QColor& ) ) );
}

menuStyleManager->addSeparator();
QAction* editSymbolAction = new QAction( tr( "Edit Symbol..." ), menuStyleManager );
//store the layer id in action, so we can later retrieve the corresponding layer
Expand Down Expand Up @@ -260,6 +273,20 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
colorAction->setProperty( "layerId", symbolNode->layerNode()->layerId() );
colorAction->setProperty( "ruleKey", symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() );
menu->addAction( colorAction );

//add recent colors action
QList<QgsRecentColorScheme *> recentSchemes;
QgsColorSchemeRegistry::instance()->schemes( recentSchemes );
if ( !recentSchemes.isEmpty() )
{
QgsColorSwatchGridAction* recentColorAction = new QgsColorSwatchGridAction( recentSchemes.at( 0 ), menu, "symbology", menu );
recentColorAction->setProperty( "layerId", symbolNode->layerNode()->layerId() );
recentColorAction->setProperty( "ruleKey", symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() );
recentColorAction->setDismissOnColorSelection( false );
menu->addAction( recentColorAction );
connect( recentColorAction, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setSymbolLegendNodeColor( const QColor& ) ) );
}

menu->addSeparator();
}

Expand Down
36 changes: 36 additions & 0 deletions src/core/qgscolorscheme.cpp
Expand Up @@ -81,6 +81,42 @@ QgsRecentColorScheme* QgsRecentColorScheme::clone() const
return new QgsRecentColorScheme();
}

void QgsRecentColorScheme::addRecentColor( const QColor& color )
{
if ( !color.isValid() )
{
return;
}

//strip alpha from color
QColor opaqueColor = color;
opaqueColor.setAlpha( 255 );

QSettings settings;
QList< QVariant > recentColorVariants = settings.value( QString( "/colors/recent" ) ).toList();

//remove colors by name
for ( int colorIdx = recentColorVariants.length() - 1; colorIdx >= 0; --colorIdx )
{
if (( recentColorVariants.at( colorIdx ).value<QColor>() ).name() == opaqueColor.name() )
{
recentColorVariants.removeAt( colorIdx );
}
}

//add color
QVariant colorVariant = QVariant( opaqueColor );
recentColorVariants.prepend( colorVariant );

//trim to 20 colors
while ( recentColorVariants.count() > 20 )
{
recentColorVariants.pop_back();
}

settings.setValue( QString( "/colors/recent" ), recentColorVariants );
}


QgsCustomColorScheme::QgsCustomColorScheme() : QgsColorScheme()
{
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgscolorscheme.h
Expand Up @@ -192,6 +192,12 @@ class CORE_EXPORT QgsRecentColorScheme : public QgsColorScheme
const QColor &baseColor = QColor() ) override;

QgsRecentColorScheme* clone() const override;

/** Adds a color to the list of recent colors.
* @param color color to add
* @note added in QGIS 2.14
*/
static void addRecentColor( const QColor& color );
};

/** \ingroup core
Expand Down
33 changes: 1 addition & 32 deletions src/gui/qgscolorbuttonv2.cpp
Expand Up @@ -513,38 +513,7 @@ void QgsColorButtonV2::setColor( const QColor &color )

void QgsColorButtonV2::addRecentColor( const QColor& color )
{
if ( !color.isValid() )
{
return;
}

//strip alpha from color
QColor opaqueColor = color;
opaqueColor.setAlpha( 255 );

QSettings settings;
QList< QVariant > recentColorVariants = settings.value( QString( "/colors/recent" ) ).toList();

//remove colors by name
for ( int colorIdx = recentColorVariants.length() - 1; colorIdx >= 0; --colorIdx )
{
if (( recentColorVariants.at( colorIdx ).value<QColor>() ).name() == opaqueColor.name() )
{
recentColorVariants.removeAt( colorIdx );
}
}

//add color
QVariant colorVariant = QVariant( opaqueColor );
recentColorVariants.prepend( colorVariant );

//trim to 20 colors
while ( recentColorVariants.count() > 20 )
{
recentColorVariants.pop_back();
}

settings.setValue( QString( "/colors/recent" ), recentColorVariants );
QgsRecentColorScheme::addRecentColor( color );
}

void QgsColorButtonV2::setButtonBackground( const QColor &color )
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgscolorswatchgrid.cpp
Expand Up @@ -362,6 +362,7 @@ QgsColorSwatchGridAction::QgsColorSwatchGridAction( QgsColorScheme* scheme, QMen
: QWidgetAction( parent )
, mMenu( menu )
, mSuppressRecurse( false )
, mDismissOnColorSelection( true )
{
mColorSwatchGrid = new QgsColorSwatchGrid( scheme, context, parent );

Expand Down Expand Up @@ -411,7 +412,7 @@ void QgsColorSwatchGridAction::setColor( const QColor &color )
{
emit colorChanged( color );
QAction::trigger();
if ( mMenu )
if ( mMenu && mDismissOnColorSelection )
{
mMenu->hide();
}
Expand Down
18 changes: 18 additions & 0 deletions src/gui/qgscolorswatchgrid.h
Expand Up @@ -195,6 +195,23 @@ class GUI_EXPORT QgsColorSwatchGridAction: public QWidgetAction
*/
void setContext( const QString &context );

/** Sets whether the parent menu should be dismissed and closed when a color is selected
* from the action's color widget.
* @param dismiss set to true (default) to immediately close the menu when a color is selected
* from the widget. If set to false, the colorChanged signal will be emitted but the menu will
* stay open.
* @see dismissOnColorSelection()
* @note added in QGIS 2.14
*/
void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }

/** Returns whether the parent menu will be dismissed after a color is selected from the
* action's color widget.
* @see setDismissOnColorSelection
* @note added in QGIS 2.14
*/
bool dismissOnColorSelection() const { return mDismissOnColorSelection; }

public slots:

/** Reload colors from scheme and redraws the widget
Expand All @@ -214,6 +231,7 @@ class GUI_EXPORT QgsColorSwatchGridAction: public QWidgetAction

//used to supress recursion with hover events
bool mSuppressRecurse;
bool mDismissOnColorSelection;

private slots:

Expand Down

0 comments on commit 38baaaf

Please sign in to comment.