QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsmaprendererjobproxy.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaprendererjobproxy.cpp
3  --------------------------
4  begin : January 2017
5  copyright : (C) 2017 by Paul Blottiere
6  email : paul dot blottiere at oslandia dot com
7 ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsmaprendererjobproxy.h"
19 
20 #include "qgsmessagelog.h"
23 #include "qgsapplication.h"
24 
25 namespace QgsWms
26 {
27 
29  bool parallelRendering
30  , int maxThreads
31  , QgsFeatureFilterProvider *featureFilterProvider
32  )
33  :
34  mParallelRendering( parallelRendering )
35  , mFeatureFilterProvider( featureFilterProvider )
36  {
37 #ifndef HAVE_SERVER_PYTHON_PLUGINS
38  Q_UNUSED( mFeatureFilterProvider )
39 #endif
40  if ( mParallelRendering )
41  {
42  QgsApplication::setMaxThreads( maxThreads );
43  QgsMessageLog::logMessage( QStringLiteral( "Parallel rendering activated with %1 threads" ).arg( maxThreads ), QStringLiteral( "server" ), Qgis::Info );
44  }
45  else
46  {
47  QgsMessageLog::logMessage( QStringLiteral( "Parallel rendering deactivated" ), QStringLiteral( "server" ), Qgis::Info );
48  }
49  }
50 
51  void QgsMapRendererJobProxy::render( const QgsMapSettings &mapSettings, QImage *image )
52  {
53  if ( mParallelRendering )
54  {
55  QgsMapRendererParallelJob renderJob( mapSettings );
56 #ifdef HAVE_SERVER_PYTHON_PLUGINS
57  renderJob.setFeatureFilterProvider( mFeatureFilterProvider );
58 #endif
59  renderJob.start();
60 
61  // Allows the main thread to manage blocking call coming from rendering
62  // threads (see discussion in https://github.com/qgis/QGIS/issues/26819).
63  QEventLoop loop;
64  QObject::connect( &renderJob, &QgsMapRendererParallelJob::finished, &loop, &QEventLoop::quit );
65  loop.exec();
66 
67  renderJob.waitForFinished();
68  *image = renderJob.renderedImage();
69  mPainter.reset( new QPainter( image ) );
70 
71  mErrors = renderJob.errors();
72  }
73  else
74  {
75  mPainter.reset( new QPainter( image ) );
76  QgsMapRendererCustomPainterJob renderJob( mapSettings, mPainter.get() );
77 #ifdef HAVE_SERVER_PYTHON_PLUGINS
78  renderJob.setFeatureFilterProvider( mFeatureFilterProvider );
79 #endif
80  renderJob.renderSynchronously();
81  mErrors = renderJob.errors();
82  }
83  }
84 
86  {
87  return mPainter.release();
88  }
89 } // namespace qgsws
QgsApplication::setMaxThreads
static void setMaxThreads(int maxThreads)
Set maximum concurrent thread count.
Definition: qgsapplication.cpp:2112
qgsmaprenderercustompainterjob.h
qgsmaprendererjobproxy.h
QgsWms::QgsMapRendererJobProxy::takePainter
QPainter * takePainter()
Takes ownership of the painter used for rendering.
Definition: qgsmaprendererjobproxy.cpp:85
qgsapplication.h
QgsMapRendererJob::setFeatureFilterProvider
void setFeatureFilterProvider(const QgsFeatureFilterProvider *f)
Set the feature filter provider used by the QgsRenderContext of each LayerRenderJob.
Definition: qgsmaprendererjob.h:241
QgsMapRendererJob::errors
Errors errors() const
List of errors that happened during the rendering job - available when the rendering has been finishe...
QgsMapRendererParallelJob
Job implementation that renders all layers in parallel.
Definition: qgsmaprendererparalleljob.h:33
QgsMapRendererParallelJob::start
void start() override
Start the rendering job and immediately return.
Definition: qgsmaprendererparalleljob.cpp:43
Qgis::Info
@ Info
Definition: qgis.h:90
QgsWms::QgsMapRendererJobProxy::QgsMapRendererJobProxy
QgsMapRendererJobProxy(bool parallelRendering, int maxThreads, QgsFeatureFilterProvider *featureFilterProvider)
Constructor for QgsMapRendererJobProxy.
Definition: qgsmaprendererjobproxy.cpp:28
QgsMapRendererCustomPainterJob
Job implementation that renders everything sequentially using a custom painter.
Definition: qgsmaprenderercustompainterjob.h:64
QgsMapRendererParallelJob::waitForFinished
void waitForFinished() override
Block until the job has finished.
Definition: qgsmaprendererparalleljob.cpp:142
QgsWms
Median cut implementation.
Definition: qgsdxfwriter.cpp:23
QgsMapRendererCustomPainterJob::renderSynchronously
void renderSynchronously()
Render the map synchronously in this thread.
Definition: qgsmaprenderercustompainterjob.cpp:216
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
QgsFeatureFilterProvider
Abstract interface for use by classes that filter the features of a layer.
Definition: qgsfeaturefilterprovider.h:41
qgsmaprendererparalleljob.h
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
QgsMapRendererParallelJob::renderedImage
QImage renderedImage() override
Gets a preview/resulting image.
Definition: qgsmaprendererparalleljob.cpp:210
QgsWms::QgsMapRendererJobProxy::render
void render(const QgsMapSettings &mapSettings, QImage *image)
Sequential or parallel map rendering.
Definition: qgsmaprendererjobproxy.cpp:51
qgsmessagelog.h
QgsMapRendererJob::finished
void finished()
emitted when asynchronous rendering is finished (or canceled).