Skip to content
Permalink
Browse files
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/qgis@6505 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Feb 3, 2007
1 parent cd7a57e commit 709fc8bedfdec38c0ea6bd6c8fa32f896a151589
Showing with 78 additions and 49 deletions.
  1. +11 −6 src/core/qgsvectorlayer.cpp
  2. +4 −1 src/core/renderer/qgsrenderer.h
  3. +58 −42 src/core/renderer/qgsuniquevaluerenderer.cpp
  4. +5 −0 src/core/renderer/qgsuniquevaluerenderer.h
@@ -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();

@@ -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)
@@ -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
@@ -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)
@@ -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
@@ -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

0 comments on commit 709fc8b

Please sign in to comment.