Skip to content

Commit

Permalink
Use represention values for classified renderers [FEATURE]
Browse files Browse the repository at this point in the history
When a field is configured with a value relation, value map or other "representable value" and the field is used as the source for a classification renderer, the represented values will be taken to label the categories.
  • Loading branch information
m-kuhn committed Dec 7, 2018
1 parent 16922a3 commit f353e4e
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/gui/symbology/qgscategorizedsymbolrendererwidget.cpp
Expand Up @@ -25,6 +25,9 @@
#include "qgscolorramp.h"
#include "qgscolorrampbutton.h"
#include "qgsstyle.h"
#include "qgsfieldformatter.h"
#include "qgsfieldformatterregistry.h"
#include "qgsapplication.h"
#include "qgslogger.h"

#include "qgssymbolselectordialog.h"
Expand Down Expand Up @@ -635,20 +638,30 @@ void QgsCategorizedSymbolRendererWidget::changeCategorySymbol()
}
}

static void _createCategories( QgsCategoryList &cats, QList<QVariant> &values, QgsSymbol *symbol )
static void _createCategories( QgsCategoryList &cats, QList<QVariant> &values, QgsSymbol *symbol, QgsVectorLayer *layer, const QString &attrName )
{
// sort the categories first
QgsSymbolLayerUtils::sortVariantList( values, Qt::AscendingOrder );

int num = values.count();

const QgsFields fields = layer->fields();
for ( int i = 0; i < num; i++ )
{
QVariant value = values[i];
QgsSymbol *newSymbol = symbol->clone();
if ( ! value.isNull() )
{
cats.append( QgsRendererCategory( value, newSymbol, value.toString(), true ) );
int fieldIdx = fields.lookupField( attrName );
QString categoryName = value.toString();
if ( fieldIdx != -1 )
{
QgsField field = fields.at( fieldIdx );
const QgsEditorWidgetSetup setup = field.editorWidgetSetup();
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
categoryName = formatter->representValue( layer, fieldIdx, setup.config(), QVariant(), value );
}
cats.append( QgsRendererCategory( value, newSymbol, categoryName, true ) );
}
}

Expand Down Expand Up @@ -710,7 +723,7 @@ void QgsCategorizedSymbolRendererWidget::addCategories()
#endif

QgsCategoryList cats;
_createCategories( cats, unique_vals, mCategorizedSymbol.get() );
_createCategories( cats, unique_vals, mCategorizedSymbol.get(), mLayer, attrName );
bool deleteExisting = false;

if ( !mOldClassificationAttribute.isEmpty() &&
Expand Down

0 comments on commit f353e4e

Please sign in to comment.