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