Skip to content

Commit

Permalink
Merged maptips-branch 7939:7967 into trunk
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@7968 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
gsherman committed Jan 15, 2008
1 parent 7c6eb96 commit abe00ff
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 0 deletions.
Binary file added images/themes/default/mActionMapTips.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -65,6 +65,7 @@
#include <QtGlobal>
#include <QRegExp>
#include <QRegExpValidator>
#include <QTimer>
//
// Mac OS X Includes
// Must include before GEOS 3 due to unqualified use of 'Point'
Expand Down Expand Up @@ -106,12 +107,14 @@
#include "qgsmaplayerregistry.h"
#include "qgsmapoverviewcanvas.h"
#include "qgsmaprender.h"
#include "qgsmaptip.h"
#include "qgsmessageviewer.h"
#include "qgsoptions.h"
#include "qgspastetransformations.h"
#include "qgspluginitem.h"
#include "qgspluginmanager.h"
#include "qgspluginregistry.h"
#include "qgspoint.h"
#include "qgsproject.h"
#include "qgsprojectproperties.h"
#include "qgsproviderregistry.h"
Expand Down Expand Up @@ -324,6 +327,7 @@ static void customSrsValidation_(QgsSpatialRefSys* srs)
createCanvas();
createOverview();
createLegend();
createMapTips();

mComposer = new QgsComposer(this); // Map composer
mInternalClipboard = new QgsClipboard; // create clipboard
Expand Down Expand Up @@ -395,6 +399,8 @@ static void customSrsValidation_(QgsSpatialRefSys* srs)
restoreWindowState();

mSplash->showMessage(tr("QGIS Ready!"), Qt::AlignHCenter | Qt::AlignBottom);

mMapTipsVisible = false;
qApp->processEvents();
} // QgisApp ctor

Expand Down Expand Up @@ -792,6 +798,12 @@ void QgisApp::createActions()
mActionEditPaste->setStatusTip(tr("Paste selected features"));
connect(mActionEditPaste, SIGNAL(triggered()), this, SLOT(editPaste()));
mActionEditPaste->setEnabled(false);

// maptips
mActionMapTips = new QAction(QIcon(myIconPath+"/mActionMapTips.png"), tr("Map Tips"), this);
mActionMapTips->setStatusTip(tr("Show information about a feature when the mouse is hovered over it"));
connect ( mActionMapTips, SIGNAL ( triggered() ), this, SLOT ( toggleMapTips() ) );
mActionMapTips->setCheckable(true);

#ifdef HAVE_PYTHON
mActionShowPythonDialog = new QAction(tr("Python console"), this);
Expand Down Expand Up @@ -1036,6 +1048,7 @@ void QgisApp::createToolBars()
mAttributesToolBar->addAction(mActionOpenTable);
mAttributesToolBar->addAction(mActionMeasure);
mAttributesToolBar->addAction(mActionMeasureArea);
mAttributesToolBar->addAction(mActionMapTips);
mAttributesToolBar->addAction(mActionShowBookmarks);
mAttributesToolBar->addAction(mActionNewBookmark);
//
Expand Down Expand Up @@ -1365,6 +1378,18 @@ bool QgisApp::createDB()
return TRUE;
}

void QgisApp::createMapTips()
{
// Set up the timer for maptips. The timer is reset everytime the mouse is moved
mpMapTipsTimer = new QTimer ( mMapCanvas );
// connect the timer to the maptips slot
connect ( mpMapTipsTimer, SIGNAL ( timeout() ), this, SLOT ( showMapTip() ) );
// set the interval to 0.850 seconds - timer will be started next time the mouse moves
mpMapTipsTimer->setInterval ( 850 );
// Create the maptips object
mpMaptip = new QgsMapTip ();
}

// Update file menu with the current list of recently accessed projects
void QgisApp::updateRecentProjectPaths()
{
Expand Down Expand Up @@ -3402,6 +3427,7 @@ void QgisApp::attributeTable()
mMapLegend->legendLayerAttributeTable();
}


void QgisApp::deleteSelected()
{
QgsMapLayer *layer = mMapLegend->currentLayer();
Expand Down Expand Up @@ -3635,6 +3661,16 @@ void QgisApp::refreshMapCanvas()
mMapCanvas->refresh();
}

void QgisApp::toggleMapTips()
{
mMapTipsVisible = !mMapTipsVisible;
// if off, stop the timer
if ( !mMapTipsVisible )
{
mpMapTipsTimer->stop();
}
}

