Skip to content

Commit

Permalink
[FEATURE] Predefined custom color palette in QGIS options and color s…
Browse files Browse the repository at this point in the history
…cheme
  • Loading branch information
nyalldawson committed Aug 13, 2014
1 parent 1a5db86 commit 0901627
Show file tree
Hide file tree
Showing 8 changed files with 368 additions and 39 deletions.
25 changes: 25 additions & 0 deletions python/core/qgscolorscheme.sip
Expand Up @@ -68,3 +68,28 @@ class QgsRecentColorScheme : QgsColorScheme

QgsColorScheme* clone() const /Factory/;
}; // class QgsRecentColorScheme

/** \ingroup core
* \class QgsCustomColorScheme
* \brief A color scheme which contains custom colors set through QGIS app options dialog.
* \note Added in version 2.5
*/
class QgsCustomColorScheme : QgsColorScheme
{
%TypeHeaderCode
#include <qgscolorscheme.h>
%End

public:

QgsCustomColorScheme();

virtual ~QgsCustomColorScheme();

virtual QString schemeName() const;

virtual QgsNamedColorList fetchColors( const QString context = QString(),
const QColor baseColor = QColor() );

QgsColorScheme* clone() const /Factory/;
}; // class QgsCustomColorScheme
129 changes: 129 additions & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -684,6 +684,37 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
}
}

//
// Color palette
//
QList< QVariant > customColorVariants = settings.value( QString( "/colors/palettecolors" ) ).toList();
QList< QVariant > customColorLabels = settings.value( QString( "/colors/palettelabels" ) ).toList();

QList<QTreeWidgetItem *> customColors;
int colorIndex = 0;
for ( QList< QVariant >::iterator it = customColorVariants.begin();
it != customColorVariants.end(); ++it )
{
QColor color = ( *it ).value<QColor>();
QString label;
if ( customColorLabels.length() > colorIndex )
{
label = customColorLabels.at( colorIndex ).toString();
}

QTreeWidgetItem* item = new QTreeWidgetItem();
item->setData( 0, PaletteLabelRole, label );
item->setText( 1, label );
setPaletteColor( item, color );
customColors.append( item );

colorIndex++;
}
mTreeCustomColors->clear();
mTreeCustomColors->insertTopLevelItems( 0, customColors );
mTreeCustomColors->resizeColumnToContents( 0 );
mTreeCustomColors->setColumnWidth( 0, mTreeCustomColors->columnWidth( 0 ) + 20 );

//
// Composer settings
//
Expand Down Expand Up @@ -1295,6 +1326,23 @@ void QgsOptions::saveOptions()
}
settings.setValue( "Map/scales", myPaths );

//
// Color palette
//
QList< QVariant > customColors;
QList< QVariant > customColorLabels;
int colorCount = mTreeCustomColors->topLevelItemCount();
for ( int i = 0; i < colorCount; i++ )
{
QTreeWidgetItem* item = mTreeCustomColors->topLevelItem( i );
QVariant label = item->data( 0, PaletteLabelRole );
QVariant color = item->data( 0, PaletteColorRole );
customColors.append( color );
customColorLabels.append( label );
}
settings.setValue( QString( "/colors/palettecolors" ), customColors );
settings.setValue( QString( "/colors/palettelabels" ), customColorLabels );

//
// Composer settings
//
Expand Down Expand Up @@ -2055,3 +2103,84 @@ void QgsOptions::saveDefaultDatumTransformations()

s.endGroup();
}


void QgsOptions::on_mButtonAddColor_clicked()
{
QColor newColor = QColorDialog::getColor( QColor(), this->parentWidget(), tr( "Select color" ) );
if ( !newColor.isValid() )
{
return;
}
activateWindow();

QString newLabel = QInputDialog::getText( this, tr( "Color label" ),
tr( "Please enter a label for the color" ) );
activateWindow();

QTreeWidgetItem* item = new QTreeWidgetItem();
item->setData( 0, PaletteLabelRole, newLabel );
item->setText( 1, newLabel );
setPaletteColor( item, newColor );
mTreeCustomColors->addTopLevelItem( item );
mTreeCustomColors->resizeColumnToContents( 0 );
mTreeCustomColors->setColumnWidth( 0, mTreeCustomColors->columnWidth( 0 ) + 20 );
}

void QgsOptions::on_mButtonRemoveColor_clicked()
{
QTreeWidgetItem* item = mTreeCustomColors->currentItem();
if ( !item )
return;
int index = mTreeCustomColors->indexOfTopLevelItem( item );
mTreeCustomColors->takeTopLevelItem( index );
}

void QgsOptions::on_mTreeCustomColors_itemDoubleClicked( QTreeWidgetItem* item, int column )
{
if ( column == 0 )
{
QColor newColor = QColorDialog::getColor( item->data( 0, PaletteColorRole ).value<QColor>(), this->parentWidget(), tr( "Select color" ) );
if ( !newColor.isValid() )
{
return;
}
setPaletteColor( item, newColor );
}
else
{
bool ok;
QString label = item->data( 0, PaletteLabelRole ).toString();
QString newLabel = QInputDialog::getText( this, tr( "Color label" ),
tr( "Please enter a label for the color" ),
QLineEdit::Normal, label, &ok );
if ( !ok )
{
return;
}

item->setText( 1, newLabel );
item->setData( 0, PaletteLabelRole, newLabel );
}
}

