Add method to retrieve most recently used color

This commit is contained in:
Nyall Dawson 2016-09-14 11:47:52 +10:00
parent 2c3548df11
commit b4d9f64796
4 changed files with 39 additions and 1 deletions

View File

@ -204,8 +204,15 @@ class QgsRecentColorScheme : QgsColorScheme
/** Adds a color to the list of recent colors. /** Adds a color to the list of recent colors.
* @param color color to add * @param color color to add
* @note added in QGIS 2.14 * @note added in QGIS 2.14
* @see lastUsedColor()
*/ */
static void addRecentColor( const QColor& color ); 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 /** \ingroup core

View File

@ -117,6 +117,18 @@ void QgsRecentColorScheme::addRecentColor( const QColor& color )
settings.setValue( QString( "/colors/recent" ), recentColorVariants ); 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() QgsCustomColorScheme::QgsCustomColorScheme() : QgsColorScheme()
{ {

View File

@ -205,8 +205,15 @@ class CORE_EXPORT QgsRecentColorScheme : public QgsColorScheme
/** Adds a color to the list of recent colors. /** Adds a color to the list of recent colors.
* @param color color to add * @param color color to add
* @note added in QGIS 2.14 * @note added in QGIS 2.14
* @see lastUsedColor()
*/ */
static void addRecentColor( const QColor& color ); 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 /** \ingroup core

View File

@ -15,7 +15,7 @@ __revision__ = '$Format:%H$'
import qgis # NOQA import qgis # NOQA
from qgis.testing import unittest, start_app 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.QtCore import QCoreApplication, QSettings
from qgis.PyQt.QtGui import QColor from qgis.PyQt.QtGui import QColor
@ -113,6 +113,18 @@ class TestQgsColorScheme(unittest.TestCase):
scheme.erase() 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__": if __name__ == "__main__":
unittest.main() unittest.main()