Skip to content

Commit

Permalink
Make QgsMapLayer::name a Q_PROPERTY (#3259)
Browse files Browse the repository at this point in the history
Make QgsMapLayer::name a Q_PROPERTY

And align setName / name / nameChanged

Deprecates setLayerName / layerNameChanged
  • Loading branch information
m-kuhn authored Jul 3, 2016
1 parent 143cfab commit 15c2f0a
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 24 deletions.
21 changes: 19 additions & 2 deletions python/core/qgsmaplayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ class QgsMapLayer : QObject
/** Set the display name of the layer
* @param name New name for the layer
*/
void setLayerName( const QString & name );
void setLayerName( const QString & name ) /Deprecated/;

/**
* Set the display name of the layer
* @param name New name for the layer
*
* @note added in 2.16
*/
void setName( const QString& name );

/** Get the display name of the layer
* @return the layer name
Expand Down Expand Up @@ -631,9 +639,18 @@ class QgsMapLayer : QObject
/** Emit a signal with status (e.g. to be caught by QgisApp and display a msg on status bar) */
void statusChanged( const QString& theStatus );

/** Emit a signal that the layer name has been changed */
/** Emit a signal that the layer name has been changed
* @deprecated since 2.16 use nameChanged() instead
*/
void layerNameChanged();

/**
* Emitted when the name has been changed
*
* @note added in 2.16
*/
void nameChanged();

/** Emit a signal that layer's CRS has been reset */
void layerCrsChanged();

Expand Down
8 changes: 4 additions & 4 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3568,7 +3568,7 @@ bool QgisApp::addVectorLayers( const QStringList &theLayerQStringList, const QSt
Q_ASSERT( elements.size() >= 4 );
if ( layer->name() != elements.at( 1 ) )
{
layer->setLayerName( QString( "%1 %2 %3" ).arg( layer->name(), elements.at( 1 ), elements.at( 3 ) ) );
layer->setName( QString( "%1 %2 %3" ).arg( layer->name(), elements.at( 1 ), elements.at( 3 ) ) );
}

myList << layer;
Expand Down Expand Up @@ -7503,7 +7503,7 @@ QgsVectorLayer *QgisApp::pasteAsNewMemoryVector( const QString & theLayerName )
if ( !layer )
return nullptr;

layer->setLayerName( layerName );
layer->setName( layerName );

mMapCanvas->freeze();

Expand Down Expand Up @@ -8891,7 +8891,7 @@ void QgisApp::showOptionsDialog( QWidget *parent, const QString& currentPage )
{
// if the layer capitalization has changed, we need to update all layer names
Q_FOREACH ( QgsMapLayer* layer, QgsMapLayerRegistry::instance()->mapLayers() )
layer->setLayerName( layer->originalName() );
layer->setName( layer->originalName() );
}

//update any open compositions so they reflect new composer settings
Expand Down Expand Up @@ -11089,7 +11089,7 @@ QgsPluginLayer* QgisApp::addPluginLayer( const QString& uri, const QString& base
if ( !layer )
return nullptr;

layer->setLayerName( baseName );
layer->setName( baseName );

QgsMapLayerRegistry::instance()->addMapLayer( layer );

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsrasterlayerproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ void QgsRasterLayerProperties::apply()
/*
* General Tab
*/
mRasterLayer->setLayerName( mLayerOrigNameLineEd->text() );
mRasterLayer->setName( mLayerOrigNameLineEd->text() );

// set up the scale based layer visibility stuff....
mRasterLayer->setScaleBasedVisibility( chkUseScaleDependentRendering->isChecked() );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsvectorlayerproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ void QgsVectorLayerProperties::apply()
}
Q_NOWARN_DEPRECATED_POP

mLayer->setLayerName( mLayerOrigNameLineEdit->text() );
mLayer->setName( mLayerOrigNameLineEdit->text() );

// Apply fields settings
mFieldsPropertiesDialog->apply();
Expand Down
2 changes: 1 addition & 1 deletion src/core/layertree/qgslayertreelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ QString QgsLayerTreeLayer::layerName() const
void QgsLayerTreeLayer::setLayerName( const QString& n )
{
if ( mLayer )
mLayer->setLayerName( n );
mLayer->setName( n );
else
mLayerName = n;
}
Expand Down
21 changes: 13 additions & 8 deletions src/core/qgsmaplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
, mStyleManager( new QgsMapLayerStyleManager( this ) )
{
// Set the display name = internal name
QgsDebugMsg( "original name: '" + mLayerOrigName + '\'' );
mLayerName = capitaliseLayerName( mLayerOrigName );
QgsDebugMsg( "display name: '" + mLayerName + '\'' );

mShortName = "";
//mShortName.replace( QRegExp( "[\\W]" ), "_" );
Expand All @@ -86,6 +84,8 @@ QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
mMinScale = 0;
mMaxScale = 100000000;
mScaleBasedVisibility = false;

connect( this, SIGNAL( nameChanged() ), this, SIGNAL( layerNameChanged() ) );
}

QgsMapLayer::~QgsMapLayer()
Expand All @@ -105,16 +105,21 @@ QString QgsMapLayer::id() const
return mID;
}

/** Write property of QString layerName. */
void QgsMapLayer::setLayerName( const QString & name )
{
QgsDebugMsg( "new original name: '" + name + '\'' );
setName( name );
}

void QgsMapLayer::setName( const QString& name )
{
QString newName = capitaliseLayerName( name );
QgsDebugMsg( "new display name: '" + name + '\'' );
if ( name == mLayerOrigName && newName == mLayerName ) return;
if ( name == mLayerOrigName && newName == mLayerName )
return;

mLayerOrigName = name; // store the new original name
mLayerName = newName;
emit layerNameChanged();

emit nameChanged();
}

/** Read property of QString layerName. */
Expand Down Expand Up @@ -434,7 +439,7 @@ bool QgsMapLayer::readLayerXML( const QDomElement& layerElement )
// set name
mnl = layerElement.namedItem( "layername" );
mne = mnl.toElement();
setLayerName( mne.text() );
setName( mne.text() );

//short name
QDomElement shortNameElem = layerElement.firstChildElement( "shortname" );
Expand Down
26 changes: 23 additions & 3 deletions src/core/qgsmaplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class CORE_EXPORT QgsMapLayer : public QObject
{
Q_OBJECT

Q_PROPERTY( QString name READ name WRITE setName NOTIFY nameChanged )

public:
/** Layers enum defining the types of layers that can be added to a map */
enum LayerType
Expand Down Expand Up @@ -79,8 +81,17 @@ class CORE_EXPORT QgsMapLayer : public QObject

/** Set the display name of the layer
* @param name New name for the layer
* @deprecated Since 2.16, use setName instead
*/
Q_DECL_DEPRECATED void setLayerName( const QString & name );

/**
* Set the display name of the layer
* @param name New name for the layer
*
* @note added in 2.16
*/
void setLayerName( const QString & name );
void setName( const QString& name );

/** Get the display name of the layer
* @return the layer name
Expand Down Expand Up @@ -651,8 +662,17 @@ class CORE_EXPORT QgsMapLayer : public QObject
/** Emit a signal with status (e.g. to be caught by QgisApp and display a msg on status bar) */
void statusChanged( const QString& theStatus );

/** Emit a signal that the layer name has been changed */
void layerNameChanged();
/** Emit a signal that the layer name has been changed
* @deprecated since 2.16 use nameChanged() instead
*/
Q_DECL_DEPRECATED void layerNameChanged();

/**
* Emitted when the name has been changed
*
* @note added in 2.16
*/
void nameChanged();

/** Emit a signal that layer's CRS has been reset */
void layerCrsChanged();
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ void QgsVectorLayer::setDataSource( const QString& dataSource, const QString& ba

mDataSource = dataSource;
mLayerName = capitaliseLayerName( baseName );
setLayerName( mLayerName );
setName( mLayerName );
setDataProvider( provider );

if ( !mValid )
Expand Down Expand Up @@ -1791,7 +1791,7 @@ bool QgsVectorLayer::setDataProvider( QString const & provider )
}

if ( !lName.isEmpty() )
setLayerName( lName );
setName( lName );
}

QgsDebugMsg( "Beautified layer name " + name() );
Expand Down
2 changes: 1 addition & 1 deletion src/core/raster/qgsrasterlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ void QgsRasterLayer::setDataProvider( QString const & provider )
// set the layer name (uppercase first character)
if ( ! mLayerName.isEmpty() ) // XXX shouldn't this happen in parent?
{
setLayerName( mLayerName );
setName( mLayerName );
}

//mBandCount = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/server/qgswmsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ QImage* QgsWMSServer::getLegendGraphics()
Q_FOREACH ( const QString& layerId, layerIds )
{
QgsMapLayer *ml = QgsMapLayerRegistry::instance()->mapLayer( layerId );
ml->setLayerName( layerNameMap[ layerId ] );
ml->setName( layerNameMap[ layerId ] );
}
// clear map layer registry
QgsMapLayerRegistry::instance()->removeAllMapLayers();
Expand Down

0 comments on commit 15c2f0a

Please sign in to comment.