Quantum GIS API Documentation  1.8
src/gui/qgsmaptool.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgsmaptool.cpp  -  base class for map canvas tools
00003     ----------------------
00004     begin                : January 2006
00005     copyright            : (C) 2006 by Martin Dobias
00006     email                : wonder.sk at gmail 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 
00016 #include "qgslogger.h"
00017 #include "qgsmaptool.h"
00018 #include "qgsmapcanvas.h"
00019 #include "qgsmaptopixel.h"
00020 #include "qgsmaprenderer.h"
00021 #include <QAction>
00022 #include <QAbstractButton>
00023 
00024 QgsMapTool::QgsMapTool( QgsMapCanvas* canvas )
00025     : QObject( canvas ), mCanvas( canvas ), mCursor( Qt::CrossCursor ), mAction( NULL ), mButton( NULL )
00026 {
00027 }
00028 
00029 
00030 QgsMapTool::~QgsMapTool()
00031 {
00032   mCanvas->unsetMapTool( this );
00033 }
00034 
00035 
00036 QgsPoint QgsMapTool::toMapCoordinates( const QPoint& point )
00037 {
00038   return mCanvas->getCoordinateTransform()->toMapCoordinates( point );
00039 }
00040 
00041 
00042 QgsPoint QgsMapTool::toLayerCoordinates( QgsMapLayer* layer, const QPoint& point )
00043 {
00044   QgsPoint pt = toMapCoordinates( point );
00045   return toLayerCoordinates( layer, pt );
00046 }
00047 
00048 QgsPoint QgsMapTool::toLayerCoordinates( QgsMapLayer* layer, const QgsPoint& point )
00049 {
00050   return mCanvas->mapRenderer()->mapToLayerCoordinates( layer, point );
00051 }
00052 
00053 QgsPoint QgsMapTool::toMapCoordinates( QgsMapLayer* layer, const QgsPoint& point )
00054 {
00055   return mCanvas->mapRenderer()->layerToMapCoordinates( layer, point );
00056 }
00057 
00058 QgsRectangle QgsMapTool::toLayerCoordinates( QgsMapLayer* layer, const QgsRectangle& rect )
00059 {
00060   return mCanvas->mapRenderer()->mapToLayerCoordinates( layer, rect );
00061 }
00062 
00063 QPoint QgsMapTool::toCanvasCoordinates( const QgsPoint& point )
00064 {
00065   double x = point.x(), y = point.y();
00066   mCanvas->getCoordinateTransform()->transformInPlace( x, y );
00067   return QPoint(( int )( x + 0.5 ), ( int )( y + 0.5 ) ); // round the values
00068 }
00069 
00070 
00071 void QgsMapTool::activate()
00072 {
00073   // make action and/or button active
00074   if ( mAction )
00075     mAction->setChecked( true );
00076   if ( mButton )
00077     mButton->setChecked( true );
00078 
00079   // set cursor (map tools usually set it in constructor)
00080   mCanvas->setCursor( mCursor );
00081   QgsDebugMsg( "Cursor has been set" );
00082 }
00083 
00084 
00085 void QgsMapTool::deactivate()
00086 {
00087   if ( mAction )
00088     mAction->setChecked( false );
00089   if ( mButton )
00090     mButton->setChecked( false );
00091 }
00092 
00093 void QgsMapTool::setAction( QAction* action )
00094 {
00095   mAction = action;
00096 }
00097 
00098 QAction* QgsMapTool::action()
00099 {
00100   return mAction;
00101 }
00102 
00103 void QgsMapTool::setButton( QAbstractButton* button )
00104 {
00105   mButton = button;
00106 }
00107 
00108 QAbstractButton* QgsMapTool::button()
00109 {
00110   return mButton;
00111 }
00112 
00113 
00114 void QgsMapTool::canvasMoveEvent( QMouseEvent *e )
00115 {
00116   Q_UNUSED( e );
00117 }
00118 
00119 void QgsMapTool::canvasDoubleClickEvent( QMouseEvent *e )
00120 {
00121   Q_UNUSED( e );
00122 }
00123 
00124 void QgsMapTool::canvasPressEvent( QMouseEvent *e )
00125 {
00126   Q_UNUSED( e );
00127 }
00128 
00129 void QgsMapTool::canvasReleaseEvent( QMouseEvent *e )
00130 {
00131   Q_UNUSED( e );
00132 }
00133 
00134 void QgsMapTool::keyPressEvent( QKeyEvent *e )
00135 {
00136   Q_UNUSED( e );
00137 }
00138 
00139 void QgsMapTool::keyReleaseEvent( QKeyEvent *e )
00140 {
00141   Q_UNUSED( e );
00142 }
00143 
00144 #ifdef HAVE_TOUCH
00145 bool QgsMapTool::gestureEvent( QGestureEvent *e )
00146 {
00147   Q_UNUSED( e );
00148 }
00149 #endif
00150 
00151 void QgsMapTool::renderComplete()
00152 {
00153 }
00154 
00155 bool QgsMapTool::isTransient()
00156 {
00157   return false;
00158 }
00159 
00160 bool QgsMapTool::isEditTool()
00161 {
00162   return false;
00163 }
00164 
00165 QgsMapCanvas* QgsMapTool::canvas()
00166 {
00167   return mCanvas;
00168 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines