25#include <QtConcurrentMap>
27#include "moc_qgsprofilerenderer.cpp"
37 if ( std::unique_ptr< QgsAbstractProfileGenerator > generator{ source->createProfileGenerator( mRequest ) } )
38 mGenerators.emplace_back( std::move( generator ) );
44 : mGenerators( std::move( generators ) )
60 res.reserve( mGenerators.size() );
61 for (
const auto &it : mGenerators )
63 res.append( it->sourceId() );
75 Q_ASSERT( mJobs.empty() );
77 mJobs.reserve( mGenerators.size() );
78 for (
const auto &it : mGenerators )
80 auto job = std::make_unique< ProfileJob >();
81 job->generator = it.get();
82 job->context = mContext;
83 mJobs.emplace_back( std::move( job ) );
86 connect( &mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsProfilePlotRenderer::onGeneratingFinished );
88 mFuture = QtConcurrent::map( mJobs, generateProfileStatic );
89 mFutureWatcher.setFuture( mFuture );
99 Q_ASSERT( mJobs.empty() );
100 mJobs.reserve( mGenerators.size() );
102 for (
const auto &it : mGenerators )
104 auto job = std::make_unique< ProfileJob >();
105 job->generator = it.get();
106 job->context = mContext;
107 it.get()->generateProfile( job->context );
108 job->results.reset( job->generator->takeResults() );
109 job->complete =
true;
110 job->invalidatedResults.reset();
111 mJobs.emplace_back( std::move( job ) );
122 disconnect( &mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsProfilePlotRenderer::onGeneratingFinished );
124 for (
const auto &job : mJobs )
126 if ( job->generator )
128 if (
QgsFeedback *feedback = job->generator->feedback() )
135 mFutureWatcher.waitForFinished();
137 onGeneratingFinished();
145 for (
const auto &job : mJobs )
147 if ( job->generator )
149 if (
QgsFeedback *feedback = job->generator->feedback() )
162 disconnect( &mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsProfilePlotRenderer::onGeneratingFinished );
163 mFutureWatcher.waitForFinished();
165 onGeneratingFinished();
170 return mStatus != Idle;
175 if ( mContext == context )
179 const double distanceRangeChanged = context.
distanceRange() != mContext.distanceRange();
180 const double elevationRangeChanged = context.
elevationRange() != mContext.elevationRange();
183 for (
auto &job : mJobs )
189 if ( !jobNeedsRegeneration )
193 job->context = mContext;
194 if ( job->results && job->complete )
195 job->invalidatedResults = std::move( job->results );
196 job->results.reset();
197 job->complete =
false;
204 for (
auto &job : mJobs )
210 if ( !jobNeedsRegeneration )
214 job->context = mContext;
215 if ( job->results && job->complete )
216 job->invalidatedResults = std::move( job->results );
217 job->results.reset();
218 job->complete =
false;
225 replaceSourceInternal( source,
false );
230 return replaceSourceInternal( source,
true );
242 QString sourceId = generator->sourceId();
244 for (
auto &job : mJobs )
246 if ( job->generator && job->generator->sourceId() == sourceId )
250 if ( clearPreviousResults || job->generator->type() != generator->type() )
252 job->results.reset();
253 job->complete =
false;
255 else if ( job->results )
257 job->results->copyPropertiesFromGenerator( generator.get() );
259 job->generator = generator.get();
262 for (
auto it = mGenerators.begin(); it != mGenerators.end(); )
264 if ( ( *it )->sourceId() == sourceId )
265 it = mGenerators.erase( it );
269 mGenerators.emplace_back( std::move( generator ) );
280 mStatus = Generating;
282 connect( &mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsProfilePlotRenderer::onGeneratingFinished );
284 mFuture = QtConcurrent::map( mJobs, generateProfileStatic );
285 mFutureWatcher.setFuture( mFuture );
290 double min = std::numeric_limits< double >::max();
291 double max = std::numeric_limits< double >::lowest();
292 for (
const auto &job : mJobs )
294 if ( job->complete && job->results )
297 min = std::min( min, jobRange.
lower() );
298 max = std::max( max, jobRange.
upper() );
306 QImage res( width, height, QImage::Format_ARGB32_Premultiplied );
307 res.setDotsPerMeterX( 96 / 25.4 * 1000 );
308 res.setDotsPerMeterY( 96 / 25.4 * 1000 );
309 res.fill( Qt::transparent );
317 const double mapUnitsPerPixel = ( distanceMax - distanceMin ) / width;
320 render( context, width, height, distanceMin, distanceMax, zMin, zMax, sourceId );
321 QRectF plotArea( QPointF( 0, 0 ), QPointF( width, height ) );
328QTransform QgsProfilePlotRenderer::computeRenderTransform(
double width,
double height,
double distanceMin,
double distanceMax,
double zMin,
double zMax )
330 QTransform transform;
331 transform.translate( 0, height );
332 transform.scale( width / ( distanceMax - distanceMin ), -height / ( zMax - zMin ) );
333 transform.translate( -distanceMin, -zMin );
340 QPainter *painter = context.
painter();
345 profileRenderContext.
setWorldTransform( computeRenderTransform( width, height, distanceMin, distanceMax, zMin, zMax ) );
350 for (
auto &job : mJobs )
352 if ( ( sourceId.isEmpty() || job->generator->sourceId() == sourceId ) )
355 if ( job->complete && job->results )
357 job->results->renderResults( profileRenderContext );
359 else if ( !job->complete && job->invalidatedResults )
362 job->invalidatedResults->renderResults( profileRenderContext );
371 auto subSections = std::make_unique< QgsSimpleLineSymbolLayer >( QColor( 255, 0, 0, 255 ), 0.5 );
372 subSections->setPenCapStyle( Qt::FlatCap );
373 return std::make_unique<QgsLineSymbol>(
QgsSymbolLayerList() << subSections.release() );
378 mSubsectionsSymbol.reset( symbol );
383 return mSubsectionsSymbol.get();
388 QgsCurve *profileCurve = mRequest.profileCurve();
389 if ( !profileCurve || profileCurve->
numPoints() < 3 || !mSubsectionsSymbol )
392 QTransform transform = computeRenderTransform( plotArea.width(), plotArea.height(), distanceMin, distanceMax, zMin, zMax );
395 profileCurve->
points( points );
396 QgsPoint firstPoint = points.takeFirst();
399 mSubsectionsSymbol->startRender( context );
400 double accumulatedDistance = 0.;
401 for (
const QgsPoint &point : points )
403 accumulatedDistance += point.distance( firstPoint );
404 QPointF output = transform.map( QPointF( accumulatedDistance, 0. ) );
405 QPolygonF polyLine( QVector<QPointF> { QPointF( output.x() + plotArea.left(), plotArea.top() ), QPointF( output.x() + plotArea.left(), plotArea.bottom() ) } );
406 mSubsectionsSymbol->renderPolyline( polyLine,
nullptr, context );
409 mSubsectionsSymbol->stopRender( context );
415 if ( !mRequest.profileCurve() )
416 return bestSnapResult;
418 double bestSnapDistance = std::numeric_limits< double >::max();
420 for (
const auto &job : mJobs )
423 if ( job->complete && job->results )
431 if ( snapDistance < bestSnapDistance )
433 bestSnapDistance = snapDistance;
434 bestSnapResult = jobSnapResult;
441 return bestSnapResult;
446 QVector<QgsProfileIdentifyResults> res;
447 if ( !mRequest.profileCurve() )
450 for (
const auto &job : mJobs )
453 if ( job->complete && job->results )
455 res.append( job->results->identify( point, context ) );
465 QVector<QgsProfileIdentifyResults> res;
466 if ( !mRequest.profileCurve() )
469 for (
const auto &job : mJobs )
472 if ( job->complete && job->results )
474 res.append( job->results->identify( distanceRange, elevationRange, context ) );
484 QVector<QgsAbstractProfileResults::Feature > res;
485 for (
const auto &job : mJobs )
491 if ( job->complete && job->results )
493 res.append( job->results->asFeatures( type, feedback ) );
500void QgsProfilePlotRenderer::onGeneratingFinished()
506void QgsProfilePlotRenderer::generateProfileStatic( std::unique_ptr< ProfileJob > &job )
511 Q_ASSERT( job->generator );
513 job->generator->generateProfile( job->context );
515 job->results.reset( job->generator->takeResults() );
516 job->complete =
true;
517 job->invalidatedResults.reset();
@ RespectsMaximumErrorMapUnit
Generated profile respects the QgsProfileGenerationContext::maximumErrorMapUnits() property.
@ RespectsElevationRange
Generated profile respects the QgsProfileGenerationContext::elevationRange() property.
@ RespectsDistanceRange
Generated profile respects the QgsProfileGenerationContext::distanceRange() property.
@ Antialiasing
Use antialiasing while drawing.
ProfileExportType
Types of export for elevation profiles.
Interface for classes which can generate elevation profiles.
virtual QgsAbstractProfileGenerator * createProfileGenerator(const QgsProfileRequest &request)=0
Given a profile request, returns a new profile generator ready for generating elevation profiles.
Abstract base class for curved geometry type.
virtual int numPoints() const =0
Returns the number of points in the curve.
virtual void points(QgsPointSequence &pt) const =0
Returns a list of points within the curve.
QgsRange which stores a range of double values.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const
Tells whether the operation has been canceled already.
A line symbol type, for rendering LineString and MultiLineString geometries.
Perform transforms between map coordinates and device coordinates.
Point geometry type, with support for z-dimension and m-values.
Encapsulates the context in which an elevation profile is to be generated.
double maximumErrorMapUnits() const
Returns the maximum allowed error in the generated result, in profile curve map units.
QgsDoubleRange elevationRange() const
Returns the range of elevations to include in the generation.
QgsDoubleRange distanceRange() const
Returns the range of distances to include in the generation.
Encapsulates the context of identifying profile results.
void setSubsectionsSymbol(QgsLineSymbol *symbol)
Sets the symbol used to draw the subsections.
QgsProfileSnapResult snapPoint(const QgsProfilePoint &point, const QgsProfileSnapContext &context)
Snap a point to the results.
void render(QgsRenderContext &context, double width, double height, double distanceMin, double distanceMax, double zMin, double zMax, const QString &sourceId=QString())
Renders a portion of the profile using the specified render context.
void renderSubsectionsIndicator(QgsRenderContext &context, const QRectF &plotArea, double distanceMin, double distanceMax, double zMin, double zMax)
Renders the vertices of the profile curve as vertical lines using the specified render context.
void regenerateInvalidatedResults()
Starts a background regeneration of any invalidated results and immediately returns.
QVector< QgsAbstractProfileResults::Feature > asFeatures(Qgis::ProfileExportType type, QgsFeedback *feedback=nullptr)
Exports the profile results as a set of features.
QImage renderToImage(int width, int height, double distanceMin, double distanceMax, double zMin, double zMax, const QString &sourceId=QString(), double devicePixelRatio=1.0)
Renders a portion of the profile to an image with the given width and height.
void cancelGenerationWithoutBlocking()
Triggers cancellation of the generation job without blocking.
QgsLineSymbol * subsectionsSymbol()
Returns the line symbol used to draw the subsections.
void invalidateAllRefinableSources()
Invalidates previous results from all refinable sources.
void cancelGeneration()
Stop the generation job - does not return until the job has terminated.
QgsProfilePlotRenderer(const QList< QgsAbstractProfileSource * > &sources, const QgsProfileRequest &request)
Constructor for QgsProfilePlotRenderer, using the provided list of profile sources to generate the re...
void generateSynchronously()
Generate the profile results synchronously in this thread.
void startGeneration()
Start the generation job and immediately return.
QgsDoubleRange zRange() const
Returns the limits of the retrieved elevation values.
QVector< QgsProfileIdentifyResults > identify(const QgsProfilePoint &point, const QgsProfileIdentifyContext &context)
Identify results visible at the specified profile point.
void waitForFinished()
Block until the current job has finished.
~QgsProfilePlotRenderer() override
bool isActive() const
Returns true if the generation job is currently running in background.
QStringList sourceIds() const
Returns the ordered list of source IDs for the sources used by the renderer.
bool invalidateResults(QgsAbstractProfileSource *source)
Invalidates the profile results from the source with matching ID.
void replaceSource(QgsAbstractProfileSource *source)
Replaces the existing source with matching ID.
void setContext(const QgsProfileGenerationContext &context)
Sets the context in which the profile generation will occur.
void generationFinished()
Emitted when the profile generation is finished (or canceled).
static std::unique_ptr< QgsLineSymbol > defaultSubSectionsSymbol()
Returns the default line symbol to use for subsections lines.
Encapsulates a point on a distance-elevation profile.
double elevation() const
Returns the elevation of the point.
double distance() const
Returns the distance of the point.
Abstract base class for storage of elevation profiles.
void setWorldTransform(const QTransform &transform)
Sets the transform from world coordinates to painter coordinates.
void setDistanceRange(const QgsDoubleRange &range)
Sets the range of distances to include in the render.
void setElevationRange(const QgsDoubleRange &range)
Sets the range of elevations to include in the render.
Encapsulates properties and constraints relating to fetching elevation profiles from different source...
Encapsulates the context of snapping a profile point.
double displayRatioElevationVsDistance
Display ratio of elevation vs distance units.
Encapsulates results of snapping a profile point.
bool isValid() const
Returns true if the result is a valid point.
QgsProfilePoint snappedPoint
Snapped point.
T lower() const
Returns the lower bound of the range.
T upper() const
Returns the upper bound of the range.
Contains information about the context of a rendering operation.
void setDevicePixelRatio(float ratio)
Sets the device pixel ratio.
QPainter * painter()
Returns the destination QPainter for the render operation.
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
static QgsRenderContext fromQPainter(QPainter *painter)
Creates a default render context given a pixel based QPainter destination.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
QVector< QgsPoint > QgsPointSequence
QList< QgsSymbolLayer * > QgsSymbolLayerList