Skip to content

Commit b44ef92

Browse files
committed
Default names for colors when adding to schemes
1 parent 5b3e014 commit b44ef92

File tree

7 files changed

+36
-6
lines changed

7 files changed

+36
-6
lines changed

python/core/symbology-ng/qgssymbollayerv2utils.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ class QgsSymbolLayerV2Utils
191191
static QgsVectorColorRampV2* loadColorRamp( QDomElement& element ) /Factory/;
192192
static QDomElement saveColorRamp( QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc );
193193

194+
/**
195+
* Returns a friendly display name for a color
196+
* @param color source color
197+
* @returns display name for color
198+
* @note added in 2.5
199+
*/
200+
static QString colorToName( const QColor& color );
201+
194202
/**
195203
* Attempts to parse a string as a list of colors using a variety of common formats, including hex
196204
* codes, rgb and rgba strings.

src/app/qgsoptions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "qgsdialog.h"
3838
#include "qgscomposer.h"
3939
#include "qgscolorschemeregistry.h"
40+
#include "qgssymbollayerv2utils.h"
4041

4142
#include <QInputDialog>
4243
#include <QFileDialog>
@@ -2087,7 +2088,7 @@ void QgsOptions::on_mButtonAddColor_clicked()
20872088
}
20882089
activateWindow();
20892090

2090-
mTreeCustomColors->addColor( newColor );
2091+
mTreeCustomColors->addColor( newColor, QgsSymbolLayerV2Utils::colorToName( newColor ) );
20912092
}
20922093

20932094
void QgsOptions::on_mButtonImportColors_clicked()

src/app/qgsprojectproperties.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "qgsrelationmanager.h"
4848
#include "qgisapp.h"
4949
#include "qgscolorschemeregistry.h"
50+
#include "qgssymbollayerv2utils.h"
5051

5152
//qt includes
5253
#include <QColorDialog>
@@ -1700,7 +1701,7 @@ void QgsProjectProperties::on_mButtonAddColor_clicked()
17001701
}
17011702
activateWindow();
17021703

1703-
mTreeProjectColors->addColor( newColor );
1704+
mTreeProjectColors->addColor( newColor, QgsSymbolLayerV2Utils::colorToName( newColor ) );
17041705
}
17051706

17061707
void QgsProjectProperties::on_mButtonImportColors_clicked()

src/core/qgscolorscheme.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ QgsNamedColorList QgsRecentColorScheme::fetchColors( const QString context,
6969
QgsNamedColorList colorList;
7070
foreach ( QVariant color, recentColorVariants )
7171
{
72-
colorList.append( qMakePair( color.value<QColor>(), QString() ) );
72+
colorList.append( qMakePair( color.value<QColor>(), QgsSymbolLayerV2Utils::colorToName( color.value<QColor>() ) ) );
7373
}
7474
return colorList;
7575
}

src/core/symbology-ng/qgssymbollayerv2utils.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2732,6 +2732,18 @@ QDomElement QgsSymbolLayerV2Utils::saveColorRamp( QString name, QgsVectorColorRa
27322732
return rampEl;
27332733
}
27342734

2735+
QString QgsSymbolLayerV2Utils::colorToName( const QColor &color )
2736+
{
2737+
if ( !color.isValid() )
2738+
{
2739+
return QString();
2740+
}
2741+
2742+
//TODO - utilise a color names database (such as X11) to return nicer names
2743+
//for now, just return hex codes
2744+
return color.name();
2745+
}
2746+
27352747
QList<QColor> QgsSymbolLayerV2Utils::parseColorList( const QString colorStr )
27362748
{
27372749
QList<QColor> colors;

src/core/symbology-ng/qgssymbollayerv2utils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,14 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
229229
static QgsVectorColorRampV2* loadColorRamp( QDomElement& element );
230230
static QDomElement saveColorRamp( QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc );
231231

232+
/**
233+
* Returns a friendly display name for a color
234+
* @param color source color
235+
* @returns display name for color
236+
* @note added in 2.5
237+
*/
238+
static QString colorToName( const QColor& color );
239+
232240
/**
233241
* Attempts to parse a string as a list of colors using a variety of common formats, including hex
234242
* codes, rgb and rgba strings.

src/gui/qgscolorschemelist.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void QgsColorSchemeList::pasteColors()
106106
QgsNamedColorList::const_iterator colorIt = pastedColors.constBegin();
107107
for ( ; colorIt != pastedColors.constEnd(); ++colorIt )
108108
{
109-
mModel->addColor(( *colorIt ).first, ( *colorIt ).second );
109+
mModel->addColor(( *colorIt ).first, !( *colorIt ).second.isEmpty() ? ( *colorIt ).second : QgsSymbolLayerV2Utils::colorToName(( *colorIt ).first ) );
110110
}
111111
}
112112

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

157157
return true;
@@ -422,7 +422,7 @@ bool QgsColorSchemeModel::dropMimeData( const QMimeData *data, Qt::DropAction ac
422422
QModelIndex colorIdx = index( beginRow, 0, QModelIndex() );
423423
setData( colorIdx, QVariant(( *colorIt ).first ) );
424424
QModelIndex labelIdx = index( beginRow, 1, QModelIndex() );
425-
setData( labelIdx, QVariant(( *colorIt ).second ) );
425+
setData( labelIdx, !( *colorIt ).second.isEmpty() ? ( *colorIt ).second : QgsSymbolLayerV2Utils::colorToName(( *colorIt ).first ) );
426426
beginRow++;
427427
}
428428

0 commit comments

Comments
 (0)