qt/tools/MainWindow.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 \file MainWindow.cpp
22 
23  \brief A simple main window to show example of TerraLib Qt Tools.
24 */
25 
26 // Example
27 #include "MainWindow.h"
28 #include "SelectionTool.h"
29 
30 // TerraLib
31 #include "../../Config.h"
32 #include <terralib/common.h>
35 #include <terralib/dataaccess.h>
36 #include <terralib/geometry.h>
37 #include <terralib/maptools.h>
38 #include <terralib/se.h>
39 #include <terralib/raster/Raster.h>
51 
52 // Qt
53 #include <QAction>
54 #include <QActionGroup>
55 #include <QContextMenuEvent>
56 #include <QDockWidget>
57 #include <QFileDialog>
58 #include <QMenu>
59 #include <QStatusBar>
60 #include <QToolBar>
61 
62 // Boost
63 #include <boost/lexical_cast.hpp>
64 
65 // STL
66 #include <cassert>
67 #include <memory>
68 
69 std::size_t MainWindow::ms_id = 0;
70 
71 MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags f)
72  : QMainWindow(parent, f),
73  m_tool(0)
74 {
75  // Creates the tool bar
76  m_toolBar = addToolBar("Tools");
77 
78  // Creates the menu
79  m_menu = new QMenu(this);
80 
81  // Setups the tool actions
82  setupActions();
83 
84  // Creates the Map Display
85  QSize size(800, 600);
88  setCentralWidget(m_display);
89 
90  // Install zoom wheel tool. It's always active!
91  m_display->installEventFilter(new te::qt::widgets::ZoomWheel(m_display, 2.0, m_display));
92 
93  // Install zoom keyboard tool. It's always active!
94  m_display->installEventFilter(new te::qt::widgets::ZoomKeyboard(m_display, 2.0, m_display));
95 
96  // Install coordinate tracking tool. It's always active!
98  connect(coordTracking, SIGNAL(coordTracked(QPointF&)), SLOT(onCoordTracked(QPointF&)));
99  m_display->installEventFilter(coordTracking);
100 
101  // Creates the status progress bar
103  statusBar()->addPermanentWidget(pb);
104 
105  // Creates the multi task progress bar
107 
108  QDockWidget* docWidget = new QDockWidget(this);
109  docWidget->setWidget(pbw);
110  docWidget->setFixedWidth(250);
111  docWidget->setAllowedAreas(Qt::RightDockWidgetArea);
112  docWidget->setWindowTitle(tr("Threads Progress"));
113  addDockWidget(Qt::RightDockWidgetArea, docWidget);
114 
115  // Adjusting
116  setWindowTitle(tr("TerraLib Qt Tools Example"));
117  setMinimumSize(60, 60);
118  resize(size);
119 }
120 
122 {
123  m_layers.clear();
124 }
125 
127 {
128  // Add Vector Data
129  QAction* addVectorData = new QAction(QIcon::fromTheme("list-add"), tr("Add Vector Data..."), this);
130  connect(addVectorData, SIGNAL(triggered()), SLOT(onAddVectorDataTriggered()));
131  m_toolBar->addAction(addVectorData);
132 
133  // Add Raster Data
134  QAction* addRasterData = new QAction(QIcon::fromTheme("list-add"), tr("Add Raster Data..."), this);
135  connect(addRasterData, SIGNAL(triggered()), SLOT(onAddRasterDataTriggered()));
136  m_toolBar->addAction(addRasterData);
137 
138  m_toolBar->addSeparator();
139 
140  // List of created actions
141  QList<QAction*> actions;
142 
143  // Pan
144  QAction* setPan = new QAction(QIcon::fromTheme("pan"), tr("Pan"), this);
145  setPan->setCheckable(true);
146  connect(setPan, SIGNAL(triggered()), SLOT(onPanTriggered()));
147  actions << setPan;
148 
149  // Zoom In
150  QAction* setZoomIn = new QAction(QIcon::fromTheme("zoom-in"), tr("Zoom In"), this);
151  setZoomIn->setCheckable(true);
152  connect(setZoomIn, SIGNAL(triggered()), SLOT(onZoomInTriggered()));
153  actions << setZoomIn;
154 
155  // Zoom In
156  QAction* setZoomOut = new QAction(QIcon::fromTheme("zoom-out"), tr("Zoom Out"), this);
157  setZoomOut->setCheckable(true);
158  connect(setZoomOut, SIGNAL(triggered()), SLOT(onZoomOutTriggered()));
159  actions << setZoomOut;
160 
161  // Zoom Area
162  QAction* setZoomArea = new QAction(QIcon::fromTheme("zoom-area"), tr("Zoom Area"), this);
163  setZoomArea->setCheckable(true);
164  connect(setZoomArea, SIGNAL(triggered()), SLOT(onZoomAreaTriggered()));
165  actions << setZoomArea;
166 
167  // Distance
168  QAction* setDistance = new QAction(QIcon::fromTheme("distance-measure"), tr("Measure Distance"), this);
169  setDistance->setCheckable(true);
170  connect(setDistance, SIGNAL(triggered()), SLOT(onDistanceTriggered()));
171  actions << setDistance;
172 
173  // Area
174  QAction* setArea = new QAction(QIcon::fromTheme("area-measure"), tr("Measure Area"), this);
175  setArea->setCheckable(true);
176  connect(setArea, SIGNAL(triggered()), SLOT(onAreaTriggered()));
177  actions << setArea;
178 
179  // Angle
180  QAction* setAngle = new QAction(QIcon::fromTheme("angle-measure"), tr("Measure Angle"), this);
181  setAngle->setCheckable(true);
182  connect(setAngle, SIGNAL(triggered()), SLOT(onAngleTriggered()));
183  actions << setAngle;
184 
185  // Selection
186  QAction* setSelection = new QAction(QIcon::fromTheme("pointer"), tr("Selection"), this);
187  setSelection->setCheckable(true);
188  connect(setSelection, SIGNAL(triggered()), SLOT(onSelectionTriggered()));
189  actions << setSelection;
190 
191  // Tools group
192  QActionGroup* toolsGroup = new QActionGroup(this);
193 
194  // Add actions on group and toolbar
195  QList<QAction*>::iterator it;
196  for(it = actions.begin(); it != actions.end(); ++it)
197  {
198  toolsGroup->addAction(*it);
199  m_toolBar->addAction(*it);
200  m_menu->addAction(*it);
201  }
202 
203  m_toolBar->addSeparator();
204 
205  // Stop All
206  QAction* stopAll = new QAction(QIcon::fromTheme("process-stop"), tr("Stop All"), this);
207  connect(stopAll, SIGNAL(triggered()), SLOT(onStopAllTriggered()));
208  m_toolBar->addAction(stopAll);
209 }
210 
211 void MainWindow::addDataSetLayer(const QString& path, const std::string& driver)
212 {
213  // Creates and connects data source
214  std::string connInfo ("file://");
215  connInfo += path.toUtf8().data();
216 
217  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().open(boost::lexical_cast<std::string>(ms_id++), driver, connInfo);
218 
219  // Transactor and catalog loader
220  std::unique_ptr<te::da::DataSourceTransactor> transactor(ds->getTransactor());
221 
222  // Get the number of data set types that belongs to the data source
223  std::vector<std::string> datasets = transactor->getDataSetNames();
224  assert(!datasets.empty());
225 
226  // MapDisplay extent
227  te::gm::Envelope env;
228 
229  int srid = 0;
230 
231  for(std::size_t i = 0; i < datasets.size(); ++i)
232  {
233  // Gets DataSet Type
234  //std::unique_ptr<te::da::DataSetType> dt(cl->getDataSetType(datasets[i]));
235  std::unique_ptr<te::da::DataSetType> dt = transactor->getDataSetType(datasets[i]);
236  te::dt::Property* p = dt->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE);
237  std::string propname = p->getName();
238 
239  // Creates a DataSetLayer
240  te::map::DataSetLayer* layer = new te::map::DataSetLayer(te::common::Convert2String(static_cast<unsigned int>(ms_id++)), datasets[i]);
241  layer->setDataSourceId(ds->getId());
242  layer->setDataSetName(datasets[i]);
243  layer->setRendererType("ABSTRACT_LAYER_RENDERER");
245 
246  // Gets the layer extent
247  te::gm::Envelope* e = 0;
248  if(driver == "OGR")
249  e = (transactor->getExtent(datasets[i], propname)).get(); // getExtent(dt->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE));
250  else
251  {
252  std::unique_ptr<te::rst::Raster> raster(te::map::GetRaster(layer));
253  e = raster->getExtent();
254  layer->setSRID(raster->getSRID());
255  srid = raster->getSRID();
256  }
257 
258  // Updates MapDisplay Extent
259  env.Union(*e);
260 
261  // Sets the layer extent
262  layer->setExtent(*e);
263 
264  //delete e;
265 
266  // Adding layer to layer list
267  m_layers.push_back(layer);
268  }
269 
270  // Updates MapDisplay layer list, srid and extent
272  m_display->setSRID(srid, false);
273  m_display->setExtent(env, true);
274 }
275 
276 void MainWindow::contextMenuEvent(QContextMenuEvent* e)
277 {
278  m_menu->popup(e->globalPos());
279 }
280 
282 {
283  QString path = QFileDialog::getOpenFileName(this, tr("Select a vector file..."), TERRALIB_DATA_DIR "/shp/", tr("ShapeFile *.shp"));
284  if(!path.isNull())
285  addDataSetLayer(path, "OGR");
286 }
287 
289 {
290  QString path = QFileDialog::getOpenFileName(this, tr("Select a raster file..."), TERRALIB_DATA_DIR "/rasters/", tr("TIFF *.tif"));
291  if(!path.isNull())
292  addDataSetLayer(path, "GDAL");
293 }
294 
296 {
297  delete m_tool;
298  m_tool = new te::qt::widgets::Pan(m_display, Qt::OpenHandCursor, Qt::ClosedHandCursor);
299  m_display->installEventFilter(m_tool);
300 }
301 
303 {
304  delete m_tool;
306  m_display->installEventFilter(m_tool);
307 }
308 
310 {
311  delete m_tool;
313  m_display->installEventFilter(m_tool);
314 }
315 
317 {
318  delete m_tool;
319  m_tool = new te::qt::widgets::ZoomArea(m_display, Qt::BlankCursor);
320  m_display->installEventFilter(m_tool);
321 }
322 
324 {
325  delete m_tool;
327  m_display->installEventFilter(m_tool);
328 }
329 
331 {
332  delete m_tool;
334  m_display->installEventFilter(m_tool);
335 }
336 
338 {
339  delete m_tool;
341  m_display->installEventFilter(m_tool);
342 }
343 
345 {
346  delete m_tool;
347  m_tool = new SelectionTool(m_display, dynamic_cast<te::map::DataSetLayer*>(m_layers.begin()->get()));
348  m_display->installEventFilter(m_tool);
349 }
350 
352 {
354 }
355 
356 void MainWindow::onCoordTracked(QPointF& coordinate)
357 {
358  QString text = QString::fromUtf8("Coordinates: (") + QString::number(coordinate.x()) + " , " + QString::number(coordinate.y()) + ")";
359  QStatusBar* sb = statusBar();
360  sb->showMessage(text);
361 }
This class implements a concrete tool to geographic zoom operation using the mouse click...
virtual void setDataSourceId(const std::string &id)
This class can be used to inform the progress of a task.
This class implements a concrete tool to geographic zoom operation using the mouse wheel...
A class that defines the interface of a qt widget to group a set of ProgressWidgetItem.
boost::shared_ptr< DataSource > DataSourcePtr
A singleton class used to manage tasks progresses and their viewers.
Raster property.
This class implements a concrete tool to geographic zoom in operation using a boundary rectangle...
Definition: ZoomArea.h:49
void setDataSetName(const std::string &name)
void addDataSetLayer(const QString &path, const std::string &driver)
void setSRID(int srid)
It sets the Spatial Reference System ID associated to the Layer.
An abstract class for raster data strucutures.
This file contains include headers for TerraLib Symbology Encoding module.
static te::dt::Date ds(2010, 01, 01)
This class implements a concrete tool to geographic zoom in operation using a boundary rectangle...
It models a property definition.
Definition: Property.h:59
A multi thread Qt4 widget to control the display of a set of layers.
This class implements a concrete tool to measure operation (distance, area, and angle).
void setRendererType(const std::string &t)
This class implements a concrete tool to geographic zoom operation using the keyboard.
virtual void setLayerList(const std::list< te::map::AbstractLayerPtr > &layers)
It sets the layer list to be showed in the Map Display.
A class that defines the interface of a qt widget to group a set of ProgressWidgetItem.
This class implements a concrete tool to geographic pan operation.
Definition: Pan.h:49
virtual void setResizePolicy(const ResizePolicy &policy)
Sets the resize policy to this map display.
void onAddVectorDataTriggered()
void contextMenuEvent(QContextMenuEvent *e)
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
std::list< te::map::AbstractLayer * > m_layers
An Envelope defines a 2D rectangular region.
te::qt::widgets::AbstractTool * m_tool
This class implements a concrete tool to geographic coordinate tracking on mouse move operation...
MainWindow(QWidget *parent=0)
Constructor.
static te::dt::TimeDuration dt(20, 30, 50, 11)
virtual void setVisibility(Visibility v)
It sets the layer visibility.
A class that defines the interface of a qt bar progress viewer.
te::gm::Polygon * p
void cancelTasks(unsigned int type)
Cancels the task with the given task type and inform all viewers that a task was canceled.
virtual void setExtent(const te::gm::Envelope &mbr)
It sets the Layer extent (or minimum bounding box).
This class implements a concrete tool to geographic zoom operation using the mouse click...
Definition: ZoomClick.h:49
te::qt::widgets::MapDisplay * m_display
This class implements a concrete tool to geographic coordinate tracking on mouse move operation...
Definition: CoordTracking.h:52
This class implements a concrete tool to geographic pan operation.
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
An example of MapDisplay Tool. The only purpose of this tool is to show how you can implement a new t...
This class implements a concrete tool to geographic zoom operation using the mouse wheel...
Definition: ZoomWheel.h:49
void onCoordTracked(QPointF &coordinate)
This file contains include headers for the TerraLib Common Runtime module.
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56
This file contains include headers for the Vector Geometry model of TerraLib.
This class implements a concrete tool to measure operation (distance, area, and angle).
Definition: Measure.h:66
static std::size_t ms_id
This file contains include headers for the Data Access module of TerraLib.
A class that defines the interface of a qt bar progress viewer.
virtual void setSRID(const int &srid, bool doRefresh=true)
It sets a new Spatial Reference System to be used by the Map Display.
This file contains include headers for the Map Tools module of TerraLib.
void onAddRasterDataTriggered()
virtual void setExtent(te::gm::Envelope &e, bool doRefresh=true)
It sets the world visible area and refreshes the contents in the map display.
A multi thread Qt4 widget to control the display of a set of layers.
This class implements a concrete tool to geographic zoom operation using the keyboard.
Definition: ZoomKeyboard.h:49
const std::string & getName() const
It returns the property name.
Definition: Property.h:127