Skip to content
Permalink
Browse files

[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.
  • Loading branch information
nyalldawson committed Jul 21, 2016
1 parent 68f63eb commit 12a21474276322084e9c67ad46dd68aa486c34f1
@@ -151,6 +151,8 @@ class QgsUserColorScheme : QgsGplColorScheme

virtual bool isEditable() const;

virtual QgsColorScheme::SchemeFlags flags() const;

/** Sets the name for the scheme
* @param name new name
*/
@@ -161,6 +163,12 @@ class QgsUserColorScheme : QgsGplColorScheme
*/
bool erase();

/** Sets whether a this scheme should be shown in color button menus.
* @param show set to true to show in color button menus, or false to hide from menus
* @note added in QGIS 3.0
*/
void setShowSchemeInMenu( bool show );

protected:

virtual QString gplFilePath();
@@ -142,12 +142,19 @@ class QgsColorSchemeList: QTreeView
*/
bool isDirty() const;

/** Returns the scheme currently selected in the list.
* @note added in QGIS 3.0
* @see setScheme()
*/
QgsColorScheme* scheme();

public slots:

/** Sets the color scheme to show in the list
* @param scheme QgsColorScheme for colors to show in the list
* @param context context string provided to color scheme
* @param baseColor base color for color scheme
* @see scheme()
*/
void setScheme( QgsColorScheme* scheme, const QString &context = QString(), const QColor &baseColor = QColor() );

@@ -373,6 +373,21 @@ QgsUserColorScheme* QgsUserColorScheme::clone() const
return new QgsUserColorScheme( mFilename );
}

QgsColorScheme::SchemeFlags QgsUserColorScheme::flags() const
{
QgsColorScheme::SchemeFlags f = QgsGplColorScheme::flags();

QSettings s;
QStringList showInMenuSchemes = s.value( QString( "/colors/showInMenuList" ) ).toStringList();

if ( showInMenuSchemes.contains( mName ) )
{
f |= QgsColorScheme::ShowInColorButtonMenu;
}

return f;
}

bool QgsUserColorScheme::erase()
{
QString filePath = gplFilePath();
@@ -385,6 +400,23 @@ bool QgsUserColorScheme::erase()
return QFile::remove( filePath );
}

void QgsUserColorScheme::setShowSchemeInMenu( bool show )
{
QSettings s;
QStringList showInMenuSchemes = s.value( QString( "/colors/showInMenuList" ) ).toStringList();

if ( show && !showInMenuSchemes.contains( mName ) )
{
showInMenuSchemes << mName;
}
else if ( !show && showInMenuSchemes.contains( mName ) )
{
showInMenuSchemes.removeAll( mName );
}

s.setValue( "/colors/showInMenuList", showInMenuSchemes );
}

QString QgsUserColorScheme::gplFilePath()
{
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
@@ -152,6 +152,8 @@ class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme

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

virtual QgsColorScheme::SchemeFlags flags() const override;

/** Sets the name for the scheme
* @param name new name
*/
@@ -162,6 +164,12 @@ class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme
*/
bool erase();

/** Sets whether a this scheme should be shown in color button menus.
* @param show set to true to show in color button menus, or false to hide from menus
* @note added in QGIS 3.0
*/
void setShowSchemeInMenu( bool show );

protected:

QString mName;
@@ -231,6 +231,11 @@ bool QgsColorSchemeList::isDirty() const
return mModel->isDirty();
}

QgsColorScheme*QgsColorSchemeList::scheme()
{
return mScheme;
}

//
// QgsColorSchemeModel
//
@@ -182,12 +182,19 @@ class GUI_EXPORT QgsColorSchemeList: public QTreeView
*/
bool isDirty() const;

/** Returns the scheme currently selected in the list.
* @note added in QGIS 3.0
* @see setScheme()
*/
QgsColorScheme* scheme();

public slots:

/** Sets the color scheme to show in the list
* @param scheme QgsColorScheme for colors to show in the list
* @param context context string provided to color scheme
* @param baseColor base color for color scheme
* @see scheme()
*/
void setScheme( QgsColorScheme* scheme, const QString &context = QString(), const QColor &baseColor = QColor() );

@@ -29,6 +29,7 @@
#include <QMouseEvent>
#include <QInputDialog>


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

mSchemeList->setScheme( schemeList.at( activeScheme ) );

mSchemeComboBox->setCurrentIndex( activeScheme );
mActionImportColors->setEnabled( schemeList.at( activeScheme )->isEditable() );
mActionPasteColors->setEnabled( schemeList.at( activeScheme )->isEditable() );
mAddColorToSchemeButton->setEnabled( schemeList.at( activeScheme )->isEditable() );
mRemoveColorsFromSchemeButton->setEnabled( schemeList.at( activeScheme )->isEditable() );
QgsUserColorScheme* userScheme = dynamic_cast<QgsUserColorScheme*>( schemeList.at( activeScheme ) );
mActionRemovePalette->setEnabled( userScheme ? true : false );
updateActionsForCurrentScheme();

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

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

