QGIS API Documentation 3.39.0-Master (d85f3c2a281)
Loading...
Searching...
No Matches
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
43 QgsRectangle clippedExtent = extent;
44 if ( mMap.terrainGenerator()->type() == QgsTerrainGenerator::Flat )
45 {
46 // The flat terrain generator might have non-square tiles, clipped at the scene's extent.
47 // We need to produce non-square textures for those cases.
48 clippedExtent = extent.intersect( mMap.extent() );
49 mapSettings.setExtent( clippedExtent );
50 }
51 if ( !qgsDoubleNear( clippedExtent.width(), clippedExtent.height() ) )
52 {
53 if ( clippedExtent.height() > clippedExtent.width() )
54 size.setWidth( std::round( size.width() * clippedExtent.width() / clippedExtent.height() ) );
55 else if ( clippedExtent.height() < clippedExtent.width() )
56 size.setHeight( std::round( size.height() * clippedExtent.height() / clippedExtent.width() ) );
57 }
58 mapSettings.setOutputSize( size );
59
60 QgsEventTracing::addEvent( QgsEventTracing::AsyncBegin, QStringLiteral( "3D" ), QStringLiteral( "Texture" ), tileId.text() );
61
63 connect( job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
64
65 JobData jobData;
66 jobData.jobId = ++mLastJobId;
67 jobData.tileId = tileId;
68 jobData.job = job;
69 jobData.extent = extent;
70 jobData.debugText = debugText;
71
72 mJobs.insert( job, jobData ); //store job data just before launching the job
73 job->start();
74
75 // QgsDebugMsgLevel( QStringLiteral("added job: %1 .... in queue: %2").arg( jobData.jobId ).arg( jobs.count() ), 2);
76 return jobData.jobId;
77}
78
79void QgsTerrainTextureGenerator::cancelJob( int jobId )
80{
81 for ( const JobData &jd : std::as_const( mJobs ) )
82 {
83 if ( jd.jobId == jobId )
84 {
85 // QgsDebugMsgLevel( QStringLiteral("canceling job %1").arg( jobId ), 2 );
86 jd.job->cancelWithoutBlocking();
87 disconnect( jd.job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
88 jd.job->deleteLater();
89 mJobs.remove( jd.job );
90 return;
91 }
92 }
93 Q_ASSERT( false && "requested job ID does not exist!" );
94}
95
96void QgsTerrainTextureGenerator::waitForFinished()
97{
98 for ( auto it = mJobs.keyBegin(); it != mJobs.keyEnd(); it++ )
99 disconnect( *it, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
100 QVector<QgsMapRendererSequentialJob *> toBeDeleted;
101 for ( auto it = mJobs.constBegin(); it != mJobs.constEnd(); it++ )
102 {
103 QgsMapRendererSequentialJob *mapJob = it.key();
104 mapJob->waitForFinished();
105 JobData jobData = it.value();
106 toBeDeleted.push_back( mapJob );
107
108 QImage img = mapJob->renderedImage();
109
110 if ( mMap.showTerrainTilesInfo() )
111 {
112 // extra tile information for debugging
113 QPainter p( &img );
114 p.setPen( Qt::red );
115 p.setBackgroundMode( Qt::OpaqueMode );
116 QFont font = p.font();
117 font.setPixelSize( std::max( 30, mMap.mapTileResolution() / 6 ) );
118 p.setFont( font );
119 p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
120 p.drawText( img.rect(), jobData.debugText, QTextOption( Qt::AlignCenter ) );
121 p.end();
122 }
123
124 // pass QImage further
125 emit tileReady( jobData.jobId, img );
126 }
127
128 for ( QgsMapRendererSequentialJob *mapJob : toBeDeleted )
129 {
130 mJobs.remove( mapJob );
131 mapJob->deleteLater();
132 }
133}
134
135void QgsTerrainTextureGenerator::onRenderingFinished()
136{
137 QgsMapRendererSequentialJob *mapJob = static_cast<QgsMapRendererSequentialJob *>( sender() );
138
139 Q_ASSERT( mJobs.contains( mapJob ) );
140 JobData jobData = mJobs.value( mapJob );
141
142 QImage img = mapJob->renderedImage();
143
144 if ( mMap.showTerrainTilesInfo() )
145 {
146 // extra tile information for debugging
147 QPainter p( &img );
148 p.setPen( Qt::red );
149 p.setBackgroundMode( Qt::OpaqueMode );
150 QFont font = p.font();
151 font.setPixelSize( std::max( 30, mMap.mapTileResolution() / 6 ) );
152 p.setFont( font );
153 p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
154 p.drawText( img.rect(), jobData.debugText, QTextOption( Qt::AlignCenter ) );
155 p.end();
156 }
157
158 mapJob->deleteLater();
159 mJobs.remove( mapJob );
160
161 // QgsDebugMsgLevel( QStringLiteral("finished job %1 ... in queue: %2").arg( jobData.jobId).arg( jobs.count() ), 2 );
162
163 QgsEventTracing::addEvent( QgsEventTracing::AsyncEnd, QStringLiteral( "3D" ), QStringLiteral( "Texture" ), jobData.tileId.text() );
164
165 // pass QImage further
166 emit tileReady( jobData.jobId, img );
167}
168
169QgsMapSettings QgsTerrainTextureGenerator::baseMapSettings()
170{
171 QgsMapSettings mapSettings;
172
173 mapSettings.setOutputSize( mTextureSize );
174 mapSettings.setDestinationCrs( mMap.crs() );
175 mapSettings.setBackgroundColor( mMap.backgroundColor() );
176 mapSettings.setFlag( Qgis::MapSettingsFlag::DrawLabeling, mMap.showLabels() );
178 mapSettings.setTransformContext( mMap.transformContext() );
179 mapSettings.setPathResolver( mMap.pathResolver() );
180 mapSettings.setRendererUsage( mMap.rendererUsage() );
181
182 QList<QgsMapLayer *> layers;
183 QgsMapThemeCollection *mapThemes = mMap.mapThemeCollection();
184 QString mapThemeName = mMap.terrainMapTheme();
185 if ( mapThemeName.isEmpty() || !mapThemes || !mapThemes->hasMapTheme( mapThemeName ) )
186 {
187 layers = mMap.layers();
188 }
189 else
190 {
191 layers = mapThemes->mapThemeVisibleLayers( mapThemeName );
192 mapSettings.setLayerStyleOverrides( mapThemes->mapThemeStyleOverrides( mapThemeName ) );
193 }
194 layers.erase( std::remove_if( layers.begin(),
195 layers.end(),
196 []( const QgsMapLayer * layer ) { return layer->renderer3D(); } ),
197 layers.end() );
198 mapSettings.setLayers( layers );
199
200 return mapSettings;
201}
202
@ 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:76
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.
double width() const
Returns the width of the rectangle.
double height() const
Returns the height of the rectangle.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
@ 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:5857