Skip to content

Commit

Permalink
Added support for suppressing bad layers handler on startup. (#46539)
Browse files Browse the repository at this point in the history
* 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 4c3f56a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/app/main.cpp
Expand Up @@ -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" )
Expand Down Expand Up @@ -575,6 +576,7 @@ int main( int argc, char *argv[] )

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

QString dxfOutputFile;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
19 changes: 14 additions & 5 deletions src/app/qgisapp.cpp
Expand Up @@ -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 )
{
Expand All @@ -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();

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion src/app/qgisapp.h
Expand Up @@ -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 );
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 4c3f56a

Please sign in to comment.