Skip to content

Commit

Permalink
Merge pull request #4567 from pblottiere/clone
Browse files Browse the repository at this point in the history
Add clone() methods for layers
  • Loading branch information
m-kuhn committed May 19, 2017
2 parents c82bffe + aa2739c commit 95ac7d0
Show file tree
Hide file tree
Showing 18 changed files with 481 additions and 36 deletions.
1 change: 1 addition & 0 deletions doc/api_break.dox
Expand Up @@ -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}
Expand Down
23 changes: 23 additions & 0 deletions python/core/qgsmaplayer.sip
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions python/core/qgsmaplayerstylemanager.sip
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions python/core/qgspluginlayer.sip
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions python/core/raster/qgsrasterlayer.sip
Expand Up @@ -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
{
Expand Down
36 changes: 2 additions & 34 deletions src/app/qgisapp.cpp
Expand Up @@ -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();
}
}

Expand All @@ -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();
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgsmaplayer.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
*/
Expand Down Expand Up @@ -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 );

Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsmaplayerstylemanager.cpp
Expand Up @@ -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 )
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsmaplayerstylemanager.h
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions src/core/qgspluginlayer.h
Expand Up @@ -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();

Expand Down

0 comments on commit 95ac7d0

Please sign in to comment.