QGIS API Documentation  2.12.0-Lyon
qgslabelsearchtree.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslabelsearchtree.cpp
3  ---------------------
4  begin : November 2010
5  copyright : (C) 2010 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "qgslabelsearchtree.h"
16 #include "labelposition.h"
17 
18 bool searchCallback( QgsLabelPosition* pos, void* context )
19 {
20  QList<QgsLabelPosition*>* list = static_cast< QList<QgsLabelPosition*>* >( context );
21  list->push_back( pos );
22  return true;
23 }
24 
26 {
27 }
28 
30 {
31  clear();
32 }
33 
35 {
36  double c_min[2]; c_min[0] = p.x() - 0.1; c_min[1] = p.y() - 0.1;
37  double c_max[2]; c_max[0] = p.x() + 0.1; c_max[1] = p.y() + 0.1;
38 
39  QList<QgsLabelPosition*> searchResults;
40  mSpatialIndex.Search( c_min, c_max, searchCallback, &searchResults );
41 
42  //tolerance +-0.1 could be high in case of degree crs, so check if p is really contained in the results
43  posList.clear();
44  QList<QgsLabelPosition*>::const_iterator resultIt = searchResults.constBegin();
45  for ( ; resultIt != searchResults.constEnd(); ++resultIt )
46  {
47  if (( *resultIt )->labelRect.contains( p ) )
48  {
49  posList.push_back( *resultIt );
50  }
51  }
52 }
53 
55 {
56  double c_min[2]; c_min[0] = r.xMinimum(); c_min[1] = r.yMinimum();
57  double c_max[2]; c_max[0] = r.xMaximum(); c_max[1] = r.yMaximum();
58 
59  QList<QgsLabelPosition*> searchResults;
60  mSpatialIndex.Search( c_min, c_max, searchCallback, &searchResults );
61 
62  posList.clear();
63  QList<QgsLabelPosition*>::const_iterator resultIt = searchResults.constBegin();
64  for ( ; resultIt != searchResults.constEnd(); ++resultIt )
65  {
66  posList.push_back( *resultIt );
67  }
68 }
69 
70 bool QgsLabelSearchTree::insertLabel( LabelPosition* labelPos, int featureId, const QString& layerName, const QString& labeltext, const QFont& labelfont, bool diagram, bool pinned )
71 {
72  if ( !labelPos )
73  {
74  return false;
75  }
76 
77  double c_min[2];
78  double c_max[2];
79  labelPos->getBoundingBox( c_min, c_max );
80 
81  QVector<QgsPoint> cornerPoints;
82  cornerPoints.reserve( 4 );
83  for ( int i = 0; i < 4; ++i )
84  {
85  cornerPoints.push_back( QgsPoint( labelPos->getX( i ), labelPos->getY( i ) ) );
86  }
87  QgsLabelPosition* newEntry = new QgsLabelPosition( featureId, labelPos->getAlpha(), cornerPoints, QgsRectangle( c_min[0], c_min[1], c_max[0], c_max[1] ),
88  labelPos->getWidth(), labelPos->getHeight(), layerName, labeltext, labelfont, labelPos->getUpsideDown(), diagram, pinned );
89  mSpatialIndex.Insert( c_min, c_max, newEntry );
90  mOwnedPositions << newEntry;
91  return true;
92 }
93 
95 {
96  mSpatialIndex.RemoveAll();
97 
98  //PAL rtree iterator is buggy and doesn't iterate over all items, so we can't iterate through the tree to delete positions
99  qDeleteAll( mOwnedPositions );
100  mOwnedPositions.clear();
101 }
void clear()
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void label(const QgsPoint &p, QList< QgsLabelPosition * > &posList) const
Returns label position(s) at a given point.
void push_back(const T &value)
double getWidth() const
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:196
double x() const
Get the x value of the point.
Definition: qgspoint.h:126
void getBoundingBox(double amin[2], double amax[2]) const
Return bounding box - amin: xmin,ymin - amax: xmax,ymax.
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:201
double getY(int i=0) const
get the down-left y coordinate
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:186
double getAlpha() const
get alpha
bool searchCallback(QgsLabelPosition *pos, void *context)
A class to represent a point.
Definition: qgspoint.h:63
bool insertLabel(LabelPosition *labelPos, int featureId, const QString &layerName, const QString &labeltext, const QFont &labelfont, bool diagram=false, bool pinned=false)
Inserts label position.
void reserve(int size)
bool getUpsideDown() const
double getHeight() const
void clear()
Removes and deletes all the entries.
LabelPosition is a candidate feature label position.
Definition: labelposition.h:48
void push_back(const T &value)
double y() const
Get the y value of the point.
Definition: qgspoint.h:134
double getX(int i=0) const
get the down-left x coordinate
const_iterator constEnd() const
const_iterator constBegin() const
void labelsInRect(const QgsRectangle &r, QList< QgsLabelPosition * > &posList) const
Returns label position(s) in given rectangle.
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:191