void QgisApp::toggleEditing()
{
if(mMapCanvas && mMapCanvas->isDrawing())
Expand Down Expand Up @@ -3663,8 +3699,26 @@ void QgisApp::showMouseCoordinate(QgsPoint & p)
{
mCoordsLabel->setMinimumWidth(mCoordsLabel->width());
}

if ( mMapTipsVisible )
{
// store the point, we need it for when the maptips timer fires
mLastMapPosition = p;

// we use this slot to control the timer for maptips since it is fired each time
// the mouse moves.
if ( mMapCanvas->underMouse() )
{
// Clear the maptip (this is done conditionally)
mpMaptip->clear ( mMapCanvas );
// don't start the timer if the mouse is not over the map canvas
mpMapTipsTimer->start();
QgsDebugMsg("Started maptips timer");
}
}
}


void QgisApp::showScale(double theScale)
{
if (theScale >= 1.0)
Expand Down Expand Up @@ -4685,6 +4739,50 @@ void QgisApp::showStatusMessage(QString theMessage)
statusBar()->message(theMessage);
}

void QgisApp::showMapTip()

{
/* Show the maptip using tooltip */
// Stop the timer while we look for a maptip
mpMapTipsTimer->stop();

// Only show tooltip if the mouse is over the canvas
if ( mMapCanvas->underMouse() )
{
QPoint myPointerPos = mMapCanvas->mouseLastXY();

// Following is debug stuff
QgsDebugMsg ( "Mouse IS over canvas" );
QgsDebugMsg ( "Maptips timer fired:" );
QgsDebugMsg ( mLastMapPosition.stringRep() );
QgsDebugMsg ( "Pixel coordinates of mouse position:" );
QgsDebugMsg ( QString::number ( myPointerPos.x() ) + "," + QString::number ( myPointerPos.y() ) );
// end debug stuff

// Make sure there is an active layer before proceeding

QgsMapLayer* mypLayer = mMapCanvas->currentLayer();
if ( mypLayer )
{
QgsDebugMsg("Current layer for maptip display is: " + mypLayer->source());
// only process vector layers
if ( mypLayer->type() == QgsMapLayer::VECTOR )
{


// Show the maptip if the maptips button is depressed
if(mMapTipsVisible)
{
mpMaptip->showMapTip ( mypLayer, mLastMapPosition, myPointerPos, mMapCanvas );
}
}
}
else
{
QgsDebugMsg ( "Maptips require an active layer" );
}
}
}
void QgisApp::projectPropertiesProjections()
{
// Driver to display the project props dialog and switch to the
Expand Down
25 changes: 25 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -43,6 +43,7 @@ class QgsHelpViewer;
class QgsLegend;
class QgsMapCanvas;
class QgsMapLayer;
class QgsMapTip;
class QgsMapTool;
class QgsPoint;
class QgsProviderRegistry;
Expand All @@ -57,6 +58,7 @@ class QgsRect;
#include <QAbstractSocket>

#include "qgsconfig.h"
#include <qgspoint.h>

/*! \class QgisApp
* \brief Main window for the Qgis application
Expand Down Expand Up @@ -379,6 +381,12 @@ public slots:
//! Shows a warning when an old project file is read.
void warnOlderProjectVersion(QString);

//! Toggle map tips on/off
void toggleMapTips();

//! Show the map tip
void showMapTip();

signals:
/** emitted when a key is pressed and we want non widget sublasses to be able
to pick up on this (e.g. maplayer) */
Expand Down Expand Up @@ -450,6 +458,7 @@ public slots:
void createOverview();
void createCanvas();
bool createDB();
void createMapTips();
//toolbars ----------------------------------------
QToolBar *mFileToolBar;
QToolBar *mLayerToolBar;
Expand Down Expand Up @@ -509,6 +518,7 @@ public slots:
QAction *mActionZoomLast;
QAction *mActionZoomToLayer;
QAction *mActionIdentify;
QAction *mActionMapTips;
QAction *mActionSelect;
QAction *mActionOpenTable;
QAction *mActionMeasure;
Expand Down Expand Up @@ -638,6 +648,21 @@ public slots:
*/
QString mRasterFileFilter;

/** Timer for map tips
*/
QTimer *mpMapTipsTimer;

/** Point of last mouse position in map coordinates (used with MapTips)
*/
QgsPoint mLastMapPosition;

/* Maptip object
*/
QgsMapTip * mpMaptip;

// Flag to indicate if maptips are on or off
bool mMapTipsVisible;

#ifdef HAVE_PYTHON
QgsPythonDialog* mPythonConsole;
#endif
Expand Down
1 change: 1 addition & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -21,6 +21,7 @@ qgsprojectionselector.cpp
qgsquickprint.cpp
qgsrubberband.cpp
qgsvertexmarker.cpp
qgsmaptip.cpp
)

