All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Plugin.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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 "../../../../common/Translator.h"
29 #include "../../../../common/Logger.h"
30 #include "../../../../dataaccess/dataset/DataSetType.h"
31 #include "../../../../dataaccess/datasource/DataSourceInfoManager.h"
32 #include "../../../../dataaccess/datasource/DataSourceManager.h"
33 #include "../../../../maptools/AbstractLayer.h"
34 #include "../../../widgets/datasource/core/DataSourceTypeManager.h"
35 #include "../../../widgets/layer/utils/DataSet2Layer.h"
36 #include "../../../widgets/Utils.h"
37 
38 #include "../../../af/ApplicationController.h"
39 #include "../../../af/Project.h"
40 #include "../../../af/Utils.h"
41 #include "../../../af/events/LayerEvents.h"
42 
43 #include "GDALType.h"
44 #include "Plugin.h"
45 
46 // Boost
47 #include <boost/uuid/random_generator.hpp>
48 #include <boost/uuid/uuid_io.hpp>
49 #include <boost/filesystem.hpp>
50 
51 // Qt
52 #include <QAction>
53 #include <QFileDialog>
54 #include <QMenu>
55 #include <QFileInfo>
56 #include <QMessageBox>
57 
58 std::list<te::da::DataSetTypePtr> GetDataSetsInfo(const te::da::DataSourceInfoPtr& info)
59 {
60  std::list<te::da::DataSetTypePtr> res;
61 
62  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(info->getId(), info->getType(), info->getConnInfo());
63 
64  std::vector<std::string> dsets = ds->getDataSetNames();
65 
66  std::vector<std::string>::iterator it;
67 
68  for(it = dsets.begin(); it != dsets.end(); ++it)
69  res.push_back(te::da::DataSetTypePtr(ds->getDataSetType(*it).release()));
70 
71  return res;
72 }
73 
74 void GetLayers(const te::da::DataSourceInfoPtr& info, std::list<te::map::AbstractLayerPtr>& layers)
75 {
76  std::list<te::map::AbstractLayerPtr> res;
77  std::list<te::da::DataSetTypePtr> dss = GetDataSetsInfo(info);
78 
79  std::transform(dss.begin(), dss.end(), std::back_inserter(layers), te::qt::widgets::DataSet2Layer(info->getId()));
80 }
81 
82 
84 QObject(),
85  te::plugin::Plugin(pluginInfo)
86 {
87 }
88 
90 {
91 }
92 
94 {
95  if(m_initialized)
96  return;
97 
99 
100  TE_LOG_TRACE(TE_TR("TerraLib Qt GDAL widget startup!"));
101 
102  m_initialized = true;
103 
104  //Initializing action
105  QAction* act = te::qt::af::ApplicationController::getInstance().findAction("Project.Add Layer.Tabular File");
106  QMenu* mnu = te::qt::af::ApplicationController::getInstance().findMenu("Project.Add Layer");
107 
108  if(act != 0 && mnu != 0)
109  {
110  QWidget* parent = act->parentWidget();
111  m_openFile = new QAction(QIcon::fromTheme("file-raster"), tr("Raster File..."), parent);
112  m_openFile->setObjectName("Project.Add Layer.Raster File");
113  mnu->insertAction(act, m_openFile);
114  //mnu->addAction(m_openFile);
115 
117 
118  connect (m_openFile, SIGNAL(triggered()), SLOT(openFileDialog()));
119  }
120 }
121 
123 {
124  if(!m_initialized)
125  return;
126 
127  te::da::DataSourceInfoManager::getInstance().removeByType("GDAL");
129 
130  TE_LOG_TRACE(TE_TR("TerraLib Qt GDAL widget shutdown!"));
131 
132  m_initialized = false;
133 
134  delete m_openFile;
135 }
136 
138 {
139  QString filter = tr("Image File (*.png *.jpg *.jpeg *.tif *.tiff *.geotif *.geotiff);; Web Map Service - WMS (*.xml *.wms);; Web Coverage Service - WCS (*.xml *.wcs);; All Files (*.*)");
140 
141  QStringList fileNames = QFileDialog::getOpenFileNames(te::qt::af::ApplicationController::getInstance().getMainWindow(), tr("Open Raster File"), te::qt::widgets::GetFilePathFromSettings("raster"), filter);
142 
143  if(fileNames.isEmpty())
144  return;
145 
146  QFileInfo info(fileNames.value(0));
147 
148  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "raster");
149 
150  std::list<te::map::AbstractLayerPtr> layers;
151 
152  for(QStringList::iterator it = fileNames.begin(); it != fileNames.end(); ++it)
153  {
155 
156  ds->setAccessDriver("GDAL");
157 
158  std::map<std::string, std::string> dsinfo;
159  dsinfo["URI"] = it->toStdString();
160 
161  ds->setConnInfo(dsinfo);
162 
163  ds->setDescription("A single raster file");
164 
165  boost::uuids::basic_random_generator<boost::mt19937> gen;
166  boost::uuids::uuid u = gen();
167  std::string id = boost::uuids::to_string(u);
168 
169  ds->setId(id);
170 
171  boost::filesystem::path mpath(it->toStdString());
172 
173  std::string fileBaseName = mpath.stem().string();
174 
175  ds->setTitle(fileBaseName);
176 
177  ds->setType("GDAL");
178 
180 
181  GetLayers(ds, layers);
182  }
183 
184  // If there is a parent folder layer that is selected, get it as the parent of the layer to be added;
185  // otherwise, add the layer as a top level layer
186  te::map::AbstractLayerPtr parentLayer(0);
187 
188  std::list<te::map::AbstractLayerPtr> selectedLayers = te::qt::af::ApplicationController::getInstance().getProject()->getSelectedLayers();
189 
190  if(selectedLayers.size() == 1 && selectedLayers.front()->getType() == "FOLDERLAYER")
191  parentLayer = selectedLayers.front();
192 
193  std::list<te::map::AbstractLayerPtr>::iterator it;
194  for(it = layers.begin(); it != layers.end(); ++it)
195  {
196  te::qt::af::evt::LayerAdded evt(*it, parentLayer);
198  }
199 }
200 
#define TE_LOG_TRACE(msg)
Use this tag in order to log a message to a specified logger with the TRACE level.
Definition: Logger.h:136
This event signals that a new layer was created.
Definition: LayerEvents.h:66
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
Plugin(const te::plugin::PluginInfo &pluginInfo)
Definition: Plugin.cpp:83
void startup()
Do nothing! Just set plugin as started.
Definition: Plugin.cpp:93
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
Definition: Utils.cpp:351
void GetLayers(const te::da::DataSourceInfoPtr &info, std::list< te::map::AbstractLayerPtr > &layers)
Definition: Plugin.cpp:74
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:345
GDAL data source type.
#define PLUGIN_CALL_BACK_IMPL(PLUGIN_CLASS_NAME)
This macro should be used by C++ plugins in order to declare the exportable/callable DLL function...
Definition: Config.h:57
TEQTAFEXPORT void AddActionToCustomToolbars(QAction *act)
Check QSettings for existance of act and adds it if necessary.
Definition: Utils.cpp:630
Plugin implementation for the GDAL data source widget.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
~Plugin()
Virtual destructor.
Definition: Plugin.cpp:89
void shutdown()
Do nothing! Just set plugin as stopped.
Definition: Plugin.cpp:122
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.
Definition: Utils.cpp:360
The basic information about a plugin.
Definition: PluginInfo.h:61
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
std::list< te::da::DataSetTypePtr > GetDataSetsInfo(const te::da::DataSourceInfoPtr &info)
Definition: Plugin.cpp:58
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr