Skip to content

Commit 38baaaf

Browse files
committed
[FEATURE] Show recent colors in layer right click menus
1 parent 87c58f4 commit 38baaaf

File tree

8 files changed

+113
-33
lines changed

8 files changed

+113
-33
lines changed

python/core/qgscolorscheme.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ class QgsRecentColorScheme : QgsColorScheme
194194
const QColor &baseColor = QColor() );
195195

196196
virtual QgsRecentColorScheme* clone() const /Factory/;
197+
198+
/** Adds a color to the list of recent colors.
199+
* @param color color to add
200+
* @note added in QGIS 2.14
201+
*/
202+
static void addRecentColor( const QColor& color );
197203
};
198204

199205
/** \ingroup core

python/gui/qgscolorswatchgrid.sip

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ class QgsColorSwatchGridAction: QWidgetAction
139139
*/
140140
void setContext( const QString &context );
141141

142+
/** Sets whether the parent menu should be dismissed and closed when a color is selected
143+
* from the action's color widget.
144+
* @param dismiss set to true (default) to immediately close the menu when a color is selected
145+
* from the widget. If set to false, the colorChanged signal will be emitted but the menu will
146+
* stay open.
147+
* @see dismissOnColorSelection()
148+
* @note added in QGIS 2.14
149+
*/
150+
void setDismissOnColorSelection( bool dismiss );
151+
152+
/** Returns whether the parent menu will be dismissed after a color is selected from the
153+
* action's color widget.
154+
* @see setDismissOnColorSelection
155+
* @note added in QGIS 2.14
156+
*/
157+
bool dismissOnColorSelection() const;
158+
142159
public slots:
143160

144161
/** Reload colors from scheme and redraws the widget

src/app/qgsapplayertreeviewmenuprovider.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,19 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
142142
//store the layer id in action, so we can later retrieve the corresponding layer
143143
colorAction->setProperty( "layerId", vlayer->id() );
144144
menuStyleManager->addAction( colorAction );
145+
146+
//add recent colors action
147+
QList<QgsRecentColorScheme *> recentSchemes;
148+
QgsColorSchemeRegistry::instance()->schemes( recentSchemes );
149+
if ( !recentSchemes.isEmpty() )
150+
{
151+
QgsColorSwatchGridAction* recentColorAction = new QgsColorSwatchGridAction( recentSchemes.at( 0 ), menuStyleManager, "symbology", menuStyleManager );
152+
recentColorAction->setProperty( "layerId", vlayer->id() );
153+
recentColorAction->setDismissOnColorSelection( false );
154+
menuStyleManager->addAction( recentColorAction );
155+
connect( recentColorAction, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setVectorSymbolColor( const QColor& ) ) );
156+
}
157+
145158
menuStyleManager->addSeparator();
146159
QAction* editSymbolAction = new QAction( tr( "Edit Symbol..." ), menuStyleManager );
147160
//store the layer id in action, so we can later retrieve the corresponding layer
@@ -260,6 +273,20 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
260273
colorAction->setProperty( "layerId", symbolNode->layerNode()->layerId() );
261274
colorAction->setProperty( "ruleKey", symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() );
262275
menu->addAction( colorAction );
276+
277+
//add recent colors action
278+
QList<QgsRecentColorScheme *> recentSchemes;
279+
QgsColorSchemeRegistry::instance()->schemes( recentSchemes );
280+
if ( !recentSchemes.isEmpty() )
281+
{
282+
QgsColorSwatchGridAction* recentColorAction = new QgsColorSwatchGridAction( recentSchemes.at( 0 ), menu, "symbology", menu );
283+
recentColorAction->setProperty( "layerId", symbolNode->layerNode()->layerId() );
284+
recentColorAction->setProperty( "ruleKey", symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() );
285+
recentColorAction->setDismissOnColorSelection( false );
286+
menu->addAction( recentColorAction );
287+
connect( recentColorAction, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setSymbolLegendNodeColor( const QColor& ) ) );
288+
}
289+
263290
menu->addSeparator();
264291
}
265292

src/core/qgscolorscheme.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,42 @@ QgsRecentColorScheme* QgsRecentColorScheme::clone() const
8181
return new QgsRecentColorScheme();
8282
}
8383

84+
void QgsRecentColorScheme::addRecentColor( const QColor& color )
85+
{
86+
if ( !color.isValid() )
87+
{
88+
return;
89+
}
90+
91+
//strip alpha from color
92+
QColor opaqueColor = color;
93+
opaqueColor.setAlpha( 255 );
94+
95+
QSettings settings;
96+
QList< QVariant > recentColorVariants = settings.value( QString( "/colors/recent" ) ).toList();
97+
98+
//remove colors by name
99+
for ( int colorIdx = recentColorVariants.length() - 1; colorIdx >= 0; --colorIdx )
100+
{
101+
if (( recentColorVariants.at( colorIdx ).value<QColor>() ).name() == opaqueColor.name() )
102+
{
103+
recentColorVariants.removeAt( colorIdx );
104+
}
105+
}
106+
107+
//add color
108+
QVariant colorVariant = QVariant( opaqueColor );
109+
recentColorVariants.prepend( colorVariant );
110+
111+
//trim to 20 colors
112+
while ( recentColorVariants.count() > 20 )
113+
{
114+
recentColorVariants.pop_back();
115+
}
116+
117+
settings.setValue( QString( "/colors/recent" ), recentColorVariants );
118+
}
119+
84120

85121
QgsCustomColorScheme::QgsCustomColorScheme() : QgsColorScheme()
86122
{

src/core/qgscolorscheme.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ class CORE_EXPORT QgsRecentColorScheme : public QgsColorScheme
192192
const QColor &baseColor = QColor() ) override;
193193

194194
QgsRecentColorScheme* clone() const override;
195+
196+
/** Adds a color to the list of recent colors.
197+
* @param color color to add
198+
* @note added in QGIS 2.14
199+
*/
200+
static void addRecentColor( const QColor& color );
195201
};
196202

