Skip to content

Commit

Permalink
Default names for colors when adding to schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 12, 2014
1 parent 5b3e014 commit b44ef92
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 6 deletions.
8 changes: 8 additions & 0 deletions python/core/symbology-ng/qgssymbollayerv2utils.sip
Expand Up @@ -191,6 +191,14 @@ class QgsSymbolLayerV2Utils
static QgsVectorColorRampV2* loadColorRamp( QDomElement& element ) /Factory/;
static QDomElement saveColorRamp( QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc );

/**
* Returns a friendly display name for a color
* @param color source color
* @returns display name for color
* @note added in 2.5
*/
static QString colorToName( const QColor& color );

/**
* Attempts to parse a string as a list of colors using a variety of common formats, including hex
* codes, rgb and rgba strings.
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsoptions.cpp
Expand Up @@ -37,6 +37,7 @@
#include "qgsdialog.h"
#include "qgscomposer.h"
#include "qgscolorschemeregistry.h"
#include "qgssymbollayerv2utils.h"

#include <QInputDialog>
#include <QFileDialog>
Expand Down Expand Up @@ -2087,7 +2088,7 @@ void QgsOptions::on_mButtonAddColor_clicked()
}
activateWindow();

mTreeCustomColors->addColor( newColor );
mTreeCustomColors->addColor( newColor, QgsSymbolLayerV2Utils::colorToName( newColor ) );
}

void QgsOptions::on_mButtonImportColors_clicked()
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsprojectproperties.cpp
Expand Up @@ -47,6 +47,7 @@
#include "qgsrelationmanager.h"
#include "qgisapp.h"
#include "qgscolorschemeregistry.h"
#include "qgssymbollayerv2utils.h"

//qt includes
#include <QColorDialog>
Expand Down Expand Up @@ -1700,7 +1701,7 @@ void QgsProjectProperties::on_mButtonAddColor_clicked()
}
activateWindow();

mTreeProjectColors->addColor( newColor );
mTreeProjectColors->addColor( newColor, QgsSymbolLayerV2Utils::colorToName( newColor ) );
}

void QgsProjectProperties::on_mButtonImportColors_clicked()
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgscolorscheme.cpp
Expand Up @@ -69,7 +69,7 @@ QgsNamedColorList QgsRecentColorScheme::fetchColors( const QString context,
QgsNamedColorList colorList;
foreach ( QVariant color, recentColorVariants )
{
colorList.append( qMakePair( color.value<QColor>(), QString() ) );
colorList.append( qMakePair( color.value<QColor>(), QgsSymbolLayerV2Utils::colorToName( color.value<QColor>() ) ) );
}
return colorList;
}
Expand Down
12 changes: 12 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2utils.cpp
Expand Up @@ -2732,6 +2732,18 @@ QDomElement QgsSymbolLayerV2Utils::saveColorRamp( QString name, QgsVectorColorRa
return rampEl;
}

QString QgsSymbolLayerV2Utils::colorToName( const QColor &color )
{
if ( !color.isValid() )
{
return QString();
}

//TODO - utilise a color names database (such as X11) to return nicer names
//for now, just return hex codes
return color.name();
}

QList<QColor> QgsSymbolLayerV2Utils::parseColorList( const QString colorStr )
{
QList<QColor> colors;
Expand Down
8 changes: 8 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2utils.h
Expand Up @@ -229,6 +229,14 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
static QgsVectorColorRampV2* loadColorRamp( QDomElement& element );
static QDomElement saveColorRamp( QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc );

/**
* Returns a friendly display name for a color
* @param color source color
* @returns display name for color
* @note added in 2.5
*/
static QString colorToName( const QColor& color );

/**
* Attempts to parse a string as a list of colors using a variety of common formats, including hex
* codes, rgb and rgba strings.
Expand Down
6 changes: 3 additions & 3 deletions src/gui/qgscolorschemelist.cpp
Expand Up @@ -106,7 +106,7 @@ void QgsColorSchemeList::pasteColors()
QgsNamedColorList::const_iterator colorIt = pastedColors.constBegin();
for ( ; colorIt != pastedColors.constEnd(); ++colorIt )
{
mModel->addColor(( *colorIt ).first, ( *colorIt ).second );
mModel->addColor(( *colorIt ).first, !( *colorIt ).second.isEmpty() ? ( *colorIt ).second : QgsSymbolLayerV2Utils::colorToName(( *colorIt ).first ) );
}
}

Expand Down Expand Up @@ -151,7 +151,7 @@ bool QgsColorSchemeList::importColorsFromGpl( QFile &file )
QgsNamedColorList::const_iterator colorIt = importedColors.constBegin();
for ( ; colorIt != importedColors.constEnd(); ++colorIt )
{
mModel->addColor(( *colorIt ).first, ( *colorIt ).second );
mModel->addColor(( *colorIt ).first, !( *colorIt ).second.isEmpty() ? ( *colorIt ).second : QgsSymbolLayerV2Utils::colorToName(( *colorIt ).first ) );
}

return true;
Expand Down Expand Up @@ -422,7 +422,7 @@ bool QgsColorSchemeModel::dropMimeData( const QMimeData *data, Qt::DropAction ac
QModelIndex colorIdx = index( beginRow, 0, QModelIndex() );
setData( colorIdx, QVariant(( *colorIt ).first ) );
QModelIndex labelIdx = index( beginRow, 1, QModelIndex() );
setData( labelIdx, QVariant(( *colorIt ).second ) );
setData( labelIdx, !( *colorIt ).second.isEmpty() ? ( *colorIt ).second : QgsSymbolLayerV2Utils::colorToName(( *colorIt ).first ) );
beginRow++;
}

Expand Down

0 comments on commit b44ef92

Please sign in to comment.