Skip to content

Commit e29dd79

Browse files
committedDec 5, 2016
Api + test cleanups
1 parent 6021d78 commit e29dd79

File tree

7 files changed

+101
-32
lines changed

7 files changed

+101
-32
lines changed
 

‎python/core/qgstaskmanager.sip

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,36 @@ class QgsTask : QObject
2020
Terminated, /*!< Task was terminated or errored */
2121
};
2222

23+
//! Task flags
24+
enum Flag
25+
{
26+
CancelSupport, //!< Task can be cancelled
27+
ProgressReport, //!< Task will report its progress
28+
AllFlags, //!< Task supports all flags
29+
};
30+
typedef QFlags<QgsTask::Flag> Flags;
31+
2332
/** Constructor for QgsTask.
2433
* @param description text description of task
34+
* @param flags task flags
2535
*/
26-
QgsTask( const QString& description = QString() );
36+
QgsTask( const QString& description = QString(), const Flags& flags = AllFlags );
37+
38+
//! Returns the flags associated with the task.
39+
Flags flags() const;
2740

2841
//! Starts the task.
2942
void start();
3043

3144
//! Notifies the task that it should terminate.
3245
//! @see isCancelled()
33-
void terminate();
46+
void cancel();
47+
48+
//! Returns true if the task can be cancelled.
49+
bool canCancel() const;
3450

3551
//! Returns true if the task is active, ie it is not complete and has
36-
//! not been terminated.
52+
//! not been cancelled.
3753
bool isActive() const;
3854

3955
//! Returns the current task status.
@@ -104,6 +120,8 @@ class QgsTask : QObject
104120

105121
};
106122

123+
QFlags<QgsTask::Flag> operator|(QgsTask::Flag f1, QFlags<QgsTask::Flag> f2);
124+
107125
/** \ingroup core
108126
* \class QgsTaskManager
109127
* \brief Task manager for managing a set of long-running QgsTask tasks. This class can be created directly,
@@ -169,7 +187,7 @@ class QgsTaskManager : QObject
169187
long taskId( QgsTask* task ) const;
170188

171189
//! Instructs all tasks tracked by the manager to terminate.
172-
void terminateAll();
190+
void cancelAll();
173191

174192
signals:
175193

‎python/gui/qgstaskmanagerwidget.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @see QgsTaskManager
55
* @note introduced in QGIS 2.16
66
*/
7-
class QgsTaskManagerWidget : QTreeView
7+
class QgsTaskManagerWidget : QWidget
88
{
99
%TypeHeaderCode
1010
#include <qgstaskmanagerwidget.h>

‎src/core/qgstaskmanager.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
// QgsTask
2424
//
2525

26-
QgsTask::QgsTask( const QString &name )
26+
QgsTask::QgsTask( const QString &name, const Flags& flags )
2727
: QObject()
28+
, mFlags( flags )
2829
, mDescription( name )
2930
, mStatus( Queued )
3031
, mProgress( 0.0 )
@@ -39,7 +40,7 @@ void QgsTask::start()
3940
run();
4041
}
4142

42-
void QgsTask::terminate()
43+
void QgsTask::cancel()
4344
{
4445
mShouldTerminate = true;
4546
}
@@ -89,7 +90,7 @@ QgsTaskManager::QgsTaskManager( QObject* parent )
8990
QgsTaskManager::~QgsTaskManager()
9091
{
9192
//first tell all tasks to cancel
92-
terminateAll();
93+
cancelAll();
9394

9495
//then clean them up, including waiting for them to terminate
9596
QMap< long, TaskInfo >::const_iterator it = mTasks.constBegin();
@@ -166,15 +167,15 @@ long QgsTaskManager::taskId( QgsTask *task ) const
166167
return -1;
167168
}
168169

169-
void QgsTaskManager::terminateAll()
170+
void QgsTaskManager::cancelAll()
170171
{
171172
QMap< long, TaskInfo >::iterator it = mTasks.begin();
172173
for ( ; it != mTasks.end(); ++it )
173174
{
174175
QgsTask* task = it.value().task;
175176
if ( task->isActive() )
176177
{
177-
task->terminate();
178+
task->cancel();
178179
}
179180
}
180181
}
@@ -209,7 +210,7 @@ bool QgsTaskManager::cleanupAndDeleteTask( QgsTask *task )
209210
return false;
210211

211212
if ( task->isActive() )
212-
task->terminate();
213+
task->cancel();
213214

