Added custom color dialog to color palette dialog.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/* ColorButton.cpp
|
||||
*
|
||||
* Copyright (C) 2014 Jim Evins <evins@snaught.com>
|
||||
* Copyright (C) 2014-2016 Jim Evins <evins@snaught.com>
|
||||
*
|
||||
* This file is part of gLabels-qt.
|
||||
*
|
||||
@@ -74,6 +74,8 @@ void ColorButton::setColorNode( ColorNode colorNode )
|
||||
setIcon( QIcon( ColorSwatch( SWATCH_W, SWATCH_H, colorNode.color() ) ) );
|
||||
setText( "" );
|
||||
}
|
||||
|
||||
mDialog->setColorNode( colorNode );
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* ColorButton.h
|
||||
*
|
||||
* Copyright (C) 2014 Jim Evins <evins@snaught.com>
|
||||
* Copyright (C) 2014-2016 Jim Evins <evins@snaught.com>
|
||||
*
|
||||
* This file is part of gLabels-qt.
|
||||
*
|
||||
|
||||
+41
-45
@@ -1,6 +1,6 @@
|
||||
/* ColorHistory.cpp
|
||||
*
|
||||
* Copyright (C) 2014 Jim Evins <evins@snaught.com>
|
||||
* Copyright (C) 2014-2016 Jim Evins <evins@snaught.com>
|
||||
*
|
||||
* This file is part of gLabels-qt.
|
||||
*
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "ColorHistory.h"
|
||||
|
||||
#include <QSettings>
|
||||
#include <QtDebug>
|
||||
|
||||
|
||||
ColorHistory::ColorHistory()
|
||||
@@ -43,74 +44,69 @@ ColorHistory* ColorHistory::instance()
|
||||
|
||||
void ColorHistory::addColor( const QColor &color )
|
||||
{
|
||||
QColor oldColors[MAX_COLORS];
|
||||
QColor newColors[MAX_COLORS];
|
||||
int n;
|
||||
QList<QColor> colorList = readColorList();
|
||||
|
||||
readColorArray( oldColors, &n );
|
||||
// Remove any occurances of this color already in list
|
||||
colorList.removeAll( color );
|
||||
|
||||
int i;
|
||||
newColors[0] = color;
|
||||
for ( i = 0; ( i < (MAX_COLORS-1) ) && (i < n); i++ )
|
||||
// Now add to list
|
||||
colorList.append( color );
|
||||
|
||||
// Remove oldest colors, if size exceeds current max
|
||||
while ( colorList.size() > MAX_COLORS )
|
||||
{
|
||||
newColors[i+1] = oldColors[i];
|
||||
colorList.removeFirst();
|
||||
}
|
||||
|
||||
writeColorArray( newColors, i+1 );
|
||||
writeColorList( colorList );
|
||||
|
||||
emit changed();
|
||||
}
|
||||
|
||||
|
||||
QColor ColorHistory::getColor( int i )
|
||||
QList<QColor> ColorHistory::getColors()
|
||||
{
|
||||
QColor colors[MAX_COLORS];
|
||||
int n;
|
||||
|
||||
readColorArray( colors, &n );
|
||||
|
||||
if ( (n > 0) && (i < n) )
|
||||
{
|
||||
return colors[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
return QColor( 0, 0, 0, 0 );
|
||||
}
|
||||
return readColorList();
|
||||
}
|
||||
|
||||
|
||||
void ColorHistory::readColorArray( QColor array[MAX_COLORS], int* n )
|
||||
QList<QColor> ColorHistory::readColorList()
|
||||
{
|
||||
QStringList defaultList;
|
||||
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<QColor>();
|
||||
}
|
||||
settings.endArray();
|
||||
|
||||
QStringList colorNameList = settings.value( "colors", defaultList ).toStringList();
|
||||
settings.endGroup();
|
||||
|
||||
QList<QColor> 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::writeColorArray( const QColor array[MAX_COLORS], int n )
|
||||
void ColorHistory::writeColorList( const QList<QColor>& colorList )
|
||||
{
|
||||
QSettings settings;
|
||||
|
||||
settings.beginGroup( "ColorHistory" );
|
||||
|
||||
settings.beginWriteArray( "history" );
|
||||
for ( int i = 0; (i < n) && (i < MAX_COLORS); i++ )
|
||||
// Build name list
|
||||
QStringList colorNameList;
|
||||
foreach ( QColor color, colorList )
|
||||
{
|
||||
settings.setArrayIndex(i);
|
||||
settings.setValue( "color", array[i] );
|
||||
colorNameList << color.name();
|
||||
}
|
||||
settings.endArray();
|
||||
|
||||
// Save
|
||||
QSettings settings;
|
||||
settings.beginGroup( "ColorHistory" );
|
||||
settings.setValue( "colors", colorNameList );
|
||||
settings.endGroup();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* ColorHistory.h
|
||||
*
|
||||
* Copyright (C) 2014 Jim Evins <evins@snaught.com>
|
||||
* Copyright (C) 2014-2016 Jim Evins <evins@snaught.com>
|
||||
*
|
||||
* This file is part of gLabels-qt.
|
||||
*
|
||||
@@ -26,14 +26,14 @@
|
||||
|
||||
|
||||
///
|
||||
/// Barcode Backends Database
|
||||
/// Color History
|
||||
///
|
||||
class ColorHistory : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
static const int MAX_COLORS = 10;
|
||||
static const int MAX_COLORS = 9;
|
||||
|
||||
/////////////////////////////////
|
||||
// Life Cycle
|
||||
@@ -57,15 +57,15 @@ signals:
|
||||
/////////////////////////////////
|
||||
public:
|
||||
void addColor( const QColor &color );
|
||||
QColor getColor( int i );
|
||||
QList<QColor> getColors();
|
||||
|
||||
|
||||
/////////////////////////////////
|
||||
// Private Methods
|
||||
/////////////////////////////////
|
||||
private:
|
||||
void readColorArray( QColor array[MAX_COLORS], int* n );
|
||||
void writeColorArray( const QColor array[MAX_COLORS], int n );
|
||||
QList<QColor> readColorList();
|
||||
void writeColorList( const QList<QColor>& colorList );
|
||||
|
||||
|
||||
/////////////////////////////////
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* ColorPaletteDialog.cpp
|
||||
*
|
||||
* Copyright (C) 2014 Jim Evins <evins@snaught.com>
|
||||
* Copyright (C) 2014-2016 Jim Evins <evins@snaught.com>
|
||||
*
|
||||
* This file is part of gLabels-qt.
|
||||
*
|
||||
@@ -27,6 +27,8 @@
|
||||
#include <QHBoxLayout>
|
||||
#include <QGridLayout>
|
||||
#include <QFrame>
|
||||
#include <QColorDialog>
|
||||
#include <QtDebug>
|
||||
|
||||
|
||||
ColorPaletteDialog::ColorTableEntry ColorPaletteDialog::mColorTable[] = {
|
||||
@@ -160,6 +162,12 @@ ColorPaletteDialog::ColorPaletteDialog( const QString& defaultLabel,
|
||||
}
|
||||
|
||||
|
||||
void ColorPaletteDialog::setColorNode( const ColorNode& colorNode )
|
||||
{
|
||||
mColorNode = colorNode;
|
||||
}
|
||||
|
||||
|
||||
void ColorPaletteDialog::setKeys( const QList<QString> keyList )
|
||||
{
|
||||
// TODO
|
||||
@@ -197,7 +205,7 @@ void ColorPaletteDialog::onPaletteItemActivated( int id )
|
||||
void ColorPaletteDialog::onHistoryItemActivated( int id )
|
||||
{
|
||||
mColorNode.setFieldFlag( false );
|
||||
mColorNode.setColor( mColorHistory->getColor( id ) );
|
||||
mColorNode.setColor( mColorHistory->getColors()[id] );
|
||||
mColorNode.setKey( "" );
|
||||
|
||||
emit colorChanged( mColorNode, false );
|
||||
@@ -207,8 +215,27 @@ void ColorPaletteDialog::onHistoryItemActivated( int id )
|
||||
|
||||
void ColorPaletteDialog::onCustomColorItemActivated()
|
||||
{
|
||||
// TODO
|
||||
accept();
|
||||
QColorDialog dlg( mColorNode.color(), this );
|
||||
dlg.setWindowTitle( tr("Custom Color") );
|
||||
|
||||
if ( dlg.exec() )
|
||||
{
|
||||
ColorNode newColorNode;
|
||||
|
||||
newColorNode.setFieldFlag( false );
|
||||
newColorNode.setColor( dlg.currentColor() );
|
||||
newColorNode.setKey( "" );
|
||||
|
||||
if ( newColorNode != mColorNode )
|
||||
{
|
||||
mColorNode = newColorNode;
|
||||
|
||||
mColorHistory->addColor( mColorNode.color() );
|
||||
|
||||
emit colorChanged( mColorNode, false );
|
||||
accept();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -220,14 +247,19 @@ void ColorPaletteDialog::onColorHistoryChanged()
|
||||
|
||||
void ColorPaletteDialog::loadCustomColorHistory()
|
||||
{
|
||||
for ( int i = 0; i < PALETTE_COLS; i++ )
|
||||
QList<QColor> colorList = mColorHistory->getColors();
|
||||
|
||||
int id = 0;
|
||||
foreach ( QColor color, colorList )
|
||||
{
|
||||
QColor color = mColorHistory->getColor( i );
|
||||
mHistoryItem[id]->setColor( id, color, QString(tr("Custom color #%1").arg(id+1) ) );
|
||||
mHistoryItem[id]->setEnabled( true );
|
||||
id++;
|
||||
}
|
||||
|
||||
if ( color.alpha() != 0 )
|
||||
{
|
||||
mHistoryItem[i]->setColor( i, color, QString(tr("Custom color #%d").arg(i) ) );
|
||||
mHistoryItem[i]->setEnabled( true );
|
||||
}
|
||||
while ( id < PALETTE_ROWS )
|
||||
{
|
||||
mHistoryItem[id]->setEnabled( false );
|
||||
id++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* ColorPaletteDialog.h
|
||||
*
|
||||
* Copyright (C) 2014 Jim Evins <evins@snaught.com>
|
||||
* Copyright (C) 2014-2016 Jim Evins <evins@snaught.com>
|
||||
*
|
||||
* This file is part of gLabels-qt.
|
||||
*
|
||||
@@ -58,6 +58,7 @@ signals:
|
||||
// Public Methods
|
||||
/////////////////////////////////
|
||||
public:
|
||||
void setColorNode( const ColorNode& colorNode );
|
||||
void setKeys( const QList<QString> keyList );
|
||||
void clearKeys();
|
||||
|
||||
@@ -87,7 +88,7 @@ private:
|
||||
QColor mDefaultColor;
|
||||
ColorNode mColorNode;
|
||||
|
||||
static const int PALETTE_COLS = 9;
|
||||
static const int PALETTE_COLS = ColorHistory::MAX_COLORS;
|
||||
static const int PALETTE_ROWS = 4;
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -224,7 +224,7 @@ QStringList Settings::recentTemplateList()
|
||||
QStringList defaultList;
|
||||
|
||||
mInstance->beginGroup( "Recent" );
|
||||
QStringList returnList = mInstance->value( "templateList", defaultList ).toStringList();
|
||||
QStringList returnList = mInstance->value( "templates", defaultList ).toStringList();
|
||||
mInstance->endGroup();
|
||||
|
||||
return returnList;
|
||||
@@ -235,7 +235,7 @@ void Settings::addToRecentTemplateList( const QString& name )
|
||||
{
|
||||
mInstance->beginGroup( "Recent" );
|
||||
|
||||
QStringList list = mInstance->value( "templateList" ).toStringList();
|
||||
QStringList list = mInstance->value( "templates" ).toStringList();
|
||||
|
||||
list.removeAll( name );
|
||||
list.prepend( name );
|
||||
@@ -244,7 +244,7 @@ void Settings::addToRecentTemplateList( const QString& name )
|
||||
list.removeLast();
|
||||
}
|
||||
|
||||
mInstance->setValue( "templateList", list );
|
||||
mInstance->setValue( "templates", list );
|
||||
|
||||
mInstance->endGroup();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user