Skip to content

Commit 12a2147

Browse files
committed
[FEATURE] Add an option to show user color schemes menus
This adds the ability for users to set whether a user created color scheme should show up in the color button drop-down menus. It's accessed through the color picker dialog, on the lists tab. Just add a new color scheme, then from the scheme menu tick the new "show in buttons" option. Handy if you have sets of common palettes and want them to be instantly available through the color menu.
1 parent 68f63eb commit 12a2147

10 files changed

+148
-15
lines changed

python/core/qgscolorscheme.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ class QgsUserColorScheme : QgsGplColorScheme
151151

152152
virtual bool isEditable() const;
153153

154+
virtual QgsColorScheme::SchemeFlags flags() const;
155+
154156
/** Sets the name for the scheme
155157
* @param name new name
156158
*/
@@ -161,6 +163,12 @@ class QgsUserColorScheme : QgsGplColorScheme
161163
*/
162164
bool erase();
163165

166+
/** Sets whether a this scheme should be shown in color button menus.
167+
* @param show set to true to show in color button menus, or false to hide from menus
168+
* @note added in QGIS 3.0
169+
*/
170+
void setShowSchemeInMenu( bool show );
171+
164172
protected:
165173

166174
virtual QString gplFilePath();

python/gui/qgscolorschemelist.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,19 @@ class QgsColorSchemeList: QTreeView
142142
*/
143143
bool isDirty() const;
144144

145+
/** Returns the scheme currently selected in the list.
146+
* @note added in QGIS 3.0
147+
* @see setScheme()
148+
*/
149+
QgsColorScheme* scheme();
150+
145151
public slots:
146152

147153
/** Sets the color scheme to show in the list
148154
* @param scheme QgsColorScheme for colors to show in the list
149155
* @param context context string provided to color scheme
150156
* @param baseColor base color for color scheme
157+
* @see scheme()
151158
*/
152159
void setScheme( QgsColorScheme* scheme, const QString &context = QString(), const QColor &baseColor = QColor() );
153160

src/core/qgscolorscheme.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,21 @@ QgsUserColorScheme* QgsUserColorScheme::clone() const
373373
return new QgsUserColorScheme( mFilename );
374374
}
375375

376+
QgsColorScheme::SchemeFlags QgsUserColorScheme::flags() const
377+
{
378+
QgsColorScheme::SchemeFlags f = QgsGplColorScheme::flags();
379+
380+
QSettings s;
381+
QStringList showInMenuSchemes = s.value( QString( "/colors/showInMenuList" ) ).toStringList();
382+
383+
if ( showInMenuSchemes.contains( mName ) )
384+
{
385+
f |= QgsColorScheme::ShowInColorButtonMenu;
386+
}
387+
388+
return f;
389+
}
390+
376391
bool QgsUserColorScheme::erase()
377392
{
378393
QString filePath = gplFilePath();
@@ -385,6 +400,23 @@ bool QgsUserColorScheme::erase()
385400
return QFile::remove( filePath );
386401
}
387402

403+
void QgsUserColorScheme::setShowSchemeInMenu( bool show )
404+
{
405+
QSettings s;
406+
QStringList showInMenuSchemes = s.value( QString( "/colors/showInMenuList" ) ).toStringList();
407+
408+
if ( show && !showInMenuSchemes.contains( mName ) )
409+
{
410+
showInMenuSchemes << mName;
411+
}
412+
else if ( !show && showInMenuSchemes.contains( mName ) )
413+
{
414+
showInMenuSchemes.removeAll( mName );
415+
}
416+
417+
s.setValue( "/colors/showInMenuList", showInMenuSchemes );
418+
}
419+
388420
QString QgsUserColorScheme::gplFilePath()
389421
{
390422
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";

src/core/qgscolorscheme.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme
152152

153153
virtual bool isEditable() const override { return true; }
154154

155+
virtual QgsColorScheme::SchemeFlags flags() const override;
156+
155157
/** Sets the name for the scheme
156158
* @param name new name
157159
*/
@@ -162,6 +164,12 @@ class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme
162164
*/
163165
bool erase();
164166

167+
/** Sets whether a this scheme should be shown in color button menus.
168+
* @param show set to true to show in color button menus, or false to hide from menus
169+
* @note added in QGIS 3.0
170+
*/
171+
void setShowSchemeInMenu( bool show );
172+
165173
protected:
166174

167175
QString mName;

src/gui/qgscolorschemelist.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ bool QgsColorSchemeList::isDirty() const
231231
return mModel->isDirty();
232232
}
233233

234+
QgsColorScheme*QgsColorSchemeList::scheme()
235+
{
236+
return mScheme;
237+
}
238+
234239
//
235240
// QgsColorSchemeModel
236241
//

