qt/rastervisual/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 "ReadPixelTool.h"
29 
30 // TerraLib
31 #include <terralib/common.h>
33 #include <terralib/dataaccess.h>
34 #include <terralib/geometry.h>
35 #include <terralib/maptools.h>
36 #include <terralib/raster.h>
37 #include <terralib/se.h>
49 
50 // Qt
51 #include <QAction>
52 #include <QActionGroup>
53 #include <QContextMenuEvent>
54 #include <QMenu>
55 #include <QStatusBar>
56 #include <QToolBar>
57 #include <QDockWidget>
58 #include <QLayout>
59 
60 // STL
61 #include <cassert>
62 
63 #include <boost/lexical_cast.hpp>
64 
65 std::size_t MainWindow::ms_id = 0;
66 
67 MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags f)
68  : QMainWindow(parent, f),
69  m_tool(0),
70  m_startTools(false),
71  m_rvW(0)
72 {
73  // Creates the tool bar
74  m_toolBar = addToolBar("Tools");
75 
76  // Creates the menu
77  m_menu = new QMenu(this);
78 
79  // Creates the status bar
80  statusBar();
81 
82  // Setup dock window
84 
85  // Add File Chooser Interface
87  fc->setFilterPattern("*.tif");
88  statusBar()->addPermanentWidget(fc);
89 
90  connect(fc, SIGNAL(resourceSelected(QString)), this, SLOT(onFileSelected(QString)));
91 
92 
93  // Setups the tool actions
94  setupActions();
95 
97 
98  // Creates the Map Display
99  QSize size(512, 512);
100  m_display = new te::qt::widgets::MapDisplay(size, this);
102  setCentralWidget(m_display);
103 
104  //progress
106 
107  // Adjusting
108  setWindowTitle(tr("TerraLib Qt Raster Visual Example"));
109  setMinimumSize(400, 400);
110  resize(size);
111 }
112 
114 {
115  delete m_dlgViewer;
116 
119 }
120 
122 {
123  // List of created actions
124 
125  // Pan
126  QAction* setPan = new QAction(QIcon::fromTheme("pan"), tr("Pan"), this);
127  setPan->setCheckable(true);
128  connect(setPan, SIGNAL(triggered()), SLOT(onPanTriggered()));
129  m_actions << setPan;
130 
131  // Zoom In
132  QAction* setZoomIn = new QAction(QIcon::fromTheme("zoom-in"), tr("Zoom In"), this);
133  setZoomIn->setCheckable(true);
134  connect(setZoomIn, SIGNAL(triggered()), SLOT(onZoomInTriggered()));
135  m_actions << setZoomIn;
136 
137  // Zoom In
138  QAction* setZoomOut = new QAction(QIcon::fromTheme("zoom-out"), tr("Zoom Out"), this);
139  setZoomOut->setCheckable(true);
140  connect(setZoomOut, SIGNAL(triggered()), SLOT(onZoomOutTriggered()));
141  m_actions << setZoomOut;
142 
143  // Zoom Area
144  QAction* setZoomArea = new QAction(QIcon::fromTheme("zoom-area"), tr("Zoom Area"), this);
145  setZoomArea->setCheckable(true);
146  connect(setZoomArea, SIGNAL(triggered()), SLOT(onZoomAreaTriggered()));
147  m_actions << setZoomArea;
148 
149  // Zoom Area
150  QAction* setReadPixel = new QAction(QIcon::fromTheme("readPixel"), tr("Read Pixel"), this);
151  setReadPixel->setCheckable(true);
152  connect(setReadPixel, SIGNAL(triggered()), SLOT(onReadPixelTriggered()));
153  m_actions << setReadPixel;
154 
155  // Raster Style
156  QAction* rasterStyle = new QAction(QIcon::fromTheme("raster-visual"), tr("Raster Visual"), this);
157  rasterStyle->setCheckable(false);
158  connect(rasterStyle, SIGNAL(triggered()), SLOT(onRasterStyleTriggered()));
159  m_actions << rasterStyle;
160 
161  // Tools group
162  QActionGroup* toolsGroup = new QActionGroup(this);
163 
164  // Add actions on group and toolbar
165  QList<QAction*>::iterator it;
166  for(it = m_actions.begin(); it != m_actions.end(); ++it)
167  {
168  toolsGroup->addAction(*it);
169  m_toolBar->addAction(*it);
170  m_menu->addAction(*it);
171  }
172 }
173 
175 {
176  if(m_rvW == 0)
177  {
178  //create dock
179  QDockWidget* dw = new QDockWidget("Raster Visual", this);
180 
181  dw->layout()->setAlignment(Qt::AlignTop);
182 
184 
185  //m_rvW->setVerticalLayout();
186 
187  dw->setWidget(m_rvW);
188 
189  m_rvW->show();
190 
191  connect(m_rvW, SIGNAL(symbolizerChanged()), this, SLOT(onSymbolizerUpdated()));
192 
193  addDockWidget(Qt::RightDockWidgetArea, dw);
194  }
195 
197 }
198 
199 
200 void MainWindow::addRasterLayer(const QString& path)
201 {
202  // * Under revision *
203  // std::string datasetName = path.toUtf8().data();
204  //
205  // // set input raster name
206  // std::map<std::string, std::string> rinfo;
207  // rinfo["URI"] = datasetName;
208  //
209  // // open input raster
210  // te::da::DataSource* ds = te::da::DataSourceFactory::make("GDAL");
211  // ds->open(rinfo);
212 
213  // te::da::DataSourceTransactor* tr = ds->getTransactor();
214  // te::da::DataSourceCatalogLoader* cl = tr->getCatalogLoader();
215 
216  // cl->loadCatalog();
217 
218  // // Gets the number of data set types that belongs to the data source
219  // boost::ptr_vector<std::string> datasets;
220  // tr->getCatalogLoader()->getDataSets(datasets);
221  // assert(!datasets.empty());
222 
223  // // Gets the first dataset
224  // std::string dataSetName(datasets[0]);
225  // te::da::DataSet* dataSet = tr->getDataSet(dataSetName);
226  // te::rst::Raster* raster = dataSet->getRaster();
227 
228  // te::gm::Envelope* extent = raster->getExtent();
229 
230  //// Creates a Layer
231  // te::map::RasterLayer* rasterLayer = new te::map::RasterLayer(boost::lexical_cast<std::string>(ms_id++), datasetName);
232  // rasterLayer->setDataSource(ds);
233  // rasterLayer->setDataSetName(dataSetName);
234  // rasterLayer->setVisibility(te::map::VISIBLE);
235 
236  // // Creates a Layer Renderer
237  // te::map::RasterLayerRenderer* r = new te::map::RasterLayerRenderer();
238  // rasterLayer->setRenderer(r);
239 
240  // // Adding layer to layer list
241  // m_layers.push_back(rasterLayer);
242 
243  // // Storing the data source
244  // m_ds.push_back(ds);
245 
246  // // No more necessary
247  // delete cl;
248  // delete tr;
249 
250  // // Updates MapDisplay layer list and extent
251  // m_display->setLayerList(m_layers);
252  // m_display->setSRID(raster->getSRID());
253  // m_display->setExtent(*extent);
254 
255 
256  // if(m_rvW)
257  // {
258  // //get raster properties
259  // te::map::RasterLayer* layer = dynamic_cast<te::map::RasterLayer*>(*m_layers.begin());
260 
261  // if(layer->getRasterSymbolizer())
262  // {
263  // te::rst::RasterProperty* prop = 0;
264  //
265  // prop = (te::rst::RasterProperty*)layer->getRasterProperty()->clone();
266 
267  // te::se::RasterSymbolizer* rs = (te::se::RasterSymbolizer*)layer->getRasterSymbolizer()->clone();
268 
269  // disconnect(m_rvW, SIGNAL(symbolizerChanged()), this, SLOT(onSymbolizerUpdated()));
270 
271  // m_rvW->setBandProperty(prop->getBandProperties());
272 
273  // m_rvW->setRasterSymbolizer(rs);
274 
275  // connect(m_rvW, SIGNAL(symbolizerChanged()), this, SLOT(onSymbolizerUpdated()));
276 
277  // delete rs;
278 
279  // delete prop;
280  // }
281  // }
282 }
283 
284 void MainWindow::contextMenuEvent(QContextMenuEvent* e)
285 {
286  m_menu->popup(e->globalPos());
287 }
288 
290 {
291  m_display->refresh();
292 }
293 
295 {
296  if(m_startTools == false)
297  {
298  // Install zoom wheel tool. It's always active!
299  m_display->installEventFilter(new te::qt::widgets::ZoomWheel(m_display, 2.0, m_display));
300 
301  // Install zoom keyboard tool. It's always active!
302  m_display->installEventFilter(new te::qt::widgets::ZoomKeyboard(m_display, 2.0, m_display));
303 
304  // Install coordinate tracking tool. It's always active!
306  connect(coordTracking, SIGNAL(coordTracked(QPointF&)), SLOT(onCoordTracked(QPointF&)));
307  m_display->installEventFilter(coordTracking);
308 
309  m_startTools = true;
310  }
311 }
312 
314 {
315  bool status = false;
316 
317  if(m_layers.empty() == false)
318  {
319  status = true;
320  }
321 
322  QList<QAction*>::iterator it;
323  for(it = m_actions.begin(); it != m_actions.end(); ++it)
324  {
325  (*it)->setEnabled(status);
326  }
327 
328  if(m_rvW)
329  {
330  m_rvW->setEnabled(status);
331  }
332 }
333 
335 {
336  delete m_tool;
337  m_tool = new te::qt::widgets::Pan(m_display, Qt::OpenHandCursor, Qt::ClosedHandCursor);
338  m_display->installEventFilter(m_tool);
339 }
340 
342 {
343  delete m_tool;
345  m_display->installEventFilter(m_tool);
346 }
347 
349 {
350  delete m_tool;
352  m_display->installEventFilter(m_tool);
353 }
354 
356 {
357  delete m_tool;
358  m_tool = new te::qt::widgets::ZoomArea(m_display, Qt::BlankCursor);
359  m_display->installEventFilter(m_tool);
360 }
361 
362 void MainWindow::onCoordTracked(QPointF& coordinate)
363 {
364  QString text = QString::fromUtf8("Coordinates: (") + QString::number(coordinate.x()) + " , " + QString::number(coordinate.y()) + ")";
365  QStatusBar* sb = statusBar();
366  sb->showMessage(text);
367 }
368 
370 {
371  // * Under revision *
372  /*delete m_tool;
373  m_tool = new ReadPixelTool(m_display, dynamic_cast<te::map::RasterLayer*>(*m_layers.begin()));
374  m_display->installEventFilter(m_tool);*/
375 }
376 
378 {
379  // * Under revision *
380  // Creates the rastersymbolizer dialog
381  //te::qt::widgets::RasterSymbolizerDialog dlg;
382 
383  //te::map::RasterLayer* layer = dynamic_cast<te::map::RasterLayer*>(*m_layers.begin());
384 
385  //te::rst::RasterProperty* prop = 0;
386  //
387  //prop = (te::rst::RasterProperty*)layer->getRasterProperty()->clone();
388 
389  //dlg.setRasterProperty(layer->getRaster(), prop);
390 
391  //dlg.setRasterSymbolizer(layer->getRasterSymbolizer());
392 
393  //if(dlg.exec() == QDialog::Accepted)
394  //{
395  // te::se::RasterSymbolizer* rs = static_cast<te::se::RasterSymbolizer*>(dlg.getRasterSymbolizer());
396 
397  // //add symbolizer to a layer style
398  // te::se::Rule* rule = new te::se::Rule();
399  // rule->push_back(rs);
400 
401  // te::se::Style* s = new te::se::CoverageStyle();
402  // s->push_back(rule);
403 
404  // layer->setStyle(s);
405 
406  // updateDisplay();
407  //}
408 
409  //delete prop;
410 }
411 
413 {
415  m_layers.clear();
416 
418  m_ds.clear();
419 
421  m_display->setExtent(bb);
422 
423  addRasterLayer(s);
424 
425  startTools();
426 
428 }
429 
431 {
432  //add symbolizer to a layer style
433  te::se::Rule* rule = new te::se::Rule();
435 
437  s->push_back(rule);
438 
439  // * Under revision *
440  //te::map::RasterLayer* layer = dynamic_cast<te::map::RasterLayer*>(*m_layers.begin());
441  //layer->setStyle(s);
442 
443  updateDisplay();
444 }
This class implements a concrete tool to geographic zoom operation using the mouse click...
virtual void refresh(bool redraw=false)
It updates the contents in the map display.
te::qt::widgets::ProgressViewerDialog * m_dlgViewer
This class implements a concrete tool to geographic zoom operation using the mouse wheel...
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
A singleton class used to manage tasks progresses and their viewers.
void MapDisplay()
It retrieves data from a data source, create a set of layer and show map display. ...
This class implements a concrete tool to geographic zoom in operation using a boundary rectangle...
Definition: ZoomArea.h:49
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
Definition: CoverageStyle.h:45
Defines a component for choose a file.
A widget used to configure a Raster Symbolizer SE element.
te::se::RasterSymbolizer * getRasterSymbolizer()
This file contains include headers for TerraLib Symbology Encoding module.
void push_back(const std::string &semanticTypeIdentifier)
Definition: Style.cpp:75
A Qt4 widget to control the display of a set of layers.
void push_back(Symbolizer *s)
Definition: Rule.cpp:138
This class implements a concrete tool to geographic zoom in operation using a boundary rectangle...
This class implements a concrete tool to measure operation (distance, area, and angle).
te::qt::widgets::RasterSymbolizerWidget * m_rvW
This class implements a concrete tool to geographic zoom operation using the keyboard.
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 contextMenuEvent(QContextMenuEvent *e)
std::list< te::map::AbstractLayer * > m_layers
An Envelope defines a 2D rectangular region.
Defines a component for choose a file.
Definition: FileChooser.h:60
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.
A widget used to configure a Raster Symbolizer SE element.
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.
std::vector< te::da::DataSource * > m_ds
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:76
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.
This file contains include headers for the Vector Geometry model of TerraLib.
static std::size_t ms_id
This file contains include headers for the Data Access module of TerraLib.
This file contains include headers for the Map Tools module of TerraLib.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
virtual void setExtent(te::gm::Envelope &e, bool doRefresh=true)
It sets the world visible area and refreshes the contents in the map display.
void setFilterPattern(const QString &filter)
Defines the filter for files of interest.
Definition: FileChooser.cpp:70
void addRasterLayer(const QString &path)
This class implements a concrete tool to geographic zoom operation using the keyboard.
Definition: ZoomKeyboard.h:49
void onFileSelected(QString s)
QList< QAction * > m_actions