Skip to content
Permalink
Browse files
Merge pull request #4567 from pblottiere/clone
Add clone() methods for layers
  • Loading branch information
m-kuhn committed May 19, 2017
2 parents c82bffe + aa2739c commit 95ac7d0f7be3300f52133938479dcbbfbd6f1401
@@ -1696,6 +1696,7 @@ QgsPluginLayer {#qgis_api_break_3_0_QgsPluginLayer}
--------------

- createMapRenderer(): default implementation (which called plugin's draw() method) has been removed. Plugin layers must implement createMapRenderer().
- clone(): new pure virtual method. Plugin layer must implement clone().


QgsPluginLayerRegistry {#qgis_api_break_3_0_QgsPluginLayerRegistry}
@@ -67,6 +67,15 @@ class QgsMapLayer : QObject
virtual ~QgsMapLayer();


virtual QgsMapLayer *clone() const = 0;
%Docstring
Returns a new instance equivalent to this one except for the id which
is still unique.
:return: a new layer instance
.. versionadded:: 3.0
:rtype: QgsMapLayer
%End

QgsMapLayer::LayerType type() const;
%Docstring
Returns the type of the layer.
@@ -470,6 +479,12 @@ Invoked by QgsProject.read().
:rtype: QVariant
%End

void setCustomProperties( const QgsObjectCustomProperties &properties );
%Docstring
Set custom properties for layer. Current properties are dropped.
.. versionadded:: 3.0
%End

void removeCustomProperty( const QString &key );
%Docstring
Remove a custom property from layer. Properties are stored in a map and saved in project file.
@@ -1028,6 +1043,14 @@ Signal emitted when the blend mode is changed, through QgsMapLayer.setBlendMode(
%End

protected:

void clone( QgsMapLayer *layer ) const;
%Docstring
Copies attributes like name, short name, ... into another layer.
\param layer The copy recipient
.. versionadded:: 3.0
%End

virtual void setExtent( const QgsRectangle &rect );
%Docstring
Set the extent
@@ -133,6 +133,15 @@ Write configuration (for project saving)
Return list of all defined style names
:rtype: list of str
%End

QMap<QString, QgsMapLayerStyle> mapLayerStyles() const;
%Docstring
Gets available styles for the associated map layer.
:return: A map of map layer style by style name
.. versionadded:: 3.0
:rtype: QMap<str, QgsMapLayerStyle>
%End

QgsMapLayerStyle style( const QString &name ) const;
%Docstring
Return data of a stored style - accessed by its unique name
@@ -26,6 +26,14 @@ In order to be readable from project files, they should set these attributes in
QgsPluginLayer( const QString &layerType, const QString &layerName = QString() );
~QgsPluginLayer();

virtual QgsPluginLayer *clone() const = 0;
%Docstring
Returns a new instance equivalent to this one.
:return: a new layer instance
.. versionadded:: 3.0
:rtype: QgsPluginLayer
%End

QString pluginLayerType();
%Docstring
Return plugin layer type (the same as used in QgsPluginLayerRegistry)
@@ -334,6 +334,17 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSin
virtual ~QgsVectorLayer();


virtual QgsVectorLayer *clone() const /Factory/;
%Docstring
Returns a new instance equivalent to this one. A new provider is
created for the same data source and renderers for features and diagrams
are cloned too. Moreover, each attributes (transparency, extent, selected
features and so on) are identicals.
:return: a new layer instance
.. versionadded:: 3.0
:rtype: QgsVectorLayer
%End

QString storageType() const;
%Docstring
Returns the permanent storage type for this layer as a friendly name.
@@ -496,6 +507,7 @@ Setup the coordinate system transformation for the layer
:rtype: QgsActionManager
%End


int selectedFeatureCount() const;
%Docstring
The number of features that are selected in this layer
@@ -1418,6 +1430,14 @@ Assembles mUpdatedFields considering provider fields, joined fields and added fi
:rtype: QgsFieldConstraints.Constraints
%End

QMap< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength> fieldConstraintsAndStrength( int fieldIndex ) const;
%Docstring
Returns a map of constraint with their strength for a specific field of the layer.
\param fieldIndex field index
.. versionadded:: 3.0
:rtype: QMap< QgsFieldConstraints.Constraint, QgsFieldConstraints.ConstraintStrength>
%End

void setFieldConstraint( int index, QgsFieldConstraints::Constraint constraint, QgsFieldConstraints::ConstraintStrength strength = QgsFieldConstraints::ConstraintStrengthHard );
%Docstring
Sets a constraint for a specified field index. Any constraints inherited from the layer's
@@ -41,6 +41,13 @@ class QgsRasterLayer : QgsMapLayer
/** \brief The destructor */
~QgsRasterLayer();

/** Returns a new instance equivalent to this one. A new provider is
* created for the same data source and renderer is cloned too.
* \returns a new layer instance
* \since QGIS 3.0
*/
virtual QgsRasterLayer *clone() const /Factory/;

/** \brief This enumerator describes the types of shading that can be used */
enum ColorShadingAlgorithm
{
@@ -8984,39 +8984,7 @@ void QgisApp::duplicateLayers( const QList<QgsMapLayer *> &lyrList )
}
else if ( vlayer )
{
QgsVectorLayer *dupVLayer = new QgsVectorLayer( vlayer->source(), layerDupName, vlayer->providerType() );
if ( vlayer->dataProvider() )
{
dupVLayer->setProviderEncoding( vlayer->dataProvider()->encoding() );
}

//add variables defined in layer properties
QStringList variableNames = vlayer->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
QStringList variableValues = vlayer->customProperty( QStringLiteral( "variableValues" ) ).toStringList();

int varIndex = 0;
Q_FOREACH ( const QString &variableName, variableNames )
{
if ( varIndex >= variableValues.length() )
{
break;
}

QVariant varValue = variableValues.at( varIndex );
varIndex++;
QgsExpressionContextUtils::setLayerVariable( dupVLayer, variableName, varValue );
}

Q_FOREACH ( const QgsVectorLayerJoinInfo &join, vlayer->vectorJoins() )
dupVLayer->addJoin( join );

for ( int fld = 0; fld < vlayer->fields().count(); fld++ )
{
if ( vlayer->fields().fieldOrigin( fld ) == QgsFields::OriginExpression )
dupVLayer->addExpressionField( vlayer->expressionField( fld ), vlayer->fields().at( fld ) );
}

dupLayer = dupVLayer;
dupLayer = vlayer->clone();
}
}

@@ -9025,7 +8993,7 @@ void QgisApp::duplicateLayers( const QList<QgsMapLayer *> &lyrList )
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( selectedLyr );
if ( rlayer )
{
dupLayer = new QgsRasterLayer( rlayer->source(), layerDupName );
dupLayer = rlayer->clone();
}
}

@@ -94,6 +94,38 @@ QgsMapLayer::~QgsMapLayer()
delete mStyleManager;
}

void QgsMapLayer::clone( QgsMapLayer *layer ) const
{
layer->setBlendMode( blendMode() );

Q_FOREACH ( const QString &s, styleManager()->styles() )
{
layer->styleManager()->addStyle( s, styleManager()->style( s ) );
}

layer->setName( name() );
layer->setShortName( shortName() );
layer->setExtent( extent() );
layer->setMinimumScale( minimumScale() );
layer->setMaximumScale( maximumScale() );
layer->setScaleBasedVisibility( hasScaleBasedVisibility() );
layer->setTitle( title() );
layer->setAbstract( abstract() );
layer->setKeywordList( keywordList() );
layer->setDataUrl( dataUrl() );
layer->setDataUrlFormat( dataUrlFormat() );
layer->setAttribution( attribution() );
layer->setAttributionUrl( attributionUrl() );
layer->setMetadataUrl( metadataUrl() );
layer->setMetadataUrlType( metadataUrlType() );
layer->setMetadataUrlFormat( metadataUrlFormat() );
layer->setLegendUrl( legendUrl() );
layer->setLegendUrlFormat( legendUrlFormat() );
layer->setDependencies( dependencies() );
layer->setCrs( crs() );
layer->setCustomProperties( mCustomProperties );
}

QgsMapLayer::LayerType QgsMapLayer::type() const
{
return mLayerType;
@@ -1583,6 +1615,11 @@ void QgsMapLayer::setCustomProperty( const QString &key, const QVariant &value )
mCustomProperties.setValue( key, value );
}

void QgsMapLayer::setCustomProperties( const QgsObjectCustomProperties &properties )
{
mCustomProperties = properties;
}

QVariant QgsMapLayer::customProperty( const QString &value, const QVariant &defaultValue ) const
{
return mCustomProperties.value( value, defaultValue );
@@ -110,6 +110,13 @@ class CORE_EXPORT QgsMapLayer : public QObject
//! QgsMapLayer cannot be copied
QgsMapLayer &operator=( QgsMapLayer const & ) = delete;

/** Returns a new instance equivalent to this one except for the id which
* is still unique.
* \returns a new layer instance
* \since QGIS 3.0
*/
virtual QgsMapLayer *clone() const = 0;

/** Returns the type of the layer.
*/
QgsMapLayer::LayerType type() const;
@@ -444,6 +451,11 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
QVariant customProperty( const QString &value, const QVariant &defaultValue = QVariant() ) const;

/** Set custom properties for layer. Current properties are dropped.
* \since QGIS 3.0
*/
void setCustomProperties( const QgsObjectCustomProperties &properties );

/** Remove a custom property from layer. Properties are stored in a map and saved in project file.
* \see setCustomProperty()
*/
@@ -916,6 +928,13 @@ class CORE_EXPORT QgsMapLayer : public QObject
void metadataChanged();

protected:

/** Copies attributes like name, short name, ... into another layer.
* \param layer The copy recipient
* \since QGIS 3.0
*/
void clone( QgsMapLayer *layer ) const;

//! Set the extent
virtual void setExtent( const QgsRectangle &rect );

@@ -70,6 +70,11 @@ QStringList QgsMapLayerStyleManager::styles() const
return mStyles.keys();
}

QMap<QString, QgsMapLayerStyle> QgsMapLayerStyleManager::mapLayerStyles() const
{
return mStyles;
}

QgsMapLayerStyle QgsMapLayerStyleManager::style( const QString &name ) const
{
if ( name == mCurrentStyle )
@@ -110,6 +110,13 @@ class CORE_EXPORT QgsMapLayerStyleManager : public QObject

//! Return list of all defined style names
QStringList styles() const;

/** Gets available styles for the associated map layer.
* \returns A map of map layer style by style name
* \since QGIS 3.0
*/
QMap<QString, QgsMapLayerStyle> mapLayerStyles() const;

//! Return data of a stored style - accessed by its unique name
QgsMapLayerStyle style( const QString &name ) const;

@@ -36,6 +36,12 @@ class CORE_EXPORT QgsPluginLayer : public QgsMapLayer
QgsPluginLayer( const QString &layerType, const QString &layerName = QString() );
~QgsPluginLayer();

/** Returns a new instance equivalent to this one.
* \returns a new layer instance
* \since QGIS 3.0
*/
virtual QgsPluginLayer *clone() const override = 0;

//! Return plugin layer type (the same as used in QgsPluginLayerRegistry)
QString pluginLayerType();

0 comments on commit 95ac7d0

Please sign in to comment.