Skip to content
Permalink
Browse files
Added support for suppressing bad layers handler on startup. (#46539)
* Added support for suppressing bad layers handler on startup. Shout out to Giuseppe Baiamonte on the QGIS Open Day 17 Dec 2021
* Apply fixes based on @mkuhn's review (thanks)
  • Loading branch information
timlinux committed Dec 18, 2021
1 parent 1564902 commit 4c3f56a91ff92c61c9a9bf5736c4968cb3e0aa73
Showing with 26 additions and 7 deletions.
  1. +8 −1 src/app/main.cpp
  2. +14 −5 src/app/qgisapp.cpp
  3. +4 −1 src/app/qgisapp.h
@@ -148,6 +148,7 @@ void usage( const QString &appName )
<< QStringLiteral( "\t[--nologo]\thide splash screen\n" )
<< QStringLiteral( "\t[--noversioncheck]\tdon't check for new version of QGIS at startup\n" )
<< QStringLiteral( "\t[--noplugins]\tdon't restore plugins on startup\n" )
<< QStringLiteral( "\t[--skipbadlayers]\tdon't prompt for missing layers\n" )
<< QStringLiteral( "\t[--nocustomization]\tdon't apply GUI customization\n" )
<< QStringLiteral( "\t[--customizationfile path]\tuse the given ini file as GUI customization\n" )
<< QStringLiteral( "\t[--globalsettingsfile path]\tuse the given ini file as Global Settings (defaults)\n" )
@@ -575,6 +576,7 @@ int main( int argc, char *argv[] )

bool myRestoreDefaultWindowState = false;
bool myRestorePlugins = true;
bool mySkipBadLayers = false;
bool myCustomization = true;

QString dxfOutputFile;
@@ -663,6 +665,11 @@ int main( int argc, char *argv[] )
{
myRestorePlugins = false;
}
else if ( arg == QLatin1String( "--skipbadlayers" ) || arg == QLatin1String( "-B" ) )
{
QgsDebugMsg( QStringLiteral( "Skipping bad layers" ) );
mySkipBadLayers = true;
}
else if ( arg == QLatin1String( "--nocustomization" ) || arg == QLatin1String( "-C" ) )
{
myCustomization = false;
@@ -1381,7 +1388,7 @@ int main( int argc, char *argv[] )
// this should be done in QgsApplication::init() but it doesn't know the settings dir.
QgsApplication::setMaxThreads( settings.value( QStringLiteral( "qgis/max_threads" ), -1 ).toInt() );

QgisApp *qgis = new QgisApp( mypSplash, myRestorePlugins, mySkipVersionCheck, rootProfileFolder, profileName ); // "QgisApp" used to find canonical instance
QgisApp *qgis = new QgisApp( mypSplash, myRestorePlugins, mySkipBadLayers, mySkipVersionCheck, rootProfileFolder, profileName ); // "QgisApp" used to find canonical instance
qgis->setObjectName( QStringLiteral( "QgisApp" ) );

myApp.connect(
@@ -926,7 +926,7 @@ static bool cmpByText_( QAction *a, QAction *b )
QgisApp *QgisApp::sInstance = nullptr;

// constructor starts here
QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCheck, const QString &rootProfileLocation, const QString &activeProfile, QWidget *parent, Qt::WindowFlags fl )
QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipBadLayers, bool skipVersionCheck, const QString &rootProfileLocation, const QString &activeProfile, QWidget *parent, Qt::WindowFlags fl )
: QMainWindow( parent, fl )
, mSplash( splash )
{
@@ -938,7 +938,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
tr( "Multiple instances of QGIS application object detected.\nPlease contact the developers.\n" ) );
abort();
}

mSkipBadLayers = skipBadLayers;
sInstance = this;
QgsRuntimeProfiler *profiler = QgsApplication::profiler();

@@ -1484,8 +1484,12 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
QgsApplication::dataItemProviderRegistry()->addProvider( new QgsHtmlDataItemProvider() );

// set handler for missing layers (will be owned by QgsProject)
mAppBadLayersHandler = new QgsHandleBadLayersHandler();
QgsProject::instance()->setBadLayerHandler( mAppBadLayersHandler );
if ( !mSkipBadLayers )
{
QgsDebugMsg( QStringLiteral( "NOT creating bad layers handler" ) );
mAppBadLayersHandler = new QgsHandleBadLayersHandler();
QgsProject::instance()->setBadLayerHandler( mAppBadLayersHandler );
}

mSplash->showMessage( tr( "Starting Python" ), Qt::AlignHCenter | Qt::AlignBottom, splashTextColor );
qApp->processEvents();
@@ -7020,8 +7024,13 @@ bool QgisApp::addProject( const QString &projectFile )
bool returnCode = false;
std::unique_ptr< QgsProjectDirtyBlocker > dirtyBlocker = std::make_unique< QgsProjectDirtyBlocker >( QgsProject::instance() );
QObject connectionScope; // manually control scope of layersChanged lambda connection - we need the connection automatically destroyed when this function finishes

bool badLayersHandled = false;
connect( mAppBadLayersHandler, &QgsHandleBadLayersHandler::layersChanged, &connectionScope, [&badLayersHandled] { badLayersHandled = true; } );
if ( !mSkipBadLayers )
{
QgsDebugMsg( QStringLiteral( "NOT Skipping bad layers" ) );
connect( mAppBadLayersHandler, &QgsHandleBadLayersHandler::layersChanged, &connectionScope, [&badLayersHandled] { badLayersHandled = true; } );
}

// close the previous opened project if any
closeProject();
@@ -202,7 +202,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
Q_OBJECT
public:
//! Constructor
QgisApp( QSplashScreen *splash, bool restorePlugins = true,
QgisApp( QSplashScreen *splash, bool restorePlugins = true, bool skipBadLayers = false,
bool skipVersionCheck = false, const QString &rootProfileLocation = QString(),
const QString &activeProfile = QString(),
QWidget *parent = nullptr, Qt::WindowFlags fl = Qt::Window );
@@ -214,6 +214,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QgisApp( QgisApp const & ) = delete;
QgisApp &operator=( QgisApp const & ) = delete;


/**
* Returns and adjusted uri for the layer based on current and available CRS as well as the last selected image format
* \since QGIS 2.8
@@ -2072,6 +2073,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void activeLayerChanged( QgsMapLayer *layer );

private:
//Flag to allow user to bypass badlayer checks.
bool mSkipBadLayers;
void createPreviewImage( const QString &path, const QIcon &overlayIcon = QIcon() );
void startProfile( const QString &name );
void endProfile();

0 comments on commit 4c3f56a

Please sign in to comment.