Skip to content
Permalink
Browse files

Labeling hooks replaced with labeling engine interface (nicer solution).

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
wonder committed Aug 8, 2009
1 parent 1aa0f67 commit 175e70a27156be24e13d92d797bad952ba34d5d5
@@ -111,8 +111,7 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
mActiveCommand( NULL ),
mRendererV2( NULL ),
mUsingRendererV2( false ),
mLabelingPrepareLayerHook( NULL ),
mLabelingRegisterFeatureHook( NULL )
mLabelingEngine( NULL )
{
mActions = new QgsAttributeAction;

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


@@ -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
@@ -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;
@@ -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 */
@@ -23,6 +23,7 @@
#include <qgisgui.h>
#include <qgsmapcanvas.h>
#include <qgsvectorlayer.h>
#include <qgsmaplayerregistry.h>

#include "labeling.h"
#include "labelinggui.h"
@@ -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 )
@@ -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();
@@ -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 );
@@ -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);
}


//////////////////////////////////////////////////////////////////////////
//
@@ -24,6 +24,8 @@
//QGIS includes
#include "../qgisplugin.h"

#include "qgsmaplayer.h" // for MOC

//forward declarations
class QAction;
class QPainter;
@@ -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

0 comments on commit 175e70a

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