All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataSetLayerSelector.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/widgets/layer/selector/DataSetLayerSelector.cpp
22 
23  \brief A simple widget that allows the selection of datasets from a given data source.
24 */
25 
26 // TerraLib
27 #include "../../../../common/Translator.h"
28 #include "../../../../dataaccess/datasource/DataSource.h"
29 #include "../../../../dataaccess/datasource/DataSourceManager.h"
30 #include "../../../../maptools/DataSetLayer.h"
31 #include "../../dataset/selector/DataSetSelectorDialog.h"
32 #include "../../Exception.h"
33 #include "../utils/DataSet2Layer.h"
34 #include "DataSetLayerSelector.h"
35 
36 // STL
37 #include <algorithm>
38 #include <cassert>
39 #include <iterator>
40 #include <memory>
41 
43  : AbstractLayerSelector(parent, f)
44 {
45 }
46 
48 {
49 }
50 
51 void te::qt::widgets::DataSetLayerSelector::set(const std::list<te::da::DataSourceInfoPtr>& datasources)
52 {
53  m_datasources = datasources;
54 }
55 
56 std::list<te::map::AbstractLayerPtr> te::qt::widgets::DataSetLayerSelector::getLayers()
57 {
58  std::list<te::map::AbstractLayerPtr> layers;
59 
60  for(std::list<te::da::DataSourceInfoPtr>::iterator it = m_datasources.begin(); it != m_datasources.end(); ++it)
61  {
62  te::da::DataSourcePtr datasource = te::da::DataSourceManager::getInstance().find((*it)->getId());
63 
64  if(datasource.get() == 0)
65  {
66  datasource = te::da::DataSourceManager::getInstance().get((*it)->getId(), (*it)->getAccessDriver(), (*it)->getConnInfo());
67 
68  if(datasource.get() == 0)
69  throw Exception(TE_TR("Could not retrieve the data source instance!"));
70  }
71 
72  if(!datasource->isOpened())
73  datasource->open();
74 
75  std::vector<std::string> datasetNames = datasource->getDataSetNames();
76 
77  if(datasetNames.size() == 1) // In this case not show the DataSetSelectorDialog!
78  {
79  DataSet2Layer converter((*it)->getId());
80  std::auto_ptr<te::da::DataSetType> dt = datasource->getDataSetType(datasetNames[0]);
81  te::da::DataSetTypePtr dtpt(dt.release());
82  te::map::DataSetLayerPtr layer = converter(dtpt);
83  layers.push_back(layer);
84  }
85  else
86  {
87  std::auto_ptr<DataSetSelectorDialog> ldialog(new DataSetSelectorDialog(static_cast<QWidget*>(parent())));
88 
89  ldialog->set(*it, true);
90 
91  int retval = ldialog->exec();
92 
93  if(retval == QDialog::Rejected)
94  continue;
95 
96  std::list<te::da::DataSetTypePtr> datasets = ldialog->getCheckedDataSets();
97  std::list<std::string> geomProps = ldialog->getCheckedGeomProperties();
98 
99  assert(datasets.size() == geomProps.size());
100 
101  std::transform(datasets.begin(), datasets.end(), geomProps.begin(), std::back_inserter(layers), DataSet2Layer((*it)->getId()));
102  }
103  }
104 
105  return layers;
106 }
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
A simple widget that allows the selection of datasets from a given data source.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
An abstract class for layer selection widgets.
std::list< te::map::AbstractLayerPtr > getLayers()
void set(const std::list< te::da::DataSourceInfoPtr > &datasources)
DataSetLayerSelector(QWidget *parent=0, Qt::WindowFlags f=0)
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
boost::intrusive_ptr< DataSetLayer > DataSetLayerPtr
Definition: DataSetLayer.h:171