Add method to retrieve all labels from QgsLabelingResults, instead of just labels within a rect

This commit is contained in:
Nyall Dawson 2021-06-08 10:24:16 +10:00
parent 8bfb959e29
commit 8e2fb49718
7 changed files with 56 additions and 1 deletions

View File

@ -25,6 +25,13 @@ Class that stores computed placement from labeling engine.
~QgsLabelingResults(); ~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; QList<QgsLabelPosition> labelsAtPosition( const QgsPointXY &p ) const;
%Docstring %Docstring
Returns the details of any labels placed at the specified point (in map coordinates). Returns the details of any labels placed at the specified point (in map coordinates).

View File

@ -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

View File

@ -23,6 +23,12 @@ QgsLabelingResults::QgsLabelingResults()
QgsLabelingResults::~QgsLabelingResults() = default; QgsLabelingResults::~QgsLabelingResults() = default;
QList<QgsLabelPosition> QgsLabelingResults::allLabels() const
{
return mLabelSearchTree ? mLabelSearchTree->allLabels() : QList<QgsLabelPosition>();
}
QList<QgsLabelPosition> QgsLabelingResults::labelsAtPosition( const QgsPointXY &p ) const QList<QgsLabelPosition> QgsLabelingResults::labelsAtPosition( const QgsPointXY &p ) const
{ {
QList<QgsLabelPosition> positions; QList<QgsLabelPosition> positions;

View File

@ -40,6 +40,13 @@ class CORE_EXPORT QgsLabelingResults
//! QgsLabelingResults cannot be copied. //! QgsLabelingResults cannot be copied.
QgsLabelingResults &operator=( const QgsLabelingResults &rh ) = delete; 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). * Returns the details of any labels placed at the specified point (in map coordinates).
*/ */

View File

@ -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 void QgsLabelSearchTree::labelsInRect( const QgsRectangle &r, QList<QgsLabelPosition *> &posList ) const
{ {
QList<QgsLabelPosition *> searchResults; QList<QgsLabelPosition *> searchResults;

View File

@ -74,6 +74,13 @@ class CORE_EXPORT QgsLabelSearchTree
//TODO: why does this break bindings with QList<QgsLabelPosition>? //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 * Returns label position(s) in given rectangle. QgsLabelSearchTree keeps ownership, don't delete the LabelPositions
* \note not available in Python bindings * \note not available in Python bindings

View File

@ -1961,7 +1961,17 @@ void TestQgsLabelingEngine::labelingResults()
QVERIFY( results ); QVERIFY( results );
// retrieve some labels // 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.count(), 1 );
QCOMPARE( labels.at( 0 ).featureId, 1 ); QCOMPARE( labels.at( 0 ).featureId, 1 );
QCOMPARE( labels.at( 0 ).labelText, QStringLiteral( "1" ) ); QCOMPARE( labels.at( 0 ).labelText, QStringLiteral( "1" ) );