197203
/** \ingroup core

src/gui/qgscolorbuttonv2.cpp

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -513,38 +513,7 @@ void QgsColorButtonV2::setColor( const QColor &color )
513513

514514
void QgsColorButtonV2::addRecentColor( const QColor& color )
515515
{
516-
if ( !color.isValid() )
517-
{
518-
return;
519-
}
520-
521-
//strip alpha from color
522-
QColor opaqueColor = color;
523-
opaqueColor.setAlpha( 255 );
524-
525-
QSettings settings;
526-
QList< QVariant > recentColorVariants = settings.value( QString( "/colors/recent" ) ).toList();
527-
528-
//remove colors by name
529-
for ( int colorIdx = recentColorVariants.length() - 1; colorIdx >= 0; --colorIdx )
530-
{
531-
if (( recentColorVariants.at( colorIdx ).value<QColor>() ).name() == opaqueColor.name() )
532-
{
533-
recentColorVariants.removeAt( colorIdx );
534-
}
535-
}
536-
537-
//add color
538-
QVariant colorVariant = QVariant( opaqueColor );
539-
recentColorVariants.prepend( colorVariant );
540-
541-
//trim to 20 colors
542-
while ( recentColorVariants.count() > 20 )
543-
{
544-
recentColorVariants.pop_back();
545-
}
546-
547-
settings.setValue( QString( "/colors/recent" ), recentColorVariants );
516+
QgsRecentColorScheme::addRecentColor( color );
548517
}
549518

550519
void QgsColorButtonV2::setButtonBackground( const QColor &color )

src/gui/qgscolorswatchgrid.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ QgsColorSwatchGridAction::QgsColorSwatchGridAction( QgsColorScheme* scheme, QMen
362362
: QWidgetAction( parent )
363363
, mMenu( menu )
364364
, mSuppressRecurse( false )
365+
, mDismissOnColorSelection( true )
365366
{
366367
mColorSwatchGrid = new QgsColorSwatchGrid( scheme, context, parent );
367368

@@ -411,7 +412,7 @@ void QgsColorSwatchGridAction::setColor( const QColor &color )
411412
{
412413
emit colorChanged( color );
413414
QAction::trigger();
414-
if ( mMenu )
415+
if ( mMenu && mDismissOnColorSelection )
415416
{
416417
mMenu->hide();
417418
}

src/gui/qgscolorswatchgrid.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,23 @@ class GUI_EXPORT QgsColorSwatchGridAction: public QWidgetAction
195195
*/
196196
void setContext( const QString &context );
197197

198+
/** Sets whether the parent menu should be dismissed and closed when a color is selected
199+
* from the action's color widget.
200+
* @param dismiss set to true (default) to immediately close the menu when a color is selected
201+
* from the widget. If set to false, the colorChanged signal will be emitted but the menu will
202+
* stay open.
203+
* @see dismissOnColorSelection()
204+
* @note added in QGIS 2.14
205+
*/
206+
void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
207+
208+
/** Returns whether the parent menu will be dismissed after a color is selected from the
209+
* action's color widget.
210+
* @see setDismissOnColorSelection
211+
* @note added in QGIS 2.14
212+
*/
213+
bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
214+
198215
public slots:
199216

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

215232
//used to supress recursion with hover events
216233
bool mSuppressRecurse;
234+
bool mDismissOnColorSelection;
217235

218236
private slots:
219237

0 commit comments

Comments
 (0)