Quantum GIS API Documentation  1.8
src/gui/qgsmaptip.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgsmaptips.cpp  -  Query a layer and show a maptip on the canvas
00003     ---------------------
00004     begin                : October 2007
00005     copyright            : (C) 2007 by Gary Sherman
00006     email                : sherman @ mrcc dot com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 // QGIS includes
00016 #include <qgsmapcanvas.h>
00017 #include <qgsvectorlayer.h>
00018 
00019 // Qt includes
00020 #include <QPoint>
00021 #include <QToolTip>
00022 #include <QSettings>
00023 
00024 #include "qgsmaptip.h"
00025 
00026 QgsMapTip::QgsMapTip()
00027 {
00028   // init the visible flag
00029   mMapTipVisible = false;
00030 }
00031 
00032 QgsMapTip::~QgsMapTip()
00033 {
00034 
00035 }
00036 
00037 void QgsMapTip::showMapTip( QgsMapLayer *thepLayer,
00038                             QgsPoint & theMapPosition,
00039                             QPoint & thePixelPosition,
00040                             QgsMapCanvas *thepMapCanvas )
00041 {
00042   // Do the search using the active layer and the preferred label
00043   // field for the layer. The label field must be defined in the layer configuration
00044   // file/database. The code required to do this is similar to identify, except
00045   // we only want the first qualifying feature and we will only display the
00046   // field defined as the label field in the layer configuration file/database.
00047   //
00048   // TODO: Define the label (display) field for each map layer in the map configuration file/database
00049 
00050   // Show the maptip on the canvas
00051   QString myTipText = fetchFeature( thepLayer, theMapPosition, thepMapCanvas );
00052   mMapTipVisible = !myTipText.isEmpty();
00053 
00054   if ( mMapTipVisible )
00055   {
00056     QToolTip::showText( thepMapCanvas->mapToGlobal( thePixelPosition ), myTipText, thepMapCanvas );
00057     // store the point so we can use it to clear the maptip later
00058     mLastPosition = thePixelPosition;
00059   }
00060 }
00061 
00062 void QgsMapTip::clear( QgsMapCanvas *mpMapCanvas )
00063 {
00064   if ( !mMapTipVisible )
00065     return;
00066 
00067   // set the maptip to blank
00068   QToolTip::showText( mpMapCanvas->mapToGlobal( mLastPosition ), "", mpMapCanvas );
00069   // reset the visible flag
00070   mMapTipVisible = false;
00071 }
00072 
00073 QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint &mapPosition, QgsMapCanvas *mpMapCanvas )
00074 {
00075   QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
00076   if ( !vlayer )
00077     return "";
00078 
00079   // Get the setting for the search radius from user preferences, if it exists
00080   QSettings settings;
00081   double identifyValue = settings.value( "/Map/identifyRadius", QGis::DEFAULT_IDENTIFY_RADIUS ).toDouble();
00082 
00083   // create the search rectangle
00084   double searchRadius = mpMapCanvas->extent().width() * ( identifyValue / 100.0 );
00085 
00086   QgsRectangle r;
00087   r.setXMinimum( mapPosition.x() - searchRadius );
00088   r.setYMinimum( mapPosition.y() - searchRadius );
00089   r.setXMaximum( mapPosition.x() + searchRadius );
00090   r.setYMaximum( mapPosition.y() + searchRadius );
00091 
00092   r = mpMapCanvas->mapRenderer()->mapToLayerCoordinates( layer, r );
00093 
00094   int idx = vlayer->fieldNameIndex( vlayer->displayField() );
00095   if ( idx < 0 )
00096     return "";
00097 
00098   QgsFeature feature;
00099 
00100   vlayer->select( QgsAttributeList() << idx, r, true, true );
00101   if ( !vlayer->nextFeature( feature ) )
00102     return "";
00103 
00104   return feature.attributeMap().value( idx, "" ).toString();
00105 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines