Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgslabelsearchtree.cpp
Go to the documentation of this file.
1 #include "qgslabelsearchtree.h"
2 #include "labelposition.h"
3 
4 bool searchCallback( QgsLabelPosition* pos, void* context )
5 {
6  QList<QgsLabelPosition*>* list = static_cast< QList<QgsLabelPosition*>* >( context );
7  list->push_back( pos );
8  return true;
9 }
10 
12 {
13 }
14 
16 {
17  clear();
18 }
19 
20 void QgsLabelSearchTree::label( const QgsPoint& p, QList<QgsLabelPosition*>& posList )
21 {
22  double c_min[2]; c_min[0] = p.x() - 0.1; c_min[1] = p.y() - 0.1;
23  double c_max[2]; c_max[0] = p.x() + 0.1; c_max[1] = p.y() + 0.1;
24 
25  QList<QgsLabelPosition*> searchResults;
26  mSpatialIndex.Search( c_min, c_max, searchCallback, &searchResults );
27 
28  //tolerance +-0.1 could be high in case of degree crs, so check if p is really contained in the results
29  posList.clear();
30  QList<QgsLabelPosition*>::const_iterator resultIt = searchResults.constBegin();
31  for ( ; resultIt != searchResults.constEnd(); ++resultIt )
32  {
33  if (( *resultIt )->labelRect.contains( p ) )
34  {
35  posList.push_back( *resultIt );
36  }
37  }
38 }
39 
40 bool QgsLabelSearchTree::insertLabel( LabelPosition* labelPos, int featureId, const QString& layerName, bool diagram )
41 {
42  if ( !labelPos )
43  {
44  return false;
45  }
46 
47  double c_min[2];
48  double c_max[2];
49  labelPos->getBoundingBox( c_min, c_max );
50 
51  QVector<QgsPoint> cornerPoints;
52  for ( int i = 0; i < 4; ++i )
53  {
54  cornerPoints.push_back( QgsPoint( labelPos->getX( i ), labelPos->getY( i ) ) );
55  }
56  QgsLabelPosition* newEntry = new QgsLabelPosition( featureId, labelPos->getAlpha(), cornerPoints, QgsRectangle( c_min[0], c_min[1], c_max[0], c_max[1] ),
57  labelPos->getWidth(), labelPos->getHeight(), layerName, labelPos->getUpsideDown(), diagram );
58  mSpatialIndex.Insert( c_min, c_max, newEntry );
59  return true;
60 }
61 
63 {
64  RTree<QgsLabelPosition*, double, 2, double>::Iterator indexIt;
65  mSpatialIndex.GetFirst( indexIt );
66  while ( !mSpatialIndex.IsNull( indexIt ) )
67  {
68  delete mSpatialIndex.GetAt( indexIt );
69  mSpatialIndex.GetNext( indexIt );
70  }
71  mSpatialIndex.RemoveAll();
72 }