Skip to content

Commit a9b6758

Browse files
author
mhugent
committed
Don't draw labels of features for which the value is not contained in the unique value renderer
git-svn-id: http://svn.osgeo.org/qgis/trunk@6505 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 0a98df6 commit a9b6758

File tree

4 files changed

+78
-49
lines changed

4 files changed

+78
-49
lines changed

src/core/qgsvectorlayer.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ void QgsVectorLayer::drawLabels(QPainter * p, QgsRect & viewExtent, QgsMapToPixe
261261
{
262262
QgsDebugMsg("Starting draw of labels");
263263

264-
if ( /*1 == 1 */ mRenderer && mLabelOn)
264+
if (mRenderer && mLabelOn)
265265
{
266266
QgsAttributeList attributes = mRenderer->classificationAttributes();
267267

@@ -286,19 +286,24 @@ void QgsVectorLayer::drawLabels(QPainter * p, QgsRect & viewExtent, QgsMapToPixe
286286
// don't render labels of deleted features
287287
if (!mDeletedFeatureIds.contains(fet.featureId()))
288288
{
289-
bool sel = mSelectedFeatureIds.contains(fet.featureId());
290-
mLabel->renderLabel ( p, viewExtent, ct,
289+
if(mRenderer->willRenderFeature(&fet))
290+
{
291+
bool sel = mSelectedFeatureIds.contains(fet.featureId());
292+
mLabel->renderLabel ( p, viewExtent, ct,
291293
theMapToPixelTransform, fet, sel, 0, scale);
294+
}
292295
}
293296
featureCount++;
294297
}
295298

296299
// render labels of not-commited features
297300
for (QgsFeatureList::iterator it = mAddedFeatures.begin(); it != mAddedFeatures.end(); ++it)
298301
{
299-
bool sel = mSelectedFeatureIds.contains((*it).featureId());
300-
mLabel->renderLabel ( p, viewExtent, ct,
301-
theMapToPixelTransform, *it, sel, 0, scale);
302+
if(mRenderer->willRenderFeature(&(*it)))
303+
{
304+
bool sel = mSelectedFeatureIds.contains((*it).featureId());
305+
mLabel->renderLabel ( p, viewExtent, ct, theMapToPixelTransform, *it, sel, 0, scale);
306+
}
302307
}
303308
}
304309
catch (QgsCsException &e)

src/core/renderer/qgsrenderer.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ class CORE_EXPORT QgsRenderer
4444
QgsRenderer();
4545
/** Virtual destructor because we have virtual methods... */
4646
virtual ~QgsRenderer();
47+
/** Determines if a feature will be rendered or not
48+
@param f a pointer to the feature to determine if rendering will happen*/
49+
virtual bool willRenderFeature(QgsFeature *f) {return true;}
4750
/**A vector layer passes features to a renderer object to change the brush and pen of the qpainter
4851
@param p the painter storing brush and pen
4952
@param f a pointer to the feature to be rendered
50-
@param pic pointer to a marker from SVG (is only used by marker renderers)
53+
@param pic pointer to an image (used for point symbols)
5154
@param scalefactor pointer to the scale factor for the marker image*/
5255
virtual void renderFeature(QPainter* p, QgsFeature& f,QImage* pic, double* scalefactor, bool selected, double widthScale = 1.)=0;
5356
/**Reads the renderer configuration from an XML file

src/core/renderer/qgsuniquevaluerenderer.cpp

+58-42
Original file line numberDiff line numberDiff line change
@@ -93,62 +93,78 @@ int QgsUniqueValueRenderer::classificationField()
9393
{
9494
return mClassificationField;
9595
}
96+
97+
bool QgsUniqueValueRenderer::willRenderFeature(QgsFeature *f)
98+
{
99+
return (symbolForFeature(f) != 0);
100+
}
96101

97102
void QgsUniqueValueRenderer::renderFeature(QPainter* p, QgsFeature& f,QImage* img,
98103
double* scalefactor, bool selected, double widthScale)
99104
{
100-
const QgsAttributeMap& attrs = f.attributeMap();
101-
QString value = attrs[mClassificationField].fieldValue();
105+
QgsSymbol* symbol = symbolForFeature(&f);
106+
if(!symbol) //no matching symbol
107+
{
108+
if ( img && mVectorType == QGis::Point )
109+
{
110+
img->fill(0);
111+
}
112+
else if ( mVectorType != QGis::Point )
113+
{
114+
p->setPen(Qt::NoPen);
115+
p->setBrush(Qt::NoBrush);
116+
}
117+
return;
118+
}
102119

103-
std::map<QString,QgsSymbol*>::iterator it=mSymbols.find(value);
104-
if(it!=mSymbols.end())
120+
// Point
121+
if ( img && mVectorType == QGis::Point )
105122
{
106-
QgsSymbol* symbol = it->second;
107-
108-
// Point
109-
if ( img && mVectorType == QGis::Point ) {
110-
*img = symbol->getPointSymbolAsImage( widthScale,
111-
selected, mSelectionColor );
112-
113-
if ( scalefactor ) *scalefactor = 1;
114-
}
115-
116-
// Line, polygon
117-
else if ( mVectorType != QGis::Point )
123+
*img = symbol->getPointSymbolAsImage( widthScale, selected, mSelectionColor );
124+
if ( scalefactor )
118125
{
119-
if( !selected )
120-
{
121-
QPen pen=symbol->pen();
122-
pen.setWidthF ( widthScale * pen.width() );
123-
p->setPen(pen);
124-
p->setBrush(symbol->brush());
125-
}
126-
else
127-
{
128-
QPen pen=symbol->pen();
129-
pen.setWidthF ( widthScale * pen.width() );
130-
pen.setColor(mSelectionColor);
131-
QBrush brush=symbol->brush();
126+
*scalefactor = 1;
127+
}
128+
}
129+
130+
// Line, polygon
131+
else if ( mVectorType != QGis::Point )
132+
{
133+
if( !selected )
134+
{
135+
QPen pen=symbol->pen();
136+
pen.setWidthF ( widthScale * pen.width() );
137+
p->setPen(pen);
138+
p->setBrush(symbol->brush());
139+
}
140+
else
141+
{
142+
QPen pen=symbol->pen();
143+
pen.setWidthF ( widthScale * pen.width() );
144+
pen.setColor(mSelectionColor);
145+
QBrush brush=symbol->brush();
132146
brush.setColor(mSelectionColor);
133147
p->setPen(pen);
134148
p->setBrush(brush);
135-
}
136149
}
137150
}
138-
else
151+
}
152+
153+
QgsSymbol* QgsUniqueValueRenderer::symbolForFeature(const QgsFeature* f)
154+
{
155+
//first find out the value
156+
const QgsAttributeMap& attrs = f->attributeMap();
157+
QString value = attrs[mClassificationField].fieldValue();
158+
159+
std::map<QString,QgsSymbol*>::iterator it=mSymbols.find(value);
160+
if(it == mSymbols.end())
139161
{
140-
//no matching symbol found. In this case, set Qt::NoPen, Qt::NoBrush or transparent image
141-
if ( img && mVectorType == QGis::Point )
142-
{
143-
img->fill(0);
144-
}
145-
else if ( mVectorType != QGis::Point )
146-
{
147-
p->setPen(Qt::NoPen);
148-
p->setBrush(Qt::NoBrush);
149-
}
162+
return 0;
163+
}
164+
else
165+
{
166+
return it->second;
150167
}
151-
152168
}
153169

154170
void QgsUniqueValueRenderer::readXML(const QDomNode& rnode, QgsVectorLayer& vl)

src/core/renderer/qgsuniquevaluerenderer.h

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class CORE_EXPORT QgsUniqueValueRenderer: public QgsRenderer
2828
QgsUniqueValueRenderer(const QgsUniqueValueRenderer& other);
2929
QgsUniqueValueRenderer& operator=(const QgsUniqueValueRenderer& other);
3030
virtual ~QgsUniqueValueRenderer();
31+
/** Determines if a feature will be rendered or not
32+
@param f a pointer to the feature to determine if rendering will happen*/
33+
bool willRenderFeature(QgsFeature *f);
3134
void renderFeature(QPainter* p, QgsFeature& f,QImage* img, double* scalefactor, bool selected, double widthScale = 1.);
3235
/**Reads the renderer configuration from an XML file
3336
@param rnode the DOM node to read
@@ -58,6 +61,8 @@ class CORE_EXPORT QgsUniqueValueRenderer: public QgsRenderer
5861
int mClassificationField;
5962
/**Symbols for the unique values*/
6063
std::map<QString, QgsSymbol*> mSymbols;
64+
/**Returns the symbol for a feature or 0 if there isn't any*/
65+
QgsSymbol* symbolForFeature(const QgsFeature* f);
6166
};
6267

6368
inline bool QgsUniqueValueRenderer::needsAttributes() const

0 commit comments

Comments
 (0)