Skip to content

Commit 8e2fb49

Browse files
committedJun 9, 2021
Add method to retrieve all labels from QgsLabelingResults, instead of just labels within a rect
1 parent 8bfb959 commit 8e2fb49

File tree

7 files changed

+56
-1
lines changed

7 files changed

+56
-1
lines changed
 

‎python/core/auto_generated/labeling/qgslabelingresults.sip.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ Class that stores computed placement from labeling engine.
2525
~QgsLabelingResults();
2626

2727

28+
QList< QgsLabelPosition > allLabels() const;
29+
%Docstring
30+
Returns a list of all labels generated by the labeling run.
31+
32+
.. versionadded:: 3.20
33+
%End
34+
2835
QList<QgsLabelPosition> labelsAtPosition( const QgsPointXY &p ) const;
2936
%Docstring
3037
Returns the details of any labels placed at the specified point (in map coordinates).

‎python/core/auto_generated/labeling/qgslabelsearchtree.sip.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ Removes and deletes all the entries.
4141

4242

4343

44+
QList< QgsLabelPosition > allLabels() const;
45+
%Docstring
46+
Returns a list of all labels generated by the labeling run.
47+
48+
.. versionadded:: 3.20
49+
%End
50+
4451

4552

4653

‎src/core/labeling/qgslabelingresults.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ QgsLabelingResults::QgsLabelingResults()
2323

2424
QgsLabelingResults::~QgsLabelingResults() = default;
2525

26+
27+
QList<QgsLabelPosition> QgsLabelingResults::allLabels() const
28+
{
29+
return mLabelSearchTree ? mLabelSearchTree->allLabels() : QList<QgsLabelPosition>();
30+
}
31+
2632
QList<QgsLabelPosition> QgsLabelingResults::labelsAtPosition( const QgsPointXY &p ) const
2733
{
2834
QList<QgsLabelPosition> positions;

‎src/core/labeling/qgslabelingresults.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class CORE_EXPORT QgsLabelingResults
4040
//! QgsLabelingResults cannot be copied.
4141
QgsLabelingResults &operator=( const QgsLabelingResults &rh ) = delete;
4242

43+
/**
44+
* Returns a list of all labels generated by the labeling run.
45+
*
46+
* \since QGIS 3.20
47+
*/
48+
QList< QgsLabelPosition > allLabels() const;
49+
4350
/**
4451
* Returns the details of any labels placed at the specified point (in map coordinates).
4552
*/

‎src/core/labeling/qgslabelsearchtree.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ void QgsLabelSearchTree::label( const QgsPointXY &point, QList<QgsLabelPosition
4242
}
4343
}
4444

45+
QList<QgsLabelPosition> QgsLabelSearchTree::allLabels() const
46+
{
47+
QList<QgsLabelPosition> res;
48+
res.reserve( mOwnedPositions.size() );
49+
for ( const std::unique_ptr< QgsLabelPosition > &pos : mOwnedPositions )
50+
{
51+
res.append( * pos );
52+
}
53+
return res;
54+
}
55+
4556
void QgsLabelSearchTree::labelsInRect( const QgsRectangle &r, QList<QgsLabelPosition *> &posList ) const
4657
{
4758
QList<QgsLabelPosition *> searchResults;

‎src/core/labeling/qgslabelsearchtree.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ class CORE_EXPORT QgsLabelSearchTree
7474

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

77+
/**
78+
* Returns a list of all labels generated by the labeling run.
79+
*
80+
* \since QGIS 3.20
81+
*/
82+
QList< QgsLabelPosition > allLabels() const;
83+
7784
/**
7885
* Returns label position(s) in given rectangle. QgsLabelSearchTree keeps ownership, don't delete the LabelPositions
7986
* \note not available in Python bindings

‎tests/src/core/testqgslabelingengine.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,17 @@ void TestQgsLabelingEngine::labelingResults()
19611961
QVERIFY( results );
19621962

19631963
// retrieve some labels
1964-
QList<QgsLabelPosition> labels = results->labelsAtPosition( QgsPointXY( -654732, 7003282 ) );
1964+
QList<QgsLabelPosition> labels = results->allLabels();
1965+
QCOMPARE( labels.count(), 3 );
1966+
std::sort( labels.begin(), labels.end(), []( const QgsLabelPosition & a, const QgsLabelPosition & b )
1967+
{
1968+
return a.labelText.compare( b.labelText );
1969+
} );
1970+
QCOMPARE( labels.at( 0 ).labelText, QStringLiteral( "1" ) );
1971+
QCOMPARE( labels.at( 1 ).labelText, QStringLiteral( "8888" ) );
1972+
QCOMPARE( labels.at( 2 ).labelText, QStringLiteral( "33333" ) );
1973+
1974+
labels = results->labelsAtPosition( QgsPointXY( -654732, 7003282 ) );
19651975
QCOMPARE( labels.count(), 1 );
19661976
QCOMPARE( labels.at( 0 ).featureId, 1 );
19671977
QCOMPARE( labels.at( 0 ).labelText, QStringLiteral( "1" ) );

0 commit comments

Comments
 (0)
Please sign in to comment.