Zoom.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/Zoom.cpp
22 
23  \brief This is a utility class to geographic zoom operation.
24 */
25 
26 // TerraLib
27 #include "../../../geometry/Coord2D.h"
28 #include "../../../geometry/Envelope.h"
29 #include "../canvas/MapDisplay.h"
30 #include "Zoom.h"
31 
32 // Qt
33 #include <QMouseEvent>
34 
35 te::qt::widgets::Zoom::Zoom(te::qt::widgets::MapDisplay* display, const double& zoomFactor, const ZoomType& type, QObject* parent)
36  : AbstractTool(display, parent),
37  m_zoomFactor(zoomFactor),
38  m_zoomType(type)
39 {
40 }
41 
43 
45 {
46  m_zoomType = type;
47 }
48 
49 void te::qt::widgets::Zoom::applyZoom(const QPointF& point, bool /*centralize*/)
50 {
51  // Gets the current display extent
52  const te::gm::Envelope& currentExtent = m_display->getExtent();
53  if(!currentExtent.isValid())
54  return;
55 
56  // Adjusting zoom factor based on zoomType
57  double factor = m_zoomFactor;
58  if(m_zoomType == In)
59  factor = 1 / factor;
60 
61  // If point is not null, the zoom extent will be centered on this point. Otherwise, keep the current center.
62  te::gm::Envelope extent;
63  if(point.isNull() == true)
64  {
65  te::gm::Coord2D center;
66  point.isNull() ? center = currentExtent.getCenter() : center = te::gm::Coord2D(point.x(), point.y());
67 
68  // Bulding the zoom extent based on zoom factor value and the given point
69  double w = currentExtent.getWidth() * factor * 0.5;
70  double h = currentExtent.getHeight() * factor * 0.5;
71 
72  extent.init(center.x - w, center.y - h, center.x + w, center.y + h);
73  }
74  else
75  {
76  te::gm::Coord2D reference = te::gm::Coord2D(point.x(), point.y());
77 
78  double dxPercentage = (point.x() - currentExtent.getLowerLeftX()) / currentExtent.getWidth();
79  double dyPercentage = (point.y() - currentExtent.getLowerLeftY()) / currentExtent.getHeight();
80 
81  // Bulding the zoom extent based on zoom factor value and the given point
82  double w = currentExtent.getWidth() * factor;
83  double h = currentExtent.getHeight() * factor;
84 
85  double leftWidth = w * dxPercentage;
86  double rightWidth = w * (1. - dxPercentage);
87 
88  double lowerHeight = h * dyPercentage;
89  double upperHeight = h * (1. - dyPercentage);
90 
91  extent.init(reference.x - leftWidth, reference.y - lowerHeight, reference.x + rightWidth, reference.y + upperHeight);
92  }
93 
94  // Updates the map display with the new extent
95  m_display->setExtent(extent);
96 }
97 
99 {
100  return true;
101 }
102 
104 {
105  m_display->repaint();
106 
107  return true;
108 }
109 
111 {
112  m_display->repaint();
113 
114  return true;
115 }
double m_zoomFactor
Factor used to zoom. i.e. A factor value of 2.0 (default) will generate a new extent twice (%) bigger...
Definition: Zoom.h:110
void init(const double &llx, const double &lly, const double &urx, const double &ury)
It initializes (sets) the envelope bounds.
virtual bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
Definition: Zoom.cpp:98
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
This is a utility class to geographic zoom operation.
virtual ~Zoom()
Destructor.
void setZoomType(const ZoomType &type)
Sets the zoom operation type.
Definition: Zoom.cpp:44
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
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
virtual bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
Definition: Zoom.cpp:103
This class defines an interface for objects that can receive application events and respond to them...
Definition: AbstractTool.h:63
ZoomType
Defines the zoom type.
Definition: Zoom.h:58
ZoomType m_zoomType
Enum that indicates the zoom type.
Definition: Zoom.h:111
virtual bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
Definition: Zoom.cpp:110
Coord2D getCenter() const
It returns the rectangle&#39;s center coordinate.
An Envelope defines a 2D rectangular region.
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
Zoom(MapDisplay *display, const double &zoomFactor=2.0, const ZoomType &type=In, QObject *parent=0)
It constructs a zoom associated with the given map display.
Definition: Zoom.cpp:35
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.
void applyZoom(const QPointF &point=QPointF(), bool centralize=true)
Performs the zoom operation on map display, considering the zoom factor, zoom type and the given poin...
Definition: Zoom.cpp:49