Skip to content
Permalink
Browse files

[FEATURE] Show recent colors in layer right click menus

  • Loading branch information
nyalldawson committed Jan 8, 2016
1 parent 87c58f4 commit 38baaafd0539d0031d9e1e3990c77a0103d137e2
@@ -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
@@ -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
@@ -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
@@ -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();
}

@@ -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()
{
@@ -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
@@ -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 )
@@ -362,6 +362,7 @@ QgsColorSwatchGridAction::QgsColorSwatchGridAction( QgsColorScheme* scheme, QMen
: QWidgetAction( parent )
, mMenu( menu )
, mSuppressRecurse( false )
, mDismissOnColorSelection( true )
{
mColorSwatchGrid = new QgsColorSwatchGrid( scheme, context, parent );

@@ -411,7 +412,7 @@ void QgsColorSwatchGridAction::setColor( const QColor &color )
{
emit colorChanged( color );
QAction::trigger();
if ( mMenu )
if ( mMenu && mDismissOnColorSelection )
{
mMenu->hide();
}
@@ -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
@@ -214,6 +231,7 @@ class GUI_EXPORT QgsColorSwatchGridAction: public QWidgetAction

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

private slots:

0 comments on commit 38baaaf

Please sign in to comment.
You can’t perform that action at this time.