Skip to content
Permalink
Browse files

One common class for labeling configurations (QgsAbstractVectorLayerL…

…abeling)

This is +/- equivalent of QgsFeatureRendererV2 for vector layer labels

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 24, 2015
1 parent 44ae0b8 commit 249c87822f38f5220f0eaeb2686df6cac91238a6
@@ -43,6 +43,7 @@
#include "qgsexpression.h"
#include "qgsdatadefined.h"
#include "qgslabelingenginev2.h"
#include "qgsvectorlayerlabeling.h"

#include <qgslogger.h>
#include <qgsvectorlayer.h>
@@ -3610,7 +3611,6 @@ void QgsPalLabeling::clearActiveLayer( const QString &layerID )
Q_UNUSED( layerID );
}

#include "qgsvectorlayerlabeling.h"

int QgsPalLabeling::prepareLayer( QgsVectorLayer* layer, QStringList& attrNames, QgsRenderContext& ctx )
{
@@ -3619,7 +3619,10 @@ int QgsPalLabeling::prepareLayer( QgsVectorLayer* layer, QStringList& attrNames,
return 0;
}

QgsVectorLayerLabelProvider* lp = layer->labeling().provider( layer );
if ( !layer->labeling() )
return 0;

QgsVectorLayerLabelProvider* lp = layer->labeling()->provider( layer );
if ( !lp )
return 0;

@@ -139,7 +139,7 @@ QgsRuleBasedLabeling::Rule*QgsRuleBasedLabeling::Rule::create( QDomElement& rule
return rule;
}

QDomElement QgsRuleBasedLabeling::Rule::save( QDomDocument& doc )
QDomElement QgsRuleBasedLabeling::Rule::save( QDomDocument& doc ) const
{
QDomElement ruleElem = doc.createElement( "rule" );

@@ -161,7 +161,7 @@ QDomElement QgsRuleBasedLabeling::Rule::save( QDomDocument& doc )
// ruleElem.setAttribute( "checkstate", 0 );
//ruleElem.setAttribute( "key", mRuleKey );

for ( RuleList::iterator it = mChildren.begin(); it != mChildren.end(); ++it )
for ( RuleList::const_iterator it = mChildren.constBegin(); it != mChildren.constEnd(); ++it )
{
Rule* rule = *it;
ruleElem.appendChild( rule->save( doc ) );
@@ -265,7 +265,12 @@ QgsRuleBasedLabeling*QgsRuleBasedLabeling::create( QDomElement& element )
return rl;
}

QDomElement QgsRuleBasedLabeling::save( QDomDocument& doc )
QString QgsRuleBasedLabeling::type() const
{
return "rule-based";
}

QDomElement QgsRuleBasedLabeling::save( QDomDocument& doc ) const
{
QDomElement elem = doc.createElement( "labeling" );
elem.setAttribute( "type", "rule-based" );
@@ -276,3 +281,8 @@ QDomElement QgsRuleBasedLabeling::save( QDomDocument& doc )

return elem;
}

QgsVectorLayerLabelProvider* QgsRuleBasedLabeling::provider( QgsVectorLayer* layer ) const
{
return new QgsRuleBasedLabelProvider( *this, layer, false );
}
@@ -3,18 +3,18 @@

#include <QStringList>

#include "qgsvectorlayerlabeling.h"

class QDomDocument;
class QDomElement;

class QgsExpression;
class QgsFeature;
class QgsPalLayerSettings;
class QgsRenderContext;
class QgsVectorLayer;
class QgsVectorLayerLabelProvider;


class CORE_EXPORT QgsRuleBasedLabeling
class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
{
public:
class Rule;
@@ -49,7 +49,7 @@ class CORE_EXPORT QgsRuleBasedLabeling
*
* @return True if this rule is an else rule
*/
bool isElse() { return mElseRule; }
bool isElse() const { return mElseRule; }

void setLabel( QString label ) { mLabel = label; }
/**
@@ -93,13 +93,13 @@ class CORE_EXPORT QgsRuleBasedLabeling
*
* @return A list of rules
*/
RuleList& children() { return mChildren; }
const RuleList& children() const { return mChildren; }
/**
* The parent rule
*
* @return Parent rule
*/
Rule* parent() { return mParent; }
const Rule* parent() const { return mParent; }

//! add child rule, take ownership, sets this as parent
void appendChild( Rule* rule );
@@ -117,7 +117,7 @@ class CORE_EXPORT QgsRuleBasedLabeling
static Rule* create( QDomElement& ruleElem );

//! store labeling info to XML element
QDomElement save( QDomDocument& doc );
QDomElement save( QDomDocument& doc ) const;

// evaluation

@@ -162,13 +162,16 @@ class CORE_EXPORT QgsRuleBasedLabeling
~QgsRuleBasedLabeling();

Rule* rootRule() { return mRootRule; }
const Rule* rootRule() const { return mRootRule; }

//! Create the instance from a DOM element with saved configuration
static QgsRuleBasedLabeling* create( QDomElement& element );

//! Save configuration into a DOM element
QDomElement save( QDomDocument& doc );
// implementation of parent interface

virtual QString type() const override;
virtual QDomElement save( QDomDocument& doc ) const override;
virtual QgsVectorLayerLabelProvider* provider( QgsVectorLayer* layer ) const override;

protected:
Rule* mRootRule;
@@ -132,7 +132,7 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
, mRendererV2( NULL )
, mLabel( 0 )
, mLabelOn( false )
, mLabeling( new QgsVectorLayerLabeling )
, mLabeling( new QgsVectorLayerSimpleLabeling )
, mLabelFontNotFoundNotified( false )
, mFeatureBlendMode( QPainter::CompositionMode_SourceOver ) // Default to normal feature blending
, mLayerTransparency( 0 )
@@ -1234,6 +1234,15 @@ bool QgsVectorLayer::hasLabelsEnabled( void ) const
return mLabelOn;
}

void QgsVectorLayer::setLabeling( QgsAbstractVectorLayerLabeling* labeling )
{
if ( mLabeling == labeling )
return;

delete mLabeling;
mLabeling = labeling;
}

bool QgsVectorLayer::startEditing()
{
if ( !mDataProvider )
@@ -63,7 +63,7 @@ class QgsSymbolV2;
class QgsVectorDataProvider;
class QgsVectorLayerEditBuffer;
class QgsVectorLayerJoinBuffer;
class QgsVectorLayerLabeling;
class QgsAbstractVectorLayerLabeling;
class QgsPointV2;

typedef QList<int> QgsAttributeList;
@@ -1262,10 +1262,15 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
*/
Q_DECL_DEPRECATED bool hasLabelsEnabled() const;

/** Access to labeling configuration
/** Access to labeling configuration.
* @note added in 2.12
*/
QgsVectorLayerLabeling& labeling() { return *mLabeling; }
const QgsAbstractVectorLayerLabeling* labeling() const { return mLabeling; }

/** Set labeling configuration. Takes ownership of the object.
* @note added in 2.12
*/
void setLabeling( QgsAbstractVectorLayerLabeling* labeling );

/** Returns true if the provider is in editing mode */
virtual bool isEditable() const override;
@@ -2092,7 +2097,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
bool mLabelOn;

/** Labeling configuration */
QgsVectorLayerLabeling* mLabeling;
QgsAbstractVectorLayerLabeling* mLabeling;

/** Whether 'labeling font not found' has be shown for this layer (only show once in QgsMessageBar, on first rendering) */
bool mLabelFontNotFoundNotified;
@@ -4,72 +4,28 @@
#include "qgsrulebasedlabeling.h"
#include "qgsvectorlayer.h"

QgsVectorLayerLabeling::QgsVectorLayerLabeling()
: mMode( SimpleLabels )
//, mSimpleLabeling( 0 )
, mRuleBasedLabeling( 0 )
{

}

QgsVectorLayerLabeling::~QgsVectorLayerLabeling()
QgsAbstractVectorLayerLabeling::~QgsAbstractVectorLayerLabeling()
{
//delete mSimpleLabeling;
delete mRuleBasedLabeling;
}

//QgsPalLayerSettings QgsVectorLayerLabeling::simpleLabeling()
//{
//}

//void QgsVectorLayerLabeling::setSimpleLabeling(QgsPalLayerSettings* settings)
//{
// delete mSimpleLabeling;
// mSimpleLabeling = settings;
//}

void QgsVectorLayerLabeling::setRuleBasedLabeling( QgsRuleBasedLabeling* settings )
QgsVectorLayerLabelProvider* QgsVectorLayerSimpleLabeling::provider( QgsVectorLayer* layer ) const
{
delete mRuleBasedLabeling;
mRuleBasedLabeling = settings;
}
if ( layer->customProperty( "labeling" ).toString() == QString( "pal" ) && layer->labelsEnabled() )
return new QgsVectorLayerLabelProvider( layer, false );

QgsVectorLayerLabelProvider* QgsVectorLayerLabeling::provider( QgsVectorLayer* layer )
{
if ( mMode == SimpleLabels )
{
if ( layer->customProperty( "labeling" ).toString() == QString( "pal" ) && layer->labelsEnabled() )
return new QgsVectorLayerLabelProvider( layer, false );
}
else // rule-based
{
if ( mRuleBasedLabeling )
return new QgsRuleBasedLabelProvider( *mRuleBasedLabeling, layer, false );
}
return 0;
}

/*
QgsAbstractLabelProvider* QgsVectorLayerLabeling::addProviderToEngine( QgsVectorLayer* layer, QgsLabelingEngineV2* engine, QgsRenderContext& context )
QString QgsVectorLayerSimpleLabeling::type() const
{
if ( mMode == SimpleLabels )
{
QgsVectorLayerLabelProvider* provider = 0;
if ( layer->labelsEnabled() )
{
provider = new QgsVectorLayerLabelProvider( layer, false );
engine->addProvider( provider );
if ( !provider->prepare( context, attributeNames ) )
{
engine->removeProvider( provider );
provider = 0; // deleted by engine
}
}
}
else // rule-based
{
return 0; // TODO
}
return "simple";
}

QDomElement QgsVectorLayerSimpleLabeling::save( QDomDocument& doc ) const
{
// all configuration is kept in layer custom properties (for compatibility)
QDomElement elem = doc.createElement( "labeling" );
elem.setAttribute( "type", "simple" );
return elem;
}
*/
@@ -1,49 +1,48 @@
#ifndef QGSVECTORLAYERLABELING_H
#define QGSVECTORLAYERLABELING_H

class QDomDocument;
class QDomElement;
class QString;

class QgsPalLayerSettings;
class QgsRuleBasedLabeling;
class QgsVectorLayer;
class QgsVectorLayerLabelProvider;

class CORE_EXPORT QgsVectorLayerLabeling
/**
* Abstract base class - its implementations define different approaches to the labeling of a vector layer.
*
* @note added in 2.12
*/
class CORE_EXPORT QgsAbstractVectorLayerLabeling
{
public:
enum Mode
{
//NoLabels, //!< the layer does not participate in labeling
//Obstacles, //!< no labels are shown, but layer's features act as obstacles for other labels
SimpleLabels, //!< the layer is labelled with one style
RuleBasedLabels //!< the layer is labelled with multiple styles defined with rules
};

//! Defaults to no labels
QgsVectorLayerLabeling();
~QgsVectorLayerLabeling();
virtual ~QgsAbstractVectorLayerLabeling();

Mode mode() const { return mMode; }
void setMode( Mode m ) { mMode = m; }
//! Unique type string of the labeling configuration implementation
virtual QString type() const = 0;

//QgsPalLayerSettings simpleLabeling();
//! Factory for label provider implementation
virtual QgsVectorLayerLabelProvider* provider( QgsVectorLayer* layer ) const = 0;

//QgsPalLayerSettings* simpleLabeling() const { return mSimpleLabeling; }
//! Assign simple labeling configuration (takes ownership)
//void setSimpleLabeling( QgsPalLayerSettings* settings );

QgsRuleBasedLabeling* ruleBasedLabeling() const { return mRuleBasedLabeling; }
//! Assign rule-based labeling configuration (takes ownership)
void setRuleBasedLabeling( QgsRuleBasedLabeling* settings );

//! Factory for label provider implementation - according to the current mode
QgsVectorLayerLabelProvider* provider( QgsVectorLayer* layer );
//! Return labeling configuration as XML element
virtual QDomElement save( QDomDocument& doc ) const = 0;
};

protected:
Mode mMode;
//QgsPalLayerSettings* mSimpleLabeling;
QgsRuleBasedLabeling* mRuleBasedLabeling;
/**
* Basic implementation of the labeling interface.
*
* The configuration is kept in layer's custom properties for backward compatibility.
*
* @note added in 2.12
*/
class CORE_EXPORT QgsVectorLayerSimpleLabeling : public QgsAbstractVectorLayerLabeling
{
public:

virtual QString type() const override;
virtual QgsVectorLayerLabelProvider* provider( QgsVectorLayer* layer ) const override;
virtual QDomElement save( QDomDocument& doc ) const override;
};


#endif // QGSVECTORLAYERLABELING_H
@@ -23,7 +23,7 @@ class QgsAbstractFeatureSource;
/**
* @brief The QgsVectorLayerLabelProvider class implements a label provider
* for vector layers. Parameters for the labeling are taken from the layer's
* custom properties.
* custom properties or from the given settings.
*
* @note added in QGIS 2.12
*/

0 comments on commit 249c878

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