Skip to content

Commit

Permalink
[FEATURE] Add an option to show user color schemes menus
Browse files Browse the repository at this point in the history
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 12a2147
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 15 deletions.
8 changes: 8 additions & 0 deletions python/core/qgscolorscheme.sip
Expand Up @@ -151,6 +151,8 @@ class QgsUserColorScheme : QgsGplColorScheme


virtual bool isEditable() const; virtual bool isEditable() const;


virtual QgsColorScheme::SchemeFlags flags() const;

/** Sets the name for the scheme /** Sets the name for the scheme
* @param name new name * @param name new name
*/ */
Expand All @@ -161,6 +163,12 @@ class QgsUserColorScheme : QgsGplColorScheme
*/ */
bool erase(); 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: protected:


virtual QString gplFilePath(); virtual QString gplFilePath();
Expand Down
7 changes: 7 additions & 0 deletions python/gui/qgscolorschemelist.sip
Expand Up @@ -142,12 +142,19 @@ class QgsColorSchemeList: QTreeView
*/ */
bool isDirty() const; bool isDirty() const;


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

public slots: public slots:


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


Expand Down
32 changes: 32 additions & 0 deletions src/core/qgscolorscheme.cpp
Expand Up @@ -373,6 +373,21 @@ QgsUserColorScheme* QgsUserColorScheme::clone() const
return new QgsUserColorScheme( mFilename ); 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() bool QgsUserColorScheme::erase()
{ {
QString filePath = gplFilePath(); QString filePath = gplFilePath();
Expand All @@ -385,6 +400,23 @@ bool QgsUserColorScheme::erase()
return QFile::remove( filePath ); 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 QgsUserColorScheme::gplFilePath()
{ {
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes"; QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgscolorscheme.h
Expand Up @@ -152,6 +152,8 @@ class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme


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


virtual QgsColorScheme::SchemeFlags flags() const override;

/** Sets the name for the scheme /** Sets the name for the scheme
* @param name new name * @param name new name
*/ */
Expand All @@ -162,6 +164,12 @@ class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme
*/ */
bool erase(); 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: protected:


QString mName; QString mName;
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgscolorschemelist.cpp
Expand Up @@ -231,6 +231,11 @@ bool QgsColorSchemeList::isDirty() const
return mModel->isDirty(); return mModel->isDirty();
} }


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

// //
// QgsColorSchemeModel // QgsColorSchemeModel
// //
Expand Down
7 changes: 7 additions & 0 deletions src/gui/qgscolorschemelist.h
Expand Up @@ -182,12 +182,19 @@ class GUI_EXPORT QgsColorSchemeList: public QTreeView
*/ */
bool isDirty() const; bool isDirty() const;


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

public slots: public slots:


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


Expand Down
50 changes: 38 additions & 12 deletions src/gui/qgscompoundcolorwidget.cpp
Expand Up @@ -29,6 +29,7 @@
#include <QMouseEvent> #include <QMouseEvent>
#include <QInputDialog> #include <QInputDialog>



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


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

mSchemeComboBox->setCurrentIndex( activeScheme ); mSchemeComboBox->setCurrentIndex( activeScheme );
mActionImportColors->setEnabled( schemeList.at( activeScheme )->isEditable() ); updateActionsForCurrentScheme();
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 );


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


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


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


//copy action defaults to disabled //copy action defaults to disabled
mActionCopyColors->setEnabled( false ); mActionCopyColors->setEnabled( false );
Expand Down Expand Up @@ -579,6 +573,15 @@ void QgsCompoundColorWidget::on_mTabWidget_currentChanged( int index )
mValueRadio->setEnabled( enabled ); 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() void QgsCompoundColorWidget::saveSettings()
{ {
//save changes to scheme //save changes to scheme
Expand Down Expand Up @@ -838,3 +841,26 @@ void QgsCompoundColorWidget::on_mAddColorToSchemeButton_clicked()
{ {
mSchemeList->addColor( mColorPreview->color(), QgsSymbolLayerV2Utils::colorToName( mColorPreview->color() ) ); 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 );
}
}
7 changes: 7 additions & 0 deletions src/gui/qgscompoundcolorwidget.h
Expand Up @@ -106,6 +106,10 @@ class GUI_EXPORT QgsCompoundColorWidget : public QWidget, private Ui::QgsCompoun
void on_mSampleButton_clicked(); void on_mSampleButton_clicked();
void on_mTabWidget_currentChanged( int index ); void on_mTabWidget_currentChanged( int index );


private slots:

void on_mActionShowInButtons_toggled( bool state );

private: private:


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

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


#endif // QGSCOMPOUNDCOLORWIDGET_H #endif // QGSCOMPOUNDCOLORWIDGET_H
10 changes: 9 additions & 1 deletion src/ui/qgscompoundcolorwidget.ui
Expand Up @@ -513,7 +513,7 @@
<item row="0" column="0"> <item row="0" column="0">
<widget class="QTabWidget" name="mTabWidget"> <widget class="QTabWidget" name="mTabWidget">
<property name="currentIndex"> <property name="currentIndex">
<number>0</number> <number>2</number>
</property> </property>
<property name="iconSize"> <property name="iconSize">
<size> <size>
Expand Down Expand Up @@ -899,6 +899,14 @@
<string>Copy selected colors</string> <string>Copy selected colors</string>
</property> </property>
</action> </action>
<action name="mActionShowInButtons">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Show in Color Buttons</string>
</property>
</action>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>
Expand Down
29 changes: 27 additions & 2 deletions tests/src/python/test_qgscolorscheme.py
Expand Up @@ -14,8 +14,9 @@


import qgis # NOQA import qgis # NOQA


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


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


class TestQgsColorScheme(unittest.TestCase): 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): def testCreateScheme(self):
"""Test creating a new color scheme""" """Test creating a new color scheme"""
dummyScheme = DummyColorScheme() dummyScheme = DummyColorScheme()
Expand Down Expand Up @@ -88,6 +98,21 @@ def testClone(self):
colorsClone = dummySchemeClone.fetchColors() colorsClone = dummySchemeClone.fetchColors()
self.assertEqual(colors, colorsClone) 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__": if __name__ == "__main__":
unittest.main() unittest.main()

0 comments on commit 12a2147

Please sign in to comment.