Skip to content

Commit

Permalink
Don't draw labels of features for which the value is not contained in…
Browse files Browse the repository at this point in the history
… the unique value renderer

git-svn-id: http://svn.osgeo.org/qgis/trunk@6505 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Feb 3, 2007
1 parent 0a98df6 commit a9b6758
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 49 deletions.
17 changes: 11 additions & 6 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -261,7 +261,7 @@ void QgsVectorLayer::drawLabels(QPainter * p, QgsRect & viewExtent, QgsMapToPixe
{
QgsDebugMsg("Starting draw of labels");

if ( /*1 == 1 */ mRenderer && mLabelOn)
if (mRenderer && mLabelOn)
{
QgsAttributeList attributes = mRenderer->classificationAttributes();

Expand All @@ -286,19 +286,24 @@ void QgsVectorLayer::drawLabels(QPainter * p, QgsRect & viewExtent, QgsMapToPixe
// don't render labels of deleted features
if (!mDeletedFeatureIds.contains(fet.featureId()))
{
bool sel = mSelectedFeatureIds.contains(fet.featureId());
mLabel->renderLabel ( p, viewExtent, ct,
if(mRenderer->willRenderFeature(&fet))
{
bool sel = mSelectedFeatureIds.contains(fet.featureId());
mLabel->renderLabel ( p, viewExtent, ct,
theMapToPixelTransform, fet, sel, 0, scale);
}
}
featureCount++;
}

// render labels of not-commited features
for (QgsFeatureList::iterator it = mAddedFeatures.begin(); it != mAddedFeatures.end(); ++it)
{
bool sel = mSelectedFeatureIds.contains((*it).featureId());
mLabel->renderLabel ( p, viewExtent, ct,
theMapToPixelTransform, *it, sel, 0, scale);
if(mRenderer->willRenderFeature(&(*it)))
{
bool sel = mSelectedFeatureIds.contains((*it).featureId());
mLabel->renderLabel ( p, viewExtent, ct, theMapToPixelTransform, *it, sel, 0, scale);
}
}
}
catch (QgsCsException &e)
Expand Down
5 changes: 4 additions & 1 deletion src/core/renderer/qgsrenderer.h
Expand Up @@ -44,10 +44,13 @@ class CORE_EXPORT QgsRenderer
QgsRenderer();
/** Virtual destructor because we have virtual methods... */
virtual ~QgsRenderer();
/** Determines if a feature will be rendered or not
@param f a pointer to the feature to determine if rendering will happen*/
virtual bool willRenderFeature(QgsFeature *f) {return true;}
/**A vector layer passes features to a renderer object to change the brush and pen of the qpainter
@param p the painter storing brush and pen
@param f a pointer to the feature to be rendered
@param pic pointer to a marker from SVG (is only used by marker renderers)
@param pic pointer to an image (used for point symbols)
@param scalefactor pointer to the scale factor for the marker image*/
virtual void renderFeature(QPainter* p, QgsFeature& f,QImage* pic, double* scalefactor, bool selected, double widthScale = 1.)=0;
/**Reads the renderer configuration from an XML file
Expand Down
100 changes: 58 additions & 42 deletions src/core/renderer/qgsuniquevaluerenderer.cpp
Expand Up @@ -93,62 +93,78 @@ int QgsUniqueValueRenderer::classificationField()
{
return mClassificationField;
}

bool QgsUniqueValueRenderer::willRenderFeature(QgsFeature *f)
{
return (symbolForFeature(f) != 0);
}

void QgsUniqueValueRenderer::renderFeature(QPainter* p, QgsFeature& f,QImage* img,
double* scalefactor, bool selected, double widthScale)
{
const QgsAttributeMap& attrs = f.attributeMap();
QString value = attrs[mClassificationField].fieldValue();
QgsSymbol* symbol = symbolForFeature(&f);
if(!symbol) //no matching symbol
{
if ( img && mVectorType == QGis::Point )
{
img->fill(0);
}
else if ( mVectorType != QGis::Point )
{
p->setPen(Qt::NoPen);
p->setBrush(Qt::NoBrush);
}
return;
}

std::map<QString,QgsSymbol*>::iterator it=mSymbols.find(value);
if(it!=mSymbols.end())
// Point
if ( img && mVectorType == QGis::Point )
{
QgsSymbol* symbol = it->second;

// Point
if ( img && mVectorType == QGis::Point ) {
*img = symbol->getPointSymbolAsImage( widthScale,
selected, mSelectionColor );

if ( scalefactor ) *scalefactor = 1;
}

// Line, polygon
else if ( mVectorType != QGis::Point )
*img = symbol->getPointSymbolAsImage( widthScale, selected, mSelectionColor );
if ( scalefactor )
{
if( !selected )
{
QPen pen=symbol->pen();
pen.setWidthF ( widthScale * pen.width() );
p->setPen(pen);
p->setBrush(symbol->brush());
}
else
{
QPen pen=symbol->pen();
pen.setWidthF ( widthScale * pen.width() );
pen.setColor(mSelectionColor);
QBrush brush=symbol->brush();
*scalefactor = 1;
}
}

// Line, polygon
else if ( mVectorType != QGis::Point )
{
if( !selected )
{
QPen pen=symbol->pen();
pen.setWidthF ( widthScale * pen.width() );
p->setPen(pen);
p->setBrush(symbol->brush());
}
else
{
QPen pen=symbol->pen();
pen.setWidthF ( widthScale * pen.width() );
pen.setColor(mSelectionColor);
QBrush brush=symbol->brush();
brush.setColor(mSelectionColor);
p->setPen(pen);
p->setBrush(brush);
}
}
}
else
}

QgsSymbol* QgsUniqueValueRenderer::symbolForFeature(const QgsFeature* f)
{
//first find out the value
const QgsAttributeMap& attrs = f->attributeMap();
QString value = attrs[mClassificationField].fieldValue();

std::map<QString,QgsSymbol*>::iterator it=mSymbols.find(value);
if(it == mSymbols.end())
{
//no matching symbol found. In this case, set Qt::NoPen, Qt::NoBrush or transparent image
if ( img && mVectorType == QGis::Point )
{
img->fill(0);
}
else if ( mVectorType != QGis::Point )
{
p->setPen(Qt::NoPen);
p->setBrush(Qt::NoBrush);
}
return 0;
}
else
{
return it->second;
}

}

void QgsUniqueValueRenderer::readXML(const QDomNode& rnode, QgsVectorLayer& vl)
Expand Down
5 changes: 5 additions & 0 deletions src/core/renderer/qgsuniquevaluerenderer.h
Expand Up @@ -28,6 +28,9 @@ class CORE_EXPORT QgsUniqueValueRenderer: public QgsRenderer
QgsUniqueValueRenderer(const QgsUniqueValueRenderer& other);
QgsUniqueValueRenderer& operator=(const QgsUniqueValueRenderer& other);
virtual ~QgsUniqueValueRenderer();
/** Determines if a feature will be rendered or not
@param f a pointer to the feature to determine if rendering will happen*/
bool willRenderFeature(QgsFeature *f);
void renderFeature(QPainter* p, QgsFeature& f,QImage* img, double* scalefactor, bool selected, double widthScale = 1.);
/**Reads the renderer configuration from an XML file
@param rnode the DOM node to read
Expand Down Expand Up @@ -58,6 +61,8 @@ class CORE_EXPORT QgsUniqueValueRenderer: public QgsRenderer
int mClassificationField;
/**Symbols for the unique values*/
std::map<QString, QgsSymbol*> mSymbols;
/**Returns the symbol for a feature or 0 if there isn't any*/
QgsSymbol* symbolForFeature(const QgsFeature* f);
};

inline bool QgsUniqueValueRenderer::needsAttributes() const
Expand Down

0 comments on commit a9b6758

Please sign in to comment.