QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
24 namespace QgsWms
25 {
26 
28  bool parallelRendering
29  , int maxThreads
30  , QgsFeatureFilterProvider *featureFilterProvider
31  )
32  :
33  mParallelRendering( parallelRendering )
34  , mFeatureFilterProvider( featureFilterProvider )
35  {
36 #ifndef HAVE_SERVER_PYTHON_PLUGINS
37  Q_UNUSED( mFeatureFilterProvider );
38 #endif
39  if ( mParallelRendering )
40  {
41  QgsApplication::setMaxThreads( maxThreads );
42  QgsMessageLog::logMessage( QStringLiteral( "Parallel rendering activated with %1 threads" ).arg( maxThreads ), QStringLiteral( "server" ), Qgis::Info );
43  }
44  else
45  {
46  QgsMessageLog::logMessage( QStringLiteral( "Parallel rendering deactivated" ), QStringLiteral( "server" ), Qgis::Info );
47  }
48  }
49 
50  void QgsMapRendererJobProxy::render( const QgsMapSettings &mapSettings, QImage *image )
51  {
52  if ( mParallelRendering )
53  {
54  QgsMapRendererParallelJob renderJob( mapSettings );
55 #ifdef HAVE_SERVER_PYTHON_PLUGINS
56  renderJob.setFeatureFilterProvider( mFeatureFilterProvider );
57 #endif
58  renderJob.start();
59 
60  // Allows the main thread to manage blocking call coming from rendering
61  // threads (see discussion in https://issues.qgis.org/issues/18988).
62  QEventLoop loop;
63  QObject::connect( &renderJob, &QgsMapRendererParallelJob::finished, &loop, &QEventLoop::quit );
64  loop.exec();
65 
66  renderJob.waitForFinished();
67  *image = renderJob.renderedImage();
68  mPainter.reset( new QPainter( image ) );
69  }
70  else
71  {
72  mPainter.reset( new QPainter( image ) );
73  QgsMapRendererCustomPainterJob renderJob( mapSettings, mPainter.get() );
74 #ifdef HAVE_SERVER_PYTHON_PLUGINS
75  renderJob.setFeatureFilterProvider( mFeatureFilterProvider );
76 #endif
77  renderJob.renderSynchronously();
78  }
79  }
80 
82  {
83  return mPainter.release();
84  }
85 
86 } // namespace qgsws
void finished()
emitted when asynchronous rendering is finished (or canceled).
Job implementation that renders everything sequentially using a custom painter.
void waitForFinished() override
Block until the job has finished.
void render(const QgsMapSettings &mapSettings, QImage *image)
Sequential or parallel map rendering.
QgsMapRendererJobProxy(bool parallelRendering, int maxThreads, QgsFeatureFilterProvider *featureFilterProvider)
Constructor for QgsMapRendererJobProxy.
void setFeatureFilterProvider(const QgsFeatureFilterProvider *f)
Set the feature filter provider used by the QgsRenderContext of each LayerRenderJob.
The QgsMapSettings class contains configuration for rendering of the map.
QPainter * takePainter()
Takes ownership of the painter used for rendering.
Job implementation that renders all layers in parallel.
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).
Abstract interface for use by classes that filter the features of a layer.
Median cut implementation.
void start() override
Start the rendering job and immediately return.
static void setMaxThreads(int maxThreads)
Set maximum concurrent thread count.
QImage renderedImage() override
Gets a preview/resulting image.