QgsColorScheme* scheme = schemeList.at( index );
mSchemeList->setScheme( scheme );
mActionImportColors->setEnabled( scheme->isEditable() );
mActionPasteColors->setEnabled( scheme->isEditable() );
mAddColorToSchemeButton->setEnabled( scheme->isEditable() );
mRemoveColorsFromSchemeButton->setEnabled( scheme->isEditable() );
QgsUserColorScheme* userScheme = dynamic_cast<QgsUserColorScheme*>( scheme );
mActionRemovePalette->setEnabled( userScheme ? true : false );

updateActionsForCurrentScheme();

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

void QgsCompoundColorWidget::on_mActionShowInButtons_toggled( bool state )
{
QgsUserColorScheme* scheme = dynamic_cast< QgsUserColorScheme* >( mSchemeList->scheme() );
if ( scheme )
{
scheme->setShowSchemeInMenu( state );
}
}

void QgsCompoundColorWidget::saveSettings()
{
//save changes to scheme
@@ -838,3 +841,26 @@ void QgsCompoundColorWidget::on_mAddColorToSchemeButton_clicked()
{
mSchemeList->addColor( mColorPreview->color(), QgsSymbolLayerV2Utils::colorToName( mColorPreview->color() ) );
}

void QgsCompoundColorWidget::updateActionsForCurrentScheme()
{
QgsColorScheme* scheme = mSchemeList->scheme();

mActionImportColors->setEnabled( scheme->isEditable() );
mActionPasteColors->setEnabled( scheme->isEditable() );
mAddColorToSchemeButton->setEnabled( scheme->isEditable() );
mRemoveColorsFromSchemeButton->setEnabled( scheme->isEditable() );

QgsUserColorScheme* userScheme = dynamic_cast<QgsUserColorScheme*>( scheme );
mActionRemovePalette->setEnabled( userScheme ? true : false );
if ( userScheme )
{
mActionShowInButtons->setEnabled( true );
whileBlocking( mActionShowInButtons )->setChecked( userScheme->flags() & QgsColorScheme::ShowInColorButtonMenu );
}
else
{
whileBlocking( mActionShowInButtons )->setChecked( false );
mActionShowInButtons->setEnabled( false );
}
}
@@ -106,6 +106,10 @@ class GUI_EXPORT QgsCompoundColorWidget : public QWidget, private Ui::QgsCompoun
void on_mSampleButton_clicked();
void on_mTabWidget_currentChanged( int index );

private slots:

void on_mActionShowInButtons_toggled( bool state );

private:

bool mAllowAlpha;
@@ -144,6 +148,9 @@ class GUI_EXPORT QgsCompoundColorWidget : public QWidget, private Ui::QgsCompoun
/** Returns the path to the user's palette folder
*/
QString gplFilePath();

//! Updates the state of actions for the current selected scheme
void updateActionsForCurrentScheme();
};

#endif // QGSCOMPOUNDCOLORWIDGET_H
@@ -513,7 +513,7 @@
<item row="0" column="0">
<widget class="QTabWidget" name="mTabWidget">
<property name="currentIndex">
<number>0</number>
<number>2</number>
</property>
<property name="iconSize">
<size>
@@ -899,6 +899,14 @@
<string>Copy selected colors</string>
</property>
</action>
<action name="mActionShowInButtons">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Show in Color Buttons</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
@@ -14,8 +14,9 @@

import qgis # NOQA

from qgis.testing import unittest
from qgis.core import QgsColorScheme
from qgis.testing import unittest, start_app
from qgis.core import QgsColorScheme, QgsUserColorScheme
from qgis.PyQt.QtCore import QCoreApplication, QSettings
from qgis.PyQt.QtGui import QColor

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

class TestQgsColorScheme(unittest.TestCase):

@classmethod
def setUpClass(cls):
"""Run before all tests"""
QCoreApplication.setOrganizationName("QGIS_Test")
QCoreApplication.setOrganizationDomain("QGIS_TestPyQgsColorScheme.com")
QCoreApplication.setApplicationName("QGIS_TestPyQgsColorScheme")
QSettings().clear()
start_app()

def testCreateScheme(self):
"""Test creating a new color scheme"""
dummyScheme = DummyColorScheme()
@@ -88,6 +98,21 @@ def testClone(self):
colorsClone = dummySchemeClone.fetchColors()
self.assertEqual(colors, colorsClone)

def testUserScheme(self):
""" Tests for user color schemes """

scheme = QgsUserColorScheme("user_test.gpl")
self.assertEqual(scheme.schemeName(), 'user_test.gpl')
self.assertTrue(scheme.isEditable())

self.assertFalse(scheme.flags() & QgsColorScheme.ShowInColorButtonMenu)
scheme.setShowSchemeInMenu(True)
self.assertTrue(scheme.flags() & QgsColorScheme.ShowInColorButtonMenu)
scheme.setShowSchemeInMenu(False)
self.assertFalse(scheme.flags() & QgsColorScheme.ShowInColorButtonMenu)

scheme.erase()


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

0 comments on commit 12a2147

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