/* ColorHistory.cpp * * Copyright (C) 2014-2016 Jim Evins * * This file is part of gLabels-qt. * * gLabels-qt is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * gLabels-qt is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with gLabels-qt. If not, see . */ #include "ColorHistory.h" #include #include namespace glabels { ColorHistory::ColorHistory() { // empty } ColorHistory* ColorHistory::instance() { static ColorHistory* singletonInstance = nullptr; if ( singletonInstance == nullptr ) { singletonInstance = new ColorHistory(); } return singletonInstance; } void ColorHistory::addColor( const QColor &color ) { QList colorList = readColorList(); // Remove any occurances of this color already in list colorList.removeAll( color ); // Now add to list colorList.append( color ); // Remove oldest colors, if size exceeds current max while ( colorList.size() > MAX_COLORS ) { colorList.removeFirst(); } writeColorList( colorList ); emit changed(); } QList ColorHistory::getColors() { return readColorList(); } QList ColorHistory::readColorList() { QStringList defaultList; QSettings settings; settings.beginGroup( "ColorHistory" ); QStringList colorNameList = settings.value( "colors", defaultList ).toStringList(); settings.endGroup(); QList colorList; foreach ( QString colorName, colorNameList ) { colorList << QColor( colorName ); } // Remove oldest colors, if size exceeds current max while ( colorList.size() > MAX_COLORS ) { colorList.removeFirst(); } return colorList; } void ColorHistory::writeColorList( const QList& colorList ) { // Build name list QStringList colorNameList; foreach ( QColor color, colorList ) { colorNameList << color.name(); } // Save QSettings settings; settings.beginGroup( "ColorHistory" ); settings.setValue( "colors", colorNameList ); settings.endGroup(); } } // namespace glabels