All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 <QtGui/QAction>
53 #include <QtGui/QFileDialog>
54 #include <QtGui/QMenu>
55 #include <QtCore/QFileInfo>
56 #include <QtGui/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 // it initializes the Translator support for the TerraLib PostGIS driver support
102 
103  TE_LOG_TRACE(TE_QT_PLUGIN_DATASOURCE_GDAL("TerraLib Qt GDAL widget startup!"));
104 
105  m_initialized = true;
106 
107  //Initializing action
108  QAction* act = te::qt::af::ApplicationController::getInstance().findAction("Project.Add Layer.Tabular File");
109  QMenu* mnu = te::qt::af::ApplicationController::getInstance().findMenu("Project.Add Layer");
110 
111  if(act != 0 && mnu != 0)
112  {
113  QWidget* parent = act->parentWidget();
114  m_openFile = new QAction(QIcon::fromTheme("file-raster"), tr("Raster File..."), parent);
115  m_openFile->setObjectName("Project.Add Layer.Raster File");
116  mnu->insertAction(act, m_openFile);
117  //mnu->addAction(m_openFile);
118 
120 
121  connect (m_openFile, SIGNAL(triggered()), SLOT(openFileDialog()));
122  }
123 }
124 
126 {
127  if(!m_initialized)
128  return;
129 
130  te::da::DataSourceInfoManager::getInstance().removeByType("GDAL");
132 
133  TE_LOG_TRACE(TE_QT_PLUGIN_DATASOURCE_GDAL("TerraLib Qt GDAL widget shutdown!"));
134 
135  m_initialized = false;
136 
137  delete m_openFile;
138 }
139 
141 {
142  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 (*.*)");
143 
144  QStringList fileNames = QFileDialog::getOpenFileNames(te::qt::af::ApplicationController::getInstance().getMainWindow(), tr("Open Raster File"), te::qt::widgets::GetFilePathFromSettings("raster"), filter);
145 
146  if(fileNames.isEmpty())
147  return;
148 
149  QFileInfo info(fileNames.value(0));
150 
151  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "raster");
152 
153  std::list<te::map::AbstractLayerPtr> layers;
154 
155  for(QStringList::iterator it = fileNames.begin(); it != fileNames.end(); ++it)
156  {
158 
159  ds->setAccessDriver("GDAL");
160 
161  std::map<std::string, std::string> dsinfo;
162  dsinfo["URI"] = it->toStdString();
163 
164  ds->setConnInfo(dsinfo);
165 
166  ds->setDescription("A single raster file");
167 
168  boost::uuids::basic_random_generator<boost::mt19937> gen;
169  boost::uuids::uuid u = gen();
170  std::string id = boost::uuids::to_string(u);
171 
172  ds->setId(id);
173 
174  boost::filesystem::path mpath(it->toStdString());
175 
176  std::string fileBaseName = mpath.stem().string();
177 
178  ds->setTitle(fileBaseName);
179 
180  ds->setType("GDAL");
181 
183 
184  GetLayers(ds, layers);
185  }
186 
187  // If there is a parent folder layer that is selected, get it as the parent of the layer to be added;
188  // otherwise, add the layer as a top level layer
189  te::map::AbstractLayerPtr parentLayer(0);
190 
191  std::list<te::map::AbstractLayerPtr> selectedLayers = te::qt::af::ApplicationController::getInstance().getProject()->getSelectedLayers();
192 
193  if(selectedLayers.size() == 1 && selectedLayers.front()->getType() == "FOLDERLAYER")
194  parentLayer = selectedLayers.front();
195 
196  std::list<te::map::AbstractLayerPtr>::iterator it;
197  for(it = layers.begin(); it != layers.end(); ++it)
198  {
199  te::qt::af::evt::LayerAdded evt(*it, parentLayer);
201  }
202 }
203 
The basic information about a plugin.
Definition: PluginInfo.h:61
QMenu * findMenu(const QString &id) const
Returns the menu registered with key id.
#define TE_QT_PLUGIN_DATASOURCE_GDAL(message)
It marks a string in order to get translated. This is a special mark used in the DataAccess module of...
Definition: Config.h:53
void startup()
Do nothing! Just set plugin as started.
Definition: Plugin.cpp:93
std::list< te::da::DataSetTypePtr > GetDataSetsInfo(const te::da::DataSourceInfoPtr &info)
Definition: Plugin.cpp:58
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
Definition: Utils.cpp:360
A class that represents a data source component.
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
Definition: Utils.cpp:351
GDAL data source type.
static ApplicationController & getInstance()
It gives access to the controller singleton.
#define TE_LOG_TRACE(msg)
Use this tag in order to log a message to a specified logger with the TRACE level.
Definition: Config.h:418
This event signals that a new layer was created.
Definition: LayerEvents.h:61
#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
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
void GetLayers(const te::da::DataSourceInfoPtr &info, std::list< te::map::AbstractLayerPtr > &layers)
Definition: Plugin.cpp:74
#define TE_ADD_TEXT_DOMAIN(domain, domaindir, codeset)
It adds the given text domain located at domain-dir with the given codeset to the multilingual system...
Definition: Config.h:118
void shutdown()
Do nothing! Just set plugin as stopped.
Definition: Plugin.cpp:125
Plugin implementation for the GDAL data source widget.
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
~Plugin()
Virtual destructor.
Definition: Plugin.cpp:89
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
#define TE_QT_PLUGIN_DATASOURCE_GDAL_TEXT_DOMAIN_DIR
It contains the translation catalog directory.
Definition: Config.h:46
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
void broadcast(te::qt::af::evt::Event *evt)
Send events in broadcast for all registered components.
#define TE_QT_PLUGIN_DATASOURCE_GDAL_TEXT_DOMAIN
It contains the name of the text domain used in the translation of messages in TerraLib GDAL driver i...
Definition: Config.h:39
QAction * findAction(const QString &id) const
Returns the action identified by id or NULL if there&#39;s not an action identified by id...
te::qt::af::Project * getProject()
Get the current project.
TEQTAFEXPORT void AddActionToCustomToolbars(QAction *act)
Check QSettings for existance of act and adds it if necessary.
Definition: Utils.cpp:651
Plugin(const te::plugin::PluginInfo &pluginInfo)
Definition: Plugin.cpp:83
const std::list< te::map::AbstractLayerPtr > getSelectedLayers() const
It gets all the layers that are selected.
Definition: Project.cpp:144