/
qgsrendereditemresults.cpp
175 lines (153 loc) · 5.66 KB
/
qgsrendereditemresults.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/***************************************************************************
qgsrendereditemresults.cpp
-------------------
begin : August 2021
copyright : (C) Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsrendereditemresults.h"
#include "qgsrendereditemdetails.h"
#include "qgsrendercontext.h"
#include "qgslogger.h"
#include "qgsrenderedannotationitemdetails.h"
#include "RTree.h"
///@cond PRIVATE
class QgsRenderedItemResultsSpatialIndex : public RTree<const QgsRenderedItemDetails *, float, 2, float>
{
public:
void insert( const QgsRenderedItemDetails *details, const QgsRectangle &bounds )
{
std::array< float, 4 > scaledBounds = scaleBounds( bounds );
this->Insert(
{
scaledBounds[0], scaledBounds[ 1]
},
{
scaledBounds[2], scaledBounds[3]
},
details );
}
/**
* Performs an intersection check against the index, for data intersecting the specified \a bounds.
*
* The \a callback function will be called once for each matching data object encountered.
*/
bool intersects( const QgsRectangle &bounds, const std::function< bool( const QgsRenderedItemDetails *details )> &callback ) const
{
std::array< float, 4 > scaledBounds = scaleBounds( bounds );
this->Search(
{
scaledBounds[0], scaledBounds[ 1]
},
{
scaledBounds[2], scaledBounds[3]
},
callback );
return true;
}
private:
std::array<float, 4> scaleBounds( const QgsRectangle &bounds ) const
{
return
{
static_cast< float >( bounds.xMinimum() ),
static_cast< float >( bounds.yMinimum() ),
static_cast< float >( bounds.xMaximum() ),
static_cast< float >( bounds.yMaximum() )
};
}
};
///@endcond
QgsRenderedItemResults::QgsRenderedItemResults()
: mAnnotationItemsIndex( std::make_unique< QgsRenderedItemResultsSpatialIndex >() )
{
}
QgsRenderedItemResults::~QgsRenderedItemResults() = default;
QList<QgsRenderedItemDetails *> QgsRenderedItemResults::renderedItems() const
{
QList< QgsRenderedItemDetails * > res;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
res.reserve( static_cast< int >( mDetails.size() ) );
#else
res.reserve( mDetails.size() );
#endif
std::transform( mDetails.begin(), mDetails.end(), std::back_inserter( res ), []( const auto & detail ) {return detail.get();} );
return res;
}
QList<const QgsRenderedAnnotationItemDetails *> QgsRenderedItemResults::renderedAnnotationItemsInBounds( const QgsRectangle &bounds ) const
{
QList<const QgsRenderedAnnotationItemDetails *> res;
mAnnotationItemsIndex->intersects( bounds, [&res]( const QgsRenderedItemDetails * details )->bool
{
res << qgis::down_cast< const QgsRenderedAnnotationItemDetails * >( details );
return true;
} );
return res;
}
void QgsRenderedItemResults::appendResults( const QList<QgsRenderedItemDetails *> &results, const QgsRenderContext &context )
{
const QgsCoordinateTransform layerToMapTransform = context.coordinateTransform();
for ( QgsRenderedItemDetails *details : results )
{
try
{
const QgsRectangle transformedBounds = layerToMapTransform.transformBoundingBox( details->boundingBox() );
details->setBoundingBox( transformedBounds );
}
catch ( QgsCsException & )
{
QgsDebugMsg( QStringLiteral( "Could not transform rendered item's bounds to map CRS" ) );
}
if ( QgsRenderedAnnotationItemDetails *annotationDetails = dynamic_cast< QgsRenderedAnnotationItemDetails * >( details ) )
mAnnotationItemsIndex->insert( annotationDetails, annotationDetails->boundingBox() );
mDetails.emplace_back( std::unique_ptr< QgsRenderedItemDetails >( details ) );
}
}
void QgsRenderedItemResults::transferResults( QgsRenderedItemResults *other, const QStringList &layerIds )
{
for ( auto it = other->mDetails.begin(); it != other->mDetails.end(); )
{
if ( layerIds.contains( ( *it )->layerId() ) )
{
if ( QgsRenderedAnnotationItemDetails *annotationDetails = dynamic_cast< QgsRenderedAnnotationItemDetails * >( ( *it ).get() ) )
mAnnotationItemsIndex->insert( annotationDetails, annotationDetails->boundingBox() );
mDetails.emplace_back( std::move( *it ) );
other->mDetails.erase( it );
}
else
{
it++;
}
}
}
void QgsRenderedItemResults::transferResults( QgsRenderedItemResults *other )
{
for ( auto it = other->mDetails.begin(); it != other->mDetails.end(); ++it )
{
if ( QgsRenderedAnnotationItemDetails *annotationDetails = dynamic_cast< QgsRenderedAnnotationItemDetails * >( ( *it ).get() ) )
mAnnotationItemsIndex->insert( annotationDetails, annotationDetails->boundingBox() );
mDetails.emplace_back( std::move( *it ) );
}
other->mDetails.clear();
}
void QgsRenderedItemResults::eraseResultsFromLayers( const QStringList &layerIds )
{
for ( auto it = mDetails.begin(); it != mDetails.end(); )
{
if ( layerIds.contains( ( *it )->layerId() ) )
{
mDetails.erase( it );
}
else
{
it++;
}
}
}