vp/AbstractAction.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/vp/AbstractAction.cpp
22 
23  \brief This file defines the abstract class AbstractAction
24 */
25 
26 // Terralib
27 #include "../../../common/SystemApplicationSettings.h"
28 #include "../../../dataaccess/utils/Utils.h"
29 #include "../../af/events/LayerEvents.h"
30 #include "../../af/ApplicationController.h"
31 #include "AbstractAction.h"
32 
33 //QT
34 #include <QApplication>
35 #include <QCheckBox>
36 #include <QDir>
37 #include <QFileInfo>
38 #include <QMessageBox>
39 
40 // STL
41 #include <cassert>
42 
43 //Boost
44 #include <boost/filesystem.hpp>
45 
47  :
48 
49  m_menu(menu),
50  m_action(nullptr)
51 {
52 }
53 
55 
56 void te::qt::plugins::vp::AbstractAction::createAction(std::string name, std::string pixmap)
57 {
58  assert(m_menu);
59 
60  m_action = new QAction(m_menu);
61 
62  m_action->setText(name.c_str());
63 
64  if(pixmap.empty() == false)
65  m_action->setIcon(QIcon::fromTheme(pixmap.c_str()));
66 
67  connect(m_action, SIGNAL(triggered(bool)), this, SLOT(onActionActivated(bool)));
68 
69  m_menu->addAction(m_action);
70 }
71 
73 {
74  te::da::DataSourcePtr dataSource = te::da::GetDataSource(layer->getDataSourceId(), true);
75  std::string type = dataSource->getType();
76 
77  if(type == "OGR")
78  {
79  std::string fpath = dataSource->getConnectionInfo().uri();
80  bool hasSpatialIndex = hasShapeFileSpatialIndex(QString(fpath.c_str()));
81 
82  if(hasSpatialIndex == false)
83  {
85 
86  QSettings settings(QSettings::IniFormat,
87  QSettings::UserScope,
88  QApplication::instance()->organizationName(),
89  QApplication::instance()->applicationName());
90 
91  bool question = settings.value("vp_config/default_question",
92  te::common::SystemApplicationSettings::getInstance().getValue("Application.vectorProcessingDefaultQuestion").c_str()).toBool();
93  bool option = settings.value("vp_config/spatial_index_as_default",
94  te::common::SystemApplicationSettings::getInstance().getValue("Application.vectorProcessingSpatialIndexAsDefault").c_str()).toBool();
95 
96  if(question == true)
97  {
98 
99  QCheckBox *askAgain = new QCheckBox("Would you like to save this setting and not ask again?");
100 
101  QMessageBox question(QMessageBox::Question, tr("Vector Processing"),
102  tr("Would you like to add spatial index on output layer?"));
103  question.addButton(QMessageBox::No);
104  question.addButton(QMessageBox::Yes);
105  question.setCheckBox(askAgain);
106 
107  int addSpatialIndex = question.exec();
108 
109  std::unique_ptr<QCheckBox> checkBoxState(question.checkBox());
110 
111  if(addSpatialIndex == QMessageBox::Yes)
112  {
113  if(checkBoxState->isChecked() == true)
114  {
115  settings.setValue("vp_config/default_question", false);
116  settings.setValue("vp_config/spatial_index_as_default", true);
117  }
118 
119  createSpatialIndex(layer);
120  }
121  else
122  {
123  if(checkBoxState->isChecked() == true)
124  {
125  settings.setValue("vp_config/default_question", false);
126  settings.setValue("vp_config/spatial_index_as_default", false);
127  }
128  }
129  }
130 
131  if(option == true)
132  {
133  createSpatialIndex(layer);
134  }
135  }
136  }
137 
138 
139  te::qt::af::evt::LayerAdded evt(layer.get());
140 
141  emit triggered(&evt);
142 }
143 
144 std::list<te::map::AbstractLayerPtr> te::qt::plugins::vp::AbstractAction::getLayers()
145 {
148 
149  emit triggered(&e);
150  emit triggered(&ed);
151 
152  std::list<te::map::AbstractLayerPtr> allLayers = e.m_layers;
153 
154  std::list<te::map::AbstractLayerPtr> layers;
155 
156  for (std::list<te::map::AbstractLayerPtr>::iterator it = allLayers.begin(); it != allLayers.end(); ++it)
157  {
158  if ((*it)->isValid())
159  layers.push_back(*it);
160  }
161 
162  te::map::AbstractLayerPtr selectedlayer = ed.m_layer;
163 
164  if (!selectedlayer || !selectedlayer->isValid())
165  return layers;
166 
167  std::list<te::map::AbstractLayerPtr> result;
168 
169  result.push_back(selectedlayer);
170 
171  for (std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin(); it != layers.end(); ++it)
172  {
173  if ((*it)->getId() != selectedlayer->getId())
174  result.push_back(*it);
175  }
176 
177  return result;
178 }
179 
180 
182 {
183  QFileInfo fileInfo(path);
184 
185  // Gets the ShapeFile name
186  QString fileName = fileInfo.fileName();
187 
188  // Builds the ShapeFile directory
189  QDir dir(fileInfo.absolutePath());
190 
191  // Looking for .qix file
192  QString qixFile = fileName;
193  qixFile.replace(".shp", ".qix");
194  if(dir.exists(qixFile))
195  return true;
196 
197  // Looking for .sbn file
198  QString sbnFile = fileName;
199  sbnFile.replace(".shp", ".sbn");
200  if(dir.exists(sbnFile))
201  return true;
202 
203  return false;
204 }
205 
207 {
208  te::da::DataSourcePtr dataSource = te::da::GetDataSource(layer->getDataSourceId(), true);
209 
210  try
211  {
212  std::string command = "CREATE SPATIAL INDEX ON " + layer->getDataSetName();
213 
214  dataSource->execute(command);
215 
216  QApplication::restoreOverrideCursor();
217  }
218  catch(...)
219  {
220  QMessageBox::information(te::qt::af::AppCtrlSingleton::getInstance().getMainWindow(), QObject::tr("Spatial Index"), "Error creating spatial index.");
221  }
222 }
223 
225 {
226  // Get user settings
227  QSettings settings(QSettings::IniFormat,
228  QSettings::UserScope,
229  QApplication::instance()->organizationName(),
230  QApplication::instance()->applicationName());
231 
232  bool contains = settings.contains(QString("vp_config/default_question"));
233  if (contains == false)
234  {
235  settings.beginGroup("vp_config");
236  settings.endGroup();
237 
238  settings.setValue("vp_config/default_question", true);
239  settings.setValue("vp_config/spatial_index_as_default", false);
240  }
241 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
This event signals that a new layer was created.
Definition: LayerEvents.h:71
te::map::AbstractLayerPtr m_layer
Layer selected.
Definition: LayerEvents.h:336
This event is used to get a single layer selected in layer tree.
Definition: LayerEvents.h:325
boost::shared_ptr< DataSource > DataSourcePtr
void createAction(std::string name, std::string pixmap="")
Create and set the actions parameters.
This file defines the abstract class AbstractAction.
void createSpatialIndex(te::map::AbstractLayerPtr layer)
bool hasShapeFileSpatialIndex(const QString &path)
static T & getInstance()
It returns a reference to the singleton instance.
Definition: Singleton.h:126
void addNewLayer(te::map::AbstractLayerPtr layer)
Add a new layer into layer explorer widget.
AbstractAction(QMenu *menu)
Constructor.
QAction * m_action
Action used to call the process.
std::list< te::map::AbstractLayerPtr > m_layers
Definition: LayerEvents.h:345
std::list< te::map::AbstractLayerPtr > getLayers()
Get the list of layers from app.
virtual ~AbstractAction()
Destructor.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void triggered(te::qt::af::evt::Event *e)
virtual void onActionActivated(bool checked)=0
Slot function used when a action was selected.