Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replaced QgsPalGeometry with QgsTextLabelFeature and QgsDiagramLabelF…
…eature

This code has been funded by Tuscany Region (Italy) - SITA (CIG: 63526840AE) and commissioned to Gis3W s.a.s.
  • Loading branch information
wonder-sk committed Sep 21, 2015
1 parent 59037eb commit 26c1e0f
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 190 deletions.
6 changes: 3 additions & 3 deletions src/core/dxf/qgsdxfpallabeling.cpp
Expand Up @@ -38,14 +38,14 @@ void QgsDxfLabelProvider::drawLabel( QgsRenderContext& context, pal::LabelPositi
//debug: print label infos
if ( mDxfExport )
{
QgsPalGeometry *g = dynamic_cast< QgsPalGeometry* >( label->getFeaturePart()->getUserGeometry() );
if ( !g )
QgsTextLabelFeature* lf = dynamic_cast<QgsTextLabelFeature*>( label->getFeaturePart()->userFeature() );
if ( !lf )
return;

const QgsPalLayerSettings& tmpLyr = mSettings;

//label text
QString txt = g->text( label->getPartId() );
QString txt = lf->text( label->getPartId() );

//angle
double angle = label->getAlpha() * 180 / M_PI;
Expand Down
4 changes: 2 additions & 2 deletions src/core/pal/feature.cpp
Expand Up @@ -56,9 +56,9 @@

namespace pal
{
Feature::Feature( Layer* l, QgsFeatureId fid, PalGeometry* userG, double lx, double ly )
Feature::Feature( Layer* l, QgsFeatureId fid, QgsLabelFeature* userFeat, double lx, double ly )
: layer( l )
, userGeom( userG )
, userFeature( userFeat )
, label_x( lx )
, label_y( ly )
, distlabel( 0 )
Expand Down
10 changes: 5 additions & 5 deletions src/core/pal/feature.h
Expand Up @@ -31,7 +31,6 @@
#define _FEATURE_H

#include "qgsgeometry.h"
#include "palgeometry.h"
#include "pointset.h"
#include "util.h"
#include "labelposition.h"
Expand All @@ -40,6 +39,8 @@
#include <cmath>
#include <QString>

class QgsLabelFeature;

namespace pal
{
/** Optional additional info about label (for curved labels) */
Expand All @@ -48,7 +49,6 @@ namespace pal
public:
typedef struct
{
unsigned short chr;
double width;
} CharacterInfo;

Expand Down Expand Up @@ -78,7 +78,7 @@ namespace pal
friend class FeaturePart;

public:
Feature( Layer* l, QgsFeatureId fid, PalGeometry* userG, double lx, double ly );
Feature( Layer* l, QgsFeatureId fid, QgsLabelFeature* userFeat, double lx, double ly );
~Feature();

void setLabelInfo( LabelInfo* info ) { labelInfo = info; }
Expand Down Expand Up @@ -161,7 +161,7 @@ namespace pal

protected:
Layer *layer;
PalGeometry *userGeom;
QgsLabelFeature* userFeature;
double label_x;
double label_y;
double distlabel;
Expand Down Expand Up @@ -291,7 +291,7 @@ namespace pal
void print();
#endif

PalGeometry* getUserGeometry() { return mFeature->userGeom; }
QgsLabelFeature* userFeature() { return mFeature->userFeature; }

void setLabelSize( double lx, double ly ) { mFeature->label_x = lx; mFeature->label_y = ly; }
double getLabelWidth() const { return mFeature->label_x; }
Expand Down
17 changes: 6 additions & 11 deletions src/core/pal/layer.cpp
Expand Up @@ -41,7 +41,6 @@
#include <vector>

#include "qgslabelingenginev2.h"
#include "qgspalgeometry.h"

namespace pal
{
Expand Down Expand Up @@ -110,7 +109,7 @@ namespace pal
mDefaultPriority = priority;
}

bool Layer::registerFeature( QgsFeatureId fid, PalGeometry *userGeom, double label_x, double label_y, const QString &labelText,
bool Layer::registerFeature( QgsLabelFeature* label, QgsFeatureId fid, const GEOSGeometry* userGeom, double label_x, double label_y, const QString &labelText,
double labelPosX, double labelPosY, bool fixedPos, double angle, bool fixedAngle,
int xQuadOffset, int yQuadOffset, double xOffset, double yOffset, bool alwaysShow, double repeatDistance )
{
Expand All @@ -128,9 +127,8 @@ namespace pal
}

// Split MULTI GEOM and Collection in simple geometries
const GEOSGeometry *the_geom = userGeom->getGeosGeometry();

Feature* f = new Feature( this, fid, userGeom, label_x, label_y );
Feature* f = new Feature( this, fid, label, label_x, label_y );
if ( fixedPos )
{
f->setFixedPosition( labelPosX, labelPosY );
Expand Down Expand Up @@ -162,7 +160,7 @@ namespace pal
FeaturePart* biggest_part = NULL;

// break the (possibly multi-part) geometry into simple geometries
QLinkedList<const GEOSGeometry*>* simpleGeometries = unmulti( the_geom );
QLinkedList<const GEOSGeometry*>* simpleGeometries = unmulti( userGeom );
if ( simpleGeometries == NULL ) // unmulti() failed?
{
delete f;
Expand Down Expand Up @@ -235,8 +233,6 @@ namespace pal
}
delete simpleGeometries;

userGeom->releaseGeosGeometry( the_geom );

mMutex.unlock();

// if using only biggest parts...
Expand All @@ -262,17 +258,16 @@ namespace pal

bool Layer::registerFeature( QgsLabelFeature* label )
{
QgsPalGeometry* g = label->geometry();
if ( !registerFeature( g->featureId(), g, label->size().width(), label->size().height(), label->labelText(),
if ( !registerFeature( label, label->id(), label->geometry(), label->size().width(), label->size().height(), label->labelText(),
label->fixedPosition().x(), label->fixedPosition().y(), label->hasFixedPosition(),
label->fixedAngle(), label->hasFixedAngle(),
label->quadOffset().x(), label->quadOffset().y(),
label->positionOffset().x(), label->positionOffset().y(),
label->alwaysShow(), label->repeatDistance() ) )
return false;

pal::Feature* pf = getFeature( g->featureId() );
pf->setLabelInfo( g->info() );
pal::Feature* pf = getFeature( label->id() );
pf->setLabelInfo( label->curvedLabelInfo() );
pf->setPriority( label->priority() );
pf->setDistLabel( label->distLabel() );
pf->setFixedQuadrant( label->hasFixedQuadrant() );
Expand Down
5 changes: 2 additions & 3 deletions src/core/pal/layer.h
Expand Up @@ -31,7 +31,6 @@
#define _LAYER_H_

#include "pal.h"
#include "palgeometry.h"
#include <QMutex>
#include <QLinkedList>
#include <QHash>
Expand Down Expand Up @@ -229,7 +228,7 @@ namespace pal

/** Register a feature in the layer.
* @param fid unique identifier
* @param userGeom user's geometry that implements the PalGeometry interface
* @param userGeom user's geometry (does not take ownership)
* @param label_x label width
* @param label_y label height
* @param labelText label text
Expand All @@ -249,7 +248,7 @@ namespace pal
*
* @return true on success (i.e. valid geometry)
*/
bool registerFeature( QgsFeatureId fid, PalGeometry *userGeom, double label_x = -1, double label_y = -1,
bool registerFeature( QgsLabelFeature* label, QgsFeatureId fid, const GEOSGeometry *userGeom, double label_x = -1, double label_y = -1,
const QString& labelText = QString(), double labelPosX = 0.0, double labelPosY = 0.0,
bool fixedPos = false, double angle = 0.0, bool fixedAngle = false,
int xQuadOffset = 0, int yQuadOffset = 0, double xOffset = 0.0, double yOffset = 0.0,
Expand Down
26 changes: 0 additions & 26 deletions src/core/pal/palgeometry.h
Expand Up @@ -35,32 +35,6 @@
namespace pal
{

/**
* \brief Interface that allows Pal to access user's geometries
*/
class CORE_EXPORT PalGeometry
{
public:

/**
* \brief get the GEOSGeometry of the feature
* This method is called by Pal each time it needs a geom's coordinates
*
* @return GEOSGeometry * a pointer the geos geom
*/
virtual const GEOSGeometry* getGeosGeometry() = 0;


/**
* \brief Called by Pal when it doesn't need the coordinates anymore
* @param the_geom is the geoms geom from PalGeometry::getfeomGeometry()
*/
virtual void releaseGeosGeometry( const GEOSGeometry *the_geom ) = 0;


virtual ~PalGeometry() {}
};

} // end namespace pal

#endif
2 changes: 0 additions & 2 deletions src/core/qgsdiagramrendererv2.h
Expand Up @@ -29,7 +29,6 @@ class QgsDiagramRendererV2;
class QgsFeature;
class QgsRenderContext;
class QDomElement;
class QgsPalGeometry;
class QgsCoordinateTransform;
class QgsMapToPixel;
class QgsVectorLayer;
Expand Down Expand Up @@ -75,7 +74,6 @@ class CORE_EXPORT QgsDiagramLayerSettings
//assigned when layer gets prepared
const QgsCoordinateTransform* ct;
const QgsMapToPixel* xform;
QList<QgsPalGeometry*> geometries;
QgsFields fields;

int xPosColumn; //attribute index for x coordinate (or -1 if position not data defined)
Expand Down
30 changes: 19 additions & 11 deletions src/core/qgslabelingenginev2.cpp
Expand Up @@ -16,7 +16,6 @@
#include "qgslabelingenginev2.h"

#include "qgslogger.h"
#include "qgspalgeometry.h"
#include "qgsproject.h"

#include "feature.h"
Expand Down Expand Up @@ -70,15 +69,15 @@ void QgsLabelingEngineV2::run( QgsRenderContext& context )
{
pal::Pal p;

SearchMethod s;
pal::SearchMethod s;
switch ( mSearchMethod )
{
default:
case QgsPalLabeling::Chain: s = CHAIN; break;
case QgsPalLabeling::Popmusic_Tabu: s = POPMUSIC_TABU; break;
case QgsPalLabeling::Popmusic_Chain: s = POPMUSIC_CHAIN; break;
case QgsPalLabeling::Popmusic_Tabu_Chain: s = POPMUSIC_TABU_CHAIN; break;
case QgsPalLabeling::Falp: s = FALP; break;
case QgsPalLabeling::Chain: s = pal::CHAIN; break;
case QgsPalLabeling::Popmusic_Tabu: s = pal::POPMUSIC_TABU; break;
case QgsPalLabeling::Popmusic_Chain: s = pal::POPMUSIC_CHAIN; break;
case QgsPalLabeling::Popmusic_Tabu_Chain: s = pal::POPMUSIC_TABU_CHAIN; break;
case QgsPalLabeling::Falp: s = pal::FALP; break;
}
p.setSearch( s );

Expand Down Expand Up @@ -117,7 +116,7 @@ void QgsLabelingEngineV2::run( QgsRenderContext& context )
flags.testFlag( QgsAbstractLabelProvider::DrawAllLabels ) );

// extra flags for placement of labels for linestrings
l->setArrangementFlags(( LineArrangementFlags ) provider->linePlacementFlags() );
l->setArrangementFlags(( pal::LineArrangementFlags ) provider->linePlacementFlags() );

// set label mode (label per feature is the default)
l->setLabelMode( flags.testFlag( QgsAbstractLabelProvider::LabelPerFeaturePart ) ? pal::Layer::LabelPerFeaturePart : pal::Layer::LabelPerFeature );
Expand Down Expand Up @@ -264,8 +263,8 @@ void QgsLabelingEngineV2::run( QgsRenderContext& context )
if ( context.renderingStopped() )
break;

QgsPalGeometry* palGeometry = dynamic_cast< QgsPalGeometry* >(( *it )->getFeaturePart()->getUserGeometry() );
if ( !palGeometry )
QgsLabelFeature* lf = ( *it )->getFeaturePart()->userFeature();
if ( !lf )
{
continue;
}
Expand Down Expand Up @@ -344,7 +343,7 @@ QgsAbstractLabelProvider* QgsLabelingEngineV2::providerById( const QString& id )



QgsLabelFeature::QgsLabelFeature( QgsFeatureId id, QgsPalGeometry* geometry, const QSizeF& size )
QgsLabelFeature::QgsLabelFeature( QgsFeatureId id, GEOSGeometry* geometry, const QSizeF& size )
: mId( id )
, mGeometry( geometry )
, mSize( size )
Expand All @@ -357,9 +356,18 @@ QgsLabelFeature::QgsLabelFeature( QgsFeatureId id, QgsPalGeometry* geometry, con
, mAlwaysShow( false )
, mIsObstacle( false )
, mObstacleFactor( 1 )
, mInfo( 0 )
{
}

QgsLabelFeature::~QgsLabelFeature()
{
if ( mGeometry )
GEOSGeom_destroy_r( QgsGeometry::getGEOSHandler(), mGeometry );

delete mInfo;
}

QgsAbstractLabelProvider::QgsAbstractLabelProvider()
: mEngine( 0 )
, mFlags( DrawLabels )
Expand Down
26 changes: 22 additions & 4 deletions src/core/qgslabelingenginev2.h
Expand Up @@ -16,6 +16,8 @@
#ifndef QGSLABELINGENGINEV2_H
#define QGSLABELINGENGINEV2_H

#include "qgsgeometry.h"

#include "qgsmapsettings.h"

#include "qgspallabeling.h"
Expand All @@ -25,6 +27,11 @@
class QgsRenderContext;
class QgsGeometry;

namespace pal
{
class LabelInfo;
}

/**
* @brief The QgsLabelFeature class describes a feature that
* should be used within the labeling engine. Those may be the usual textual labels,
Expand All @@ -34,18 +41,23 @@ class QgsGeometry;
* Instances only contain data relevant to the labeling engine (geometry, label size etc.)
* necessary for the layout. Rendering of labels is done by label providers.
*
* Individual label providers may create subclasses of QgsLabelFeature in order to add
* more data to the instances that will be later used for drawing of labels.
*
* @note added in QGIS 2.12
*/
class CORE_EXPORT QgsLabelFeature
{
public:
QgsLabelFeature( QgsFeatureId id, QgsPalGeometry* geometry, const QSizeF& size );
//! Create label feature, takes ownership of the geometry instance
QgsLabelFeature( QgsFeatureId id, GEOSGeometry* geometry, const QSizeF& size );
virtual ~QgsLabelFeature();

//! Identifier of the label (unique within the parent label provider)
QgsFeatureId id() const { return mId; }

//! Get access to the associated geometry
QgsPalGeometry* geometry() const { return mGeometry; }
GEOSGeometry* geometry() const { return mGeometry; }

//! Size of the label (in map units)
QSizeF size() const { return mSize; }
Expand Down Expand Up @@ -109,12 +121,17 @@ class CORE_EXPORT QgsLabelFeature
QString labelText() const { return mLabelText; }
void setLabelText( const QString& text ) { mLabelText = text; }

//! Get additional infor required for curved label placement. Returns null if not set
pal::LabelInfo* curvedLabelInfo() const { return mInfo; }
//! takes ownership of the instance
void setCurvedLabelInfo( pal::LabelInfo* info ) { mInfo = info; }

protected:

//! Associated ID unique within the parent label provider
QgsFeatureId mId;
//! Geometry wrapper for the feature
QgsPalGeometry* mGeometry;
//! Geometry of the feature to be labelled
GEOSGeometry* mGeometry;
//! Width and height of the label
QSizeF mSize;
double mPriority;
Expand All @@ -131,6 +148,7 @@ class CORE_EXPORT QgsLabelFeature
bool mIsObstacle;
double mObstacleFactor;
QString mLabelText;
pal::LabelInfo* mInfo;
};


Expand Down

0 comments on commit 26c1e0f

Please sign in to comment.