Skip to content
Permalink
Browse files

Features for labeling are now extracted in the main drawing loop (usi…

…ng a pair of hooks).

This avoids one more iteration through the layer.


git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@10906 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Jun 12, 2009
1 parent 96a9cba commit e8a95a504423b56b079ea29fd1fe246e58b47141
@@ -105,7 +105,9 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
mLabelOn( false ),
mFetching( false ),
mRendererV2( NULL ),
mUsingRendererV2( false )
mUsingRendererV2( false ),
mLabelingPrepareLayerHook( NULL ),
mLabelingRegisterFeatureHook( NULL )
{
mActions = new QgsAttributeAction;

@@ -696,11 +698,29 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )

QgsFeature fet;
QgsAttributeList attributes = mRendererV2->usedAttributes();

bool labeling = FALSE;
if (mLabelingPrepareLayerHook)
{
int attrIndex;
if (mLabelingPrepareLayerHook(mLabelingContext, mLabelingLayerContext, attrIndex))
{
if (!attributes.contains(attrIndex))
attributes << attrIndex;
labeling = TRUE;
}
}

select( attributes, rendererContext.extent() );

while ( nextFeature( fet ) )
{
mRendererV2->renderFeature(fet, rendererContext);

if (labeling && mLabelingRegisterFeatureHook)
{
mLabelingRegisterFeatureHook(fet, mLabelingLayerContext);
}
}

mRendererV2->stopRender(rendererContext);
@@ -739,6 +759,19 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
int featureCount = 0;
QgsFeature fet;
QgsAttributeList attributes = mRenderer->classificationAttributes();

bool labeling = FALSE;
if (mLabelingPrepareLayerHook)
{
int attrIndex;
if (mLabelingPrepareLayerHook(mLabelingContext, mLabelingLayerContext, attrIndex))
{
if (!attributes.contains(attrIndex))
attributes << attrIndex;
labeling = TRUE;
}
}

select( attributes, rendererContext.extent() );

try
@@ -801,6 +834,11 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
rendererContext.rasterScaleFactor(),
rendererContext.drawEditingInformation() );

if (labeling && mLabelingRegisterFeatureHook)
{
mLabelingRegisterFeatureHook(fet, mLabelingLayerContext);
}

++featureCount;
}
}
@@ -2087,6 +2125,18 @@ bool QgsVectorLayer::hasLabelsEnabled( void ) const
return mLabelOn;
}

void QgsVectorLayer::setLabelingHooks(LabelingPrepareLayerHook prepareLayerHook,
LabelingRegisterFeatureHook registerFeatureHook,
void* context,
void* layerContext)
{
mLabelingPrepareLayerHook = prepareLayerHook;
mLabelingRegisterFeatureHook = registerFeatureHook;
mLabelingContext = context;
mLabelingLayerContext = layerContext;
}


