Branch data Line data Source code
1 : : /*************************************************************************** 2 : : qgsmaprenderercustompainterjob.h 3 : : -------------------------------------- 4 : : Date : December 2013 5 : : Copyright : (C) 2013 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 QGSMAPRENDERERCUSTOMPAINTERJOB_H 17 : : #define QGSMAPRENDERERCUSTOMPAINTERJOB_H 18 : : 19 : : #include "qgis_core.h" 20 : : #include "qgis_sip.h" 21 : : #include "qgsmaprendererjob.h" 22 : : 23 : : #include <QEventLoop> 24 : : 25 : : /** 26 : : * \ingroup core 27 : : * \brief Abstract base class for map renderer jobs which use custom painters. 28 : : * 29 : : * \since QGIS 3.10 30 : : */ 31 : 0 : class CORE_EXPORT QgsMapRendererAbstractCustomPainterJob : public QgsMapRendererJob 32 : : { 33 : : Q_OBJECT 34 : : public: 35 : : 36 : : /** 37 : : * Constructor for QgsMapRendererAbstractCustomPainterJob, using the given 38 : : * map \a settings. 39 : : */ 40 : : QgsMapRendererAbstractCustomPainterJob( const QgsMapSettings &settings ); 41 : : 42 : : protected: 43 : : 44 : : /** 45 : : * Prepares the given \a painter ready for a map render. 46 : : * 47 : : * The \a backgroundColor argument specifies the color to use for the map's background. 48 : : */ 49 : : void preparePainter( QPainter *painter, const QColor &backgroundColor = Qt::transparent ); 50 : : 51 : : }; 52 : : 53 : : /** 54 : : * \ingroup core 55 : : * \brief Job implementation that renders everything sequentially using a custom painter. 56 : : * 57 : : * Also supports synchronous rendering in main thread for cases when rendering in background 58 : : * is not an option because of some technical limitations (e.g. printing to printer on some 59 : : * platforms). 60 : : * 61 : : * \since QGIS 2.4 62 : : */ 63 : : class CORE_EXPORT QgsMapRendererCustomPainterJob : public QgsMapRendererAbstractCustomPainterJob 64 : : { 65 : : Q_OBJECT 66 : : public: 67 : : QgsMapRendererCustomPainterJob( const QgsMapSettings &settings, QPainter *painter ); 68 : : ~QgsMapRendererCustomPainterJob() override; 69 : : 70 : : void start() override; 71 : : void cancel() override; 72 : : void cancelWithoutBlocking() override; 73 : : void waitForFinished() override; 74 : : bool isActive() const override; 75 : : bool usedCachedLabels() const override; 76 : : QgsLabelingResults *takeLabelingResults() SIP_TRANSFER override; 77 : : 78 : : //! \note not available in Python bindings 79 : 0 : const LayerRenderJobs &jobs() const { return mLayerJobs; } SIP_SKIP 80 : : 81 : : /** 82 : : * Wait for the job to be finished - and keep the thread's event loop running while waiting. 83 : : * 84 : : * With a call to waitForFinished(), the waiting is done with a synchronization primitive 85 : : * and does not involve processing of messages. That may cause issues to code which requires 86 : : * some events to be handled in the main thread. Some plugins hooking into the rendering 87 : : * pipeline may require this in order to work properly - for example, OpenLayers plugin 88 : : * which uses a QWebPage in the main thread. 89 : : * 90 : : * Ideally the "wait for finished" method should not be used at all. The code triggering 91 : : * rendering should not need to actively wait for rendering to finish. 92 : : */ 93 : : void waitForFinishedWithEventLoop( QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents ); 94 : : 95 : : /** 96 : : * Render the map synchronously in this thread. The function does not return until the map 97 : : * is completely rendered. 98 : : * 99 : : * This is an alternative to ordinary API (using start() + waiting for finished() signal). 100 : : * Users are discouraged to use this method unless they have a strong reason for doing it. 101 : : * The synchronous rendering blocks the main thread, making the application unresponsive. 102 : : * Also, it is not possible to cancel rendering while it is in progress. 103 : : */ 104 : : void renderSynchronously(); 105 : : 106 : : /** 107 : : * Prepares the job for rendering synchronously in a background thread. 108 : : * 109 : : * Must be called from the main thread. 110 : : * 111 : : * This is an alternative to ordinary API (using start() + waiting for finished() signal), 112 : : * and an alternative to renderSynchronously() (which should only ever be called from the main thread). 113 : : * 114 : : * \see renderPrepared() 115 : : * \since QGIS 3.10 116 : : */ 117 : : void prepare(); 118 : : 119 : : /** 120 : : * Render a pre-prepared job. Can be safely called in a background thread. 121 : : * 122 : : * Must be preceded by a call to prepare() 123 : : * 124 : : * This is an alternative to ordinary API (using start() + waiting for finished() signal), 125 : : * and an alternative to renderSynchronously() (which should only ever be called from the main thread). 126 : : * 127 : : * \since QGIS 3.10 128 : : */ 129 : : void renderPrepared(); 130 : : 131 : : private slots: 132 : : void futureFinished(); 133 : : 134 : : private: 135 : : static void staticRender( QgsMapRendererCustomPainterJob *self ); // function to be used within the thread 136 : : 137 : : // these methods are called within worker thread 138 : : void doRender(); 139 : : 140 : : QPainter *mPainter = nullptr; 141 : : QFuture<void> mFuture; 142 : : QFutureWatcher<void> mFutureWatcher; 143 : : std::unique_ptr< QgsLabelingEngine > mLabelingEngineV2; 144 : : 145 : : bool mActive; 146 : : LayerRenderJobs mLayerJobs; 147 : : LabelRenderJob mLabelJob; 148 : : bool mRenderSynchronously = false; 149 : : bool mPrepared = false; 150 : : bool mPrepareOnly = false; 151 : : 152 : : LayerRenderJobs mSecondPassLayerJobs; 153 : : }; 154 : : 155 : : 156 : : #endif // QGSMAPRENDERERCUSTOMPAINTERJOB_H