SRSDialog.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/widgets/srs/SRSDialog.cpp
22 
23  \brief A dialog used to select the Spatial Reference Systems for coordinates.
24 */
25 
26 #include "SRSDialog.h"
27 
28 #include "../../../srs/SpatialReferenceSystemManager.h"
29 
30 #include <ui_SRSDialogForm.h>
31 #include <QApplication>
32 #include <QMessageBox>
33 #include <QSettings>
34 #include <QString>
35 
36 #include <iostream>
37 
38 te::qt::widgets::SRSDialog::SRSDialog(QWidget* parent, Qt::WindowFlags f):
39  QDialog(parent, f),
40  m_ui(new Ui::SRSDialogForm),
41  m_isUpdate(false)
42 {
43  m_ui->setupUi(this);
44 
45  // Signals & slots
46  connect(m_ui->m_okPushButton, SIGNAL(clicked()), SLOT(onOkPushButtonClicked()));
47  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), SLOT(onCancelPushButtonClicked()));
48 
49  m_ui->m_helpPushButton->setPageReference("widgets/srs/srs.html");
50 
51  m_ui->m_authority->setPlainText("USER");
52 
53  m_ui->m_srid->setPlainText(te::srs::SpatialReferenceSystemManager::getInstance().getNewUserDefinedSRID().c_str());
54 }
55 
56 
58  unsigned int srid, const std::string& name, const std::string& p4desc):
59  QDialog(parent, f),
60  m_ui(new Ui::SRSDialogForm),
61  m_isUpdate(true)
62 {
63  m_ui->setupUi(this);
64 
65  // Signals & slots
66  connect(m_ui->m_okPushButton, SIGNAL(clicked()), SLOT(onOkPushButtonClicked()));
67  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), SLOT(onCancelPushButtonClicked()));
68 
69  m_ui->m_helpPushButton->setPageReference("widgets/srs/srs.html");
70 
71  m_ui->m_authority->setPlainText("USER");
72 
73  QString sridt;
74  sridt.setNum(srid);
75  m_ui->m_srid->setPlainText(sridt);
76  m_ui->m_name->setPlainText(name.c_str());
77  m_ui->m_p4desc->setPlainText(p4desc.c_str());
78 }
79 
80 
82 {
83  delete m_ui;
84 }
85 
87 {
88  if (m_ui->m_name->toPlainText().isEmpty())
89  {
90  QMessageBox::warning(this, tr("SRS Dialog"), tr("Inform a name for the new SRS."));
91  return;
92  }
93 
94  if (m_ui->m_p4desc->toPlainText().isEmpty())
95  {
96  QMessageBox::warning(this, tr("SRS Dialog"), tr("Inform the PROJ.4 description for the new SRS."));
97  return;
98  }
99 
100  bool exists = true;
101  std::string name = m_ui->m_name->toPlainText().toUtf8().data();
102  try
103  {
104  std::pair<std::string,unsigned int> check = te::srs::SpatialReferenceSystemManager::getInstance().getIdFromName(name);
105  }
106  catch(...)
107  {
108  exists = false;
109  }
110 
111  if (exists & !m_isUpdate)
112  {
113  QMessageBox::warning(this, tr("SRS Dialog"), tr("There is already a SRS with the given name. Please inform a different name."));
114  return;
115  }
116 
117  name = m_ui->m_p4desc->toPlainText().toUtf8().data();
118  exists = true;
119  try
120  {
121  std::pair<std::string,unsigned int> check = te::srs::SpatialReferenceSystemManager::getInstance().getIdFromP4Txt(name);
122  }
123  catch (...)
124  {
125  exists = false;
126  }
127 
128  if (exists & !m_isUpdate)
129  {
130  QMessageBox::warning(this, tr("SRS Dialog"), tr("There is already a SRS with the PROJ.4 description. Please inform a different description."));
131  return;
132  }
133 
134  QString proj4 = m_ui->m_p4desc->toPlainText();
135 
136  QRegExp rx("[, ]");// match a comma or a space
137  QStringList list = proj4.split(rx, QString::SkipEmptyParts);
138 
139  if (list.size() < 2 ||
140  (!list[0].contains("+proj=",Qt::CaseInsensitive) &&
141  !list[0].contains("+proj =",Qt::CaseInsensitive)))
142  {
143  QMessageBox::warning(this, tr("SRS Dialog"), tr("PROJ.4 description is not valid."));
144  return;
145  }
146  accept();
147 }
148 
150 {
151  reject();
152 }
153 
155 {
156  std::string id = m_ui->m_srid->toPlainText().toUtf8().data();
157  return boost::lexical_cast<unsigned int >(id);
158 }
159 
161 {
162  return m_ui->m_name->toPlainText().toUtf8().data();
163 }
164 
166 {
167  return m_ui->m_p4desc->toPlainText().toUtf8().data();
168 }
std::string getP4Desc() const
Definition: SRSDialog.cpp:165
Ui::SRSDialogForm * m_ui
Dialog form.
Definition: SRSDialog.h:82
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
SRSDialog(QWidget *parent=0, Qt::WindowFlags f=0)
Definition: SRSDialog.cpp:38
std::string getName() const
Definition: SRSDialog.cpp:160
unsigned int getSRID() const
Definition: SRSDialog.cpp:154