QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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>
22#include <qgsproject.h>
23
24#include "qgs3dmapsettings.h"
25
26#include "qgseventtracing.h"
27
29
30QgsTerrainTextureGenerator::QgsTerrainTextureGenerator( const Qgs3DMapSettings &map )
31 : mMap( map )
32 , mLastJobId( 0 )
33 , mTextureSize( QSize( mMap.mapTileResolution(), mMap.mapTileResolution() ) )
34{
35}
36
37int QgsTerrainTextureGenerator::render( const QgsRectangle &extent, QgsChunkNodeId tileId, const QString &debugText )
38{
39 QgsMapSettings mapSettings( baseMapSettings() );
40 mapSettings.setExtent( extent );
41 QSize size = QSize( mTextureSize );
42 if ( mMap.terrainGenerator()->type() == QgsTerrainGenerator::Flat )
43 {
44 // The flat terrain generator might have non-square tiles, clipped at the scene's extent.
45 // We need to produce non-square textures for those cases.
46 const QgsRectangle clippedExtent = extent.intersect( mMap.extent() );
47 if ( !qgsDoubleNear( clippedExtent.width(), clippedExtent.height() ) )
48 {
49 if ( clippedExtent.height() > clippedExtent.width() )
50 size.setWidth( std::round( size.width() * clippedExtent.width() / clippedExtent.height() ) );
51 else if ( clippedExtent.height() < clippedExtent.width() )
52 size.setHeight( std::round( size.height() * clippedExtent.height() / clippedExtent.width() ) );
53 }
54 mapSettings.setExtent( clippedExtent );
55 }
56 mapSettings.setOutputSize( size );
57
58 QgsEventTracing::addEvent( QgsEventTracing::AsyncBegin, QStringLiteral( "3D" ), QStringLiteral( "Texture" ), tileId.text() );
59
61 connect( job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
62
63 JobData jobData;
64 jobData.jobId = ++mLastJobId;
65 jobData.tileId = tileId;
66 jobData.job = job;
67 jobData.extent = extent;
68 jobData.debugText = debugText;
69
70 mJobs.insert( job, jobData ); //store job data just before launching the job
71 job->start();
72
73 // QgsDebugMsgLevel( QStringLiteral("added job: %1 .... in queue: %2").arg( jobData.jobId ).arg( jobs.count() ), 2);
74 return jobData.jobId;
75}
76
77void QgsTerrainTextureGenerator::cancelJob( int jobId )
78{
79 for ( const JobData &jd : std::as_const( mJobs ) )
80 {
81 if ( jd.jobId == jobId )
82 {
83 // QgsDebugMsgLevel( QStringLiteral("canceling job %1").arg( jobId ), 2 );
84 jd.job->cancelWithoutBlocking();
85 disconnect( jd.job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
86 jd.job->deleteLater();
87 mJobs.remove( jd.job );
88 return;
89 }
90 }
91 Q_ASSERT( false && "requested job ID does not exist!" );
92}
93
94void QgsTerrainTextureGenerator::waitForFinished()
95{
96 for ( auto it = mJobs.keyBegin(); it != mJobs.keyEnd(); it++ )
97 disconnect( *it, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
98 QVector<QgsMapRendererSequentialJob *> toBeDeleted;
99 for ( auto it = mJobs.constBegin(); it != mJobs.constEnd(); it++ )
100 {
101 QgsMapRendererSequentialJob *mapJob = it.key();
102 mapJob->waitForFinished();
103 JobData jobData = it.value();
104 toBeDeleted.push_back( mapJob );
105
106 QImage img = mapJob->renderedImage();
107
108 if ( mMap.showTerrainTilesInfo() )
109 {
110 // extra tile information for debugging
111 QPainter p( &img );
112 p.setPen( Qt::red );
113 p.setBackgroundMode( Qt::OpaqueMode );
114 QFont font = p.font();
115 font.setPixelSize( std::max( 30, mMap.mapTileResolution() / 6 ) );
116 p.setFont( font );
117 p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
118 p.drawText( img.rect(), jobData.debugText, QTextOption( Qt::AlignCenter ) );
119 p.end();
120 }
121
122 // pass QImage further
123 emit tileReady( jobData.jobId, img );
124 }
125
126 for ( QgsMapRendererSequentialJob *mapJob : toBeDeleted )
127 {
128 mJobs.remove( mapJob );
129 mapJob->deleteLater();
130 }
131}
132
133void QgsTerrainTextureGenerator::onRenderingFinished()
134{
135 QgsMapRendererSequentialJob *mapJob = static_cast<QgsMapRendererSequentialJob *>( sender() );
136
137 Q_ASSERT( mJobs.contains( mapJob ) );
138 JobData jobData = mJobs.value( mapJob );
139
140 QImage img = mapJob->renderedImage();
141
142 if ( mMap.showTerrainTilesInfo() )
143 {
144 // extra tile information for debugging
145 QPainter p( &img );
146 p.setPen( Qt::red );
147 p.setBackgroundMode( Qt::OpaqueMode );
148 QFont font = p.font();
149 font.setPixelSize( std::max( 30, mMap.mapTileResolution() / 6 ) );
150 p.setFont( font );
151 p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
152 p.drawText( img.rect(), jobData.debugText, QTextOption( Qt::AlignCenter ) );
153 p.end();
154 }
155
156 mapJob->deleteLater();
157 mJobs.remove( mapJob );
158
159 // QgsDebugMsgLevel( QStringLiteral("finished job %1 ... in queue: %2").arg( jobData.jobId).arg( jobs.count() ), 2 );
160
161 QgsEventTracing::addEvent( QgsEventTracing::AsyncEnd, QStringLiteral( "3D" ), QStringLiteral( "Texture" ), jobData.tileId.text() );
162
163 // pass QImage further
164 emit tileReady( jobData.jobId, img );
165}
166
167QgsMapSettings QgsTerrainTextureGenerator::baseMapSettings()
168{
169 QgsMapSettings mapSettings;
170
171 mapSettings.setOutputSize( mTextureSize );
172 mapSettings.setDestinationCrs( mMap.crs() );
173 mapSettings.setBackgroundColor( mMap.backgroundColor() );
174 mapSettings.setFlag( Qgis::MapSettingsFlag::DrawLabeling, mMap.showLabels() );
176 mapSettings.setTransformContext( mMap.transformContext() );
177 mapSettings.setPathResolver( mMap.pathResolver() );
178 mapSettings.setRendererUsage( mMap.rendererUsage() );
179
180 QList<QgsMapLayer *> layers;
181 QgsMapThemeCollection *mapThemes = mMap.mapThemeCollection();
182 QString mapThemeName = mMap.terrainMapTheme();
183 if ( mapThemeName.isEmpty() || !mapThemes || !mapThemes->hasMapTheme( mapThemeName ) )
184 {
185 layers = mMap.layers();
186 }
187 else
188 {
189 layers = mapThemes->mapThemeVisibleLayers( mapThemeName );
190 mapSettings.setLayerStyleOverrides( mapThemes->mapThemeStyleOverrides( mapThemeName ) );
191 }
192 layers.erase( std::remove_if( layers.begin(),
193 layers.end(),
194 []( const QgsMapLayer * layer ) { return layer->renderer3D(); } ),
195 layers.end() );
196 mapSettings.setLayers( layers );
197
198 return mapSettings;
199}
200
@ DrawLabeling
Enable drawing of labels on top of the map.
@ Render3DMap
Render is for a 3D map.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
void finished()
emitted when asynchronous rendering is finished (or canceled).
void start()
Start the rendering job and immediately return.
Job implementation that renders everything sequentially in one thread.
QImage renderedImage() override
Gets a preview/resulting image.
void waitForFinished() override
Block until the job has finished.
The QgsMapSettings class contains configuration for rendering of the map.
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers to render in the map.
void setRendererUsage(Qgis::RendererUsage rendererUsage)
Sets the rendering usage.
void setLayerStyleOverrides(const QMap< QString, QString > &overrides)
Sets the map of map layer style overrides (key: layer ID, value: style name) where a different style ...
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
void setPathResolver(const QgsPathResolver &resolver)
Sets the path resolver for conversion between relative and absolute paths during rendering operations...
void setOutputSize(QSize size)
Sets the size of the resulting map image, in pixels.
void setBackgroundColor(const QColor &color)
Sets the background color of the map.
void setFlag(Qgis::MapSettingsFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination crs (coordinate reference system) for the map render.
Container class that allows storage of map themes consisting of visible map layers and layer styles.
bool hasMapTheme(const QString &name) const
Returns whether a map theme with a matching name exists.
QList< QgsMapLayer * > mapThemeVisibleLayers(const QString &name) const
Returns the list of layers that are visible for the specified map theme.
QMap< QString, QString > mapThemeStyleOverrides(const QString &name)
Gets layer style overrides (for QgsMapSettings) of the visible layers for given map theme.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
Definition: qgsrectangle.h:230
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Definition: qgsrectangle.h:223
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
Definition: qgsrectangle.h:333
@ Flat
The whole terrain is flat area.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:3988