Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 committed Jun 12, 2009
1 parent 96a9cba commit e8a95a5
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 119 deletions.
52 changes: 51 additions & 1 deletion src/core/qgsvectorlayer.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -801,6 +834,11 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
rendererContext.rasterScaleFactor(),
rendererContext.drawEditingInformation() );

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

++featureCount;
}
}
Expand Down Expand Up @@ -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 )
Expand Down
15 changes: 15 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
11 changes: 0 additions & 11 deletions src/plugins/labeling/labeling.cpp
Expand Up @@ -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);
}

Expand Down
24 changes: 12 additions & 12 deletions src/plugins/labeling/labelinggui.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/labeling/labelinggui.h
Expand Up @@ -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();
Expand Down

0 comments on commit e8a95a5

Please sign in to comment.