bool QgsVectorLayer::startEditing()
{
if ( !mDataProvider )
@@ -54,6 +54,10 @@ typedef QList<int> QgsAttributeList;
typedef QSet<int> QgsFeatureIds;
typedef QSet<int> QgsAttributeIds;

typedef int (*LabelingPrepareLayerHook)(void*, void*, int&);
typedef void (*LabelingRegisterFeatureHook)(QgsFeature&, void*);


/** \ingroup core
* Vector layer backed by a data source provider.
*/
@@ -340,6 +344,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Label is on */
bool hasLabelsEnabled( void ) const;

void setLabelingHooks(LabelingPrepareLayerHook prepareLayerHook,
LabelingRegisterFeatureHook registerFeatureHook,
void* mLabelingContext,
void* mLabelingLayerContext);

/** Returns true if the provider is in editing mode */
virtual bool isEditable() const;

@@ -667,6 +676,12 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Label */
QgsLabel *mLabel;

LabelingPrepareLayerHook mLabelingPrepareLayerHook;
LabelingRegisterFeatureHook mLabelingRegisterFeatureHook;
void* mLabelingContext;
void* mLabelingLayerContext;


/** Display labels */
bool mLabelOn;

@@ -89,17 +89,6 @@ void Labeling::initGui()

void Labeling::doLabeling( QPainter * painter )
{
int w = painter->device()->width();
int h = painter->device()->height();


QgsMapLayer* layer = mQGisIface->activeLayer();
if (layer == NULL || layer->type() != QgsMapLayer::VectorLayer)
{
painter->drawLine(0,0,w,h);
return;
}

mLBL->doLabeling(painter);
}

@@ -42,7 +42,7 @@ LabelingGui::LabelingGui( PalLabeling* lbl, QString layerId, QWidget* parent )
populatePlacementMethods();
populateFieldNames();

PalLabeling::LayerSettings lyr = lbl->layer(layerId);
LayerSettings lyr = lbl->layer(layerId);
if (!lyr.layerId.isEmpty())
{
// load the labeling settings
@@ -74,12 +74,12 @@ QgsVectorLayer* LabelingGui::layer()
return static_cast<QgsVectorLayer*>(layer);
}

PalLabeling::LayerSettings LabelingGui::layerSettings()
LayerSettings LabelingGui::layerSettings()
{
PalLabeling::LayerSettings lyr;
LayerSettings lyr;
lyr.layerId = mLayerId;
lyr.fieldName = cboFieldName->currentText();
lyr.placement = (PalLabeling::Placement) cboPlacement->itemData(cboPlacement->currentIndex()).toInt();
lyr.placement = (LayerSettings::Placement) cboPlacement->itemData(cboPlacement->currentIndex()).toInt();
lyr.textColor = btnTextColor->color();
lyr.textFont = lblFontPreview->font();
lyr.enabled = chkEnableLabeling->isChecked();
@@ -94,18 +94,18 @@ void LabelingGui::populatePlacementMethods()
switch (layer()->geometryType())
{
case QGis::Point:
cboPlacement->addItem(tr("Around the point"), QVariant(PalLabeling::AroundPoint));
cboPlacement->addItem(tr("Around the point"), QVariant(LayerSettings::AroundPoint));
break;
case QGis::Line:
cboPlacement->addItem(tr("On the line"), QVariant(PalLabeling::OnLine));
cboPlacement->addItem(tr("Around the line"), QVariant(PalLabeling::AroundLine));
cboPlacement->addItem(tr("On the line"), QVariant(LayerSettings::OnLine));
cboPlacement->addItem(tr("Around the line"), QVariant(LayerSettings::AroundLine));
break;
case QGis::Polygon:
cboPlacement->addItem(tr("Horizontal"), QVariant(PalLabeling::Horizontal));
cboPlacement->addItem(tr("Free"), QVariant(PalLabeling::Free));
cboPlacement->addItem(tr("Around the centroid"), QVariant(PalLabeling::AroundPoint));
cboPlacement->addItem(tr("On the perimeter"), QVariant(PalLabeling::OnLine));
cboPlacement->addItem(tr("Around the perimeter"), QVariant(PalLabeling::AroundLine));
cboPlacement->addItem(tr("Horizontal"), QVariant(LayerSettings::Horizontal));
cboPlacement->addItem(tr("Free"), QVariant(LayerSettings::Free));
cboPlacement->addItem(tr("Around the centroid"), QVariant(LayerSettings::AroundPoint));
cboPlacement->addItem(tr("On the perimeter"), QVariant(LayerSettings::OnLine));
cboPlacement->addItem(tr("Around the perimeter"), QVariant(LayerSettings::AroundLine));
break;
}
}
@@ -35,7 +35,7 @@ class LabelingGui : public QDialog, private Ui::LabelingGuiBase
LabelingGui( PalLabeling* lbl, QString layerId, QWidget* parent );
~LabelingGui();

PalLabeling::LayerSettings layerSettings();
LayerSettings layerSettings();

public slots:
void changeTextColor();

0 comments on commit e8a95a5

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