Scribus
Open source desktop publishing at your fingertips
DeferredTask Class Referenceabstract

DeferredTask is an abstraction of a generally long-running operation that is done in small steps under the control of the event loop. It is an abstract class that provides a common interface for such tasks, but cannot be used directly. More...

#include <deferredtask.h>

Inheritance diagram for DeferredTask:
Collaboration diagram for DeferredTask:

Public Slots

virtual void start ()
 Starts the timer and begins processing. If you override this you must call DeferredTask::start() at the end of your start() implementation. If something goes wrong, emit aborted();.
 
void runUntilFinished ()
 Run continuously without returning control to the event loop, until either aborted(bool) or finished() are emitted. Note that EVENTS ARE NOT PROCESSED, including deleteLater() and posted events. It is not recommended that you use this method.
 
virtual void cancel ()
 Abort the search and emit aborted(true). This is treated as an abort by the user. If you reimplement this you must call DeferredTask::cancel() at the end of your implementation. Do not emit aborted(bool) yourself from your cancel() implementation, let DeferredTask::cancel() do that.
 

Signals

void aborted (bool)
 Emitted if the processing work was aborted before completion. The signal argument is true if processing was aborted by direct or indirect user action, and false if it was aborted for any other reason (such as an internal error).
 
void finished ()
 Emitted when processing is complete, from DeferredTask::done(). You can also emit your own custom signal if you want to convey more information, so long as you do so AFTER calling done().
 
void progress (int)
 Emitted to inform anybody interested of the approximate level of progress made in the processing task. The argument is a positive integer percentage from 0 to 100 inclusive. Subclasses are not required to emit this signal, but it's a good idea if the user is likely to care about what your class does.
 

Public Member Functions

virtual ~DeferredTask ()
 If you override the destructor, call DeferredTask::cleanup() at the end of your dtor.
 
bool isFinished () const
 Returns true iff processing has finished. Note that this will return false both while processing, and before start() has been called.
 
virtual const QString & lastError () const
 Return a human-readable version of the last error message. Unless overridden, returns the value of m_lastError. Value is undefined until either finished() or aborted(bool) have been emitted.
 

Protected Slots

virtual void next ()=0
 Do the next small chunk of processing, then return. You must implement this method to do whatever processing work you need to. It will be called repeatedly until it emits either aborted(bool) or finished() to indicate that it's done.
 

Protected Member Functions

 DeferredTask (QObject *parent)
 You must provide a new, public, constructor. It should set up the class but not begin processing. You must call DeferredTask::init() from your ctor, preferably early on.
 
void init ()
 Do some setup of the DeferredTask. Must be called from all subclass ctors.
 
void cleanup ()
 Do some cleanup before deletion. Must be called from all subclass dtors.
 
void done ()
 Indicate that processing has finished. You must call this before emitting finished(), since it sets up some state that must happen before any slots connected to finished() are called, and the ordering of slot calls from a signal can not be guaranteed.
 

Protected Attributes

QString m_lastError
 Human readable, translated version of last error encountered.
 

Detailed Description

DeferredTask is an abstraction of a generally long-running operation that is done in small steps under the control of the event loop. It is an abstract class that provides a common interface for such tasks, but cannot be used directly.

To use it, subclass it and add the private methods and members you need to keep track of your task. FileSearch uses this interface and might be worth looking at. You can change the constructor arguments around as much as you like, but should maintain the rest of the interface.

When you instantiate a DeferredTask subclass, it should do any general setup it requires but not begin processing. The start() slot will be called to trigger the beginning of processing. next() will then be called repeatedly by an idle timer to do small chunks of processing work. When the class emits finished() or aborted(bool), the timer will stop and next() will not be called again.

A DeferredTask is single use. It will be automatically deleted if its parent is deleted (and it hasn't yet been deleted). Otherwise, delete it when you're done with it. You should not delete it without cancelling it first.


The documentation for this class was generated from the following files: