Quantum GIS API Documentation  1.7.4
src/core/qgslabelsearchtree.cpp
Go to the documentation of this file.
00001 #include "qgslabelsearchtree.h"
00002 #include "labelposition.h"
00003 
00004 bool searchCallback( QgsLabelPosition* pos, void* context )
00005 {
00006   QList<QgsLabelPosition*>* list = static_cast< QList<QgsLabelPosition*>* >( context );
00007   list->push_back( pos );
00008   return true;
00009 }
00010 
00011 QgsLabelSearchTree::QgsLabelSearchTree()
00012 {
00013 }
00014 
00015 QgsLabelSearchTree::~QgsLabelSearchTree()
00016 {
00017   clear();
00018 }
00019 
00020 void QgsLabelSearchTree::label( const QgsPoint& p, QList<QgsLabelPosition*>& posList )
00021 {
00022   double c_min[2]; c_min[0] = p.x() - 0.1; c_min[1] = p.y() - 0.1;
00023   double c_max[2]; c_max[0] = p.x() + 0.1; c_max[1] = p.y() + 0.1;
00024 
00025   QList<QgsLabelPosition*> searchResults;
00026   mSpatialIndex.Search( c_min, c_max, searchCallback, &searchResults );
00027 
00028   //tolerance +-0.1 could be high in case of degree crs, so check if p is really contained in the results
00029   posList.clear();
00030   QList<QgsLabelPosition*>::const_iterator resultIt = searchResults.constBegin();
00031   for ( ; resultIt != searchResults.constEnd(); ++resultIt )
00032   {
00033     if (( *resultIt )->labelRect.contains( p ) )
00034     {
00035       posList.push_back( *resultIt );
00036     }
00037   }
00038 }
00039 
00040 bool QgsLabelSearchTree::insertLabel( LabelPosition* labelPos, int featureId, const QString& layerName, bool diagram )
00041 {
00042   if ( !labelPos )
00043   {
00044     return false;
00045   }
00046 
00047   double c_min[2];
00048   double c_max[2];
00049   labelPos->getBoundingBox( c_min, c_max );
00050 
00051   QVector<QgsPoint> cornerPoints;
00052   for ( int i = 0; i < 4; ++i )
00053   {
00054     cornerPoints.push_back( QgsPoint( labelPos->getX( i ), labelPos->getY( i ) ) );
00055   }
00056   QgsLabelPosition* newEntry = new QgsLabelPosition( featureId, labelPos->getAlpha(), cornerPoints, QgsRectangle( c_min[0], c_min[1], c_max[0], c_max[1] ),
00057       labelPos->getWidth(), labelPos->getHeight(), layerName, labelPos->getUpsideDown(), diagram );
00058   mSpatialIndex.Insert( c_min, c_max, newEntry );
00059   return true;
00060 }
00061 
00062 void QgsLabelSearchTree::clear()
00063 {
00064   RTree<QgsLabelPosition*, double, 2, double>::Iterator indexIt;
00065   mSpatialIndex.GetFirst( indexIt );
00066   while ( !mSpatialIndex.IsNull( indexIt ) )
00067   {
00068     delete mSpatialIndex.GetAt( indexIt );
00069     mSpatialIndex.GetNext( indexIt );
00070   }
00071   mSpatialIndex.RemoveAll();
00072 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines