Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Cleanup fragile 'capitalize layer names' option
This option was being applied in the wrong place - within the
map layer classes themselves. This meant that depending on the
user's setting for this option, a plugin calling QgsMapLayer::setName
would not be guaranteed the same behaviour across installs.
(and the same with setDataSource)

Similarly, the option was re-applied on project load, so
moving projects between installs with different values for
this setting would affect the project layer names, breaking
expressions which relied on these...

Instead, move the formatting and capitalization of layer
names to the QgisApp add*Layer methods instead, so this option
only applies on adding new layers to a project.
  • Loading branch information
nyalldawson committed Nov 14, 2017
1 parent d14b859 commit 1e4f691
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 33 deletions.
1 change: 1 addition & 0 deletions doc/api_break.dox
Expand Up @@ -1666,6 +1666,7 @@ screenUpdateRequested() were removed. These members have had no effect for a num
- setMaximumScale() and setMinimumScale(), maximumScale() and minimumScale() had the opposite meaning to other min/max scales in the API, and their definitions have now been swapped. setMaximumScale
now sets the maximum (i.e. largest scale, or most zoomed in) at which the layer will appear, and setMinimumScale now sets the minimum (i.e. smallest scale,
or most zoomed out) at which the layer will appear. The same is true for the maximumScale and minimumScale getters.
- capitalizeLayerName() was removed. Use formatLayerName() instead.


QgsMapLayerActionRegistry {#qgis_api_break_3_0_QgsMapLayerActionRegistry}
Expand Down
6 changes: 4 additions & 2 deletions python/core/qgsmaplayer.sip
Expand Up @@ -512,9 +512,11 @@ Invoked by QgsProject.read().
Sets layer's spatial reference system
%End

static QString capitalizeLayerName( const QString &name );
static QString formatLayerName( const QString &name );
%Docstring
A convenience function to (un)capitalize the layer name
A convenience function to capitalize and format a layer ``name``.

.. versionadded:: 3.0
:rtype: str
%End

Expand Down
27 changes: 16 additions & 11 deletions src/app/qgisapp.cpp
Expand Up @@ -9620,8 +9620,6 @@ void QgisApp::showOptionsDialog( QWidget *parent, const QString &currentPage )
QgsSettings mySettings;
QString oldScales = mySettings.value( QStringLiteral( "Map/scales" ), PROJECT_SCALES ).toString();

bool oldCapitalize = mySettings.value( QStringLiteral( "qgis/capitalizeLayerName" ), QVariant( false ) ).toBool();

QList< QgsOptionsWidgetFactory * > factories;
Q_FOREACH ( const QPointer< QgsOptionsWidgetFactory > &f, mOptionsWidgetFactories )
{
Expand All @@ -9646,13 +9644,6 @@ void QgisApp::showOptionsDialog( QWidget *parent, const QString &currentPage )
applyDefaultSettingsToCanvas( canvas );
}

if ( oldCapitalize != mySettings.value( QStringLiteral( "qgis/capitalizeLayerName" ), QVariant( false ) ).toBool() )
{
// if the layer capitalization has changed, we need to update all layer names
Q_FOREACH ( QgsMapLayer *layer, QgsProject::instance()->mapLayers() )
layer->setName( layer->originalName() );
}

//update any open compositions so they reflect new composer settings
//we have to push the changes to the compositions here, because compositions
//have no access to qgisapp and accordingly can't listen in to changes
Expand Down Expand Up @@ -9944,7 +9935,7 @@ void QgisApp::reloadConnections()
}


QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const QString &baseName, const QString &providerKey )
QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const QString &name, const QString &providerKey )
{
bool wasfrozen = mMapCanvas->isFrozen();

Expand All @@ -9953,6 +9944,13 @@ QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const Q
// Let render() do its own cursor management
// QApplication::setOverrideCursor(Qt::WaitCursor);

QString baseName = name;
QgsSettings settings;
if ( settings.value( QStringLiteral( "qgis/capitalizeLayerName" ), QVariant( false ) ).toBool() )
{
baseName = QgsMapLayer::formatLayerName( baseName );
}

/* Eliminate the need to instantiate the layer based on provider type.
The caller is responsible for cobbling together the needed information to
open the layer
Expand Down Expand Up @@ -11849,7 +11847,7 @@ bool QgisApp::addRasterLayer( QgsRasterLayer *rasterLayer )
// this method is a blend of addRasterLayer() functions (with and without provider)
// and addRasterLayers()
QgsRasterLayer *QgisApp::addRasterLayerPrivate(
const QString &uri, const QString &baseName, const QString &providerKey,
const QString &uri, const QString &name, const QString &providerKey,
bool guiWarning, bool guiUpdate )
{
if ( guiUpdate )
Expand All @@ -11859,6 +11857,13 @@ QgsRasterLayer *QgisApp::addRasterLayerPrivate(
freezeCanvases();
}

QString baseName = name;
QgsSettings settings;
if ( settings.value( QStringLiteral( "qgis/capitalizeLayerName" ), QVariant( false ) ).toBool() )
{
baseName = QgsMapLayer::formatLayerName( baseName );
}

QgsDebugMsg( "Creating new raster layer using " + uri
+ " with baseName of " + baseName );

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsrasterlayerproperties.cpp
Expand Up @@ -1051,7 +1051,7 @@ void QgsRasterLayerProperties::apply()

void QgsRasterLayerProperties::mLayerOrigNameLineEd_textEdited( const QString &text )
{
leDisplayName->setText( mRasterLayer->capitalizeLayerName( text ) );
leDisplayName->setText( mRasterLayer->formatLayerName( text ) );
}

void QgsRasterLayerProperties::buttonBuildPyramids_clicked()
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -804,7 +804,7 @@ QString QgsVectorLayerProperties::htmlMetadata()

void QgsVectorLayerProperties::mLayerOrigNameLineEdit_textEdited( const QString &text )
{
txtDisplayName->setText( mLayer->capitalizeLayerName( text ) );
txtDisplayName->setText( mLayer->formatLayerName( text ) );
}

void QgsVectorLayerProperties::mCrsSelector_crsChanged( const QgsCoordinateReferenceSystem &crs )
Expand Down
24 changes: 10 additions & 14 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -49,7 +49,7 @@
#include "qgsvectordataprovider.h"
#include "qgsxmlutils.h"
#include "qgssettings.h" // TODO: get rid of it [MD]

#include "qgsstringutils.h"

QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
const QString &lyrname,
Expand All @@ -60,7 +60,7 @@ QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
, mStyleManager( new QgsMapLayerStyleManager( this ) )
{
// Set the display name = internal name
mLayerName = capitalizeLayerName( mLayerOrigName );
mLayerName = lyrname;

//mShortName.replace( QRegExp( "[\\W]" ), "_" );

Expand Down Expand Up @@ -137,12 +137,11 @@ QString QgsMapLayer::id() const

void QgsMapLayer::setName( const QString &name )
{
QString newName = capitalizeLayerName( name );
if ( name == mLayerOrigName && newName == mLayerName )
if ( name == mLayerOrigName && name == mLayerName )
return;

mLayerOrigName = name; // store the new original name
mLayerName = newName;
mLayerOrigName = name;
mLayerName = name;

emit nameChanged();
}
Expand Down Expand Up @@ -1027,17 +1026,14 @@ void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSign
emit crsChanged();
}

QString QgsMapLayer::capitalizeLayerName( const QString &name )
QString QgsMapLayer::formatLayerName( const QString &name )
{
// Capitalize the first letter of the layer name if requested
QgsSettings settings;
bool capitalizeLayerName =
settings.value( QStringLiteral( "qgis/capitalizeLayerName" ), QVariant( false ) ).toBool();

QString layerName( name );

if ( capitalizeLayerName && !layerName.isEmpty() )
layerName = layerName.at( 0 ).toUpper() + layerName.mid( 1 );
if ( !layerName.isEmpty() )
layerName = QgsStringUtils::capitalize( name, QgsStringUtils::ForceFirstLetterToCapital );

layerName.replace( '_', ' ' );

return layerName;
}
Expand Down
8 changes: 6 additions & 2 deletions src/core/qgsmaplayer.h
Expand Up @@ -524,8 +524,12 @@ class CORE_EXPORT QgsMapLayer : public QObject
//! Sets layer's spatial reference system
void setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal = true );

//! A convenience function to (un)capitalize the layer name
static QString capitalizeLayerName( const QString &name );
/**
* A convenience function to capitalize and format a layer \a name.
*
* \since QGIS 3.0
*/
static QString formatLayerName( const QString &name );

/**
* Retrieve the style URI for this layer
Expand Down
3 changes: 1 addition & 2 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -1475,8 +1475,7 @@ void QgsVectorLayer::setDataSource( const QString &dataSource, const QString &ba
QgsWkbTypes::GeometryType geomType = mValid && mDataProvider ? geometryType() : QgsWkbTypes::UnknownGeometry;

mDataSource = dataSource;
mLayerName = capitalizeLayerName( baseName );
setName( mLayerName );
setName( baseName );
setDataProvider( provider );

if ( !mValid )
Expand Down

0 comments on commit 1e4f691

Please sign in to comment.