|
20 | 20 |
|
21 | 21 | #include <QObject>
|
22 | 22 | #include <QMap>
|
23 |
| -#include <QAbstractItemModel> |
24 | 23 | #include <QFuture>
|
25 | 24 |
|
26 | 25 | /**
|
27 | 26 | * \ingroup core
|
28 | 27 | * \class QgsTask
|
29 |
| - * \brief Interface class for long running background tasks. Tasks can be controlled directly, |
| 28 | + * \brief Abstract base class for long running background tasks. Tasks can be controlled directly, |
30 | 29 | * or added to a QgsTaskManager for automatic management.
|
| 30 | + * |
| 31 | + * Derived classes should implement the process they want to execute in the background |
| 32 | + * within the run() method. This method will be called when the |
| 33 | + * task commences (ie via calling start() ). |
| 34 | + * |
| 35 | + * Long running tasks should periodically check the isCancelled() flag to detect if the task |
| 36 | + * has been cancelled via some external event. If this flag is true then the task should |
| 37 | + * clean up and terminate at the earliest possible convenience. |
| 38 | + * |
31 | 39 | * \note Added in version 3.0
|
32 | 40 | */
|
33 | 41 | class CORE_EXPORT QgsTask : public QObject
|
@@ -55,106 +63,158 @@ class CORE_EXPORT QgsTask : public QObject
|
55 | 63 | };
|
56 | 64 | Q_DECLARE_FLAGS( Flags, Flag )
|
57 | 65 |
|
58 |
| - /** Constructor for QgsTask. |
| 66 | + /** |
| 67 | + * Constructor for QgsTask. |
59 | 68 | * @param description text description of task
|
60 | 69 | * @param flags task flags
|
61 | 70 | */
|
62 | 71 | QgsTask( const QString& description = QString(), const Flags& flags = AllFlags );
|
63 | 72 |
|
64 |
| - //! Returns the flags associated with the task. |
| 73 | + /** |
| 74 | + * Returns the flags associated with the task. |
| 75 | + */ |
65 | 76 | Flags flags() const { return mFlags; }
|
66 | 77 |
|
67 |
| - //! Returns true if the task can be cancelled. |
| 78 | + /** |
| 79 | + * Returns true if the task can be cancelled. |
| 80 | + */ |
68 | 81 | bool canCancel() const { return mFlags & CanCancel; }
|
69 | 82 |
|
70 |
| - //! Returns true if the task is active, ie it is not complete and has |
71 |
| - //! not been cancelled. |
| 83 | + /** |
| 84 | + * Returns true if the task is active, ie it is not complete and has |
| 85 | + * not been cancelled. |
| 86 | + */ |
72 | 87 | bool isActive() const { return mStatus == Running; }
|
73 | 88 |
|
74 |
| - //! Returns the current task status. |
| 89 | + /** |
| 90 | + * Returns the current task status. |
| 91 | + */ |
75 | 92 | TaskStatus status() const { return mStatus; }
|
76 | 93 |
|
77 |
| - //! Returns the task's description. |
| 94 | + /** |
| 95 | + * Returns the task's description. |
| 96 | + */ |
78 | 97 | QString description() const { return mDescription; }
|
79 | 98 |
|
80 |
| - //! Returns the task's progress (between 0.0 and 100.0) |
| 99 | + /** |
| 100 | + * Returns the task's progress (between 0.0 and 100.0) |
| 101 | + */ |
81 | 102 | double progress() const { return mProgress; }
|
82 | 103 |
|
83 |
| - public slots: |
84 |
| - |
85 |
| - //! Starts the task. |
| 104 | + /** |
| 105 | + * Starts the task. Should only be called for tasks which are not being |
| 106 | + * handled by a QgsTaskManager. If the task is managed by a QgsTaskManager |
| 107 | + * then this method should not be called directly, instead it is left to the |
| 108 | + * task manager to start the task when appropriate. |
| 109 | + */ |
86 | 110 | void start();
|
87 | 111 |
|
88 |
| - //! Notifies the task that it should terminate. |
89 |
| - //! @see isCancelled() |
| 112 | + /** |
| 113 | + * Notifies the task that it should terminate. Calling this is not gauranteed |
| 114 | + * to immediately end the task, rather it sets the isCancelled() flag which |
| 115 | + * task subclasses can check and terminate their operations at an appropriate |
| 116 | + * time. |
| 117 | + * @see isCancelled() |
| 118 | + */ |
90 | 119 | void cancel();
|
91 | 120 |
|
92 |
| - //! Called when the task is placed on hold. If the task in not queued |
93 |
| - //! (ie it is running or has finished) then calling this has no effect. |
94 |
| - //! @see unhold() |
| 121 | + /** |
| 122 | + * Places the task on hold. If the task in not queued |
| 123 | + * (ie it is already running or has finished) then calling this has no effect. |
| 124 | + * Calling this method only has an effect for tasks which are managed |
| 125 | + * by a QgsTaskManager. |
| 126 | + * @see unhold() |
| 127 | + */ |
95 | 128 | void hold();
|
96 | 129 |
|
97 |
| - //! Called when the task should be unheld and re-added to the queue. If the |
98 |
| - //! task in not currently being held then calling this has no effect. |
99 |
| - //! @see unhold() |
| 130 | + /** |
| 131 | + * Releases the task from being held. For tasks managed by a QgsTaskManager |
| 132 | + * calling this will re-add them to the queue. If the |
| 133 | + * task in not currently being held then calling this has no effect. |
| 134 | + * @see hold() |
| 135 | + */ |
100 | 136 | void unhold();
|
101 | 137 |
|
102 |
| - //! Sets the task's current progress. If task reports the CanReportProgress flag then |
103 |
| - //! the derived class should call this method whenever the task wants to update its |
104 |
| - //! progress. Calling will automatically emit the progressChanged signal. |
105 |
| - //! @param progress percent of progress, from 0.0 - 100.0 |
106 |
| - void setProgress( double progress ); |
107 |
| - |
108 |
| - //! Sets the task as completed. Should be called when the task is complete. |
109 |
| - //! Calling will automatically emit the statusChanged and taskCompleted signals. |
110 |
| - void completed(); |
111 |
| - |
112 |
| - //! Sets the task as stopped. Should be called whenever the task ends for any |
113 |
| - //! reason other than successful completion. |
114 |
| - //! Calling will automatically emit the statusChanged and taskStopped signals. |
115 |
| - void stopped(); |
116 |
| - |
117 | 138 | signals:
|
118 | 139 |
|
119 |
| - //! Will be emitted by task when its progress changes |
120 |
| - //! @param progress percent of progress, from 0.0 - 100.0 |
121 |
| - //! @note derived classes should not emit this signal directly, instead they should call |
122 |
| - //! setProgress() |
| 140 | + /** |
| 141 | + * Will be emitted by task when its progress changes. |
| 142 | + * @param progress percent of progress, from 0.0 - 100.0 |
| 143 | + * @note derived classes should not emit this signal directly, instead they should call |
| 144 | + * setProgress() |
| 145 | + */ |
123 | 146 | void progressChanged( double progress );
|
124 | 147 |
|
125 |
| - //! Will be emitted by task when its status changes |
126 |
| - //! @param status new task status |
127 |
| - //! @note derived classes should not emit this signal directly, instead they should call |
128 |
| - //! completed() or stopped() |
| 148 | + /** |
| 149 | + * Will be emitted by task when its status changes. |
| 150 | + * @param status new task status |
| 151 | + * @note derived classes should not emit this signal directly, instead they should call |
| 152 | + * completed() or stopped() |
| 153 | + */ |
129 | 154 | void statusChanged( int status );
|
130 | 155 |
|
131 |
| - //! Will be emitted by task to indicate its commencement. |
132 |
| - //! @note derived classes should not emit this signal directly, it will automatically |
133 |
| - //! be emitted when the task begins |
| 156 | + /** |
| 157 | + * Will be emitted by task to indicate its commencement. |
| 158 | + * @note derived classes should not emit this signal directly, it will automatically |
| 159 | + * be emitted when the task begins |
| 160 | + */ |
134 | 161 | void begun();
|
135 | 162 |
|
136 |
| - //! Will be emitted by task to indicate its completion. |
137 |
| - //! @note derived classes should not emit this signal directly, instead they should call |
138 |
| - //! completed() |
| 163 | + /** |
| 164 | + * Will be emitted by task to indicate its successful completion. |
| 165 | + * @note derived classes should not emit this signal directly, instead they should call |
| 166 | + * completed() |
| 167 | + */ |
139 | 168 | void taskCompleted();
|
140 | 169 |
|
141 |
| - //! Will be emitted by task if it has terminated for any reason |
142 |
| - //! other then completion. |
143 |
| - //! @note derived classes should not emit this signal directly, instead they should call |
144 |
| - //! stopped()//! |
| 170 | + /** |
| 171 | + * Will be emitted by task if it has terminated for any reason |
| 172 | + * other then completion (eg when a task has been cancelled or encountered |
| 173 | + * an internal error). |
| 174 | + * @note derived classes should not emit this signal directly, instead they should call |
| 175 | + * stopped() |
| 176 | + */ |
145 | 177 | void taskStopped();
|
146 | 178 |
|
147 | 179 | protected:
|
148 | 180 |
|
149 |
| - //! Derived tasks must implement a run() method. This method will be called when the |
150 |
| - //! task commences (ie via calling start() ). |
| 181 | + /** |
| 182 | + * Performs the task's operation. This method will be called when the task commences |
| 183 | + * (ie via calling start() ), and subclasses should implement the operation they |
| 184 | + * wish to perform in the background within this method. |
| 185 | + */ |
151 | 186 | virtual void run() = 0;
|
152 | 187 |
|
153 |
| - //! Will return true if task should terminate ASAP. If the task reports the CanCancel |
154 |
| - //! flag, then derived classes' run() methods should periodically check this and |
155 |
| - //! terminate in a safe manner. |
| 188 | + /** |
| 189 | + * Will return true if task should terminate ASAP. If the task reports the CanCancel |
| 190 | + * flag, then derived classes' run() methods should periodically check this and |
| 191 | + * terminate in a safe manner. |
| 192 | + */ |
156 | 193 | bool isCancelled() const { return mShouldTerminate; }
|
157 | 194 |
|
| 195 | + /** |
| 196 | + * Sets the task as completed. Should be called when the task is complete. |
| 197 | + * Calling will automatically emit the statusChanged and taskCompleted signals. |
| 198 | + */ |
| 199 | + void completed(); |
| 200 | + |
| 201 | + /** |
| 202 | + * Sets the task as stopped. Should be called whenever the task ends for any |
| 203 | + * reason other than successful completion. |
| 204 | + * Calling will automatically emit the statusChanged and taskStopped signals. |
| 205 | + */ |
| 206 | + void stopped(); |
| 207 | + |
| 208 | + protected slots: |
| 209 | + |
| 210 | + /** |
| 211 | + * Sets the task's current progress. If task reports the CanReportProgress flag then |
| 212 | + * the derived class should call this method whenever the task wants to update its |
| 213 | + * progress. Calling will automatically emit the progressChanged signal. |
| 214 | + * @param progress percent of progress, from 0.0 - 100.0 |
| 215 | + */ |
| 216 | + void setProgress( double progress ); |
| 217 | + |
158 | 218 | private:
|
159 | 219 |
|
160 | 220 | Flags mFlags;
|
|
0 commit comments