Skip to content

Commit

Permalink
Labeling hooks replaced with labeling engine interface (nicer solution).
Browse files Browse the repository at this point in the history
Added loading/saving of labeling into the project files (using layer's custom properties)


git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11291 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Aug 8, 2009
1 parent 1aa0f67 commit 175e70a
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 211 deletions.
29 changes: 11 additions & 18 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -111,8 +111,7 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
mActiveCommand( NULL ),
mRendererV2( NULL ),
mUsingRendererV2( false ),
mLabelingPrepareLayerHook( NULL ),
mLabelingRegisterFeatureHook( NULL )
mLabelingEngine( NULL )
{
mActions = new QgsAttributeAction;

Expand Down Expand Up @@ -687,7 +686,7 @@ void QgsVectorLayer::drawRendererV2( QgsRenderContext& rendererContext, bool lab
mRendererV2->renderFeature(fet, rendererContext);

if ( labeling )
mLabelingRegisterFeatureHook(fet, mLabelingLayerContext);
mLabelingEngine->registerFeature(this, fet);
}

mRendererV2->stopRender(rendererContext);
Expand All @@ -712,7 +711,7 @@ void QgsVectorLayer::drawRendererV2Levels( QgsRenderContext& rendererContext, bo
features[sym].append( fet );

if ( labeling )
mLabelingRegisterFeatureHook(fet, mLabelingLayerContext);
mLabelingEngine->registerFeature(this, fet);
}

// find out the order
Expand Down Expand Up @@ -773,10 +772,10 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
QgsDebugMsg("attrs: " + QString::number(attributes[0]));

bool labeling = FALSE;
if ( mLabelingPrepareLayerHook && mLabelingRegisterFeatureHook )
if ( mLabelingEngine )
{
int attrIndex;
if (mLabelingPrepareLayerHook(mLabelingContext, mLabelingLayerContext, attrIndex))
if (mLabelingEngine->prepareLayer(this, attrIndex))
{
if (!attributes.contains(attrIndex))
attributes << attrIndex;
Expand Down Expand Up @@ -831,10 +830,10 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
QgsAttributeList attributes = mRenderer->classificationAttributes();

bool labeling = FALSE;
if (mLabelingPrepareLayerHook)
if (mLabelingEngine)
{
int attrIndex;
if (mLabelingPrepareLayerHook(mLabelingContext, mLabelingLayerContext, attrIndex))
if (mLabelingEngine->prepareLayer(this, attrIndex))
{
if (!attributes.contains(attrIndex))
attributes << attrIndex;
Expand Down Expand Up @@ -911,9 +910,9 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
rendererContext.rasterScaleFactor(),
rendererContext.drawEditingInformation() );

if (labeling && mLabelingRegisterFeatureHook)
if (labeling && mLabelingEngine)
{
mLabelingRegisterFeatureHook(fet, mLabelingLayerContext);
mLabelingEngine->registerFeature(this, fet);
}

++featureCount;
Expand Down Expand Up @@ -2229,15 +2228,9 @@ bool QgsVectorLayer::hasLabelsEnabled( void ) const
return mLabelOn;
}

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


Expand Down
22 changes: 12 additions & 10 deletions src/core/qgsvectorlayer.h
Expand Up @@ -53,8 +53,16 @@ typedef QList<int> QgsAttributeList;
typedef QSet<int> QgsFeatureIds;
typedef QSet<int> QgsAttributeIds;

typedef int (*LabelingPrepareLayerHook)(void*, void*, int&);
typedef void (*LabelingRegisterFeatureHook)(QgsFeature&, void*);
class QgsLabelingEngineInterface
{
public:
virtual ~QgsLabelingEngineInterface() {}
virtual int prepareLayer(QgsVectorLayer* layer, int& attrIndex) = 0;
virtual void registerFeature(QgsVectorLayer* layer, QgsFeature& feat) = 0;
//void calculateLabeling() = 0;
//void drawLabeling(QgsRenderContext& context) = 0;
};



/** \ingroup core
Expand Down Expand Up @@ -348,10 +356,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Label is on */
bool hasLabelsEnabled( void ) const;

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

/** Returns true if the provider is in editing mode */
virtual bool isEditable() const;
Expand Down Expand Up @@ -733,10 +738,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Label */
QgsLabel *mLabel;

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


/** Display labels */
Expand Down
49 changes: 44 additions & 5 deletions src/plugins/labeling/labeling.cpp
Expand Up @@ -23,6 +23,7 @@
#include <qgisgui.h>
#include <qgsmapcanvas.h>
#include <qgsvectorlayer.h>
#include <qgsmaplayerregistry.h>

#include "labeling.h"
#include "labelinggui.h"
Expand Down Expand Up @@ -124,6 +125,20 @@ void Labeling::initGui()

connect( mQGisIface->mapCanvas(), SIGNAL( renderComplete( QPainter * ) ), this, SLOT( doLabeling( QPainter * ) ) );

// connect to newly added layers so the labeling hook will be set up
connect( QgsMapLayerRegistry::instance(), SIGNAL(layerWasAdded(QgsMapLayer*)), this, SLOT(layerWasAdded(QgsMapLayer*)) );

// add labeling hooks to all existing layers
QMap<QString, QgsMapLayer*>& layers = QgsMapLayerRegistry::instance()->mapLayers();
for (QMap<QString, QgsMapLayer*>::iterator it = layers.begin(); it != layers.end(); ++it)
{
QgsMapLayer* layer = it.value();
if (layer->type() == QgsMapLayer::VectorLayer)
{
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(layer);
vlayer->setLabelingEngine(mLBL);
}
}
}

void Labeling::doLabeling( QPainter * painter )
Expand All @@ -143,15 +158,14 @@ void Labeling::run()
QMessageBox::warning(mQGisIface->mainWindow(), "Labeling", "Please select a vector layer first.");
return;
}
//QgsVectorLayer* vlayer = static_cast<QgsVectorLayer*>(layer);
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(layer);

LabelingGui myPluginGui( mLBL, layer->getLayerID(), mQGisIface->mainWindow() );
LabelingGui myPluginGui( mLBL, vlayer, mQGisIface->mainWindow() );

if (myPluginGui.exec())
{
// alter labeling
mLBL->removeLayer(layer->getLayerID());
mLBL->addLayer( myPluginGui.layerSettings() );
// alter labeling - save the changes
myPluginGui.layerSettings().writeToLayer(vlayer);

// trigger refresh
mQGisIface->mapCanvas()->refresh();
Expand All @@ -170,6 +184,21 @@ void Labeling::unload()
mQGisIface->mapCanvas()->unsetMapTool(mTool);
delete mTool;

// remove labeling hook from all layers!
QMap<QString, QgsMapLayer*>& layers = QgsMapLayerRegistry::instance()->mapLayers();
for (QMap<QString, QgsMapLayer*>::iterator it = layers.begin(); it != layers.end(); ++it)
{
QgsMapLayer* layer = it.value();
if (layer->type() == QgsMapLayer::VectorLayer)
{
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(layer);
vlayer->setLabelingEngine(NULL);
}
}

disconnect( QgsMapLayerRegistry::instance(), SIGNAL(layerWasAdded(QgsMapLayer*)), this, SLOT(layerWasAdded(QgsMapLayer*)) );
disconnect( mQGisIface->mapCanvas(), SIGNAL( renderComplete( QPainter * ) ), this, SLOT( doLabeling( QPainter * ) ) );

// remove the GUI
mQGisIface->removePluginMenu( "&Labeling", mQActionPointer );
mQGisIface->removeToolBarIcon( mQActionPointer );
Expand All @@ -181,6 +210,16 @@ void Labeling::unload()
delete mLBL;
}

void Labeling::layerWasAdded( QgsMapLayer* layer )
{
if (layer->type() != QgsMapLayer::VectorLayer)
return; // not interested in rasters

QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(layer);
// add labeling hook for the newly added layer
vlayer->setLabelingEngine(mLBL);
}


//////////////////////////////////////////////////////////////////////////
//
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/labeling/labeling.h
Expand Up @@ -24,6 +24,8 @@
//QGIS includes
#include "../qgisplugin.h"

#include "qgsmaplayer.h" // for MOC

//forward declarations
class QAction;
class QPainter;
Expand Down Expand Up @@ -62,6 +64,8 @@ class Labeling: public QObject, public QgisPlugin
//! start labeling map tool
void setTool();

void layerWasAdded( QgsMapLayer* theMapLayer );

private:

//! Pointer to the QGIS interface object
Expand Down

0 comments on commit 175e70a

Please sign in to comment.