Skip to content
Permalink
Browse files
Respect QgsTask::CanCancel flag in gui
  • Loading branch information
nyalldawson committed Dec 5, 2016
1 parent 466b0b9 commit 0dcff4491ed91d96dfbb0705dae14c9c4831bfab
Showing with 27 additions and 23 deletions.
  1. +6 −5 python/core/__init__.py
  2. +8 −7 src/core/qgstaskmanager.cpp
  3. +5 −11 src/gui/qgstaskmanagerwidget.cpp
  4. +8 −0 tests/src/python/test_qgstaskmanager.py
@@ -199,12 +199,12 @@ def copy_func(f):

class QgsTaskWrapper(QgsTask):

def __init__(self, description, function, on_finished, *args, **kwargs):
QgsTask.__init__(self, description)
def __init__(self, description, flags, function, on_finished, *args, **kwargs):
QgsTask.__init__(self, description, flags)
self.args = args
self.kwargs = kwargs
self.function = copy_func(function)
self.on_finished = copy_func(on_finished)
self.on_finished = copy_func(on_finished) if on_finished else None
self.returned_values = None
self.exception = None

@@ -235,7 +235,8 @@ def finished(self, result):
self.exception = ex


def fromFunction(cls, description, function, *args, on_finished=None, **kwargs):
return QgsTaskWrapper(description, function, on_finished, *args, **kwargs)
def fromFunction(cls, description, function, *args, on_finished=None, flags=QgsTask.AllFlags, **kwargs):
assert function
return QgsTaskWrapper(description, flags, function, on_finished, *args, **kwargs)

QgsTask.fromFunction = classmethod(fromFunction)
@@ -87,20 +87,22 @@ void QgsTask::run()

void QgsTask::cancel()
{
#if QT_VERSION < 0x050500
//can't cancel with qt < 5.5
return;
#else
if ( mOverallStatus == Complete || mOverallStatus == Terminated )
return;

mShouldTerminate = true;
QThreadPool::globalInstance()->cancel( this );

#if QT_VERSION >= 0x050500
//can't cancel queued tasks with qt < 5.5
QThreadPool::globalInstance()->cancel( this );
if ( mStatus == Queued || mStatus == OnHold )
{
// immediately terminate unstarted jobs
terminated();
}
else if ( mStatus == Terminated )
#endif

if ( mStatus == Terminated )
{
processSubTasksForTermination();
}
@@ -109,7 +111,6 @@ void QgsTask::cancel()
{
subTask.task->cancel();
}
#endif
}

void QgsTask::hold()
@@ -37,12 +37,6 @@ QgsTaskManagerWidget::QgsTaskManagerWidget( QgsTaskManager *manager, QWidget *pa

QVBoxLayout* vLayout = new QVBoxLayout();
vLayout->setMargin( 0 );
#if 0
QToolBar* toolbar = new QToolBar();
toolbar->setIconSize( QSize( 16, 16 ) );
toolbar->addAction( new QAction( "test", this ) );
vLayout->addWidget( toolbar );
#endif
mTreeView = new QTreeView();
mModel = new QgsTaskManagerModel( manager, this );
mTreeView->setModel( mModel );
@@ -54,7 +48,6 @@ QgsTaskManagerWidget::QgsTaskManagerWidget( QgsTaskManager *manager, QWidget *pa

vLayout->addWidget( mTreeView );


setLayout( vLayout );
}

@@ -202,10 +195,11 @@ Qt::ItemFlags QgsTaskManagerModel::flags( const QModelIndex &index ) const
return flags;
}

QgsTask* task = indexToTask( index );
if ( index.column() == Status )
{
//if ( static_cast< QgsTask::TaskStatus >( data( index, StatusRole ).toInt() ) == QgsTask::Running )
flags = flags | Qt::ItemIsEditable;
if ( task && task->canCancel() )
flags = flags | Qt::ItemIsEditable;
}
return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
@@ -225,7 +219,7 @@ bool QgsTaskManagerModel::setData( const QModelIndex &index, const QVariant &val
{
case Status:
{
if ( value.toBool() )
if ( value.toBool() && task->canCancel() )
task->cancel();
return true;
}
@@ -388,8 +382,8 @@ QgsTaskManagerFloatingWidget::QgsTaskManagerFloatingWidget( QgsTaskManager *mana
: QgsFloatingWidget( parent )
{
setLayout( new QVBoxLayout() );
setMinimumSize( 250, 150 );
QgsTaskManagerWidget* w = new QgsTaskManagerWidget( manager );
setMinimumSize( w->sizeHint() );
layout()->addWidget( w );
setStyleSheet( ".QgsTaskManagerFloatingWidget { border-top-left-radius: 8px;"
"border-top-right-radius: 8px; background-color: rgb(0, 0, 0, 70%); }" );
@@ -110,6 +110,14 @@ def testTaskFromFunction(self):
self.assertTrue(bad_task.exception)
self.assertEqual(bad_task.status(), QgsTask.Terminated)

def testTaskFromFunctionWithFlags(self):
""" test creating task from function with flags"""

task = QgsTask.fromFunction('test task', run, 20, flags=QgsTask.Flags())
self.assertFalse(task.canCancel())
task2 = QgsTask.fromFunction('test task', run, 20, flags=QgsTask.CanCancel)
self.assertTrue(task2.canCancel())

def testTaskFromFunctionWithKwargs(self):
""" test creating task from function using kwargs """

0 comments on commit 0dcff44

Please sign in to comment.