src/gui/qgscolorschemelist.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,19 @@ class GUI_EXPORT QgsColorSchemeList: public QTreeView
182182
*/
183183
bool isDirty() const;
184184

185+
/** Returns the scheme currently selected in the list.
186+
* @note added in QGIS 3.0
187+
* @see setScheme()
188+
*/
189+
QgsColorScheme* scheme();
190+
185191
public slots:
186192

187193
/** Sets the color scheme to show in the list
188194
* @param scheme QgsColorScheme for colors to show in the list
189195
* @param context context string provided to color scheme
190196
* @param baseColor base color for color scheme
197+
* @see scheme()
191198
*/
192199
void setScheme( QgsColorScheme* scheme, const QString &context = QString(), const QColor &baseColor = QColor() );
193200

src/gui/qgscompoundcolorwidget.cpp

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <QMouseEvent>
3030
#include <QInputDialog>
3131

32+
3233
QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor& color )
3334
: QWidget( parent )
3435
, mAllowAlpha( true )
@@ -51,13 +52,9 @@ QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor& c
5152
activeScheme = activeScheme >= mSchemeComboBox->count() ? 0 : activeScheme;
5253

5354
mSchemeList->setScheme( schemeList.at( activeScheme ) );
55+
5456
mSchemeComboBox->setCurrentIndex( activeScheme );
55-
mActionImportColors->setEnabled( schemeList.at( activeScheme )->isEditable() );
56-
mActionPasteColors->setEnabled( schemeList.at( activeScheme )->isEditable() );
57-
mAddColorToSchemeButton->setEnabled( schemeList.at( activeScheme )->isEditable() );
58-
mRemoveColorsFromSchemeButton->setEnabled( schemeList.at( activeScheme )->isEditable() );
59-
QgsUserColorScheme* userScheme = dynamic_cast<QgsUserColorScheme*>( schemeList.at( activeScheme ) );
60-
mActionRemovePalette->setEnabled( userScheme ? true : false );
57+
updateActionsForCurrentScheme();
6158

6259
//listen out for selection changes in list, so we can enable/disable the copy colors option
6360
connect( mSchemeList->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), this, SLOT( listSelectionChanged( QItemSelection, QItemSelection ) ) );
@@ -83,6 +80,7 @@ QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor& c
8380
schemeMenu->addAction( mActionNewPalette );
8481
schemeMenu->addAction( mActionImportPalette );
8582
schemeMenu->addAction( mActionRemovePalette );
83+
schemeMenu->addAction( mActionShowInButtons );
8684
mSchemeToolButton->setMenu( schemeMenu );
8785

8886
connect( mSchemeComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( schemeIndexChanged( int ) ) );
@@ -479,12 +477,8 @@ void QgsCompoundColorWidget::schemeIndexChanged( int index )
479477

480478
QgsColorScheme* scheme = schemeList.at( index );
481479
mSchemeList->setScheme( scheme );
482-
mActionImportColors->setEnabled( scheme->isEditable() );
483-
mActionPasteColors->setEnabled( scheme->isEditable() );
484-
mAddColorToSchemeButton->setEnabled( scheme->isEditable() );
485-
mRemoveColorsFromSchemeButton->setEnabled( scheme->isEditable() );
486-
QgsUserColorScheme* userScheme = dynamic_cast<QgsUserColorScheme*>( scheme );
487-
mActionRemovePalette->setEnabled( userScheme ? true : false );
480+
481+
updateActionsForCurrentScheme();
488482

489483
//copy action defaults to disabled
490484
mActionCopyColors->setEnabled( false );
@@ -579,6 +573,15 @@ void QgsCompoundColorWidget::on_mTabWidget_currentChanged( int index )
579573
mValueRadio->setEnabled( enabled );
580574
}
581575

