ZoomLeftAndRightClick.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/widgets/tools/ZoomLeftAndRightClick.cpp
22 
23  \brief This class implements a concrete tool to geographic zoom operation using the left and right mouse clicks.
24 */
25 
26 // TerraLib
27 #include "../../../geometry/Coord2D.h"
28 #include "../../../geometry/Envelope.h"
29 #include "../canvas/MapDisplay.h"
30 #include "ZoomLeftAndRightClick.h"
31 
32 // Qt
33 #include <QMouseEvent>
34 
36  te::qt::widgets::MapDisplay* display, const double& zoomFactor,
37  QObject* parent)
38  : AbstractTool(display, parent), m_zoomFactor( zoomFactor )
39 {
40 }
41 
43 
45 {
46  if(e->button() == Qt::LeftButton)
47  {
48  // Gets the current display extent
49  const te::gm::Envelope& currentExtent = m_display->getExtent();
50  if(!currentExtent.isValid())
51  return false;
52 
53  // If point is not null, the zoom extent will be centered on this point. Otherwise, keep the current center.
54  te::gm::Coord2D center;
55 #if QT_VERSION >= 0x050000
56  QPointF point( m_display->transform( e->localPos() ) );
57 #else
58  QPointF point( m_display->transform( e->posF() ) );
59 #endif
60  point.isNull() ? center = currentExtent.getCenter() : center = te::gm::Coord2D(point.x(), point.y());
61 
62  // Bulding the zoom extent based on zoom factor value and the given point
63  double w = currentExtent.getWidth() * ( 1.0 / m_zoomFactor ) * 0.5;
64  double h = currentExtent.getHeight() * ( 1.0 / m_zoomFactor ) * 0.5;
65 
66  te::gm::Envelope e(center.x - w, center.y - h, center.x + w, center.y + h);
67 
68  // Updates the map display with the new extent
69  m_display->setExtent(e);
70  }
71  else if( e->button() == Qt::RightButton)
72  {
73  // Gets the current display extent
74  const te::gm::Envelope& currentExtent = m_display->getExtent();
75  if(!currentExtent.isValid())
76  return false;
77 
78  // If point is not null, the zoom extent will be centered on this point. Otherwise, keep the current center.
79  te::gm::Coord2D center;
80 #if QT_VERSION >= 0x050000
81  QPointF point( m_display->transform( e->localPos() ) );
82 #else
83  QPointF point( m_display->transform( e->posF() ) );
84 #endif
85  point.isNull() ? center = currentExtent.getCenter() : center = te::gm::Coord2D(point.x(), point.y());
86 
87  // Bulding the zoom extent based on zoom factor value and the given point
88  double w = currentExtent.getWidth() * m_zoomFactor * 0.5;
89  double h = currentExtent.getHeight() * m_zoomFactor * 0.5;
90 
91  te::gm::Envelope e(center.x - w, center.y - h, center.x + w, center.y + h);
92 
93  // Updates the map display with the new extent
94  m_display->setExtent(e);
95  }
96 
97  return false;
98 }
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
double getWidth() const
It returns the envelope width.
A widget to control the display of a set of layers.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
This class defines an interface for objects that can receive application events and respond to them...
Definition: AbstractTool.h:63
Coord2D getCenter() const
It returns the rectangle&#39;s center coordinate.
virtual QPointF transform(const QPointF &p)
Transforms the given point, in screen coordinates, to a point in world coordinates.
An Envelope defines a 2D rectangular region.
This class implements a concrete tool to geographic zoom operation using the left and right mouse cli...
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
ZoomLeftAndRightClick(MapDisplay *display, const double &zoomFactor=2.0, QObject *parent=0)
It constructs a zoom click tool associated with the given map display.
double m_zoomFactor
Factor used to zoom. i.e. A factor value of 2.0 (default) will generate a new extent twice (%) bigger...
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
virtual void setExtent(te::gm::Envelope &e, bool doRefresh=true)
It sets the world visible area and refreshes the contents in the map display.
double getHeight() const
It returns the envelope height.
bool isValid() const
It tells if the rectangle is valid or not.