OverlayWidget.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/canvas/OverlayWidget.cpp
22 
23  \brief This file has the OverlayWidget class.
24 */
25 
26 // TerraLib
27 #include "../../../maptools/Utils.h"
28 #include "../../widgets/tools/Pan.h"
29 #include "../../widgets/tools/ZoomWheel.h"
30 #include "../canvas/Canvas.h"
31 #include "../canvas/MultiThreadMapDisplay.h"
32 #include "OverlayWidget.h"
33 #include "ui_OverlayWidgetForm.h"
34 
36 
37 te::qt::widgets::OverlayWidget::OverlayWidget(QWidget* parent, Qt::WindowFlags f)
38  : QWidget(parent, f),
39  m_ui(new Ui::OverlayWidgetForm),
40  m_panTool(nullptr), m_zoomTool(nullptr),
41  m_draft(nullptr), m_draftOriginal(nullptr)
42 {
43  m_ui->setupUi(this);
44 
45 //setup form
46  m_ui->m_applyToolButton->setIcon(QIcon::fromTheme("check"));
47  connect(m_ui->m_applyToolButton, SIGNAL(clicked()), this, SLOT(onApplyToolButtonClicked()));
48  connect(m_ui->m_opacityHorizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(onOpacityChanged(int)));
49  connect(m_ui->m_verticalSlider, SIGNAL(valueChanged(int)), this, SLOT(onVSliderChanged(int)));
50  connect(m_ui->m_horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(onHSliderChanged(int)));
51  connect(m_ui->m_hideToolButton, SIGNAL(toggled(bool)), this, SLOT(onHideToolButtonClicked(bool)));
52 
53 //build map display
54  QGridLayout* displayLayout = new QGridLayout(m_ui->m_widget);
55  m_mapDisplay = new te::qt::widgets::MultiThreadMapDisplay(m_ui->m_widget->size(), m_ui->m_widget);
56  displayLayout->addWidget(m_mapDisplay);
57  displayLayout->setContentsMargins(0,0,0,0);
58 
59  connect(m_mapDisplay, SIGNAL(extentChanged()), this, SLOT(onMapDisplayExtentChanged()));
60 
61 //start tools
62  m_panTool = new te::qt::widgets::Pan(m_mapDisplay, Qt::OpenHandCursor, Qt::ClosedHandCursor);
63  m_zoomTool = new te::qt::widgets::ZoomWheel(m_mapDisplay, 1.5);
64 
65  m_mapDisplay->installEventFilter(m_panTool);
66  m_mapDisplay->installEventFilter(m_zoomTool);
67 }
68 
70 {
71  delete m_panTool;
72  delete m_zoomTool;
73  delete m_draftOriginal;
74 }
75 
76 Ui::OverlayWidgetForm* te::qt::widgets::OverlayWidget::getForm() const
77 {
78  return m_ui.get();
79 }
80 
81 void te::qt::widgets::OverlayWidget::setList(std::list<te::map::AbstractLayerPtr>& layerList)
82 {
83  std::list<te::map::AbstractLayerPtr>::iterator it = layerList.begin();
84 
85  while(it != layerList.end())
86  {
88 
89  m_ui->m_upperComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
90  m_ui->m_lowerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
91 
92  ++it;
93  }
94 }
95 
97 {
98  //get lower layer
99  int lowerIdx = m_ui->m_lowerComboBox->currentIndex();
100  QVariant lowerV = m_ui->m_lowerComboBox->itemData(lowerIdx, Qt::UserRole);
101  te::map::AbstractLayerPtr lowerLayer = lowerV.value<te::map::AbstractLayerPtr>();
102 
103  //draw lower layer
104  std::list<te::map::AbstractLayerPtr> list;
105  list.push_back(lowerLayer);
106 
107  te::gm::Envelope e = lowerLayer->getExtent();
108 
109  m_mapDisplay->setMouseTracking(true);
110  m_mapDisplay->setLayerList(list);
111  m_mapDisplay->setSRID(lowerLayer->getSRID(), false);
112  m_mapDisplay->setExtent(e, true);
113 
114  //draw upper layer
115  int upperIdx = m_ui->m_upperComboBox->currentIndex();
116  QVariant upperV = m_ui->m_upperComboBox->itemData(upperIdx, Qt::UserRole);
117  te::map::AbstractLayerPtr upperLayer = upperV.value<te::map::AbstractLayerPtr>();
118 
119  const te::gm::Envelope& env = m_mapDisplay->getExtent();
120 
122  m_draft->fill(Qt::transparent);
123 
124  // Prepares the canvas
125  Canvas canvas(m_mapDisplay->width(), m_mapDisplay->height());
126  canvas.setDevice(m_draft, false);
127  canvas.setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
128 
129  upperLayer->draw(&canvas, env, m_mapDisplay->getSRID(), m_mapDisplay->getScale(), nullptr);
130 
131  m_mapDisplay->repaint();
132 }
133 
135 {
136  //draw upper layer
137  int upperIdx = m_ui->m_upperComboBox->currentIndex();
138  QVariant upperV = m_ui->m_upperComboBox->itemData(upperIdx, Qt::UserRole);
139  te::map::AbstractLayerPtr upperLayer = upperV.value<te::map::AbstractLayerPtr>();
140 
141  const te::gm::Envelope& env = m_mapDisplay->getExtent();
142 
144  m_draft->fill(Qt::transparent);
145 
146  // Prepares the canvas
147  {
148  Canvas canvas(m_mapDisplay->width(), m_mapDisplay->height());
149  canvas.setDevice(m_draft, false);
150  canvas.setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
151 
152  upperLayer->draw(&canvas, env, m_mapDisplay->getSRID(), m_mapDisplay->getScale(), nullptr);
153  }
154 
155  m_draftOriginal = new QPixmap(*m_mapDisplay->getDraftPixmap());
156 
158 
159  m_mapDisplay->repaint();
160 }
161 
163 {
164  drawUpperLayer();
165 }
166 
168 {
169  if(flag)
170  {
171  m_draft->fill(Qt::transparent);
172  m_mapDisplay->repaint();
173  }
174  else
175  {
176  drawUpperLayer();
177  }
178 }
179 
181 {
182  if(m_ui->m_hideToolButton->isChecked())
183  return;
184 
185  QImage img = m_draftOriginal->toImage();
186 
187  m_draft->fill(Qt::transparent);
188 
189  QPainter p(m_draft);
190  p.setOpacity(m_ui->m_opacityHorizontalSlider->value() / 255.);
191  p.drawImage(0, 0, img, 0, 0, m_ui->m_horizontalSlider->value(), m_ui->m_verticalSlider->value());
192  p.end();
193 
194  m_mapDisplay->repaint();
195 }
196 
198 {
199  m_ui->m_horizontalSlider->setMaximum(static_cast<int>(m_mapDisplay->getWidth()) - 1);
200  m_ui->m_horizontalSlider->setValue(static_cast<int>(m_mapDisplay->getWidth()) - 1);
201  m_ui->m_verticalSlider->setMaximum(static_cast<int>(m_mapDisplay->getHeight()) - 1);
202  m_ui->m_verticalSlider->setValue(static_cast<int>(m_mapDisplay->getHeight()) - 1);
203 }
204 
206 {
207  drawUpperLayer();
208 }
209 
211 {
212  drawUpperLayer();
213 }
unsigned int getWidth() const
It returns the MapDisplay current width in pixels.
void setDevice(QPaintDevice *device, bool takeOwnerShip)
It sets new device as QPrinter.
Ui::OverlayWidgetForm * getForm() const
virtual double getScale() const
Calculates and return the current scale.
void setExtent(te::gm::Envelope &e, bool doRefresh=true)
It sets the world visible area and refreshes the contents in the map display.
std::unique_ptr< Ui::OverlayWidgetForm > m_ui
Definition: OverlayWidget.h:96
This file has the OverlayWidget class.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) te
void onHideToolButtonClicked(bool flag)
void setList(std::list< te::map::AbstractLayerPtr > &layerList)
void setLayerList(const std::list< te::map::AbstractLayerPtr > &layers)
This class implements a concrete tool to geographic pan operation.
Definition: Pan.h:49
An Envelope defines a 2D rectangular region.
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
URI C++ Library.
Definition: Attributes.h:37
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
te::gm::Polygon * p
unsigned int getHeight() const
It returns the MapDisplay current height in pixels.
te::qt::widgets::Pan * m_panTool
Pan tool.
Definition: OverlayWidget.h:99
te::qt::widgets::ZoomWheel * m_zoomTool
Zoom tool.
te::qt::widgets::MultiThreadMapDisplay * m_mapDisplay
Definition: OverlayWidget.h:98
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
This class implements a concrete tool to geographic zoom operation using the mouse wheel...
Definition: ZoomWheel.h:49
virtual void setSRID(const int &srid, bool doRefresh=true)
It sets a new Spatial Reference System to be used by the Map Display.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
A multi thread Qt4 widget to control the display of a set of layers.