QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsterraintexturegenerator_p.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsterraintexturegenerator_p.cpp
3  --------------------------------------
4  Date : July 2017
5  Copyright : (C) 2017 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 
17 
20 #include <qgsmapsettings.h>
21 #include <qgsmapthemecollection.h>
22 #include <qgsproject.h>
23 
24 #include "qgs3dmapsettings.h"
25 
26 #include "qgseventtracing.h"
27 
29 
30 QgsTerrainTextureGenerator::QgsTerrainTextureGenerator( const Qgs3DMapSettings &map )
31  : mMap( map )
32  , mLastJobId( 0 )
33  , mTextureSize( QSize( mMap.mapTileResolution(), mMap.mapTileResolution() ) )
34 {
35 }
36 
37 int QgsTerrainTextureGenerator::render( const QgsRectangle &extent, QgsChunkNodeId tileId, const QString &debugText )
38 {
39  QgsMapSettings mapSettings( baseMapSettings() );
40  mapSettings.setExtent( extent );
41 
42  QgsEventTracing::addEvent( QgsEventTracing::AsyncBegin, QStringLiteral( "3D" ), QStringLiteral( "Texture" ), tileId.text() );
43 
45  connect( job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
46  job->start();
47 
48  JobData jobData;
49  jobData.jobId = ++mLastJobId;
50  jobData.tileId = tileId;
51  jobData.job = job;
52  jobData.extent = extent;
53  jobData.debugText = debugText;
54 
55  mJobs.insert( job, jobData );
56  //qDebug() << "added job: " << jobData.jobId << " .... in queue: " << jobs.count();
57  return jobData.jobId;
58 }
59 
60 void QgsTerrainTextureGenerator::cancelJob( int jobId )
61 {
62  Q_FOREACH ( const JobData &jd, mJobs )
63  {
64  if ( jd.jobId == jobId )
65  {
66  //qDebug() << "canceling job " << jobId;
67  jd.job->cancelWithoutBlocking();
68  disconnect( jd.job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
69  jd.job->deleteLater();
70  mJobs.remove( jd.job );
71  return;
72  }
73  }
74  Q_ASSERT( false && "requested job ID does not exist!" );
75 }
76 
77 void QgsTerrainTextureGenerator::waitForFinished()
78 {
79  for ( QgsMapRendererSequentialJob *job : mJobs.keys() )
80  disconnect( job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
81  QVector<QgsMapRendererSequentialJob *> toBeDeleted;
82  for ( QgsMapRendererSequentialJob *mapJob : mJobs.keys() )
83  {
84  mapJob->waitForFinished();
85  JobData jobData = mJobs.value( mapJob );
86  toBeDeleted.push_back( mapJob );
87 
88  QImage img = mapJob->renderedImage();
89 
90  if ( mMap.showTerrainTilesInfo() )
91  {
92  // extra tile information for debugging
93  QPainter p( &img );
94  p.setPen( Qt::white );
95  p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
96  p.drawText( img.rect(), jobData.debugText, QTextOption( Qt::AlignCenter ) );
97  p.end();
98  }
99 
100  // pass QImage further
101  emit tileReady( jobData.jobId, img );
102  }
103 
104  for ( QgsMapRendererSequentialJob *mapJob : toBeDeleted )
105  {
106  mJobs.remove( mapJob );
107  mapJob->deleteLater();
108  }
109 }
110 
111 void QgsTerrainTextureGenerator::onRenderingFinished()
112 {
113  QgsMapRendererSequentialJob *mapJob = static_cast<QgsMapRendererSequentialJob *>( sender() );
114 
115  Q_ASSERT( mJobs.contains( mapJob ) );
116  JobData jobData = mJobs.value( mapJob );
117 
118  QImage img = mapJob->renderedImage();
119 
120  if ( mMap.showTerrainTilesInfo() )
121  {
122  // extra tile information for debugging
123  QPainter p( &img );
124  p.setPen( Qt::white );
125  p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
126  p.drawText( img.rect(), jobData.debugText, QTextOption( Qt::AlignCenter ) );
127  p.end();
128  }
129 
130  mapJob->deleteLater();
131  mJobs.remove( mapJob );
132 
133  //qDebug() << "finished job " << jobData.jobId << " ... in queue: " << jobs.count();
134 
135  QgsEventTracing::addEvent( QgsEventTracing::AsyncEnd, QStringLiteral( "3D" ), QStringLiteral( "Texture" ), jobData.tileId.text() );
136 
137  // pass QImage further
138  emit tileReady( jobData.jobId, img );
139 }
140 
141 QgsMapSettings QgsTerrainTextureGenerator::baseMapSettings()
142 {
143  QgsMapSettings mapSettings;
144 
145  mapSettings.setOutputSize( mTextureSize );
146  mapSettings.setDestinationCrs( mMap.crs() );
147  mapSettings.setBackgroundColor( mMap.backgroundColor() );
148  mapSettings.setFlag( QgsMapSettings::DrawLabeling, mMap.showLabels() );
149  mapSettings.setFlag( QgsMapSettings::Render3DMap );
150  mapSettings.setTransformContext( mMap.transformContext() );
151  mapSettings.setPathResolver( mMap.pathResolver() );
152 
153  QgsMapThemeCollection *mapThemes = mMap.mapThemeCollection();
154  QString mapThemeName = mMap.terrainMapTheme();
155  if ( mapThemeName.isEmpty() || !mapThemes || !mapThemes->hasMapTheme( mapThemeName ) )
156  {
157  mapSettings.setLayers( mMap.terrainLayers() );
158  }
159  else
160  {
161  mapSettings.setLayers( mapThemes->mapThemeVisibleLayers( mapThemeName ) );
162  mapSettings.setLayerStyleOverrides( mapThemes->mapThemeStyleOverrides( mapThemeName ) );
163  }
164 
165  return mapSettings;
166 }
167 
QgsMapSettings::setDestinationCrs
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
sets destination coordinate reference system
Definition: qgsmapsettings.cpp:310
QgsMapSettings::setFlag
void setFlag(Flag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
Definition: qgsmapsettings.cpp:342
qgsmapthemecollection.h
qgsmaprenderercustompainterjob.h
qgsmapsettings.h
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsMapThemeCollection::mapThemeVisibleLayers
QList< QgsMapLayer * > mapThemeVisibleLayers(const QString &name) const
Returns the list of layers that are visible for the specified map theme.
Definition: qgsmapthemecollection.cpp:332
QgsMapThemeCollection::mapThemeStyleOverrides
QMap< QString, QString > mapThemeStyleOverrides(const QString &name)
Gets layer style overrides (for QgsMapSettings) of the visible layers for given map theme.
Definition: qgsmapthemecollection.cpp:386
QgsMapSettings::DrawLabeling
@ DrawLabeling
Enable drawing of labels on top of the map.
Definition: qgsmapsettings.h:307
qgseventtracing.h
QgsMapSettings::setBackgroundColor
void setBackgroundColor(const QColor &color)
Sets the background color of the map.
Definition: qgsmapsettings.h:291
QgsMapRendererSequentialJob
Job implementation that renders everything sequentially in one thread.
Definition: qgsmaprenderersequentialjob.h:34
Qgs3DMapSettings
3 Definition of the world
Definition: qgs3dmapsettings.h:54
QgsMapSettings::setTransformContext
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
Definition: qgsmapsettings.cpp:411
QgsMapSettings::setLayers
void setLayers(const QList< QgsMapLayer * > &layers)
Set list of layers for map rendering.
Definition: qgsmapsettings.cpp:287
qgsmaprenderersequentialjob.h
qgs3dmapsettings.h
QgsMapSettings::setLayerStyleOverrides
void setLayerStyleOverrides(const QMap< QString, QString > &overrides)
Set map of map layer style overrides (key: layer ID, value: style name) where a different style shoul...
Definition: qgsmapsettings.cpp:305
QgsMapSettings::Render3DMap
@ Render3DMap
Render is for a 3D map.
Definition: qgsmapsettings.h:316
QgsMapRendererSequentialJob::start
void start() override
Start the rendering job and immediately return.
Definition: qgsmaprenderersequentialjob.cpp:49
QgsMapThemeCollection::hasMapTheme
bool hasMapTheme(const QString &name) const
Returns whether a map theme with a matching name exists.
Definition: qgsmapthemecollection.cpp:258
QgsMapThemeCollection
Container class that allows storage of map themes consisting of visible map layers and layer styles.
Definition: qgsmapthemecollection.h:45
qgsterraintexturegenerator_p.h
QgsMapSettings::setOutputSize
void setOutputSize(QSize size)
Sets the size of the resulting map image.
Definition: qgsmapsettings.cpp:240
QgsMapSettings::setPathResolver
void setPathResolver(const QgsPathResolver &resolver)
Sets the path resolver for conversion between relative and absolute paths during rendering operations...
Definition: qgsmapsettings.h:434
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
qgsproject.h
QgsMapRendererSequentialJob::renderedImage
QImage renderedImage() override
Gets a preview/resulting image.
Definition: qgsmaprenderersequentialjob.cpp:119
QgsMapRendererJob::finished
void finished()
emitted when asynchronous rendering is finished (or canceled).