void QgsOptions::setPaletteColor( QTreeWidgetItem* item, QColor color )
{
QSize iconSize( 16, 16 );
QPixmap pixmap( iconSize );
pixmap.fill( QColor( 0, 0, 0, 0 ) );
QRect rect( 1, 1, iconSize.width() - 2, iconSize.height() - 2 );

// draw a slightly rounded rectangle
QPainter p;
p.begin( &pixmap );
p.setPen( Qt::NoPen );
p.setRenderHint( QPainter::Antialiasing );
p.setBrush( color );
p.drawRoundedRect( rect, 2, 2 );
p.end();

item->setIcon( 0, QIcon( pixmap ) );
item->setData( 0, PaletteColorRole, color );
item->setText( 0, color.name() );
}
10 changes: 10 additions & 0 deletions src/app/qgsoptions.h
Expand Up @@ -241,6 +241,10 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
void on_mRemoveDefaultTransformButton_clicked();
void on_mAddDefaultTransformButton_clicked();

void on_mButtonAddColor_clicked();
void on_mButtonRemoveColor_clicked();
void on_mTreeCustomColors_itemDoubleClicked( QTreeWidgetItem* item, int column );

private:
QStringList i18nList();
void initContrastEnhancement( QComboBox *cbox, QString name, QString defaultVal );
Expand All @@ -256,10 +260,16 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption

void saveDefaultDatumTransformations();

void setPaletteColor( QTreeWidgetItem *item, QColor color );

protected:
QgisAppStyleSheet* mStyleSheetBuilder;
QMap<QString, QVariant> mStyleSheetNewOpts;
QMap<QString, QVariant> mStyleSheetOldOpts;

static const int PaletteColorRole = Qt::UserRole + 1;
static const int PaletteLabelRole = Qt::UserRole + 2;

};

#endif // #ifndef QGSOPTIONS_H
47 changes: 47 additions & 0 deletions src/core/qgscolorscheme.cpp 100755 → 100644
Expand Up @@ -68,3 +68,50 @@ QgsColorScheme *QgsRecentColorScheme::clone() const
{
return new QgsRecentColorScheme();
}


QgsCustomColorScheme::QgsCustomColorScheme() : QgsColorScheme()
{

}

QgsCustomColorScheme::~QgsCustomColorScheme()
{

}

QgsNamedColorList QgsCustomColorScheme::fetchColors( const QString context, const QColor baseColor )
{
Q_UNUSED( context );
Q_UNUSED( baseColor );

//fetch predefined custom colors
QSettings settings;

QList< QVariant > customColorVariants = settings.value( QString( "/colors/palettecolors" ) ).toList();
QList< QVariant > customColorLabels = settings.value( QString( "/colors/palettelabels" ) ).toList();

//generate list from custom colors
QgsNamedColorList colorList;
int colorIndex = 0;
for ( QList< QVariant >::iterator it = customColorVariants.begin();
it != customColorVariants.end(); ++it )
{
QColor color = ( *it ).value<QColor>();
QString label;
if ( customColorLabels.length() > colorIndex )
{
label = customColorLabels.at( colorIndex ).toString();
}

colorList.append( qMakePair( color, label ) );
colorIndex++;
}

return colorList;
}

QgsColorScheme *QgsCustomColorScheme::clone() const
{
return new QgsCustomColorScheme();
}
21 changes: 21 additions & 0 deletions src/core/qgscolorscheme.h
Expand Up @@ -90,4 +90,25 @@ class CORE_EXPORT QgsRecentColorScheme : public QgsColorScheme
QgsColorScheme* clone() const;
};

/** \ingroup core
* \class QgsCustomColorScheme
* \brief A color scheme which contains custom colors set through QGIS app options dialog.
* \note Added in version 2.5
*/
class CORE_EXPORT QgsCustomColorScheme : public QgsColorScheme
{
public:

QgsCustomColorScheme();

virtual ~QgsCustomColorScheme();

virtual QString schemeName() const { return QT_TR_NOOP( "Custom color palette" ); }

virtual QgsNamedColorList fetchColors( const QString context = QString(),
const QColor baseColor = QColor() );

QgsColorScheme* clone() const;
};

#endif
1 change: 1 addition & 0 deletions src/core/qgscolorschemeregistry.cpp
Expand Up @@ -67,6 +67,7 @@ void QgsColorSchemeRegistry::addDefaultSchemes()
{
//default color schemes
addColorScheme( new QgsRecentColorScheme() );
addColorScheme( new QgsCustomColorScheme() );
}

void QgsColorSchemeRegistry::addColorScheme( QgsColorScheme *scheme )
Expand Down

0 comments on commit 0901627

Please sign in to comment.