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 "../../../../common/Translator.h"
29 #include "../../../../common/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/datasource/core/DataSourceTypeManager.h"
38 #include "../../../widgets/layer/utils/DataSet2Layer.h"
39 #include "../../../widgets/Utils.h"
40 
41 #include "../../../af/ApplicationController.h"
42 #include "../../../af/Project.h"
43 #include "../../../af/Utils.h"
44 #include "../../../af/events/LayerEvents.h"
45 
46 #include "GDALType.h"
47 #include "Plugin.h"
48 #include "../../../../cellspace/CellSpaceOperations.h"
49 
50 // Boost
51 #include <boost/uuid/random_generator.hpp>
52 #include <boost/uuid/uuid_io.hpp>
53 #include <boost/filesystem.hpp>
54 
55 // Qt
56 #include <QAction>
57 #include <QFileDialog>
58 #include <QMenu>
59 #include <QFileInfo>
60 #include <QMessageBox>
61 
62 std::list<te::da::DataSetTypePtr> GetDataSetsInfo(const te::da::DataSourceInfoPtr& info)
63 {
64  std::list<te::da::DataSetTypePtr> res;
65 
66  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(info->getId(), info->getType(), info->getConnInfo());
67 
68  std::vector<std::string> dsets = ds->getDataSetNames();
69 
70  std::vector<std::string>::iterator it;
71 
72  for(it = dsets.begin(); it != dsets.end(); ++it)
73  res.push_back(te::da::DataSetTypePtr(ds->getDataSetType(*it).release()));
74 
75  return res;
76 }
77 
78 void GetLayers(const te::da::DataSourceInfoPtr& info, std::list<te::map::AbstractLayerPtr>& layers)
79 {
80  std::list<te::map::AbstractLayerPtr> res;
81  std::list<te::da::DataSetTypePtr> dss = GetDataSetsInfo(info);
82 
83  std::transform(dss.begin(), dss.end(), std::back_inserter(layers), te::qt::widgets::DataSet2Layer(info->getId()));
84 }
85 
86 
88 QObject(),
89  te::plugin::Plugin(pluginInfo),
90  m_openFile(0)
91 {
92 }
93 
95 {
96 }
97 
99 {
100  if(m_initialized)
101  return;
102 
104 
105  TE_LOG_TRACE(TE_TR("TerraLib Qt GDAL widget startup!"));
106 
107  m_initialized = true;
108 
109  //Initializing action
110  QAction* act = te::qt::af::ApplicationController::getInstance().findAction("Project.Add Layer.Tabular File");
111  QMenu* mnu = te::qt::af::ApplicationController::getInstance().findMenu("Project.Add Layer");
112 
113  if(act != 0 && mnu != 0)
114  {
115  QWidget* parent = act->parentWidget();
116  m_openFile = new QAction(QIcon::fromTheme("file-raster"), tr("Raster File..."), parent);
117  m_openFile->setObjectName("Project.Add Layer.Raster File");
118  mnu->insertAction(act, m_openFile);
119  //mnu->addAction(m_openFile);
120 
122 
123  connect (m_openFile, SIGNAL(triggered()), SLOT(openFileDialog()));
124  }
125 }
126 
128 {
129  if(!m_initialized)
130  return;
131 
132  te::da::DataSourceInfoManager::getInstance().removeByType("GDAL");
134 
135  TE_LOG_TRACE(TE_TR("TerraLib Qt GDAL widget shutdown!"));
136 
137  m_initialized = false;
138 
139  delete m_openFile;
140 }
141 
143 {
145 
146  if(proj == 0)
147  {
148  QMessageBox::warning(te::qt::af::ApplicationController::getInstance().getMainWindow(), tr("Raster File"), tr("Error: there is no opened project!"));
149  return;
150  }
151 
152  QStringList fileNames = QFileDialog::getOpenFileNames(
154  tr("Open Raster File"), te::qt::widgets::GetFilePathFromSettings("raster"),
156 
157  if(fileNames.isEmpty())
158  return;
159 
160  QFileInfo info(fileNames.value(0));
161 
162  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "raster");
163 
164  std::list<te::map::AbstractLayerPtr> layers;
165 
166  for(QStringList::iterator it = fileNames.begin(); it != fileNames.end(); ++it)
167  {
169 
170  ds->setAccessDriver("GDAL");
171 
172  std::map<std::string, std::string> dsinfo;
173  dsinfo["URI"] = it->toLatin1().data();
174 
175  ds->setConnInfo(dsinfo);
176 
177  ds->setDescription("A single raster file");
178 
179  boost::uuids::basic_random_generator<boost::mt19937> gen;
180  boost::uuids::uuid u = gen();
181  std::string id = boost::uuids::to_string(u);
182 
183  ds->setId(id);
184 
185  boost::filesystem::path mpath(it->toStdString());
186 
187  std::string fileBaseName = mpath.stem().string();
188 
189  ds->setTitle(fileBaseName);
190 
191  ds->setType("GDAL");
192 
194  ds = te::da::DataSourceInfoManager::getInstance().getByConnInfo(ds->getConnInfoAsString());
195 
196  GetLayers(ds, layers);
197  }
198 
199  // If there is a parent folder layer that is selected, get it as the parent of the layer to be added;
200  // otherwise, add the layer as a top level layer
201  te::map::AbstractLayerPtr parentLayer(0);
202 
203  std::list<te::map::AbstractLayerPtr> selectedLayers = te::qt::af::ApplicationController::getInstance().getProject()->getSelectedLayers();
204 
205  if(selectedLayers.size() == 1 && selectedLayers.front()->getType() == "FOLDERLAYER")
206  parentLayer = selectedLayers.front();
207 
208  std::list<te::map::AbstractLayerPtr>::iterator it;
209  for(it = layers.begin(); it != layers.end(); ++it)
210  {
211  te::qt::af::evt::LayerAdded evt(*it, parentLayer);
213  }
214 }
215 
217 
#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:137
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:87
void startup()
Do nothing! Just set plugin as started.
Definition: Plugin.cpp:98
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
Definition: Utils.cpp:367
void GetLayers(const te::da::DataSourceInfoPtr &info, std::list< te::map::AbstractLayerPtr > &layers)
Definition: Plugin.cpp:78
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:346
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:54
TEQTAFEXPORT void AddActionToCustomToolbars(QAction *act)
Check QSettings for existance of act and adds it if necessary.
Definition: Utils.cpp:767
Plugin implementation for the GDAL data source widget.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
URI C++ Library.
TEQTWIDGETSEXPORT QString GetDiskRasterFileSelFilter()
Returns a disk raster file selection filter base on current supported formats.
Definition: Utils.cpp:419
~Plugin()
Virtual destructor.
Definition: Plugin.cpp:94
void shutdown()
Do nothing! Just set plugin as stopped.
Definition: Plugin.cpp:127
This class models the concept of a project for the TerraLib Application Framework.
Definition: Project.h:50
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:376
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:62
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr