|
| 1 | +/*************************************************************************** |
| 2 | + qgsfeedback.h |
| 3 | + -------------------------------------- |
| 4 | + Date : July 2016 |
| 5 | + Copyright : (C) 2016 by Martin Dobias |
| 6 | + Email : wonder dot sk at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#ifndef QGSFEEDBACK_H |
| 17 | +#define QGSFEEDBACK_H |
| 18 | + |
| 19 | +#include <QObject> |
| 20 | + |
| 21 | +/** Base class for feedback objects to be used for cancellation of something running in a worker thread. |
| 22 | + * |
| 23 | + * The class may be used as is or it may be subclassed for extended functionality |
| 24 | + * for a particular operation (e.g. report progress or pass some data for preview). |
| 25 | + * |
| 26 | + * When cancel() is called, the internal code has two options to check for cancellation state: |
| 27 | + * - if the worker thread uses an event loop (e.g. for network communication), the code can |
| 28 | + * make a queued connection to cancelled() signal and handle the cancellation in its slot. |
| 29 | + * - if the worker thread does not use an event loop, it can poll isCancelled() method regularly |
| 30 | + * to see if the operation should be cancelled. |
| 31 | + * |
| 32 | + * The class is meant to be created and destroyed in the main thread. |
| 33 | + * |
| 34 | + * For map rendering, the object may be created in constructor of a QgsMapLayerRenderer |
| 35 | + * subclass and available with QgsMapLayerRenderer::feedback() method. When a map rendering job |
| 36 | + * gets cancelled, the cancel() method is called on the feedback object of all layers. |
| 37 | + * |
| 38 | + * @note added in QGIS 3.0 |
| 39 | + */ |
| 40 | +class CORE_EXPORT QgsFeedback : public QObject |
| 41 | +{ |
| 42 | + Q_OBJECT |
| 43 | + public: |
| 44 | + //! Construct a feedback object |
| 45 | + QgsFeedback( QObject* parent = nullptr ) |
| 46 | + : QObject( parent ) |
| 47 | + , mCancelled( false ) |
| 48 | + {} |
| 49 | + |
| 50 | + virtual ~QgsFeedback() {} |
| 51 | + |
| 52 | + //! Tells the internal routines that the current operation should be cancelled. This should be run by the main thread |
| 53 | + void cancel() |
| 54 | + { |
| 55 | + if ( mCancelled ) |
| 56 | + return; // only emit the signal once |
| 57 | + mCancelled = true; |
| 58 | + emit cancelled(); |
| 59 | + } |
| 60 | + |
| 61 | + //! Tells whether the operation has been cancelled already |
| 62 | + bool isCancelled() const { return mCancelled; } |
| 63 | + |
| 64 | + signals: |
| 65 | + //! Internal routines can connect to this signal if they use event loop |
| 66 | + void cancelled(); |
| 67 | + |
| 68 | + private: |
| 69 | + //! Whether the operation has been cancelled already. False by default. |
| 70 | + bool mCancelled; |
| 71 | +}; |
| 72 | + |
| 73 | +#endif // QGSFEEDBACK_H |
0 commit comments