Skip to content

Commit c558d51

Browse files
committed
Prevent users from removing layers or closing projects when
running tasks depend on those layers It's not foolproof because there's many ways to remove layers not through the QGIS gui and tasks should be written to nicely handle layers being removed mid-way through execution. But it's still a good warning for users if they accidently try to load a new project and don't realise some task is still running in the background.
1 parent 35e387c commit c558d51

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/app/qgisapp.cpp

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4485,6 +4485,9 @@ void QgisApp::fileNewBlank()
44854485
//as file new but accepts flags to indicate whether we should prompt to save
44864486
void QgisApp::fileNew( bool thePromptToSaveFlag, bool forceBlank )
44874487
{
4488+
if ( checkTasksDependOnProject() )
4489+
return;
4490+
44884491
if ( thePromptToSaveFlag )
44894492
{
44904493
if ( !saveDirty() )
@@ -4594,6 +4597,9 @@ void QgisApp::fileNew( bool thePromptToSaveFlag, bool forceBlank )
45944597

45954598
bool QgisApp::fileNewFromTemplate( const QString& fileName )
45964599
{
4600+
if ( checkTasksDependOnProject() )
4601+
return false;
4602+
45974603
if ( !saveDirty() )
45984604
{
45994605
return false; //cancel pressed
@@ -4883,6 +4889,9 @@ void QgisApp::showAlignRasterTool()
48834889

48844890
void QgisApp::fileOpen()
48854891
{
4892+
if ( checkTasksDependOnProject() )
4893+
return;
4894+
48864895
// possibly save any pending work before opening a new project
48874896
if ( saveDirty() )
48884897
{
@@ -5289,6 +5298,9 @@ void QgisApp::openProject( QAction *action )
52895298
// possibly save any pending work before opening a different project
52905299
Q_ASSERT( action );
52915300

5301+
if ( checkTasksDependOnProject() )
5302+
return;
5303+
52925304
QString debugme = action->data().toString();
52935305
if ( saveDirty() )
52945306
addProject( debugme );
@@ -5317,6 +5329,9 @@ void QgisApp::runScript( const QString &filePath )
53175329
*/
53185330
void QgisApp::openProject( const QString & fileName )
53195331
{
5332+
if ( checkTasksDependOnProject() )
5333+
return;
5334+
53205335
// possibly save any pending work before opening a different project
53215336
if ( saveDirty() )
53225337
{
@@ -8388,6 +8403,26 @@ void QgisApp::removeLayer()
83888403
return;
83898404
}
83908405

8406+
QStringList activeTaskDescriptions;
8407+
Q_FOREACH ( QgsMapLayer * layer, mLayerTreeView->selectedLayers() )
8408+
{
8409+
QList< QgsTask* > tasks = QgsApplication::taskManager()->tasksDependentOnLayer( layer->id() );
8410+
if ( !tasks.isEmpty() )
8411+
{
8412+
Q_FOREACH ( QgsTask* task, tasks )
8413+
{
8414+
activeTaskDescriptions << tr( " • %1" ).arg( task->description() );
8415+
}
8416+
}
8417+
}
8418+
8419+
if ( !activeTaskDescriptions.isEmpty() )
8420+
{
8421+
QMessageBox::warning( this, tr( "Active tasks" ),
8422+
tr( "The following tasks are currently running which depend on this layer:\n\n%1\n\nPlease cancel these tasks and retry." ).arg( activeTaskDescriptions.join( "\n" ) ) );
8423+
return;
8424+
}
8425+
83918426
QList<QgsLayerTreeNode*> selectedNodes = mLayerTreeView->selectedNodes( true );
83928427

83938428
//validate selection
@@ -9542,7 +9577,34 @@ bool QgisApp::saveDirty()
95429577
mMapCanvas->freeze( false );
95439578

95449579
return answer != QMessageBox::Cancel;
9545-
} // QgisApp::saveDirty()
9580+
}
9581+
9582+
bool QgisApp::checkTasksDependOnProject()
9583+
{
9584+
QSet< QString > activeTaskDescriptions;
9585+
QMap<QString, QgsMapLayer*> layers = QgsMapLayerRegistry::instance()->mapLayers();
9586+
QMap<QString, QgsMapLayer*>::const_iterator layerIt = layers.constBegin();
9587+
9588+
for ( ; layerIt != layers.constEnd(); ++layerIt )
9589+
{
9590+
QList< QgsTask* > tasks = QgsApplication::taskManager()->tasksDependentOnLayer( layerIt.key() );
9591+
if ( !tasks.isEmpty() )
9592+
{
9593+
Q_FOREACH ( QgsTask* task, tasks )
9594+
{
9595+
activeTaskDescriptions.insert( tr( " • %1" ).arg( task->description() ) );
9596+
}
9597+
}
9598+
}
9599+
9600+
if ( !activeTaskDescriptions.isEmpty() )
9601+
{
9602+
QMessageBox::warning( this, tr( "Active tasks" ),
9603+
tr( "The following tasks are currently running which depend on layers in this project:\n\n%1\n\nPlease cancel these tasks and retry." ).arg( activeTaskDescriptions.toList().join( "\n" ) ) );
9604+
return true;
9605+
}
9606+
return false;
9607+
}
95469608

95479609
void QgisApp::closeProject()
95489610
{

src/app/qgisapp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
14731473
// void pasteTransformations();
14741474
//! check to see if file is dirty and if so, prompt the user th save it
14751475
bool saveDirty();
1476+
//! Checks for running tasks dependent on the open project
1477+
bool checkTasksDependOnProject();
14761478

14771479
/** Helper function to union several geometries together (used in function mergeSelectedFeatures)
14781480
@return empty geometry in case of error or if canceled */

0 commit comments

Comments
 (0)