src/terralib/qt/plugins/datasource/gdal/Plugin.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/plugins/datasource/gdal/Plugin.cpp
22 
23  \brief Plugin implementation for the GDAL data source widget.
24 */
25 
26 // TerraLib
27 #include "../../../../common/Config.h"
28 #include "../../../../core/translator/Translator.h"
29 #include "../../../../core/logger/Logger.h"
30 
31 #include "../../../../dataaccess/dataset/DataSetType.h"
32 #include "../../../../dataaccess/datasource/DataSourceInfoManager.h"
33 #include "../../../../dataaccess/datasource/DataSourceManager.h"
34 
35 #include "../../../../dataaccess/datasource/DataSourceCapabilities.h"
36 #include "../../../../maptools/AbstractLayer.h"
37 #include "../../../widgets/canvas/MultiThreadMapDisplay.h"
38 #include "../../../widgets/datasource/core/DataSourceTypeManager.h"
39 #include "../../../widgets/layer/explorer/LayerItemView.h"
40 #include "../../../widgets/layer/utils/DataSet2Layer.h"
41 #include "../../../widgets/raster/RasterInfoDialog.h"
42 #include "../../../widgets/raster/RasterInfoWidget.h"
43 #include "../../../widgets/Utils.h"
44 
45 #include "../../../af/ApplicationController.h"
46 #include "../../../af/BaseApplication.h"
47 #include "../../../af/Utils.h"
48 #include "../../../af/events/LayerEvents.h"
49 #include "../../../af/events/ApplicationEvents.h"
50 
51 #include "GDALType.h"
52 #include "Plugin.h"
53 #include "Utils.h"
54 #include "../../../../cellspace/CellSpaceOperations.h"
55 
56 // Boost
57 #include <boost/uuid/random_generator.hpp>
58 #include <boost/uuid/uuid_io.hpp>
59 #include <boost/filesystem.hpp>
60 
61 // Qt
62 #include <QAction>
63 #include <QFileDialog>
64 #include <QMenu>
65 #include <QFileInfo>
66 #include <QMessageBox>
67 #include <QToolBar>
68 
69 
70 std::list<te::da::DataSetTypePtr> GetDataSetsInfo(const te::da::DataSourceInfoPtr& info)
71 {
72  std::list<te::da::DataSetTypePtr> res;
73 
74  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(info->getId(), info->getType(), info->getConnInfo());
75 
76  std::vector<std::string> dsets = ds->getDataSetNames();
77 
78  std::vector<std::string>::iterator it;
79 
80  for(it = dsets.begin(); it != dsets.end(); ++it)
81  res.push_back(te::da::DataSetTypePtr(ds->getDataSetType(*it).release()));
82 
83  return res;
84 }
85 
87 {
89  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(info->getId(), info->getType(), info->getConnInfo());
90 
91  te::da::DataSetTypePtr dss = ds->getDataSetType(fileName);
92 
93  if (dss.get())
94  {
95  te::qt::widgets::DataSet2Layer layer(info->getId());
96  res = layer(dss);
97  }
98 
99  return res;
100 }
101 
102 void GetLayers(const te::da::DataSourceInfoPtr& info, std::list<te::map::AbstractLayerPtr>& layers)
103 {
104  std::list<te::map::AbstractLayerPtr> res;
105  std::list<te::da::DataSetTypePtr> dss = GetDataSetsInfo(info);
106 
107  std::transform(dss.begin(), dss.end(), std::back_inserter(layers), te::qt::widgets::DataSet2Layer(info->getId()));
108 }
109 
111  :
112 
113  te::core::CppPlugin(pluginInfo),
114  m_openFile(nullptr),
115  m_openMultipleFiles(nullptr),
116  m_handlerLayerExplorer(nullptr),
117  m_handlerMapDisplay(nullptr)
118 {
120 }
121 
123 
125 {
126  if(m_initialized)
127  return;
128 
130 
131  TE_LOG_TRACE(TE_TR("TerraLib Qt GDAL widget startup!"));
132 
133  m_initialized = true;
134 
135  //Initializing action
136  m_openMultipleFiles = new QAction(QIcon::fromTheme("file-raster"), tr("Raster File..."), this);
137  m_openMultipleFiles->setToolTip(tr("Add new raster file as a layer."));
138  m_openMultipleFiles->setObjectName("Project.Add Layer.Raster File");
139  m_openFile = new QAction(QIcon::fromTheme("file-raster"), tr("RAW Raster File..."), this);
140  m_openFile->setObjectName("Project.Add Layer.RAW Raster File");
141 
142 
144  e.m_category = "Dataaccess";
146  e.m_actions << m_openFile;
147 
148 
149  emit triggered(&e);
150 
151  connect (m_openFile, SIGNAL(triggered()), SLOT(openFileDialog()));
152  connect (m_openMultipleFiles, SIGNAL(triggered()), SLOT(openMultipleFilesDialog()));
153 
154  //register actions into application tool bar
155  QToolBar* toolBar = te::qt::af::AppCtrlSingleton::getInstance().getToolBar("File Tool Bar");
156 
157  if (toolBar)
158  toolBar->addAction(m_openMultipleFiles);
159 
160  //install handler
163 
165 
167  ba->getLayerExplorer()->installEventFilter(m_handlerLayerExplorer);
168  ba->getMapDisplay()->installEventFilter(m_handlerMapDisplay);
169 }
170 
172 {
173  if(!m_initialized)
174  return;
175 
176  //remove event handler
178 
180  ba->getLayerExplorer()->removeEventFilter(m_handlerLayerExplorer);
181  ba->getMapDisplay()->removeEventFilter(m_handlerMapDisplay);
182 
183  te::da::DataSourceInfoManager::getInstance().removeByType("GDAL");
185 
186  TE_LOG_TRACE(TE_TR("TerraLib Qt GDAL widget shutdown!"));
187 
188  m_initialized = false;
189 }
190 
192 {
193  std::unique_ptr< te::qt::widgets::RasterInfoDialog > diagPtr(
195  te::qt::af::AppCtrlSingleton::getInstance().getMainWindow(), nullptr ) );
196  diagPtr->exec();
197  if( diagPtr->getWidget()->getShortName().empty() )
198  {
199  return;
200  }
201 
202  QFileInfo info( diagPtr->getWidget()->getFullName().c_str() );
203  QStringList fileNames;
204  fileNames << diagPtr->getWidget()->getFullName().c_str();
205 
206  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "raster");
207 
208  std::list<te::map::AbstractLayerPtr> layers;
209 
210  for(QStringList::iterator it = fileNames.begin(); it != fileNames.end(); ++it)
211  {
214 
215  ds->setAccessDriver("GDAL");
216  std::string fpath = it->toUtf8().data();
217  ds->setConnInfo("file://" + fpath);
218 
219  ds->setDescription("A single raster file");
220 
221  boost::uuids::basic_random_generator<boost::mt19937> gen;
222  boost::uuids::uuid u = gen();
223  std::string id = boost::uuids::to_string(u);
224 
225  ds->setId(id);
226 
227  boost::filesystem::path mpath(it->toUtf8().data());
228 
229  std::string fileBaseName = mpath.stem().string();
230 
231  ds->setTitle(fileBaseName);
232 
233  ds->setType("GDAL");
234 
236  ds = te::da::DataSourceInfoManager::getInstance().getByConnInfo(ds->getConnInfoAsString());
237 
238  layer = GetLayer(ds, mpath.leaf().string());
239  if (layer.get())
240  layers.push_back(layer);
241  }
242 
243  // If there is a parent folder layer that is selected, get it as the parent of the layer to be added;
244  // otherwise, add the layer as a top level layer
245  te::map::AbstractLayerPtr parentLayer(nullptr);
246 
247  std::list<te::map::AbstractLayerPtr>::iterator it;
248  for(it = layers.begin(); it != layers.end(); ++it)
249  {
250  te::qt::af::evt::LayerAdded evt(*it, parentLayer);
251  emit triggered(&evt);
252  }
253 }
254 
256 {
257  QStringList fileNames = QFileDialog::getOpenFileNames(
259  tr("Open Multiple Raster Files"), te::qt::widgets::GetFilePathFromSettings("raster"),
261 
262  if(fileNames.isEmpty())
263  return;
264 
265  QFileInfo info(fileNames.value(0));
266 
267  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "raster");
268 
269  std::list<te::map::AbstractLayerPtr> layers;
270 
271  te::qt::plugins::gdal::CreateLayers(fileNames, layers);
272 }
273 
275 
This event signals that a new layer was created.
Definition: LayerEvents.h:71
void CreateLayers(QStringList fileNames, std::list< te::map::AbstractLayerPtr > &layers)
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
TEQTWIDGETSEXPORT QString GetDiskRasterFileSelFilter(const bool creationSupport)
Returns a disk raster file selection filter base on current supported formats.
boost::shared_ptr< DataSource > DataSourcePtr
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
Basic information about a plugin.
static te::dt::Date ds(2010, 01, 01)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
A class used to handle geofile drag and drop events.
void triggered(te::qt::af::evt::Event *e)
GDAL data source type.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
URI C++ Library.
Definition: Attributes.h:37
te::map::AbstractLayerPtr GetLayer(const te::da::DataSourceInfoPtr &info, std::string fileName)
te::map::AbstractLayerPtr GetLayer(const te::da::DataSourceInfoPtr &info, std::string fileName)
te::qt::widgets::LayerItemView * getLayerExplorer()
std::list< te::da::DataSetTypePtr > GetDataSetsInfo(const te::da::DataSourceInfoPtr &info)
#define TERRALIB_PLUGIN_CALL_BACK_IMPL(PLUGIN_CLASS_NAME)
This macro should be used by C++ plugins in order to declare the exportable/callable DLL function...
te::qt::plugins::gdal::GeoFileDragAndDropHandler * m_handlerMapDisplay
Utility routines for the TerraLib Application Framework module.
te::qt::plugins::gdal::GeoFileDragAndDropHandler * m_handlerLayerExplorer
const PluginInfo & info() const
It returns the PluginInfo of the CppPlugin.
void shutdown()
This method will be called by applicatons to shutdown plugin&#39;s functionality.
te::qt::widgets::MapDisplay * getMapDisplay()
#define TE_LOG_TRACE(message)
Use this tag in order to log a message to the TerraLib default logger with the TRACE level...
Definition: Logger.h:293
A class that represents a data source component.
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
Plugin implementation for the GDAL data source widget.
void GetLayers(const te::da::DataSourceInfoPtr &info, std::list< te::map::AbstractLayerPtr > &layers)
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
void startup()
This method will be called by applications to startup some plugin&#39;s functionality.
This class is a dialog for the RasterInfoWidget.