Skip to content
Permalink
Browse files

Store in renderer classification attribute by its name, not by index

git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11908 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Nov 3, 2009
1 parent 5438248 commit ef9190d681802f7ff53ee062cb41a44612620178
@@ -49,11 +49,11 @@ public:

virtual QgsSymbolV2* symbolForFeature(QgsFeature& feature)=0;

virtual void startRender(QgsRenderContext& context)=0;
virtual void startRender(QgsRenderContext& context, const QMap<int, QgsField>& fields)=0;

virtual void stopRender(QgsRenderContext& context)=0;

virtual QList<int> usedAttributes()=0;
virtual QList<QString> usedAttributes()=0;

virtual ~QgsFeatureRendererV2();

@@ -89,11 +89,11 @@ public:

virtual QgsSymbolV2* symbolForFeature(QgsFeature& feature);

virtual void startRender(QgsRenderContext& context);
virtual void startRender(QgsRenderContext& context, const QMap<int, QgsField>& fields);

virtual void stopRender(QgsRenderContext& context);

virtual QList<int> usedAttributes();
virtual QList<QString> usedAttributes();

QgsSymbolV2* symbol() const;
void setSymbol(QgsSymbolV2* s /Transfer/);
@@ -140,17 +140,17 @@ class QgsCategorizedSymbolRendererV2 : QgsFeatureRendererV2

public:

QgsCategorizedSymbolRendererV2(int attrNum = -1, QgsCategoryList categories = QgsCategoryList());
QgsCategorizedSymbolRendererV2(QString attrName = QString(), QgsCategoryList categories = QgsCategoryList());

virtual ~QgsCategorizedSymbolRendererV2();

virtual QgsSymbolV2* symbolForFeature(QgsFeature& feature);

virtual void startRender(QgsRenderContext& context);
virtual void startRender(QgsRenderContext& context, const QMap<int, QgsField>& fields);

virtual void stopRender(QgsRenderContext& context);

virtual QList<int> usedAttributes();
virtual QList<QString> usedAttributes();

virtual QString dump();

@@ -167,8 +167,8 @@ public:
bool deleteCategory(int catIndex);
void deleteAllCategories();

int attributeIndex() const;
void setAttributeIndex(int attr);
QString classAttribute() const;
void setClassAttribute(QString attr);

protected:

@@ -210,24 +210,24 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
%End

public:
QgsGraduatedSymbolRendererV2(int attrNum = -1, QgsRangeList ranges = QgsRangeList());
QgsGraduatedSymbolRendererV2(QString attrNum = QString(), QgsRangeList ranges = QgsRangeList());

virtual ~QgsGraduatedSymbolRendererV2();

virtual QgsSymbolV2* symbolForFeature(QgsFeature& feature);

virtual void startRender(QgsRenderContext& context);
virtual void startRender(QgsRenderContext& context, const QMap<int, QgsField>& fields);

virtual void stopRender(QgsRenderContext& context);

virtual QList<int> usedAttributes();
virtual QList<QString> usedAttributes();

virtual QString dump();

virtual QgsFeatureRendererV2* clone() /Factory/;

int attributeIndex() const;
void setAttributeIndex(int attr);
QString classAttribute() const;
void setClassAttribute(QString attr);

const QgsRangeList& ranges();

@@ -246,7 +246,7 @@ public:

static QgsGraduatedSymbolRendererV2* createRenderer(
QgsVectorLayer* vlayer,
int attrNum,
QString attrName,
int classes,
Mode mode,
QgsSymbolV2* symbol,
@@ -428,9 +428,7 @@ void QgsLegendLayer::vectorLayerSymbologyV2( QgsVectorLayer* layer )
QgsCategorizedSymbolRendererV2* r = static_cast<QgsCategorizedSymbolRendererV2*>(renderer);
if (showClassifiers)
{
const QgsFieldMap& fields = layer->dataProvider()->fields();
QString fieldName = fields[r->attributeIndex()].name();
itemList.push_back( std::make_pair( fieldName, QPixmap() ) );
itemList.push_back( std::make_pair( r->classAttribute(), QPixmap() ) );
}

int count = r->categories().count();
@@ -447,9 +445,7 @@ void QgsLegendLayer::vectorLayerSymbologyV2( QgsVectorLayer* layer )
QgsGraduatedSymbolRendererV2* r = static_cast<QgsGraduatedSymbolRendererV2*>(renderer);
if (showClassifiers)
{
const QgsFieldMap& fields = layer->dataProvider()->fields();
QString fieldName = fields[r->attributeIndex()].name();
itemList.push_back( std::make_pair( fieldName, QPixmap() ) );
itemList.push_back( std::make_pair( r->classAttribute(), QPixmap() ) );
}

int count = r->ranges().count();
@@ -666,7 +666,7 @@ unsigned char *QgsVectorLayer::drawPolygon( unsigned char *feature, QgsRenderCon

void QgsVectorLayer::drawRendererV2( QgsRenderContext& rendererContext, bool labeling )
{
mRendererV2->startRender(rendererContext);
mRendererV2->startRender(rendererContext, pendingFields());

QgsFeature fet;
while ( nextFeature( fet ) )
@@ -685,7 +685,7 @@ void QgsVectorLayer::drawRendererV2Levels( QgsRenderContext& rendererContext, bo
QHash< QgsSymbolV2*, QList<QgsFeature> > features; // key = symbol, value = array of features

// startRender must be called before symbolForFeature() calls to make sure renderer is ready
mRendererV2->startRender(rendererContext);
mRendererV2->startRender(rendererContext, pendingFields());

// 1. fetch features
QgsFeature fet;
@@ -755,9 +755,13 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
int totalFeatures = pendingFeatureCount();
int featureCount = 0;

QgsAttributeList attributes = mRendererV2->usedAttributes();
if (attributes.count() > 0)
QgsDebugMsg("attrs: " + QString::number(attributes[0]));
QgsAttributeList attributes;
foreach (QString attrName, mRendererV2->usedAttributes())
{
int attrNum = QgsFeatureRendererV2::fieldNameIndex(pendingFields(), attrName);
attributes.append( attrNum );
QgsDebugMsg("attrs: "+ attrName + " - " + QString::number(attrNum));
}

bool labeling = FALSE;
if ( mLabelingEngine )
@@ -285,6 +285,16 @@ QDomElement QgsFeatureRendererV2::save(QDomDocument& doc)
return doc.createElement(RENDERER_TAG_NAME);
}

int QgsFeatureRendererV2::fieldNameIndex( const QgsFieldMap& fields, const QString& fieldName )
{
for ( QgsFieldMap::const_iterator it = fields.constBegin(); it != fields.constEnd(); ++it )
{
if ( it->name() == fieldName )
return it.key();
}
return -1;
}

///////////////////

QgsSingleSymbolRendererV2::QgsSingleSymbolRendererV2(QgsSymbolV2* symbol)
@@ -303,7 +313,7 @@ QgsSymbolV2* QgsSingleSymbolRendererV2::symbolForFeature(QgsFeature& feature)
return mSymbol;
}

void QgsSingleSymbolRendererV2::startRender(QgsRenderContext& context)
void QgsSingleSymbolRendererV2::startRender(QgsRenderContext& context, const QgsFieldMap& fields)
{
mSymbol->startRender(context);
}
@@ -313,9 +323,9 @@ void QgsSingleSymbolRendererV2::stopRender(QgsRenderContext& context)
mSymbol->stopRender(context);
}

QList<int> QgsSingleSymbolRendererV2::usedAttributes()
QList<QString> QgsSingleSymbolRendererV2::usedAttributes()
{
return QList<int>();
return QList<QString>();
}

QgsSymbolV2* QgsSingleSymbolRendererV2::symbol() const
@@ -435,8 +445,8 @@ QString QgsRendererCategoryV2::dump()

///////////////////

QgsCategorizedSymbolRendererV2::QgsCategorizedSymbolRendererV2(int attrNum, QgsCategoryList categories)
: QgsFeatureRendererV2(RendererCategorizedSymbol), mAttrNum(attrNum), mCategories(categories)
QgsCategorizedSymbolRendererV2::QgsCategorizedSymbolRendererV2(QString attrName, QgsCategoryList categories)
: QgsFeatureRendererV2(RendererCategorizedSymbol), mAttrName(attrName), mCategories(categories)
{
for (int i = 0; i < mCategories.count(); ++i)
{
@@ -489,7 +499,7 @@ QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForFeature(QgsFeature& featur
QgsAttributeMap::const_iterator ita = attrMap.find(mAttrNum);
if (ita == attrMap.end())
{
QgsDebugMsg("attribute required by renderer not found: "+QString::number(mAttrNum));
QgsDebugMsg("attribute '"+mAttrName+"' (index "+QString::number(mAttrNum)+") required by renderer not found");
return NULL;
}

@@ -537,11 +547,14 @@ void QgsCategorizedSymbolRendererV2::deleteAllCategories()
mCategories.clear();
}

void QgsCategorizedSymbolRendererV2::startRender(QgsRenderContext& context)
void QgsCategorizedSymbolRendererV2::startRender(QgsRenderContext& context, const QgsFieldMap& fields)
{
// make sure that the hash table is up to date
rebuildHash();

// find out classification attribute index from name
mAttrNum = fieldNameIndex(fields, mAttrName);

QgsCategoryList::iterator it = mCategories.begin();
for ( ; it != mCategories.end(); ++it)
it->symbol()->startRender(context);
@@ -554,24 +567,24 @@ void QgsCategorizedSymbolRendererV2::stopRender(QgsRenderContext& context)
it->symbol()->stopRender(context);
}

QList<int> QgsCategorizedSymbolRendererV2::usedAttributes()
QList<QString> QgsCategorizedSymbolRendererV2::usedAttributes()
{
QList<int> lst;
lst.append(mAttrNum);
QList<QString> lst;
lst.append(mAttrName);
return lst;
}

QString QgsCategorizedSymbolRendererV2::dump()
{
QString s = QString("CATEGORIZED: idx %1\n").arg(mAttrNum);
QString s = QString("CATEGORIZED: idx %1\n").arg(mAttrName);
for (int i=0; i<mCategories.count();i++)
s += mCategories[i].dump();
return s;
}

QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::clone()
{
QgsCategorizedSymbolRendererV2* r = new QgsCategorizedSymbolRendererV2( mAttrNum, mCategories );
QgsCategorizedSymbolRendererV2* r = new QgsCategorizedSymbolRendererV2( mAttrName, mCategories );
r->setUsingSymbolLevels( usingSymbolLevels() );
return r;
}
@@ -614,9 +627,9 @@ QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::create(QDomElement& elemen
catElem = catElem.nextSiblingElement();
}

int attrNum = element.attribute("attr").toInt();
QString attrName = element.attribute("attr");

QgsCategorizedSymbolRendererV2* r = new QgsCategorizedSymbolRendererV2(attrNum, cats);
QgsCategorizedSymbolRendererV2* r = new QgsCategorizedSymbolRendererV2(attrName, cats);

// delete symbols if there are any more
QgsSymbolLayerV2Utils::clearSymbolMap(symbolMap);
@@ -629,7 +642,7 @@ QDomElement QgsCategorizedSymbolRendererV2::save(QDomDocument& doc)
{
QDomElement rendererElem = doc.createElement(RENDERER_TAG_NAME);
rendererElem.setAttribute("type", "categorizedSymbol");
rendererElem.setAttribute("attr", mAttrNum);
rendererElem.setAttribute("attr", mAttrName);

// categories
int i = 0;
@@ -720,8 +733,8 @@ QString QgsRendererRangeV2::dump()
///////////


QgsGraduatedSymbolRendererV2::QgsGraduatedSymbolRendererV2(int attrNum, QgsRangeList ranges)
: QgsFeatureRendererV2(RendererGraduatedSymbol), mAttrNum(attrNum), mRanges(ranges), mMode(Custom)
QgsGraduatedSymbolRendererV2::QgsGraduatedSymbolRendererV2(QString attrName, QgsRangeList ranges)
: QgsFeatureRendererV2(RendererGraduatedSymbol), mAttrName(attrName), mRanges(ranges), mMode(Custom)
{
// TODO: check ranges for sanity (NULL symbols, invalid ranges)
}
@@ -748,7 +761,7 @@ QgsSymbolV2* QgsGraduatedSymbolRendererV2::symbolForFeature(QgsFeature& feature)
QgsAttributeMap::const_iterator ita = attrMap.find(mAttrNum);
if (ita == attrMap.end())
{
QgsDebugMsg("attribute required by renderer not found: "+QString::number(mAttrNum));
QgsDebugMsg("attribute required by renderer not found: "+mAttrName+"(index "+QString::number(mAttrNum)+")");
return NULL;
}

@@ -757,8 +770,11 @@ QgsSymbolV2* QgsGraduatedSymbolRendererV2::symbolForFeature(QgsFeature& feature)

}

void QgsGraduatedSymbolRendererV2::startRender(QgsRenderContext& context)
void QgsGraduatedSymbolRendererV2::startRender(QgsRenderContext& context, const QgsFieldMap& fields)
{
// find out classification attribute index from name
mAttrNum = fieldNameIndex(fields, mAttrName);

QgsRangeList::iterator it = mRanges.begin();
for ( ; it != mRanges.end(); ++it)
it->symbol()->startRender(context);
@@ -771,10 +787,10 @@ void QgsGraduatedSymbolRendererV2::stopRender(QgsRenderContext& context)
it->symbol()->startRender(context);
}

QList<int> QgsGraduatedSymbolRendererV2::usedAttributes()
QList<QString> QgsGraduatedSymbolRendererV2::usedAttributes()
{
QList<int> lst;
lst.append(mAttrNum);
QList<QString> lst;
lst.append( mAttrName );
return lst;
}

@@ -796,15 +812,15 @@ bool QgsGraduatedSymbolRendererV2::updateRangeLabel(int rangeIndex, QString labe

QString QgsGraduatedSymbolRendererV2::dump()
{
QString s = QString("GRADUATED: idx %1\n").arg(mAttrNum);
QString s = QString("GRADUATED: attr %1\n").arg(mAttrName);
for (int i=0; i<mRanges.count();i++)
s += mRanges[i].dump();
return s;
}

QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::clone()
{
QgsGraduatedSymbolRendererV2* r = new QgsGraduatedSymbolRendererV2( mAttrNum, mRanges );
QgsGraduatedSymbolRendererV2* r = new QgsGraduatedSymbolRendererV2( mAttrName, mRanges );
r->setUsingSymbolLevels( usingSymbolLevels() );
return r;
}
@@ -870,16 +886,19 @@ static QList<double> _calcQuantileBreaks(QList<double> values, int classes)

QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
QgsVectorLayer* vlayer,
int attrNum,
QString attrName,
int classes,
Mode mode,
QgsSymbolV2* symbol,
QgsVectorColorRampV2* ramp)
{
QgsVectorDataProvider* provider = vlayer->dataProvider();

int attrNum = fieldNameIndex(vlayer->pendingFields(), attrName);

double minimum = provider->minimumValue( attrNum ).toDouble();
double maximum = provider->maximumValue( attrNum ).toDouble();
QgsDebugMsg(QString("min %1 // max %2").arg(minimum).arg(maximum));

QList<double> breaks;
if (mode == EqualInterval)
@@ -922,7 +941,7 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
ranges.append( QgsRendererRangeV2(lower, upper, newSymbol, label) );
}

return new QgsGraduatedSymbolRendererV2( attrNum, ranges );
return new QgsGraduatedSymbolRendererV2( attrName, ranges );
}


@@ -958,9 +977,9 @@ QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::create(QDomElement& element)
rangeElem = rangeElem.nextSiblingElement();
}

int attrNum = element.attribute("attr").toInt();
QString attrName = element.attribute("attr");

QgsGraduatedSymbolRendererV2* r = new QgsGraduatedSymbolRendererV2(attrNum, ranges);
QgsGraduatedSymbolRendererV2* r = new QgsGraduatedSymbolRendererV2(attrName, ranges);

// delete symbols if there are any more
QgsSymbolLayerV2Utils::clearSymbolMap(symbolMap);
@@ -973,7 +992,7 @@ QDomElement QgsGraduatedSymbolRendererV2::save(QDomDocument& doc)
{
QDomElement rendererElem = doc.createElement(RENDERER_TAG_NAME);
rendererElem.setAttribute("type", "graduatedSymbol");
rendererElem.setAttribute("attr", mAttrNum);
rendererElem.setAttribute("attr", mAttrName);

// ranges
int i = 0;

0 comments on commit ef9190d

Please sign in to comment.
You can’t perform that action at this time.