576+
void QgsCompoundColorWidget::on_mActionShowInButtons_toggled( bool state )
577+
{
578+
QgsUserColorScheme* scheme = dynamic_cast< QgsUserColorScheme* >( mSchemeList->scheme() );
579+
if ( scheme )
580+
{
581+
scheme->setShowSchemeInMenu( state );
582+
}
583+
}
584+
582585
void QgsCompoundColorWidget::saveSettings()
583586
{
584587
//save changes to scheme
@@ -838,3 +841,26 @@ void QgsCompoundColorWidget::on_mAddColorToSchemeButton_clicked()
838841
{
839842
mSchemeList->addColor( mColorPreview->color(), QgsSymbolLayerV2Utils::colorToName( mColorPreview->color() ) );
840843
}
844+
845+
void QgsCompoundColorWidget::updateActionsForCurrentScheme()
846+
{
847+
QgsColorScheme* scheme = mSchemeList->scheme();
848+
849+
mActionImportColors->setEnabled( scheme->isEditable() );
850+
mActionPasteColors->setEnabled( scheme->isEditable() );
851+
mAddColorToSchemeButton->setEnabled( scheme->isEditable() );
852+
mRemoveColorsFromSchemeButton->setEnabled( scheme->isEditable() );
853+
854+
QgsUserColorScheme* userScheme = dynamic_cast<QgsUserColorScheme*>( scheme );
855+
mActionRemovePalette->setEnabled( userScheme ? true : false );
856+
if ( userScheme )
857+
{
858+
mActionShowInButtons->setEnabled( true );
859+
whileBlocking( mActionShowInButtons )->setChecked( userScheme->flags() & QgsColorScheme::ShowInColorButtonMenu );
860+
}
861+
else
862+
{
863+
whileBlocking( mActionShowInButtons )->setChecked( false );
864+
mActionShowInButtons->setEnabled( false );
865+
}
866+
}

src/gui/qgscompoundcolorwidget.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ class GUI_EXPORT QgsCompoundColorWidget : public QWidget, private Ui::QgsCompoun
106106
void on_mSampleButton_clicked();
107107
void on_mTabWidget_currentChanged( int index );
108108

109+
private slots:
110+
111+
void on_mActionShowInButtons_toggled( bool state );
112+
109113
private:
110114

111115
bool mAllowAlpha;
@@ -144,6 +148,9 @@ class GUI_EXPORT QgsCompoundColorWidget : public QWidget, private Ui::QgsCompoun
144148
/** Returns the path to the user's palette folder
145149
*/
146150
QString gplFilePath();
151+
152+
//! Updates the state of actions for the current selected scheme
153+
void updateActionsForCurrentScheme();
147154
};
148155

149156
#endif // QGSCOMPOUNDCOLORWIDGET_H

src/ui/qgscompoundcolorwidget.ui

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@
513513
<item row="0" column="0">
514514
<widget class="QTabWidget" name="mTabWidget">
515515
<property name="currentIndex">
516-
<number>0</number>
516+
<number>2</number>
517517
</property>
518518
<property name="iconSize">
519519
<size>
@@ -899,6 +899,14 @@
899899
<string>Copy selected colors</string>
900900
</property>
901901
</action>
902+
<action name="mActionShowInButtons">
903+
<property name="checkable">
904+
<bool>true</bool>
905+
</property>
906+
<property name="text">
907+
<string>Show in Color Buttons</string>
908+
</property>
909+
</action>
902910
</widget>
903911
<customwidgets>
904912
<customwidget>

tests/src/python/test_qgscolorscheme.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
import qgis # NOQA
1616

17-
from qgis.testing import unittest
18-
from qgis.core import QgsColorScheme
17+
from qgis.testing import unittest, start_app
18+
from qgis.core import QgsColorScheme, QgsUserColorScheme
19+
from qgis.PyQt.QtCore import QCoreApplication, QSettings
1920
from qgis.PyQt.QtGui import QColor
2021

2122
# Make a dummy color scheme for testing
@@ -43,6 +44,15 @@ def clone(self):
4344

4445
class TestQgsColorScheme(unittest.TestCase):
4546

47+
@classmethod
48+
def setUpClass(cls):
49+
"""Run before all tests"""
50+
QCoreApplication.setOrganizationName("QGIS_Test")
51+
QCoreApplication.setOrganizationDomain("QGIS_TestPyQgsColorScheme.com")
52+
QCoreApplication.setApplicationName("QGIS_TestPyQgsColorScheme")
53+
QSettings().clear()
54+
start_app()
55+
4656
def testCreateScheme(self):
4757
"""Test creating a new color scheme"""
4858
dummyScheme = DummyColorScheme()
@@ -88,6 +98,21 @@ def testClone(self):
8898
colorsClone = dummySchemeClone.fetchColors()
8999
self.assertEqual(colors, colorsClone)
90100

101+
def testUserScheme(self):
102+
""" Tests for user color schemes """
103+
104+
scheme = QgsUserColorScheme("user_test.gpl")
105+
self.assertEqual(scheme.schemeName(), 'user_test.gpl')
106+
self.assertTrue(scheme.isEditable())
107+
108+
self.assertFalse(scheme.flags() & QgsColorScheme.ShowInColorButtonMenu)
109+
scheme.setShowSchemeInMenu(True)
110+
self.assertTrue(scheme.flags() & QgsColorScheme.ShowInColorButtonMenu)
111+
scheme.setShowSchemeInMenu(False)
112+
self.assertFalse(scheme.flags() & QgsColorScheme.ShowInColorButtonMenu)
113+
114+
scheme.erase()
115+
91116

92117
if __name__ == "__main__":
93118
unittest.main()

0 commit comments

Comments
 (0)