diff --git a/glabels/CMakeLists.txt b/glabels/CMakeLists.txt index c937fa6..99a5cf3 100644 --- a/glabels/CMakeLists.txt +++ b/glabels/CMakeLists.txt @@ -9,6 +9,7 @@ set (glabels_sources BarcodeMenuButton.cpp BarcodeMenuItem.cpp BarcodeStyle.cpp + ColorHistory.cpp ColorNode.cpp ColorPalletteItem.cpp ColorSwatch.cpp @@ -33,6 +34,7 @@ set (glabels_qobject_headers BarcodeMenu.h BarcodeMenuButton.h BarcodeMenuItem.h + ColorHistory.h ColorPalletteItem.h LabelModel.h LabelModelObject.h diff --git a/glabels/ColorHistory.cpp b/glabels/ColorHistory.cpp new file mode 100644 index 0000000..d8470e7 --- /dev/null +++ b/glabels/ColorHistory.cpp @@ -0,0 +1,119 @@ +/* ColorHistory.cpp + * + * Copyright (C) 2014 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 + + +namespace glabels +{ + + ColorHistory::ColorHistory() + { + } + + + ColorHistory* ColorHistory::instance() + { + static ColorHistory* singletonInstance = 0; + + if ( singletonInstance == 0 ) + { + singletonInstance = new ColorHistory(); + } + + return singletonInstance; + } + + + void ColorHistory::addColor( const QColor &color ) + { + QColor oldColors[MAX_COLORS]; + QColor newColors[MAX_COLORS]; + int n; + + readColorArray( oldColors, n ); + + int i; + newColors[0] = color; + for ( i = 0; ( i < (MAX_COLORS-1) ) && (i < n); i++ ) + { + newColors[i+1] = oldColors[i]; + } + + writeColorArray( newColors, i+1 ); + emit changed(); + } + + + QColor ColorHistory::getColor( int i ) + { + QColor colors[MAX_COLORS]; + int n; + + if ( (n > 0) && (i < n) ) + { + return colors[i]; + } + else + { + return QColor( 0, 0, 0, 0 ); + } + } + + + void ColorHistory::readColorArray( QColor array[MAX_COLORS], int& n ) + { + QSettings settings; + + settings.beginGroup( "ColorHistory" ); + + settings.beginReadArray( "history" ); + n = settings.value( "history/size", 0 ).toInt(); + for ( int i = 0; i < n; i++ ) + { + settings.setArrayIndex(i); + array[i] = settings.value( "color" ).value(); + } + settings.endArray(); + + settings.endGroup(); + } + + + void ColorHistory::writeColorArray( const QColor array[MAX_COLORS], int n ) + { + QSettings settings; + + settings.beginGroup( "ColorHistory" ); + + settings.beginWriteArray( "history" ); + for ( int i = 0; (i < n) && (i < MAX_COLORS); i++ ) + { + settings.setArrayIndex(i); + settings.setValue( "color", array[i] ); + } + settings.endArray(); + + settings.endGroup(); + } + +} diff --git a/glabels/ColorHistory.h b/glabels/ColorHistory.h new file mode 100644 index 0000000..3d11642 --- /dev/null +++ b/glabels/ColorHistory.h @@ -0,0 +1,84 @@ +/* ColorHistory.h + * + * Copyright (C) 2014 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 . + */ + +#ifndef glabels_ColorHistory_h +#define glabels_ColorHistory_h + +#include +#include + + +namespace glabels +{ + + /// + /// Barcode Backends Database + /// + class ColorHistory : public QObject + { + Q_OBJECT + + public: + static const int MAX_COLORS = 10; + + ///////////////////////////////// + // Life Cycle + ///////////////////////////////// + private: + ColorHistory(); + + public: + static ColorHistory* instance(); + + + ///////////////////////////////// + // Signals + ///////////////////////////////// + signals: + void changed(); + + + ///////////////////////////////// + // Public Methods + ///////////////////////////////// + public: + void addColor( const QColor &color ); + QColor getColor( int i ); + + + ///////////////////////////////// + // Private Methods + ///////////////////////////////// + private: + void readColorArray( QColor array[MAX_COLORS], int& n ); + void writeColorArray( const QColor array[MAX_COLORS], int n ); + + + ///////////////////////////////// + // Private Members + ///////////////////////////////// + private: + + }; + +} + + +#endif // glabels_ColorHistory_h