All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
WMSConnectorDialog.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/wms/WMSConnectorDialog.cpp
22 
23  \brief A dialog window for showing the WMS connector widget.
24 */
25 
26 // TerraLib
27 #include "../../../../common/Translator.h"
28 #include "../../../../dataaccess/datasource/DataSource.h"
29 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
30 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
31 #include "../../../../dataaccess/datasource/DataSourceManager.h"
32 #include "../../../widgets/Exception.h"
33 #include "WMSConnectorDialog.h"
34 #include "ui_WMSConnectorDialogForm.h"
35 
36 // Boost
37 #include <boost/algorithm/string/case_conv.hpp>
38 #include <boost/uuid/random_generator.hpp>
39 #include <boost/uuid/uuid_io.hpp>
40 #include <boost/lexical_cast.hpp>
41 
42 // Qt
43 #include <QMessageBox>
44 
46  : QDialog(parent, f),
47  m_ui(new Ui::WMSConnectorDialogForm)
48 {
49 // add controls
50  m_ui->setupUi(this);
51 
52 // connect signal and slots
53  connect(m_ui->m_openPushButton, SIGNAL(pressed()), this, SLOT(openPushButtonPressed()));
54  connect(m_ui->m_testPushButton, SIGNAL(pressed()), this, SLOT(testPushButtonPressed()));
55  connect(m_ui->m_helpPushButton, SIGNAL(pressed()), this, SLOT(helpPushButtonPressed()));
56 }
57 
59 {
60 }
61 
63 {
64  return m_datasource;
65 }
66 
68 {
69  return m_driver;
70 }
71 
73 {
74  m_datasource = ds;
75 
76  if(m_datasource.get() != 0)
77  {
78  const std::map<std::string, std::string>& connInfo = m_datasource->getConnInfo();
79 
80  std::map<std::string, std::string>::const_iterator it = connInfo.find("URI");
81  if(it != connInfo.end())
82  m_ui->m_serverLineEdit->setText(QString::fromStdString(it->second));
83 
84  m_ui->m_datasourceTitleLineEdit->setText(QString::fromStdString(m_datasource->getTitle()));
85 
86  m_ui->m_dataSourceDescriptionTextEdit->setText(QString::fromStdString(m_datasource->getDescription()));
87  }
88 }
89 
91 {
92  try
93  {
94  // Check if driver is loaded
95  if(te::da::DataSourceFactory::find("WMS") == 0)
96  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for WMS data sources!"));
97 
98  // Get the data source connection info based on form data
99  std::map<std::string, std::string> dsInfo;
100  getConnectionInfo(dsInfo);
101 
102  // Perform connection
103  std::auto_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("WMS");
104  ds->setConnectionInfo(dsInfo);
105  ds->open();
106  m_driver.reset(ds.release());
107 
108  if(m_driver.get() == 0)
109  throw te::qt::widgets::Exception(TE_TR("Could not open WMS data source due to an unknown error!"));
110 
111  QString title = m_ui->m_datasourceTitleLineEdit->text().trimmed();
112 
113  if(title.isEmpty())
114  title = m_ui->m_serverLineEdit->text().trimmed();
115 
116  if(m_datasource.get() == 0)
117  {
118  // Create a new data source based on the form data
119  m_datasource.reset(new te::da::DataSourceInfo);
120 
121  m_datasource->setConnInfo(dsInfo);
122 
123  boost::uuids::basic_random_generator<boost::mt19937> gen;
124  boost::uuids::uuid u = gen();
125  std::string dsId = boost::uuids::to_string(u);
126 
127  m_datasource->setId(dsId);
128  m_driver->setId(dsId);
129  m_datasource->setTitle(title.toStdString());
130  m_datasource->setDescription(m_ui->m_dataSourceDescriptionTextEdit->toPlainText().trimmed().toStdString());
131  m_datasource->setAccessDriver("WMS");
132  m_datasource->setType("WMS");
133  }
134  else
135  {
136  m_driver->setId(m_datasource->getId());
137  m_datasource->setConnInfo(dsInfo);
138  m_datasource->setTitle(title.toStdString());
139  m_datasource->setDescription(m_ui->m_dataSourceDescriptionTextEdit->toPlainText().trimmed().toStdString());
140  }
141  }
142  catch(const std::exception& e)
143  {
144  QMessageBox::warning(this,
145  tr("TerraLib Qt Components"),
146  tr(e.what()));
147  }
148  catch(...)
149  {
150  QMessageBox::warning(this,
151  tr("TerraLib Qt Components"),
152  tr("Unknown error while opening WMS data source!"));
153  }
154 
155  accept();
156 }
157 
159 {
160  try
161  {
162  // Check if driver is loaded
163  if(te::da::DataSourceFactory::find("WMS") == 0)
164  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for WMS data sources!"));
165 
166  // Get the data source connection info based on form data
167  std::map<std::string, std::string> dsInfo;
168  getConnectionInfo(dsInfo);
169 
170  // Perform connection
171  std::auto_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::make("WMS"));
172 
173  if(ds.get() == 0)
174  throw te::qt::widgets::Exception(TE_TR("Could not open WMS server!"));
175 
176  ds->setConnectionInfo(dsInfo);
177  ds->open();
178 
179  QMessageBox::information(this,
180  tr("TerraLib Qt Components"),
181  tr("Data source is ok!"));
182 
183  ds->close();
184  }
185  catch(const std::exception& e)
186  {
187  QMessageBox::warning(this,
188  tr("TerraLib Qt Components"),
189  tr(e.what()));
190  }
191  catch(...)
192  {
193  QMessageBox::warning(this,
194  tr("TerraLib Qt Components"),
195  tr("Unknown error while testing WMS data source!"));
196  }
197 }
198 
200 {
201  QMessageBox::warning(this,
202  tr("TerraLib Qt Components"),
203  tr("Not implemented yet!\nWe will provide it soon!"));
204 }
205 
206 void te::qt::plugins::wms::WMSConnectorDialog::getConnectionInfo(std::map<std::string, std::string>& connInfo) const
207 {
208  connInfo.clear();
209 
210  // Get the server URL
211  QString url = m_ui->m_serverLineEdit->text().trimmed();
212  if(url.isEmpty())
213  throw te::qt::widgets::Exception(TE_TR("Please define the server address first!"));
214 
215  if(!url.startsWith("WMS:"))
216  url.prepend("WMS:");
217 
218  connInfo["URI"] = url.toStdString();
219 }
std::auto_ptr< Ui::WMSConnectorDialogForm > m_ui
static bool find(const std::string &dsType)
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
void set(const te::da::DataSourceInfoPtr &ds)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
const te::da::DataSourceInfoPtr & getDataSource() const
static std::auto_ptr< DataSource > make(const std::string &dsType)
const te::da::DataSourcePtr & getDriver() const
WMSConnectorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
void getConnectionInfo(std::map< std::string, std::string > &connInfo) const
A dialog window for showing the WMS connector widget.
A class that represents a data source component.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr