Merge pull request #36265 from m-kuhn/export_label_positions

Abstract dxf export label provider away
This commit is contained in:
Matthias Kuhn 2020-05-08 17:49:25 +02:00 committed by GitHub
commit 753a684c9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 187 additions and 202 deletions

View File

@ -491,13 +491,16 @@ Returns list of available DXF encodings
%End
void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
%Docstring
Register name of layer for feature
:param layerId: id of layer
:param fid: id of feature
:param layer: dxf layer of feature
.. deprecated::
Will be made private in QGIS 4
%End
private:

View File

@ -452,7 +452,6 @@ SET(QGIS_CORE_SRCS
dxf/qgsdxfexport.cpp
dxf/qgsdxfpaintdevice.cpp
dxf/qgsdxfpaintengine.cpp
dxf/qgsdxfpallabeling.cpp
layout/qgsabstractreportsection.cpp
layout/qgslayout.cpp
@ -595,6 +594,7 @@ SET(QGIS_CORE_SRCS
labeling/qgslabelingenginesettings.cpp
labeling/qgslabelobstaclesettings.cpp
labeling/qgslabelsearchtree.cpp
labeling/qgslabelsink.cpp
labeling/qgslabelthinningsettings.cpp
labeling/qgspallabeling.cpp
labeling/qgsrulebasedlabeling.cpp

View File

@ -27,7 +27,6 @@
// AutoCAD 2014: http://images.autodesk.com/adsk/files/autocad_2014_pdf_dxf_reference_enu.pdf
#include "qgsdxfexport.h"
#include "qgsdxfpallabeling.h"
#include "qgsgeometrygeneratorsymbollayer.h"
#include "qgsgeometrycollection.h"
#include "qgscurvepolygon.h"
@ -718,11 +717,17 @@ void QgsDxfExport::writeEntities()
if ( job->labelProvider )
{
job->labelProvider->registerDxfFeature( fet, mRenderContext, lName );
job->labelProvider->registerFeature( fet, mRenderContext );
Q_NOWARN_DEPRECATED_PUSH
registerDxfLayer( job->featureSource.id(), fet.id(), lName );
Q_NOWARN_DEPRECATED_POP
}
else if ( job->ruleBasedLabelProvider )
{
job->ruleBasedLabelProvider->registerDxfFeature( fet, mRenderContext, lName );
job->ruleBasedLabelProvider->registerFeature( fet, mRenderContext );
Q_NOWARN_DEPRECATED_PUSH
registerDxfLayer( job->featureSource.id(), fet.id(), lName );
Q_NOWARN_DEPRECATED_POP
}
}
}

View File

@ -24,6 +24,7 @@
#include "qgsgeometry.h"
#include "qgssymbol.h" // for OutputUnit enum
#include "qgsmapsettings.h"
#include "qgslabelsink.h"
#include <QColor>
#include <QList>
@ -54,8 +55,13 @@ namespace pal // SIP_SKIP
* \ingroup core
* \class QgsDxfExport
*/
#ifdef SIP_RUN
class CORE_EXPORT QgsDxfExport
{
#else
class CORE_EXPORT QgsDxfExport : public QgsLabelSink
{
#endif
public:
/**
@ -167,7 +173,7 @@ class CORE_EXPORT QgsDxfExport
*/
QgsDxfExport();
~QgsDxfExport();
~QgsDxfExport() override;
/**
* Set map settings and assign layer name attributes
@ -505,22 +511,21 @@ class CORE_EXPORT QgsDxfExport
static QStringList encodings();
/**
* Output the label
* \param layerId id of the layer
* \param context render context
* \param label position of label
* \param settings label settings
* Add a label to the dxf output.
*
* \note not available in Python bindings
*/
void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP;
void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP override;
/**
* Register name of layer for feature
* \param layerId id of layer
* \param fid id of feature
* \param layer dxf layer of feature
*
* \deprecated Will be made private in QGIS 4
*/
void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
Q_DECL_DEPRECATED void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
private:

View File

@ -22,7 +22,7 @@
#include "qgsvectorlayerfeatureiterator.h"
#include "qgsrenderer.h"
#include "qgsvectorlayerlabeling.h"
#include "qgsdxfpallabeling.h"
#include "qgslabelsink.h"
/**
* Holds information about each layer in a DXF job.
@ -62,7 +62,7 @@ struct DxfLayerJob
QgsLabelingEngine *labelingEngine = renderContext.labelingEngine();
if ( const QgsRuleBasedLabeling *rbl = dynamic_cast<const QgsRuleBasedLabeling *>( labeling.get() ) )
{
ruleBasedLabelProvider = new QgsDxfRuleBasedLabelProvider( *rbl, vl, dxfExport );
ruleBasedLabelProvider = new QgsRuleBasedLabelSinkProvider( *rbl, vl, dxfExport );
labelingEngine->addProvider( ruleBasedLabelProvider );
if ( !ruleBasedLabelProvider->prepare( renderContext, attributes ) )
@ -74,7 +74,7 @@ struct DxfLayerJob
else
{
QgsPalLayerSettings settings = labeling->settings();
labelProvider = new QgsDxfLabelProvider( vl, QString(), dxfExport, &settings );
labelProvider = new QgsLabelSinkProvider( vl, QString(), dxfExport, &settings );
labelingEngine->addProvider( labelProvider );
if ( !labelProvider->prepare( renderContext, attributes ) )
@ -98,8 +98,8 @@ struct DxfLayerJob
QgsDxfExport *dxfExport = nullptr;
QgsCoordinateReferenceSystem crs;
QString layerName;
QgsDxfLabelProvider *labelProvider = nullptr;
QgsDxfRuleBasedLabelProvider *ruleBasedLabelProvider = nullptr;
QgsLabelSinkProvider *labelProvider = nullptr;
QgsRuleBasedLabelSinkProvider *ruleBasedLabelProvider = nullptr;
QString splitLayerAttribute;
QString layerTitle;
QSet<QString> attributes;

View File

@ -1,74 +0,0 @@
/***************************************************************************
qgsdxfpallabeling.cpp
---------------------
begin : January 2014
copyright : (C) 2014 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsdxfpallabeling.h"
#include "qgsdxfexport.h"
#include "qgspallabeling.h"
#include "qgsmapsettings.h"
#include "qgslogger.h"
QgsDxfLabelProvider::QgsDxfLabelProvider( QgsVectorLayer *layer, const QString &providerId, QgsDxfExport *dxf, const QgsPalLayerSettings *settings )
: QgsVectorLayerLabelProvider( layer, providerId, false, settings )
, mDxfExport( dxf )
{
}
void QgsDxfLabelProvider::drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const
{
Q_ASSERT( mDxfExport );
mDxfExport->drawLabel( layerId(), context, label, mSettings );
}
void QgsDxfLabelProvider::registerDxfFeature( const QgsFeature &feature, QgsRenderContext &context, const QString &dxfLayerName )
{
registerFeature( feature, context );
mDxfExport->registerDxfLayer( layerId(), feature.id(), dxfLayerName );
}
QgsDxfRuleBasedLabelProvider::QgsDxfRuleBasedLabelProvider( const QgsRuleBasedLabeling &rules, QgsVectorLayer *layer, QgsDxfExport *dxf )
: QgsRuleBasedLabelProvider( rules, layer, false )
, mDxfExport( dxf )
{
mRules->rootRule()->createSubProviders( layer, mSubProviders, this );
}
void QgsDxfRuleBasedLabelProvider::reinit( QgsVectorLayer *layer )
{
QgsDebugMsgLevel( QStringLiteral( "Entering." ), 4 );
mRules->rootRule()->createSubProviders( layer, mSubProviders, this );
}
QgsVectorLayerLabelProvider *QgsDxfRuleBasedLabelProvider::createProvider( QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings )
{
QgsDebugMsgLevel( QStringLiteral( "Entering." ), 4 );
Q_UNUSED( withFeatureLoop )
return new QgsDxfLabelProvider( layer, providerId, mDxfExport, settings );
}
void QgsDxfRuleBasedLabelProvider::drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const
{
QgsDebugMsgLevel( QStringLiteral( "Entering." ), 4 );
Q_ASSERT( mDxfExport );
mDxfExport->drawLabel( layerId(), context, label, mSettings );
}
void QgsDxfRuleBasedLabelProvider::registerDxfFeature( QgsFeature &feature, QgsRenderContext &context, const QString &dxfLayerName )
{
registerFeature( feature, context );
mDxfExport->registerDxfLayer( layerId(), feature.id(), dxfLayerName );
}

View File

@ -1,110 +0,0 @@
/***************************************************************************
qgsdxfpallabeling.h
-------------------
begin : January 2014
copyright : (C) 2014 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSDXFPALLABELING_H
#define QGSDXFPALLABELING_H
#define SIP_NO_FILE
#include "qgsvectorlayerlabelprovider.h"
#include "qgsrulebasedlabeling.h"
class QgsDxfExport;
class QgsPalLayerSettings;
class QgsRuleBasedLabeling;
/**
* \ingroup core
* Implements a derived label provider internally used for DXF export
*
* Internal class, not in public API. Added in QGIS 2.12
* \note not available in Python bindings
*/
class QgsDxfLabelProvider : public QgsVectorLayerLabelProvider
{
public:
//! construct the provider
explicit QgsDxfLabelProvider( QgsVectorLayer *layer, const QString &providerId, QgsDxfExport *dxf, const QgsPalLayerSettings *settings );
/**
* Re-implementation that writes to DXF file instead of drawing with QPainter
* \param context render context
* \param label label
*/
void drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const override;
/**
* Registration method that keeps track of DXF layer names of individual features
* \param feature feature
* \param context render context
* \param dxfLayerName name of dxf layer
*/
void registerDxfFeature( const QgsFeature &feature, QgsRenderContext &context, const QString &dxfLayerName );
protected:
//! pointer to parent DXF export where this instance is used
QgsDxfExport *mDxfExport = nullptr;
};
/**
* \ingroup core
* Implements a derived label provider for rule based labels internally used
* for DXF export
*
* Internal class, not in public API. Added in QGIS 2.15
* \note not available in Python bindings
*/
class QgsDxfRuleBasedLabelProvider : public QgsRuleBasedLabelProvider
{
public:
//! construct the provider
explicit QgsDxfRuleBasedLabelProvider( const QgsRuleBasedLabeling &rules, QgsVectorLayer *layer, QgsDxfExport *dxf );
/**
* Reinitialize the subproviders with QgsDxfLabelProviders
* \param layer layer
* \deprecated since QGIS 3.12
*/
Q_DECL_DEPRECATED void reinit( QgsVectorLayer *layer );
/**
* Re-implementation that writes to DXF file instead of drawing with QPainter
* \param context render context
* \param label label
*/
void drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const override;
/**
* Registration method that keeps track of DXF layer names of individual features
* \param feature feature
* \param context render context
* \param dxfLayerName name of dxf layer
*/
void registerDxfFeature( QgsFeature &feature, QgsRenderContext &context, const QString &dxfLayerName );
//! create QgsDxfLabelProvider
QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings ) override;
protected:
//! pointer to parent DXF export where this instance is used
QgsDxfExport *mDxfExport = nullptr;
};
#endif // QGSDXFPALLABELING_H

View File

@ -0,0 +1,57 @@
/***************************************************************************
qgslabelsink.cpp
---------------------
begin : January 2014
copyright : (C) 2014 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgslabelsink.h"
#include "qgspallabeling.h"
#include "qgsmapsettings.h"
#include "qgslogger.h"
QgsLabelSinkProvider::QgsLabelSinkProvider( QgsVectorLayer *layer, const QString &providerId, QgsLabelSink *dxf, const QgsPalLayerSettings *settings )
: QgsVectorLayerLabelProvider( layer, providerId, false, settings )
, mLabelSink( dxf )
{
}
void QgsLabelSinkProvider::drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const
{
Q_ASSERT( mLabelSink );
mLabelSink->drawLabel( layerId(), context, label, mSettings );
}
QgsRuleBasedLabelSinkProvider::QgsRuleBasedLabelSinkProvider( const QgsRuleBasedLabeling &rules, QgsVectorLayer *layer, QgsLabelSink *dxf )
: QgsRuleBasedLabelProvider( rules, layer, false )
, mLabelSink( dxf )
{
mRules->rootRule()->createSubProviders( layer, mSubProviders, this );
}
void QgsRuleBasedLabelSinkProvider::reinit( QgsVectorLayer *layer )
{
mRules->rootRule()->createSubProviders( layer, mSubProviders, this );
}
QgsVectorLayerLabelProvider *QgsRuleBasedLabelSinkProvider::createProvider( QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings )
{
Q_UNUSED( withFeatureLoop )
return new QgsLabelSinkProvider( layer, providerId, mLabelSink, settings );
}
void QgsRuleBasedLabelSinkProvider::drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const
{
Q_ASSERT( mLabelSink );
mLabelSink->drawLabel( layerId(), context, label, mSettings );
}

View File

@ -0,0 +1,99 @@
/***************************************************************************
qgslabelsink.h
-------------------
begin : January 2014
copyright : (C) 2014 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSLABELSINK_H
#define QGSLABELSINK_H
#define SIP_NO_FILE
#include "qgsvectorlayerlabelprovider.h"
#include "qgsrulebasedlabeling.h"
class QgsPalLayerSettings;
class QgsRuleBasedLabeling;
/**
* \ingroup core
* Abstract base class that can be used to intercept rendered labels from
* a labeling / rendering job.
*
* \note not available in Python bindings
* \since QGIS 3.14
*/
class QgsLabelSink
{
public:
virtual ~QgsLabelSink() = default;
/**
* The drawLabel method is called for each label that is being drawn.
* Every subclass must implement this method to draw the label or send the information from \a label
* to another desired location.
*/
virtual void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) = 0;
};
/**
* \ingroup core
* Implements a derived label provider for use with QgsLabelSink.
*
* \note not available in Python bindings
* \since QGIS 3.14
*/
class QgsLabelSinkProvider : public QgsVectorLayerLabelProvider
{
public:
//! Creates a rule based label sink provider which will draw/register labels in \a sink.
explicit QgsLabelSinkProvider( QgsVectorLayer *layer, const QString &providerId, QgsLabelSink *sink, const QgsPalLayerSettings *settings );
void drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const override;
private:
QgsLabelSink *mLabelSink = nullptr;
};
/**
* \ingroup core
* Implements a derived label provider for rule based labels for use with QgsLabelSink.
*
* \note not available in Python bindings
* \since QGIS 3.14
*/
class QgsRuleBasedLabelSinkProvider : public QgsRuleBasedLabelProvider
{
public:
//! Creates a rule based label sink provider which will draw/register labels in \a sink.
explicit QgsRuleBasedLabelSinkProvider( const QgsRuleBasedLabeling &rules, QgsVectorLayer *layer, QgsLabelSink *sink );
/**
* Reinitialize the subproviders with QgsLabelSinkProviders
* \deprecated since QGIS 3.12
*/
Q_DECL_DEPRECATED void reinit( QgsVectorLayer *layer );
void drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const override;
//! Creates a QgsRuleBasedLabelSinkProvider
QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings ) override;
private:
QgsLabelSink *mLabelSink = nullptr;
};
#endif // QGSLABELSINK_H