Skip to content

Commit

Permalink
Add method to retrieve most recently used color
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 14, 2016
1 parent 2c3548d commit b4d9f64
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
7 changes: 7 additions & 0 deletions python/core/qgscolorscheme.sip
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,15 @@ class QgsRecentColorScheme : QgsColorScheme
/** Adds a color to the list of recent colors.
* @param color color to add
* @note added in QGIS 2.14
* @see lastUsedColor()
*/
static void addRecentColor( const QColor& color );

/** Returns the most recently used color.
* @note added in QGIS 3.0
* @see addRecentColor()
*/
static QColor lastUsedColor();
};

/** \ingroup core
Expand Down
12 changes: 12 additions & 0 deletions src/core/qgscolorscheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ void QgsRecentColorScheme::addRecentColor( const QColor& color )
settings.setValue( QString( "/colors/recent" ), recentColorVariants );
}

QColor QgsRecentColorScheme::lastUsedColor()
{
//fetch recent colors
QSettings settings;
QList< QVariant > recentColorVariants = settings.value( QString( "/colors/recent" ) ).toList();

if ( recentColorVariants.isEmpty() )
return QColor();

return recentColorVariants.at( 0 ).value<QColor>();
}


QgsCustomColorScheme::QgsCustomColorScheme() : QgsColorScheme()
{
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgscolorscheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,15 @@ class CORE_EXPORT QgsRecentColorScheme : public QgsColorScheme
/** Adds a color to the list of recent colors.
* @param color color to add
* @note added in QGIS 2.14
* @see lastUsedColor()
*/
static void addRecentColor( const QColor& color );

/** Returns the most recently used color.
* @note added in QGIS 3.0
* @see addRecentColor()
*/
static QColor lastUsedColor();
};

/** \ingroup core
Expand Down
14 changes: 13 additions & 1 deletion tests/src/python/test_qgscolorscheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import qgis # NOQA

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

Expand Down Expand Up @@ -113,6 +113,18 @@ def testUserScheme(self):

scheme.erase()

def testRecentColors(self):
""" test retrieving recent colors """

# no colors
self.assertFalse(QgsRecentColorScheme().lastUsedColor().isValid())

# add a recent color
QgsRecentColorScheme().addRecentColor(QColor(255, 0, 0))
self.assertEqual(QgsRecentColorScheme().lastUsedColor(), QColor(255, 0, 0))
QgsRecentColorScheme().addRecentColor(QColor(0, 255, 0))
self.assertEqual(QgsRecentColorScheme().lastUsedColor(), QColor(0, 255, 0))


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

0 comments on commit b4d9f64

Please sign in to comment.