214215
// wait for task to terminate
215216
QMap< long, TaskInfo >::iterator it = mTasks.begin();

‎src/core/qgstaskmanager.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,36 @@ class CORE_EXPORT QgsTask : public QObject
4444
Terminated, /*!< Task was terminated or errored */
4545
};
4646

47+
//! Task flags
48+
enum Flag
49+
{
50+
CancelSupport = 1 << 1, //!< Task can be cancelled
51+
ProgressReport = 1 << 2, //!< Task will report its progress
52+
AllFlags = CancelSupport | ProgressReport, //!< Task supports all flags
53+
};
54+
Q_DECLARE_FLAGS( Flags, Flag )
55+
4756
/** Constructor for QgsTask.
4857
* @param description text description of task
58+
* @param flags task flags
4959
*/
50-
QgsTask( const QString& description = QString() );
60+
QgsTask( const QString& description = QString(), const Flags& flags = AllFlags );
61+
62+
//! Returns the flags associated with the task.
63+
Flags flags() const { return mFlags; }
5164

5265
//! Starts the task.
5366
void start();
5467

5568
//! Notifies the task that it should terminate.
5669
//! @see isCancelled()
57-
void terminate();
70+
void cancel();
71+
72+
//! Returns true if the task can be cancelled.
73+
bool canCancel() const { return mFlags & CancelSupport; }
5874

5975
//! Returns true if the task is active, ie it is not complete and has
60-
//! not been terminated.
76+
//! not been cancelled.
6177
bool isActive() const { return mStatus == Running; }
6278

6379
//! Returns the current task status.
@@ -128,13 +144,16 @@ class CORE_EXPORT QgsTask : public QObject
128144

129145
private:
130146

147+
Flags mFlags;
131148
QString mDescription;
132149
TaskStatus mStatus;
133150
double mProgress;
134151
bool mShouldTerminate;
135152

136153
};
137154

155+
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsTask::Flags )
156+
138157
/** \ingroup core
139158
* \class QgsTaskManager
140159
* \brief Task manager for managing a set of long-running QgsTask tasks. This class can be created directly,
@@ -200,7 +219,7 @@ class CORE_EXPORT QgsTaskManager : public QObject
200219
long taskId( QgsTask* task ) const;
201220

202221
//! Instructs all tasks tracked by the manager to terminate.
203-
void terminateAll();
222+
void cancelAll();
204223

205224
signals:
206225

‎src/gui/qgstaskmanagerwidget.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,41 @@
2020
#include "qgsapplication.h"
2121
#include <QPainter>
2222
#include <QMouseEvent>
23+
#include <QTreeView>
24+
#include <QLayout>
25+
#include <QToolBar>
26+
#include <QAction>
2327

2428
//
2529
// QgsTaskManagerWidget
2630
//
2731

2832
QgsTaskManagerWidget::QgsTaskManagerWidget( QgsTaskManager *manager, QWidget *parent )
29-
: QTreeView( parent )
33+
: QWidget( parent )
3034
{
3135
Q_ASSERT( manager );
3236

33-
setModel( new QgsTaskManagerModel( manager, this ) );
34-
35-
setItemDelegateForColumn( 1, new QgsProgressBarDelegate( this ) );
36-
setItemDelegateForColumn( 2, new QgsTaskStatusDelegate( this ) );
37-
38-
setHeaderHidden( true );
39-
setRootIsDecorated( false );
40-
setSelectionBehavior( QAbstractItemView::SelectRows );
37+
QVBoxLayout* vLayout = new QVBoxLayout();
38+
vLayout->setMargin( 0 );
39+
#if 0
40+
QToolBar* toolbar = new QToolBar();
41+
toolbar->setIconSize( QSize( 16, 16 ) );
42+
toolbar->addAction( new QAction( "test", this ) );
43+
vLayout->addWidget( toolbar );
44+
#endif
45+
mTreeView = new QTreeView();
46+
mTreeView->setModel( new QgsTaskManagerModel( manager, this ) );
47+
mTreeView->setItemDelegateForColumn( 1, new QgsProgressBarDelegate( this ) );
48+
mTreeView->setItemDelegateForColumn( 2, new QgsTaskStatusDelegate( this ) );
49+
mTreeView->setHeaderHidden( true );
50+
mTreeView->setRootIsDecorated( false );
51+
mTreeView->setSelectionBehavior( QAbstractItemView::SelectRows );
52+
vLayout->addWidget( mTreeView );
53+
54+
setLayout( vLayout );
4155
}
4256

4357

44-
45-
4658
//
4759
// QgsTaskManagerModel
4860
//
@@ -178,7 +190,7 @@ bool QgsTaskManagerModel::setData( const QModelIndex &index, const QVariant &val
178190
case Status:
179191
{
180192
if ( value.toBool() )
181-
task->terminate();
193+
task->cancel();
182194
return true;
183195
}
184196

‎src/gui/qgstaskmanagerwidget.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
#ifndef QGSTASKMANAGERWIDGET_H
1818
#define QGSTASKMANAGERWIDGET_H
1919

20-
#include <QTreeView>
2120
#include <QStyledItemDelegate>
2221

2322
class QgsTaskManager;
2423
class QgsTask;
24+
class QTreeView;
2525

2626
/** \ingroup gui
2727
* \class QgsTaskManagerWidget
2828
* A widget which displays tasks from a QgsTaskManager and allows for interaction with the manager
2929
* @see QgsTaskManager
3030
* @note introduced in QGIS 2.16
3131
*/
32-
class GUI_EXPORT QgsTaskManagerWidget : public QTreeView
32+
class GUI_EXPORT QgsTaskManagerWidget : public QWidget
3333
{
3434
Q_OBJECT
3535

@@ -41,6 +41,9 @@ class GUI_EXPORT QgsTaskManagerWidget : public QTreeView
4141
*/
4242
QgsTaskManagerWidget( QgsTaskManager* manager, QWidget* parent = nullptr );
4343

44+
private:
45+
46+
QTreeView* mTreeView;
4447
};
4548

