Navigation Menu

Skip to content

Commit

Permalink
Add an optional flag to QgsTask to indicate that the task can be canc…
Browse files Browse the repository at this point in the history
…eled

without any user facing prompts

Useful for non-essential tasks

(cherry picked from commit c0f0a49)
  • Loading branch information
nyalldawson authored and nirvn committed Apr 20, 2020
1 parent d372a8d commit 468f481
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgstaskmanager.sip.in
Expand Up @@ -47,6 +47,7 @@ clean up and terminate at the earliest possible convenience.
enum Flag
{
CanCancel,
CancelWithoutPrompt,
AllFlags,
};
typedef QFlags<QgsTask::Flag> Flags;
Expand Down
25 changes: 18 additions & 7 deletions src/app/qgisapp.cpp
Expand Up @@ -5655,20 +5655,31 @@ void QgisApp::fileExit()
if ( QgsApplication::taskManager()->countActiveTasks() > 0 )
{
QStringList tasks;
const auto constActiveTasks = QgsApplication::taskManager()->activeTasks();
for ( QgsTask *task : constActiveTasks )
const QList< QgsTask * > activeTasks = QgsApplication::taskManager()->activeTasks();
for ( QgsTask *task : activeTasks )
{
if ( task->flags() & QgsTask::CancelWithoutPrompt )
continue;

tasks << tr( " • %1" ).arg( task->description() );
}

// active tasks
if ( QMessageBox::question( this, tr( "Active Tasks" ),
tr( "The following tasks are currently running in the background:\n\n%1\n\nDo you want to try canceling these active tasks?" ).arg( tasks.join( QStringLiteral( "\n" ) ) ),
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )
// prompt if any tasks which require user confirmation remain, otherwise just cancel them directly and continue with shutdown.
if ( tasks.empty() )
{
// all tasks can be silently terminated without warning
QgsApplication::taskManager()->cancelAll();
}
return;
else
{
if ( QMessageBox::question( this, tr( "Active Tasks" ),
tr( "The following tasks are currently running in the background:\n\n%1\n\nDo you want to try canceling these active tasks?" ).arg( tasks.join( QStringLiteral( "\n" ) ) ),
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )
{
QgsApplication::taskManager()->cancelAll();
}
return;
}
}

QgsCanvasRefreshBlocker refreshBlocker;
Expand Down
1 change: 1 addition & 0 deletions src/core/qgstaskmanager.h
Expand Up @@ -70,6 +70,7 @@ class CORE_EXPORT QgsTask : public QObject
enum Flag
{
CanCancel = 1 << 1, //!< Task can be canceled
CancelWithoutPrompt = 1 << 2, //!< Task can be canceled without any users prompts, e.g. when closing a project or QGIS.
AllFlags = CanCancel, //!< Task supports all flags
};
Q_DECLARE_FLAGS( Flags, Flag )
Expand Down

0 comments on commit 468f481

Please sign in to comment.