src/terralib/qt/plugins/datasource/ogr/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/ogr/Plugin.cpp
22 
23  \brief Plugin implementation for the OGR data source widget.
24 */
25 
26 // TerraLib
27 #include "../../../../common/Config.h"
28 #include "../../../../core/logger/Logger.h"
29 #include "../../../../core/translator/Translator.h"
30 #include "../../../../srs/Config.h"
31 #include "../../../../dataaccess/dataset/DataSetType.h"
32 #include "../../../../dataaccess/datasource/DataSourceInfoManager.h"
33 #include "../../../../dataaccess/datasource/DataSourceManager.h"
34 #include "../../../../dataaccess/utils/Utils.h"
35 #include "../../../../maptools/AbstractLayer.h"
36 #include "../../../widgets/canvas/MultiThreadMapDisplay.h"
37 #include "../../../widgets/datasource/core/DataSourceTypeManager.h"
38 #include "../../../widgets/layer/explorer/LayerItemView.h"
39 #include "../../../widgets/Utils.h"
40 #include "../../../af/ApplicationController.h"
41 #include "../../../af/BaseApplication.h"
42 #include "../../../af/Utils.h"
43 #include "../../../af/events/ApplicationEvents.h"
45 #include "OGRType.h"
46 #include "Plugin.h"
47 #include "Utils.h"
48 
49 // OGR
50 #include <ogrsf_frmts.h>
51 
52 // Qt
53 #include <QFileInfo>
54 #include <QAction>
55 #include <QApplication>
56 #include <QFileDialog>
57 #include <QMenu>
58 #include <QMessageBox>
59 #include <QToolBar>
60 
61 QString GetFileExtensionName(const std::string& type)
62 {
63  if(type.compare("ESRI Shapefile") == 0)
64  return QObject::tr("ESRI Shapefile (*.shp *.SHP)");
65  else if(type.compare("MapInfo File") == 0)
66  return QObject::tr("MapInfo File (*.mif *.MIF)");
67  else if(type.compare("DGN") == 0)
68  return QObject::tr("DGN File (*.dgn *.DGN)");
69  else if(type.compare("CSV") == 0)
70  return QObject::tr("CSV File (*.csv *.CSV)");
71  else if(type.compare("GML") == 0)
72  return QObject::tr("GML File (*.gml *.GML)");
73  else if(type.compare("KML") == 0)
74  return QObject::tr("KML File (*.kml *.KML)");
75  else if(type.compare("GeoJSON") == 0)
76  return QObject::tr("GeoJSON File (*.geojson *.GEOJSON)");
77  else if(type.compare("DXF") == 0)
78  return QObject::tr("DXF File (*.dxf *.DXF)");
79 
80  return "";
81 }
82 
84 {
85  QString res;
86  GDALDriverManager mger;
87 
88  int count = mger.GetDriverCount();
89 
90  for(int i=0; i<count; i++)
91  {
92  std::string dName = mger.GetDriver(i)->GetDescription();
93 
94  QString drv = GetFileExtensionName(dName);
95 
96  if(!drv.isEmpty())
97  res += drv + ";;";
98  }
99 
100  return res;
101 }
102 
104  :
105 
106  te::core::CppPlugin(pluginInfo),
107  m_showWindow(nullptr),
108  m_handlerLayerExplorer(nullptr),
109  m_handlerMapDisplay(nullptr)
110 {
112 }
113 
115 
117 {
118  if(m_initialized)
119  return;
120 
122 
123  TE_LOG_TRACE(TE_TR("TerraLib Qt OGR widget startup!"));
124 
125  m_initialized = true;
126 
127  // //Initializing action
128  // QAction* act = te::qt::af::AppCtrlSingleton::getInstance().findAction("Project.Add Layer.Tabular File");
129  // QMenu* mnu = te::qt::af::AppCtrlSingleton::getInstance().findMenu("Project.Add Layer");
130 
131  // if(act != 0 && mnu != 0)
132  {
133  // QWidget* parent = act->parentWidget();
134  m_showWindow = new QAction(QIcon::fromTheme("file-vector"), tr("Vector File..."), this);
135  m_showWindow->setToolTip(tr("Add new vector file as a layer."));
136  m_showWindow->setObjectName("Project.Add Layer.Vector File");
137 
139  e.m_category = "Dataaccess";
140  e.m_actions << m_showWindow;
141 
142  emit triggered(&e);
143 
144  connect (m_showWindow, SIGNAL(triggered()), SLOT(showWindow()));
145 
146  //register actions into application tool bar
147  QToolBar* toolBar = te::qt::af::AppCtrlSingleton::getInstance().getToolBar("File Tool Bar");
148 
149  if (toolBar)
150  toolBar->addAction(m_showWindow);
151 
152  //install handler
155 
157 
159  ba->getLayerExplorer()->installEventFilter(m_handlerLayerExplorer);
160  ba->getMapDisplay()->installEventFilter(m_handlerMapDisplay);
161  }
162 }
163 
165 {
166  if(!m_initialized)
167  return;
168 
169  //remove event handler
171 
173  ba->getLayerExplorer()->removeEventFilter(m_handlerLayerExplorer);
174  ba->getMapDisplay()->removeEventFilter(m_handlerMapDisplay);
175 
176  te::da::DataSourceInfoManager::getInstance().removeByType("OGR");
178 
179  TE_LOG_TRACE(TE_TR("TerraLib Qt OGR widget shutdown!"));
180 
181  // delete m_showWindow;
182 
183  m_initialized = false;
184 }
185 
187 {
188  QStringList fileNames = QFileDialog::getOpenFileNames(te::qt::af::AppCtrlSingleton::getInstance().getMainWindow(), tr("Open Vector File"), te::qt::widgets::GetFilePathFromSettings("vector"), tr("Esri Shapefile (*.shp *.SHP);; Mapinfo File (*.mif *.MIF);; GeoJSON (*.geojson *.GeoJSON);; GML (*.gml *.GML);; KML (*.kml *.KML);; CSV (*.csv *.CSV);; All Files (*.*)"));
189 
190  if(fileNames.isEmpty())
191  return;
192 
193  QFileInfo info(fileNames.value(0));
194 
195  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "vector");
196 
197  std::list<te::map::AbstractLayerPtr> layers;
198 
199  te::qt::plugins::ogr::CreateLayers(fileNames, layers);
200 }
201 
void startup()
This method will be called by applications to startup some plugin&#39;s functionality.
te::qt::plugins::ogr::GeoFileDragAndDropHandler * m_handlerMapDisplay
QString GetFileExtensionName(const std::string &type)
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
OGR data source type.
void shutdown()
This method will be called by applicatons to shutdown plugin&#39;s functionality.
Basic information about a plugin.
void CreateLayers(QStringList fileNames, std::list< te::map::AbstractLayerPtr > &layers)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
static ApplicationController & getInstance()
It returns a reference to the singleton instance.
URI C++ Library.
Definition: Attributes.h:37
te::qt::widgets::LayerItemView * getLayerExplorer()
Utility functions for the data access module.
#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...
const PluginInfo & info() const
It returns the PluginInfo of the CppPlugin.
A class used to handle geofile drag and drop events.
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
te::qt::plugins::ogr::GeoFileDragAndDropHandler * m_handlerLayerExplorer
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
Plugin implementation for the OGR data source widget.
void triggered(te::qt::af::evt::Event *e)