SET(QGIS_GUI_MOC_HDRS
Expand Down
129 changes: 129 additions & 0 deletions src/gui/qgsmaptip.cpp
@@ -0,0 +1,129 @@
/***************************************************************************
qgsmaptips.cpp - Query a layer and show a maptip on the canvas
---------------------
begin : October 2007
copyright : (C) 2007 by Gary Sherman
email : sherman @ mrcc 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. *
* *
***************************************************************************/
// QGIS includes
#include <qgsmapcanvas.h>
#include <qgsmaplayer.h>
#include <qgsvectordataprovider.h>
#include <qgsfield.h>

// Qt includes
#include <QPoint>
#include <QToolTip>
#include <QSettings>

#include "qgsmaptip.h"

QgsMapTip::QgsMapTip ()
{
// init the visible flag
mMapTipVisible = false;
}

QgsMapTip::~QgsMapTip()
{

}

void QgsMapTip::showMapTip ( QgsMapLayer * thepLayer,
QgsPoint & theMapPosition,
QPoint & thePixelPosition,
QgsMapCanvas *thepMapCanvas )
{
// Do the search using the active layer and the preferred label
// field for the layer. The label field must be defined in the layer configuration
// file/database. The code required to do this is similar to identify, except
// we only want the first qualifying feature and we will only display the
// field defined as the label field in the layer configuration file/database.
//
// TODO: Define the label (display) field for each map layer in the map configuration file/database

// Show the maptip on the canvas
QString myTipText = fetchFeature ( thepLayer, theMapPosition, thepMapCanvas );
if ( myTipText.length() > 0 )
{
mMapTipVisible = true;
QToolTip::showText ( thepMapCanvas->mapToGlobal ( thePixelPosition ), myTipText, thepMapCanvas );
// store the point so we can use it to clear the maptip later
mLastPosition = thePixelPosition;
}
else
{
mMapTipVisible = false;
}

}

void QgsMapTip::clear ( QgsMapCanvas *mpMapCanvas )
{
if ( mMapTipVisible )
{
// set the maptip to blank
QToolTip::showText ( mpMapCanvas->mapToGlobal ( mLastPosition ), "", mpMapCanvas );
// reset the visible flag
mMapTipVisible = false;
}
}

QString QgsMapTip::fetchFeature ( QgsMapLayer *layer, QgsPoint & mapPosition, QgsMapCanvas *mpMapCanvas )
{
// Default return value
QString maptipText = "";
// Protection just in case we get passed a null layer
if(layer)
{
// Get the setting for the search radius from user preferences, if it exists
QSettings settings;
double identifyValue = settings.value ( "/Map/identifyRadius", QGis::DEFAULT_IDENTIFY_RADIUS ).toDouble();

// create the search rectangle
double searchRadius = mpMapCanvas->extent().width() * ( identifyValue / 100.0 );
QgsRect r;
r.setXmin ( mapPosition.x() - searchRadius );
r.setXmax ( mapPosition.x() + searchRadius );
r.setYmin ( mapPosition.y() - searchRadius );
r.setYmax ( mapPosition.y() + searchRadius );

// Get the data provider
QgsVectorDataProvider* dataProvider = dynamic_cast<QgsVectorLayer*> ( layer )->getDataProvider();
// Fetch the attribute list for the layer
QgsAttributeList allAttributes = dataProvider->allAttributesList();
// Select all attributes within the search radius
dataProvider->select ( allAttributes, r, true, true );
// Feature to hold the results of the fetch
QgsFeature feature;
// Get the field list for the layer
const QgsFieldMap& fields = dataProvider->fields();
// Get the label (display) field for the layer
QString fieldIndex = dynamic_cast<QgsVectorLayer*> ( layer )->displayField();
if ( dataProvider->getNextFeature ( feature ) )
{
// if we get a feature, pull out the display field and set the maptip text to its value
QgsAttributeMap attributes = feature.attributeMap();
for ( QgsAttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
{

if ( fields[it.key() ].name() == fieldIndex )
{
maptipText = it->toString();

}

}
}
}
// return the map tip
return maptipText;
}

0 comments on commit abe00ff

Please sign in to comment.