130 lines
2.7 KiB
C++
130 lines
2.7 KiB
C++
/* ColorHistory.cpp
|
|
*
|
|
* Copyright (C) 2014-2016 Jim Evins <evins@snaught.com>
|
|
*
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "ColorHistory.h"
|
|
|
|
#include <QSettings>
|
|
#include <QtDebug>
|
|
|
|
|
|
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, const QString& name )
|
|
{
|
|
QString nameColor = name + ":" + color.name();
|
|
|
|
QStringList nameColorList = readNameColorList();
|
|
|
|
// Remove any occurrences of this color already in list
|
|
nameColorList.removeAll( nameColor );
|
|
|
|
// Now add to list
|
|
nameColorList.append( nameColor );
|
|
|
|
// Remove oldest colors, if size exceeds current max
|
|
while ( nameColorList.size() > MAX_COLORS )
|
|
{
|
|
nameColorList.removeFirst();
|
|
}
|
|
|
|
writeNameColorList( nameColorList );
|
|
|
|
emit changed();
|
|
}
|
|
|
|
|
|
QList<QColor> ColorHistory::getColors()
|
|
{
|
|
QList<QColor> colorList;
|
|
|
|
for ( QString& nameColor : readNameColorList() )
|
|
{
|
|
QStringList v = nameColor.split( ':' );
|
|
colorList << QColor( v[1] );
|
|
}
|
|
|
|
return colorList;
|
|
}
|
|
|
|
|
|
QStringList ColorHistory::getNames()
|
|
{
|
|
QStringList nameList;
|
|
|
|
for ( QString& nameColor : readNameColorList() )
|
|
{
|
|
QStringList v = nameColor.split( ':' );
|
|
nameList << v[0];
|
|
}
|
|
|
|
return nameList;
|
|
}
|
|
|
|
|
|
QStringList ColorHistory::readNameColorList()
|
|
{
|
|
QStringList defaultList;
|
|
QSettings settings;
|
|
|
|
settings.beginGroup( "ColorHistory" );
|
|
QStringList nameColorList = settings.value( "colors", defaultList ).toStringList();
|
|
settings.endGroup();
|
|
|
|
// Remove oldest colors, if size exceeds current max
|
|
while ( nameColorList.size() > MAX_COLORS )
|
|
{
|
|
nameColorList.removeFirst();
|
|
}
|
|
|
|
return nameColorList;
|
|
}
|
|
|
|
|
|
void ColorHistory::writeNameColorList( const QStringList& nameColorList )
|
|
{
|
|
// Save
|
|
QSettings settings;
|
|
settings.beginGroup( "ColorHistory" );
|
|
settings.setValue( "colors", nameColorList );
|
|
settings.endGroup();
|
|
}
|
|
|
|
} // namespace glabels
|