25#include <QtConcurrentMap>
26#include <QtConcurrentRun>
28#include "moc_qgsprofilerenderer.cpp"
38 if ( std::unique_ptr< QgsAbstractProfileGenerator > generator{ source->createProfileGenerator( mRequest ) } )
39 mGenerators.emplace_back( std::move( generator ) );
45 : mGenerators( std::move( generators ) )
61 res.reserve( mGenerators.size() );
62 for (
const auto &it : mGenerators )
64 res.append( it->sourceId() );
76 Q_ASSERT( mJobs.empty() );
78 mJobs.reserve( mGenerators.size() );
79 for (
const auto &it : mGenerators )
81 auto job = std::make_unique< ProfileJob >();
82 job->generator = it.get();
83 job->context = mContext;
84 mJobs.emplace_back( std::move( job ) );
87 connect( &mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsProfilePlotRenderer::onGeneratingFinished );
89 mFuture = QtConcurrent::map( mJobs, generateProfileStatic );
90 mFutureWatcher.setFuture( mFuture );
100 Q_ASSERT( mJobs.empty() );
101 mJobs.reserve( mGenerators.size() );
103 for (
const auto &it : mGenerators )
105 auto job = std::make_unique< ProfileJob >();
106 job->generator = it.get();
107 job->context = mContext;
108 it.get()->generateProfile( job->context );
109 job->results.reset( job->generator->takeResults() );
110 job->complete =
true;
111 job->invalidatedResults.reset();
112 mJobs.emplace_back( std::move( job ) );
123 disconnect( &mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsProfilePlotRenderer::onGeneratingFinished );
125 for (
const auto &job : mJobs )
127 if ( job->generator )
129 if (
QgsFeedback *feedback = job->generator->feedback() )
136 mFutureWatcher.waitForFinished();
138 onGeneratingFinished();
146 for (
const auto &job : mJobs )
148 if ( job->generator )
150 if (
QgsFeedback *feedback = job->generator->feedback() )
163 disconnect( &mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsProfilePlotRenderer::onGeneratingFinished );
164 mFutureWatcher.waitForFinished();
166 onGeneratingFinished();
171 return mStatus != Idle;
176 if ( mContext == context )
180 const double distanceRangeChanged = context.
distanceRange() != mContext.distanceRange();
181 const double elevationRangeChanged = context.
elevationRange() != mContext.elevationRange();
184 for (
auto &job : mJobs )
190 if ( !jobNeedsRegeneration )
194 job->context = mContext;
195 if ( job->results && job->complete )
196 job->invalidatedResults = std::move( job->results );
197 job->results.reset();
198 job->complete =
false;
205 for (
auto &job : mJobs )
211 if ( !jobNeedsRegeneration )
215 job->context = mContext;
216 if ( job->results && job->complete )
217 job->invalidatedResults = std::move( job->results );
218 job->results.reset();
219 job->complete =
false;
226 replaceSourceInternal( source,
false );
231 return replaceSourceInternal( source,
true );
243 QString sourceId = generator->sourceId();
245 for (
auto &job : mJobs )
247 if ( job->generator && job->generator->sourceId() == sourceId )
251 if ( clearPreviousResults )
253 job->results.reset();
254 job->complete =
false;
256 else if ( job->results )
258 job->results->copyPropertiesFromGenerator( generator.get() );
260 job->generator = generator.get();
263 for (
auto it = mGenerators.begin(); it != mGenerators.end(); )
265 if ( ( *it )->sourceId() == sourceId )
266 it = mGenerators.erase( it );
270 mGenerators.emplace_back( std::move( generator ) );
281 mStatus = Generating;
283 connect( &mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsProfilePlotRenderer::onGeneratingFinished );
285 mFuture = QtConcurrent::map( mJobs, generateProfileStatic );
286 mFutureWatcher.setFuture( mFuture );
291 double min = std::numeric_limits< double >::max();
292 double max = std::numeric_limits< double >::lowest();
293 for (
const auto &job : mJobs )
295 if ( job->complete && job->results )
298 min = std::min( min, jobRange.
lower() );
299 max = std::max( max, jobRange.
upper() );
307 QImage res( width, height, QImage::Format_ARGB32_Premultiplied );
308 res.setDotsPerMeterX( 96 / 25.4 * 1000 );
309 res.setDotsPerMeterY( 96 / 25.4 * 1000 );
310 res.fill( Qt::transparent );
318 const double mapUnitsPerPixel = ( distanceMax - distanceMin ) / width;
321 render( context, width, height, distanceMin, distanceMax, zMin, zMax, sourceId );
322 QRectF plotArea( QPointF( 0, 0 ), QPointF( width, height ) );
329QTransform QgsProfilePlotRenderer::computeRenderTransform(
double width,
double height,
double distanceMin,
double distanceMax,
double zMin,
double zMax )
331 QTransform transform;
332 transform.translate( 0, height );
333 transform.scale( width / ( distanceMax - distanceMin ), -height / ( zMax - zMin ) );
334 transform.translate( -distanceMin, -zMin );
341 QPainter *painter = context.
painter();
346 profileRenderContext.
setWorldTransform( computeRenderTransform( width, height, distanceMin, distanceMax, zMin, zMax ) );
351 for (
auto &job : mJobs )
353 if ( ( sourceId.isEmpty() || job->generator->sourceId() == sourceId ) )
356 if ( job->complete && job->results )
358 job->results->renderResults( profileRenderContext );
360 else if ( !job->complete && job->invalidatedResults )
363 job->invalidatedResults->renderResults( profileRenderContext );
372 auto subSections = std::make_unique< QgsSimpleLineSymbolLayer >( QColor( 255, 0, 0, 255 ), 0.5 );
373 subSections->setPenCapStyle( Qt::FlatCap );
374 return std::make_unique<QgsLineSymbol>(
QgsSymbolLayerList() << subSections.release() );
379 mSubsectionsSymbol.reset( symbol );
384 return mSubsectionsSymbol.get();
389 QgsCurve *profileCurve = mRequest.profileCurve();
390 if ( !profileCurve || profileCurve->
numPoints() < 3 || !mSubsectionsSymbol )
393 QTransform transform = computeRenderTransform( plotArea.width(), plotArea.height(), distanceMin, distanceMax, zMin, zMax );
396 profileCurve->
points( points );
397 QgsPoint firstPoint = points.takeFirst();
400 mSubsectionsSymbol->startRender( context );
401 double accumulatedDistance = 0.;
402 for (
const QgsPoint &point : points )
404 accumulatedDistance += point.distance( firstPoint );
405 QPointF output = transform.map( QPointF( accumulatedDistance, 0. ) );
406 QPolygonF polyLine( QVector<QPointF> { QPointF( output.x() + plotArea.left(), plotArea.top() ), QPointF( output.x() + plotArea.left(), plotArea.bottom() ) } );
407 mSubsectionsSymbol->renderPolyline( polyLine,
nullptr, context );
410 mSubsectionsSymbol->stopRender( context );
416 if ( !mRequest.profileCurve() )
417 return bestSnapResult;
419 double bestSnapDistance = std::numeric_limits< double >::max();
421 for (
const auto &job : mJobs )
424 if ( job->complete && job->results )
432 if ( snapDistance < bestSnapDistance )
434 bestSnapDistance = snapDistance;
435 bestSnapResult = jobSnapResult;
442 return bestSnapResult;
447 QVector<QgsProfileIdentifyResults> res;
448 if ( !mRequest.profileCurve() )
451 for (
const auto &job : mJobs )
454 if ( job->complete && job->results )
456 res.append( job->results->identify( point, context ) );
466 QVector<QgsProfileIdentifyResults> res;
467 if ( !mRequest.profileCurve() )
470 for (
const auto &job : mJobs )
473 if ( job->complete && job->results )
475 res.append( job->results->identify( distanceRange, elevationRange, context ) );
485 QVector<QgsAbstractProfileResults::Feature > res;
486 for (
const auto &job : mJobs )
492 if ( job->complete && job->results )
494 res.append( job->results->asFeatures( type, feedback ) );
501void QgsProfilePlotRenderer::onGeneratingFinished()
507void QgsProfilePlotRenderer::generateProfileStatic( std::unique_ptr< ProfileJob > &job )
512 Q_ASSERT( job->generator );
514 job->generator->generateProfile( job->context );
516 job->results.reset( job->generator->takeResults() );
517 job->complete =
true;
518 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