Pan.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/Pan.cpp
22 
23  \brief This class implements a concrete tool to geographic pan operation.
24 */
25 
26 // TerraLib
27 #include "../../../geometry/Envelope.h"
28 #include "../canvas/MapDisplay.h"
29 #include "Pan.h"
30 
31 // Qt
32 #include <QMouseEvent>
33 #include <QPainter>
34 #include <QPixmap>
35 
36 te::qt::widgets::Pan::Pan(te::qt::widgets::MapDisplay* display, const QCursor& cursor, const QCursor& actionCursor, QObject* parent)
37  : AbstractTool(display, parent),
38  m_panStarted(false),
39  m_actionCursor(actionCursor)
40 {
41  setCursor(cursor);
42 }
43 
44 te::qt::widgets::Pan::~Pan() = default;
45 
47 {
48  if(e->button() != Qt::LeftButton)
49  return false;
50 
51  m_panStarted = true;
52  m_origin = e->pos();
53  m_delta *= 0;
55 
56  // Adjusting the action cursor
57  if(m_actionCursor.shape() != Qt::BlankCursor)
58  m_display->setCursor(m_actionCursor);
59 
60  return true;
61 }
62 
64 {
65  if(!m_panStarted)
66  return false;
67 
68  // Calculates the delta value
69  m_delta = e->pos() - m_origin;
70 
71  // Gets the draft map display pixmap
72  QPixmap* draft = m_display->getDraftPixmap();
73  draft->fill();
74 
75  // Gets the current result of map display, i.e. The draw layer composition.
76  QPixmap* result = m_display->getDisplayPixmap();
77 
78  // Let's draw!
79  QPainter painter(draft);
80  painter.drawPixmap(0, 0, *result); // Draw the current result.
81  painter.save();
82  painter.setOpacity(0.3); // Adjusting transparency feedback.
83  painter.drawPixmap(m_delta, *result); // Draw the current result translated.
84  painter.restore();
85 
86  m_display->repaint();
87 
88  return true;
89 }
90 
92 {
93  m_panStarted = false;
94 
95  // Roll back the default tool cursor
96  m_display->setCursor(m_cursor);
97 
98  if(e->button() != Qt::LeftButton || m_delta.isNull())
99  return false;
100 
101  // Clears the draft map display pixmap
102  QPixmap* draft = m_display->getDraftPixmap();
103  draft->fill(Qt::transparent);
104 
105  // Updates the map display with the new extent
106  const te::gm::Envelope& extent = m_display->getExtent();
107  double width = extent.getWidth();
108  double height = extent.getHeight();
109 
110  //Calculates the transtation, in World Coordinate System
111  QPointF oldCenter(extent.getCenter().getX(), extent.getCenter().getY());
112 
113  QPointF releasePoint(m_display->transform(e->pos()));
114  double dx = releasePoint.x() - m_referencePoint.x();
115  double dy = releasePoint.y() - m_referencePoint.y();
116 
117  //Calculates the new center of the extent, in World Coordinate System
118  QPointF newCenter(oldCenter.x() - dx, oldCenter.y() - dy);
119 
120  te::gm::Envelope newExtent(newCenter.x() - (width / 2.), newCenter.y() - (height / 2.), newCenter.x() + (width / 2.), newCenter.y() + (height / 2.));
121 
122  //Sets the new extent to the map display
123  m_display->setExtent(newExtent);
124 
125  return true;
126 }
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
Definition: Pan.cpp:91
bool m_panStarted
Flag that indicates if pan operation was started.
Definition: Pan.h:91
Pan(MapDisplay *display, const QCursor &cursor, const QCursor &actionCursor=Qt::BlankCursor, QObject *parent=0)
It constructs a pan tool associated with the given map display and with the specified cursors...
Definition: Pan.cpp:36
QPointF m_referencePoint
The reference origin point, in World Coordinate System.
Definition: Pan.h:95
static te::dt::Date dx(2010, 12, 31)
double getWidth() const
It returns the envelope width.
A widget to control the display of a set of layers.
double getY() const
It returns the y-coordinate.
Definition: Coord2D.h:108
QCursor m_cursor
The default tool cursor.
Definition: AbstractTool.h:172
This class defines an interface for objects that can receive application events and respond to them...
Definition: AbstractTool.h:63
QCursor m_actionCursor
An optional cursor to be used during the pan user action.
Definition: Pan.h:94
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.
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
Definition: Pan.cpp:63
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
Definition: Pan.cpp:46
An Envelope defines a 2D rectangular region.
virtual void setCursor(const QCursor &cursor)
It sets the tool cursor.
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
This class implements a concrete tool to geographic pan operation.
QPoint m_origin
Origin point on mouse pressed.
Definition: Pan.h:92
~Pan()
Destructor.
double getX() const
It returns the x-coordinate.
Definition: Coord2D.h:102
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
QPoint m_delta
Difference between pressed point and destination point on mouse move.
Definition: Pan.h:93
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.
virtual QPixmap * getDisplayPixmap() const
It returns the map display pixmap.