4649

‎tests/src/core/testqgstaskmanager.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class TestTask : public QgsTask
2727
public:
2828

2929
TestTask( const QString& desc = QString() ) : QgsTask( desc ), runCalled( false ) {}
30+
TestTask( const QString& desc, const QgsTask::Flags& flags ) : QgsTask( desc, flags ), runCalled( false ) {}
3031

3132
void emitProgressChanged( double progress ) { setProgress( progress ); }
3233
void emitTaskStopped() { stopped(); }
@@ -59,7 +60,9 @@ class TestTerminationTask : public TestTask
5960

6061
void run() override
6162
{
62-
QTest::qSleep( 1000 );
63+
while ( !isCancelled() )
64+
{}
65+
stopped();
6366
}
6467
};
6568

@@ -112,6 +115,8 @@ void TestQgsTaskManager::task()
112115
QCOMPARE( task->status(), QgsTask::Queued );
113116
QCOMPARE( task->description(), QString( "desc" ) );
114117
QVERIFY( !task->isActive() );
118+
QVERIFY( task->canCancel() );
119+
QVERIFY( task->flags() & QgsTask::ProgressReport );
115120

116121
QSignalSpy startedSpy( task.data(), SIGNAL( begun() ) );
117122
QSignalSpy statusSpy( task.data(), SIGNAL( statusChanged( int ) ) );
@@ -143,6 +148,16 @@ void TestQgsTaskManager::task()
143148
QCOMPARE( completeSpy.count(), 1 );
144149
QCOMPARE( statusSpy2.count(), 1 );
145150
QCOMPARE( static_cast< QgsTask::TaskStatus >( statusSpy2.last().at( 0 ).toInt() ), QgsTask::Complete );
151+
152+
// test flags
153+
task.reset( new TestTask( "desc", QgsTask::ProgressReport ) );
154+
QVERIFY( !task->canCancel() );
155+
QVERIFY( task->flags() & QgsTask::ProgressReport );
156+
QVERIFY( !( task->flags() & QgsTask::CancelSupport ) );
157+
task.reset( new TestTask( "desc", QgsTask::CancelSupport ) );
158+
QVERIFY( task->canCancel() );
159+
QVERIFY( !( task->flags() & QgsTask::ProgressReport ) );
160+
QVERIFY( task->flags() & QgsTask::CancelSupport );
146161
}
147162

148163

@@ -237,8 +252,9 @@ void TestQgsTaskManager::taskTerminationBeforeDelete()
237252
TestTask* task = new TestTerminationTask();
238253
manager->addTask( task );
239254

240-
//SHOULD NOT BE NEEDED...
241-
task->start();
255+
// wait till task spins up
256+
while ( !task->isActive() )
257+
{}
242258

243259
// if task is not terminated assert will trip
244260
delete manager;

0 commit comments

Comments
 (0)
Please sign in to comment.