Skip to content

Commit

Permalink
One common class for labeling configurations (QgsAbstractVectorLayerL…
Browse files Browse the repository at this point in the history
…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 249c878
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 132 deletions.
7 changes: 5 additions & 2 deletions src/core/qgspallabeling.cpp
Expand Up @@ -43,6 +43,7 @@
#include "qgsexpression.h" #include "qgsexpression.h"
#include "qgsdatadefined.h" #include "qgsdatadefined.h"
#include "qgslabelingenginev2.h" #include "qgslabelingenginev2.h"
#include "qgsvectorlayerlabeling.h"


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


#include "qgsvectorlayerlabeling.h"


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


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

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


Expand Down
16 changes: 13 additions & 3 deletions src/core/qgsrulebasedlabeling.cpp
Expand Up @@ -139,7 +139,7 @@ QgsRuleBasedLabeling::Rule*QgsRuleBasedLabeling::Rule::create( QDomElement& rule
return rule; return rule;
} }


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


Expand All @@ -161,7 +161,7 @@ QDomElement QgsRuleBasedLabeling::Rule::save( QDomDocument& doc )
// ruleElem.setAttribute( "checkstate", 0 ); // ruleElem.setAttribute( "checkstate", 0 );
//ruleElem.setAttribute( "key", mRuleKey ); //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; Rule* rule = *it;
ruleElem.appendChild( rule->save( doc ) ); ruleElem.appendChild( rule->save( doc ) );
Expand Down Expand Up @@ -265,7 +265,12 @@ QgsRuleBasedLabeling*QgsRuleBasedLabeling::create( QDomElement& element )
return rl; 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" ); QDomElement elem = doc.createElement( "labeling" );
elem.setAttribute( "type", "rule-based" ); elem.setAttribute( "type", "rule-based" );
Expand All @@ -276,3 +281,8 @@ QDomElement QgsRuleBasedLabeling::save( QDomDocument& doc )


return elem; return elem;
} }

QgsVectorLayerLabelProvider* QgsRuleBasedLabeling::provider( QgsVectorLayer* layer ) const
{
return new QgsRuleBasedLabelProvider( *this, layer, false );
}
21 changes: 12 additions & 9 deletions src/core/qgsrulebasedlabeling.h
Expand Up @@ -3,18 +3,18 @@


#include <QStringList> #include <QStringList>


#include "qgsvectorlayerlabeling.h"

class QDomDocument; class QDomDocument;
class QDomElement; class QDomElement;


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




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


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


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


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


// evaluation // evaluation


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


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


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


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


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


protected: protected:
Rule* mRootRule; Rule* mRootRule;
Expand Down
11 changes: 10 additions & 1 deletion src/core/qgsvectorlayer.cpp
Expand Up @@ -132,7 +132,7 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
, mRendererV2( NULL ) , mRendererV2( NULL )
, mLabel( 0 ) , mLabel( 0 )
, mLabelOn( false ) , mLabelOn( false )
, mLabeling( new QgsVectorLayerLabeling ) , mLabeling( new QgsVectorLayerSimpleLabeling )
, mLabelFontNotFoundNotified( false ) , mLabelFontNotFoundNotified( false )
, mFeatureBlendMode( QPainter::CompositionMode_SourceOver ) // Default to normal feature blending , mFeatureBlendMode( QPainter::CompositionMode_SourceOver ) // Default to normal feature blending
, mLayerTransparency( 0 ) , mLayerTransparency( 0 )
Expand Down Expand Up @@ -1234,6 +1234,15 @@ bool QgsVectorLayer::hasLabelsEnabled( void ) const
return mLabelOn; return mLabelOn;
} }


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

delete mLabeling;
mLabeling = labeling;
}

bool QgsVectorLayer::startEditing() bool QgsVectorLayer::startEditing()
{ {
if ( !mDataProvider ) if ( !mDataProvider )
Expand Down
13 changes: 9 additions & 4 deletions src/core/qgsvectorlayer.h
Expand Up @@ -63,7 +63,7 @@ class QgsSymbolV2;
class QgsVectorDataProvider; class QgsVectorDataProvider;
class QgsVectorLayerEditBuffer; class QgsVectorLayerEditBuffer;
class QgsVectorLayerJoinBuffer; class QgsVectorLayerJoinBuffer;
class QgsVectorLayerLabeling; class QgsAbstractVectorLayerLabeling;
class QgsPointV2; class QgsPointV2;


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


/** Access to labeling configuration /** Access to labeling configuration.
* @note added in 2.12 * @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 */ /** Returns true if the provider is in editing mode */
virtual bool isEditable() const override; virtual bool isEditable() const override;
Expand Down Expand Up @@ -2092,7 +2097,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
bool mLabelOn; bool mLabelOn;


/** Labeling configuration */ /** 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) */ /** Whether 'labeling font not found' has be shown for this layer (only show once in QgsMessageBar, on first rendering) */
bool mLabelFontNotFoundNotified; bool mLabelFontNotFoundNotified;
Expand Down
70 changes: 13 additions & 57 deletions src/core/qgsvectorlayerlabeling.cpp
Expand Up @@ -4,72 +4,28 @@
#include "qgsrulebasedlabeling.h" #include "qgsrulebasedlabeling.h"
#include "qgsvectorlayer.h" #include "qgsvectorlayer.h"


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

}


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


//QgsPalLayerSettings QgsVectorLayerLabeling::simpleLabeling() QgsVectorLayerLabelProvider* QgsVectorLayerSimpleLabeling::provider( QgsVectorLayer* layer ) const
//{
//}

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

void QgsVectorLayerLabeling::setRuleBasedLabeling( QgsRuleBasedLabeling* settings )
{ {
delete mRuleBasedLabeling; if ( layer->customProperty( "labeling" ).toString() == QString( "pal" ) && layer->labelsEnabled() )
mRuleBasedLabeling = settings; 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; return 0;
} }


/* QString QgsVectorLayerSimpleLabeling::type() const
QgsAbstractLabelProvider* QgsVectorLayerLabeling::addProviderToEngine( QgsVectorLayer* layer, QgsLabelingEngineV2* engine, QgsRenderContext& context )
{ {
if ( mMode == SimpleLabels ) return "simple";
{ }
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
}


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;
} }
*/
61 changes: 30 additions & 31 deletions src/core/qgsvectorlayerlabeling.h
@@ -1,49 +1,48 @@
#ifndef QGSVECTORLAYERLABELING_H #ifndef QGSVECTORLAYERLABELING_H
#define QGSVECTORLAYERLABELING_H #define QGSVECTORLAYERLABELING_H


class QDomDocument;
class QDomElement;
class QString;


class QgsPalLayerSettings;
class QgsRuleBasedLabeling;
class QgsVectorLayer; class QgsVectorLayer;
class QgsVectorLayerLabelProvider; 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: 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 virtual ~QgsAbstractVectorLayerLabeling();
QgsVectorLayerLabeling();
~QgsVectorLayerLabeling();


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


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


//QgsPalLayerSettings* simpleLabeling() const { return mSimpleLabeling; } //! Return labeling configuration as XML element
//! Assign simple labeling configuration (takes ownership) virtual QDomElement save( QDomDocument& doc ) const = 0;
//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 );


protected: /**
Mode mMode; * Basic implementation of the labeling interface.
//QgsPalLayerSettings* mSimpleLabeling; *
QgsRuleBasedLabeling* mRuleBasedLabeling; * 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 #endif // QGSVECTORLAYERLABELING_H
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerlabelprovider.h
Expand Up @@ -23,7 +23,7 @@ class QgsAbstractFeatureSource;
/** /**
* @brief The QgsVectorLayerLabelProvider class implements a label provider * @brief The QgsVectorLayerLabelProvider class implements a label provider
* for vector layers. Parameters for the labeling are taken from the layer's * 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 * @note added in QGIS 2.12
*/ */
Expand Down

0 comments on commit 249c878

Please sign in to comment.