Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add method to retrieve all labels from QgsLabelingResults, instead of…
… just labels within a rect
  • Loading branch information
nyalldawson committed Jun 9, 2021
1 parent 8bfb959 commit 8e2fb49
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1 deletion.
7 changes: 7 additions & 0 deletions python/core/auto_generated/labeling/qgslabelingresults.sip.in
Expand Up @@ -25,6 +25,13 @@ Class that stores computed placement from labeling engine.
~QgsLabelingResults();


QList< QgsLabelPosition > allLabels() const;
%Docstring
Returns a list of all labels generated by the labeling run.

.. versionadded:: 3.20
%End

QList<QgsLabelPosition> labelsAtPosition( const QgsPointXY &p ) const;
%Docstring
Returns the details of any labels placed at the specified point (in map coordinates).
Expand Down
7 changes: 7 additions & 0 deletions python/core/auto_generated/labeling/qgslabelsearchtree.sip.in
Expand Up @@ -41,6 +41,13 @@ Removes and deletes all the entries.



QList< QgsLabelPosition > allLabels() const;
%Docstring
Returns a list of all labels generated by the labeling run.

.. versionadded:: 3.20
%End




Expand Down
6 changes: 6 additions & 0 deletions src/core/labeling/qgslabelingresults.cpp
Expand Up @@ -23,6 +23,12 @@ QgsLabelingResults::QgsLabelingResults()

QgsLabelingResults::~QgsLabelingResults() = default;


QList<QgsLabelPosition> QgsLabelingResults::allLabels() const
{
return mLabelSearchTree ? mLabelSearchTree->allLabels() : QList<QgsLabelPosition>();
}

QList<QgsLabelPosition> QgsLabelingResults::labelsAtPosition( const QgsPointXY &p ) const
{
QList<QgsLabelPosition> positions;
Expand Down
7 changes: 7 additions & 0 deletions src/core/labeling/qgslabelingresults.h
Expand Up @@ -40,6 +40,13 @@ class CORE_EXPORT QgsLabelingResults
//! QgsLabelingResults cannot be copied.
QgsLabelingResults &operator=( const QgsLabelingResults &rh ) = delete;

/**
* Returns a list of all labels generated by the labeling run.
*
* \since QGIS 3.20
*/
QList< QgsLabelPosition > allLabels() const;

/**
* Returns the details of any labels placed at the specified point (in map coordinates).
*/
Expand Down
11 changes: 11 additions & 0 deletions src/core/labeling/qgslabelsearchtree.cpp
Expand Up @@ -42,6 +42,17 @@ void QgsLabelSearchTree::label( const QgsPointXY &point, QList<QgsLabelPosition
}
}

QList<QgsLabelPosition> QgsLabelSearchTree::allLabels() const
{
QList<QgsLabelPosition> res;
res.reserve( mOwnedPositions.size() );
for ( const std::unique_ptr< QgsLabelPosition > &pos : mOwnedPositions )
{
res.append( * pos );
}
return res;
}

void QgsLabelSearchTree::labelsInRect( const QgsRectangle &r, QList<QgsLabelPosition *> &posList ) const
{
QList<QgsLabelPosition *> searchResults;
Expand Down
7 changes: 7 additions & 0 deletions src/core/labeling/qgslabelsearchtree.h
Expand Up @@ -74,6 +74,13 @@ class CORE_EXPORT QgsLabelSearchTree

//TODO: why does this break bindings with QList<QgsLabelPosition>?

/**
* Returns a list of all labels generated by the labeling run.
*
* \since QGIS 3.20
*/
QList< QgsLabelPosition > allLabels() const;

/**
* Returns label position(s) in given rectangle. QgsLabelSearchTree keeps ownership, don't delete the LabelPositions
* \note not available in Python bindings
Expand Down
12 changes: 11 additions & 1 deletion tests/src/core/testqgslabelingengine.cpp
Expand Up @@ -1961,7 +1961,17 @@ void TestQgsLabelingEngine::labelingResults()
QVERIFY( results );

// retrieve some labels
QList<QgsLabelPosition> labels = results->labelsAtPosition( QgsPointXY( -654732, 7003282 ) );
QList<QgsLabelPosition> labels = results->allLabels();
QCOMPARE( labels.count(), 3 );
std::sort( labels.begin(), labels.end(), []( const QgsLabelPosition & a, const QgsLabelPosition & b )
{
return a.labelText.compare( b.labelText );
} );
QCOMPARE( labels.at( 0 ).labelText, QStringLiteral( "1" ) );
QCOMPARE( labels.at( 1 ).labelText, QStringLiteral( "8888" ) );
QCOMPARE( labels.at( 2 ).labelText, QStringLiteral( "33333" ) );

labels = results->labelsAtPosition( QgsPointXY( -654732, 7003282 ) );
QCOMPARE( labels.count(), 1 );
QCOMPARE( labels.at( 0 ).featureId, 1 );
QCOMPARE( labels.at( 0 ).labelText, QStringLiteral( "1" ) );
Expand Down

0 comments on commit 8e2fb49

Please sign in to comment.