Skip to content
Permalink
Browse files

Save ~150ms in project loading by storing QgsSettings instance

(cherry picked from commit eeb8346)
  • Loading branch information
elpaso authored and nyalldawson committed Dec 21, 2020
1 parent d90e81a commit 86374a12bd093f83d353b3a9f6456ae10d5e5d04
Showing with 20 additions and 23 deletions.
  1. +17 −23 src/core/qgsproject.cpp
  2. +3 −0 src/core/qgsproject.h
@@ -831,7 +831,7 @@ void QgsProject::clear()
mTrustLayerMetadata = false;
mCustomVariables.clear();
mMetadata = QgsProjectMetadata();
if ( !settings.value( QStringLiteral( "projects/anonymize_new_projects" ), false, QgsSettings::Core ).toBool() )
if ( !mSettings.value( QStringLiteral( "projects/anonymize_new_projects" ), false, QgsSettings::Core ).toBool() )
{
mMetadata.setCreationDateTime( QDateTime::currentDateTime() );
mMetadata.setAuthor( QgsApplication::userFullName() );
@@ -877,18 +877,18 @@ void QgsProject::clear()
writeEntry( QStringLiteral( "Paths" ), QStringLiteral( "/Absolute" ), false );

//copy default units to project
writeEntry( QStringLiteral( "Measurement" ), QStringLiteral( "/DistanceUnits" ), settings.value( QStringLiteral( "/qgis/measure/displayunits" ) ).toString() );
writeEntry( QStringLiteral( "Measurement" ), QStringLiteral( "/AreaUnits" ), settings.value( QStringLiteral( "/qgis/measure/areaunits" ) ).toString() );
writeEntry( QStringLiteral( "Measurement" ), QStringLiteral( "/DistanceUnits" ), mSettings.value( QStringLiteral( "/qgis/measure/displayunits" ) ).toString() );
writeEntry( QStringLiteral( "Measurement" ), QStringLiteral( "/AreaUnits" ), mSettings.value( QStringLiteral( "/qgis/measure/areaunits" ) ).toString() );

int red = settings.value( QStringLiteral( "qgis/default_canvas_color_red" ), 255 ).toInt();
int green = settings.value( QStringLiteral( "qgis/default_canvas_color_green" ), 255 ).toInt();
int blue = settings.value( QStringLiteral( "qgis/default_canvas_color_blue" ), 255 ).toInt();
int red = mSettings.value( QStringLiteral( "qgis/default_canvas_color_red" ), 255 ).toInt();
int green = mSettings.value( QStringLiteral( "qgis/default_canvas_color_green" ), 255 ).toInt();
int blue = mSettings.value( QStringLiteral( "qgis/default_canvas_color_blue" ), 255 ).toInt();
setBackgroundColor( QColor( red, green, blue ) );

red = settings.value( QStringLiteral( "qgis/default_selection_color_red" ), 255 ).toInt();
green = settings.value( QStringLiteral( "qgis/default_selection_color_green" ), 255 ).toInt();
blue = settings.value( QStringLiteral( "qgis/default_selection_color_blue" ), 0 ).toInt();
int alpha = settings.value( QStringLiteral( "qgis/default_selection_color_alpha" ), 255 ).toInt();
red = mSettings.value( QStringLiteral( "qgis/default_selection_color_red" ), 255 ).toInt();
green = mSettings.value( QStringLiteral( "qgis/default_selection_color_green" ), 255 ).toInt();
blue = mSettings.value( QStringLiteral( "qgis/default_selection_color_blue" ), 0 ).toInt();
int alpha = mSettings.value( QStringLiteral( "qgis/default_selection_color_alpha" ), 255 ).toInt();
setSelectionColor( QColor( red, green, blue, alpha ) );

mSnappingConfig.clearIndividualLayerSettings();
@@ -1330,9 +1330,7 @@ bool QgsProject::readProjectFile( const QString &filename, QgsProject::ReadFlags
QgsApplication::profiler()->clear( QStringLiteral( "projectload" ) );
QgsScopedRuntimeProfile profile( tr( "Setting up translations" ), QStringLiteral( "projectload" ) );

QgsSettings settings;

QString localeFileName = QStringLiteral( "%1_%2" ).arg( QFileInfo( projectFile.fileName() ).baseName(), settings.value( QStringLiteral( "locale/userLocale" ), QString() ).toString() );
QString localeFileName = QStringLiteral( "%1_%2" ).arg( QFileInfo( projectFile.fileName() ).baseName(), mSettings.value( QStringLiteral( "locale/userLocale" ), QString() ).toString() );

if ( QFile( QStringLiteral( "%1/%2.qm" ).arg( QFileInfo( projectFile.fileName() ).absolutePath(), localeFileName ) ).exists() )
{
@@ -2216,8 +2214,7 @@ bool QgsProject::writeProjectFile( const QString &filename )
qgisNode.setAttribute( QStringLiteral( "projectname" ), title() );
qgisNode.setAttribute( QStringLiteral( "version" ), Qgis::version() );

QgsSettings settings;
if ( !settings.value( QStringLiteral( "projects/anonymize_saved_projects" ), false, QgsSettings::Core ).toBool() )
if ( !mSettings.value( QStringLiteral( "projects/anonymize_saved_projects" ), false, QgsSettings::Core ).toBool() )
{
QString newSaveUser = QgsApplication::userLoginName();
QString newSaveUserFull = QgsApplication::userFullName();
@@ -3018,9 +3015,8 @@ QgsUnitTypes::DistanceUnit QgsProject::distanceUnits() const
return QgsUnitTypes::decodeDistanceUnit( distanceUnitString );

//fallback to QGIS default measurement unit
QgsSettings s;
bool ok = false;
QgsUnitTypes::DistanceUnit type = QgsUnitTypes::decodeDistanceUnit( s.value( QStringLiteral( "/qgis/measure/displayunits" ) ).toString(), &ok );
QgsUnitTypes::DistanceUnit type = QgsUnitTypes::decodeDistanceUnit( mSettings.value( QStringLiteral( "/qgis/measure/displayunits" ) ).toString(), &ok );
return ok ? type : QgsUnitTypes::DistanceMeters;
}

@@ -3036,9 +3032,8 @@ QgsUnitTypes::AreaUnit QgsProject::areaUnits() const
return QgsUnitTypes::decodeAreaUnit( areaUnitString );

//fallback to QGIS default area unit
QgsSettings s;
bool ok = false;
QgsUnitTypes::AreaUnit type = QgsUnitTypes::decodeAreaUnit( s.value( QStringLiteral( "/qgis/measure/areaunits" ) ).toString(), &ok );
QgsUnitTypes::AreaUnit type = QgsUnitTypes::decodeAreaUnit( mSettings.value( QStringLiteral( "/qgis/measure/areaunits" ) ).toString(), &ok );
return ok ? type : QgsUnitTypes::AreaSquareMeters;
}

@@ -3505,21 +3500,20 @@ QgsTransactionGroup *QgsProject::transactionGroup( const QString &providerKey, c

QgsCoordinateReferenceSystem QgsProject::defaultCrsForNewLayers() const
{
QgsSettings settings;
QgsCoordinateReferenceSystem defaultCrs;

// TODO QGIS 4.0 -- remove this method, and place it somewhere in app (where it belongs)
// in the meantime, we have a slightly hacky way to read the settings key using an enum which isn't available (since it lives in app)
if ( settings.value( QStringLiteral( "/projections/unknownCrsBehavior" ), QStringLiteral( "NoAction" ), QgsSettings::App ).toString() == QStringLiteral( "UseProjectCrs" )
|| settings.value( QStringLiteral( "/projections/unknownCrsBehavior" ), 0, QgsSettings::App ).toString() == 2 )
if ( mSettings.value( QStringLiteral( "/projections/unknownCrsBehavior" ), QStringLiteral( "NoAction" ), QgsSettings::App ).toString() == QStringLiteral( "UseProjectCrs" )
|| mSettings.value( QStringLiteral( "/projections/unknownCrsBehavior" ), 0, QgsSettings::App ).toString() == 2 )
{
// for new layers if the new layer crs method is set to either prompt or use project, then we use the project crs
defaultCrs = crs();
}
else
{
// global crs
QString layerDefaultCrs = settings.value( QStringLiteral( "/Projections/layerDefaultCrs" ), geoEpsgCrsAuthId() ).toString();
QString layerDefaultCrs = mSettings.value( QStringLiteral( "/Projections/layerDefaultCrs" ), geoEpsgCrsAuthId() ).toString();
defaultCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( layerDefaultCrs );
}

@@ -48,6 +48,7 @@
#include "qgsprojecttranslator.h"
#include "qgsattributeeditorelement.h"
#include "qgscolorscheme.h"
#include "qgssettings.h"

class QFileInfo;
class QDomDocument;
@@ -2067,6 +2068,8 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

bool mIsBeingDeleted = false;

QgsSettings mSettings;

mutable std::unique_ptr< QgsExpressionContextScope > mProjectScope;

int mBlockSnappingUpdates = 0;

0 comments on commit 86374a1

Please sign in to comment.
You can’t perform that action at this time.