Skip to content

Commit 68e2951

Browse files
committedNov 18, 2013
Allow cancellation of PAL labeling computation
1 parent 1110aaf commit 68e2951

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed
 

‎src/core/pal/pal.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ namespace pal
8585
// do not init and exit GEOS - we do it inside QGIS
8686
//initGEOS( geosNotice, geosError );
8787

88+
fnIsCancelled = 0;
89+
fnIsCancelledContext = 0;
90+
8891
layers = new QList<Layer*>();
8992

9093
lyrsMutex = new SimpleMutex();
@@ -316,6 +319,9 @@ namespace pal
316319
double scale = (( FilterContext* ) ctx )->scale;
317320
Pal* pal = (( FilterContext* )ctx )->pal;
318321

322+
if ( pal->isCancelled() )
323+
return false; // do not continue searching
324+
319325
double amin[2], amax[2];
320326
pset->getBoundingBox( amin, amax );
321327

@@ -509,6 +515,13 @@ namespace pal
509515
filterCtx.pal = this;
510516
obstacles->Search( amin, amax, filteringCallback, ( void* ) &filterCtx );
511517

518+
if ( isCancelled() )
519+
{
520+
delete fFeats;
521+
delete prob;
522+
delete obstacles;
523+
return 0;
524+
}
512525

513526
int idlp = 0;
514527
for ( i = 0; i < prob->nbft; i++ ) /* foreach feature into prob */
@@ -580,6 +593,14 @@ namespace pal
580593
j = 0;
581594
while ( fFeats->size() > 0 ) // foreach feature
582595
{
596+
if ( isCancelled() )
597+
{
598+
delete fFeats;
599+
delete prob;
600+
delete obstacles;
601+
return 0;
602+
}
603+
583604
feat = fFeats->pop_front();
584605
for ( i = 0; i < feat->nblp; i++, idlp++ ) // foreach label candidate
585606
{
@@ -798,6 +819,12 @@ namespace pal
798819
return solution;
799820
}
800821

822+
void Pal::registerCancellationCallback(Pal::FnIsCancelled fnCancelled, void *context)
823+
{
824+
fnIsCancelled = fnCancelled;
825+
fnIsCancelledContext = context;
826+
}
827+
801828
Problem* Pal::extractProblem( double scale, double bbox[4] )
802829
{
803830
// find out: nbLayers, layersName, layersFactor

‎src/core/pal/pal.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ namespace pal
172172
*/
173173
bool showPartial;
174174

175+
176+
typedef bool (*FnIsCancelled)(void* ctx);
177+
/** Callback that may be called from PAL to check whether the job has not been cancelled in meanwhile */
178+
FnIsCancelled fnIsCancelled;
179+
/** Application-specific context for the cancellation check function */
180+
void* fnIsCancelledContext;
181+
175182
/**
176183
* \brief Problem factory
177184
* Extract features to label and generates candidates for them,
@@ -339,6 +346,11 @@ namespace pal
339346
PalStat **stat,
340347
bool displayAll );
341348

349+
/** Register a function that returns whether this job has been cancelled - PAL calls it during the computation */
350+
void registerCancellationCallback( FnIsCancelled fnCancelled, void* context );
351+
352+
/** Check whether the job has been cancelled */
353+
inline bool isCancelled() { return fnIsCancelled ? fnIsCancelled( fnIsCancelledContext ) : false; }
342354

343355
Problem* extractProblem( double scale, double bbox[4] );
344356

‎src/core/pal/problem.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,13 @@ namespace pal
349349

350350
while ( list->getSize() > 0 ) // O (log size)
351351
{
352+
if ( pal->isCancelled() )
353+
{
354+
delete context;
355+
delete list;
356+
return;
357+
}
358+
352359
label = list->getBest(); // O (log size)
353360

354361

‎src/core/qgspallabeling.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3818,12 +3818,21 @@ void QgsPalLabeling::dataDefinedDropShadow( QgsPalLayerSettings& tmpLyr,
38183818
}
38193819
}
38203820

3821+
3822+
// helper function for checking for job cancellation within PAL
3823+
static bool _palIsCancelled( void* ctx )
3824+
{
3825+
return ( ( QgsRenderContext* ) ctx )->renderingStopped();
3826+
}
3827+
38213828
void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
38223829
{
38233830
Q_ASSERT( mMapSettings != NULL );
38243831
QPainter* painter = context.painter();
38253832
QgsRectangle extent = context.extent();
38263833

3834+
mPal->registerCancellationCallback( &_palIsCancelled, &context );
3835+
38273836
delete mResults;
38283837
mResults = new QgsLabelingResults;
38293838

@@ -3849,6 +3858,9 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
38493858
return;
38503859
}
38513860

3861+
if ( context.renderingStopped() )
3862+
return; // it has been cancelled
3863+
38523864
const QgsMapToPixel& xform = mMapSettings->mapToPixel();
38533865

38543866
// draw rectangles with all candidates
@@ -3876,12 +3888,23 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
38763888
QgsDebugMsgLevel( QString( "LABELING work: %1 ms ... labels# %2" ).arg( t.elapsed() ).arg( labels->size() ), 4 );
38773889
t.restart();
38783890

3891+
if ( context.renderingStopped() )
3892+
{
3893+
delete problem;
3894+
delete labels;
3895+
deleteTemporaryData();
3896+
return;
3897+
}
3898+
38793899
painter->setRenderHint( QPainter::Antialiasing );
38803900

38813901
// draw the labels
38823902
std::list<LabelPosition*>::iterator it = labels->begin();
38833903
for ( ; it != labels->end(); ++it )
38843904
{
3905+
if ( context.renderingStopped() )
3906+
break;
3907+
38853908
QgsPalGeometry* palGeometry = dynamic_cast< QgsPalGeometry* >(( *it )->getFeaturePart()->getUserGeometry() );
38863909
if ( !palGeometry )
38873910
{
@@ -3996,7 +4019,11 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
39964019

39974020
delete problem;
39984021
delete labels;
4022+
deleteTemporaryData();
4023+
}
39994024

4025+
void QgsPalLabeling::deleteTemporaryData()
4026+
{
40004027
// delete all allocated geometries for features
40014028
QHash<QgsVectorLayer*, QgsPalLayerSettings>::iterator lit;
40024029
for ( lit = mActiveLayers.begin(); lit != mActiveLayers.end(); ++lit )

‎src/core/qgspallabeling.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,8 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
813813
void dataDefinedDropShadow( QgsPalLayerSettings& tmpLyr,
814814
const QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant >& ddValues );
815815

816+
void deleteTemporaryData();
817+
816818
// hashtable of layer settings, being filled during labeling
817819
QHash<QgsVectorLayer*, QgsPalLayerSettings> mActiveLayers;
818820
// hashtable of active diagram layers

0 commit comments

Comments
 (0)
Please sign in to comment.