SRSManagerDialog.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/SRSManagerDialog.cpp
22 
23  \brief A dialog used to select the Spatial Reference Systems for coordinates.
24 */
25 
26 #include "SRSManagerDialog.h"
27 
28 #include "../../../srs/SpatialReferenceSystemManager.h"
29 
30 #include "SRSDialog.h"
31 #include <ui_SRSManagerDialogForm.h>
32 #include <QApplication>
33 #include <QMessageBox>
34 #include <QSettings>
35 #include <QString>
36 
37 #include <iostream>
38 
40  QDialog(parent, f),
41  m_ui(new Ui::SRSManagerDialogForm)
42 {
43  m_ui->setupUi(this);
44  m_selSrsId.first = -1;
45  m_selSrsId.second = "";
46 
47  // Assign the edit/add/remove button icons
48  m_ui->m_addSRSToolButton->setIcon(QIcon::fromTheme("list-add"));
49  m_ui->m_removeSRSToolButton->setIcon(QIcon::fromTheme("list-remove"));
50  m_ui->m_editSRSToolButton->setIcon(QIcon::fromTheme("preferences-system"));
51 
52  // Signals & slots
53  connect(m_ui->m_SRSTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem*, int)), SLOT(onSRSTreeWidgetItemClicked(QTreeWidgetItem* , int)));
54  connect(m_ui->m_okPushButton, SIGNAL(clicked()), SLOT(onOkPushButtonClicked()));
55  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), SLOT(onCancelPushButtonClicked()));
56  //connect(m_ui->m_helpPushButton, SIGNAL(clicked()), SLOT(onHelpPushButtonClicked()));
57  connect(m_ui->m_searchedSRSLineEdit, SIGNAL(textChanged(const QString&)),SLOT(onSearchLineEditTextChanged(const QString&)));
58  connect(m_ui->m_SRSRecentTableWidget, SIGNAL(itemClicked(QTableWidgetItem*)), SLOT(onSRSRecentTableWidgetItemClicked(QTableWidgetItem*)));
59  connect(m_ui->m_addSRSToolButton, SIGNAL(clicked()), SLOT(onAddSRSPushButtonClicked()));
60  connect(m_ui->m_editSRSToolButton, SIGNAL(clicked()), SLOT(onEditSRSPushButtonClicked()));
61  connect(m_ui->m_removeSRSToolButton, SIGNAL(clicked()), SLOT(onRemoveSRSPushButtonClicked()));
62 
63  m_ui->m_helpPushButton->setPageReference("widgets/srs/srs.html");
64 
65  // Builds the table with the recently used SRS ids
66  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
67  QString value = sett.value("SRSRecentlyUsed", "").toString();
68 
69  m_ui->m_SRSRecentTableWidget->clear();
70  if (!value.isEmpty())
71  {
72  m_recentSRS = value.split(',');
73  int aux = 0;
74  int srid;
75 
76  QStringList::const_iterator it;
77  for (it = m_recentSRS.constBegin(); it != m_recentSRS.constEnd(); ++it)
78  {
79  srid = (*it).toInt();
80  std::string auth("EPSG");
81  if (srid >= 100000)
82  auth = "USER";
83  m_ui->m_SRSRecentTableWidget->setItem(aux, 0, new QTableWidgetItem(te::srs::SpatialReferenceSystemManager::getInstance().getName(srid,auth).c_str()));
84  m_ui->m_SRSRecentTableWidget->setItem(aux, 1, new QTableWidgetItem(QString::number(srid)));
85  m_ui->m_SRSRecentTableWidget->setItem(aux, 2, new QTableWidgetItem(auth.c_str()));
86  ++aux;
87  }
88  }
89 
90  QStringList hLabels;
91  hLabels << "Name" << "ID" << "Authority";
92  m_ui->m_SRSRecentTableWidget->setHorizontalHeaderLabels(hLabels);
93 
94  m_ui->m_SRSRecentTableWidget->resizeColumnToContents(0);
95  m_ui->m_SRSRecentTableWidget->resizeColumnToContents(1);
96  m_ui->m_SRSRecentTableWidget->resizeColumnToContents(2);
97 
98 
99  // Builds the available SRS tree
100  QList<QTreeWidgetItem *> items;
101 
102  items.append(new QTreeWidgetItem((QTreeWidget*)nullptr, QStringList(tr("Geographic SRS"))));
103  items.append(new QTreeWidgetItem((QTreeWidget*)nullptr, QStringList(tr("Projected SRS"))));
104  items.append(new QTreeWidgetItem((QTreeWidget*)nullptr, QStringList(tr("User defined SRS"))));
105  m_ui->m_SRSTreeWidget->insertTopLevelItems(0, items);
106 
108  te::srs::SpatialReferenceSystemManager::iterator> its = te::srs::SpatialReferenceSystemManager::getInstance().getIterators();
109 
110  while (its.first != its.second)
111  {
112  if (its.first->m_auth_name == "EPSG")
113  {
114  if (its.first->m_auth_id < 5000)
115  {
116  QTreeWidgetItem *geog = new QTreeWidgetItem(items[0]);
117  geog->setText(0, its.first->m_name.c_str());
118  geog->setText(1, QString::number(its.first->m_auth_id));
119  geog->setText(2, "EPSG");
120  }
121  else
122  {
123  QTreeWidgetItem *proj = new QTreeWidgetItem(items[1]);
124  proj->setText(0, its.first->m_name.c_str());
125  proj->setText(1, QString::number(its.first->m_auth_id));
126  proj->setText(2, "EPSG");
127  }
128  }
129  else
130  {
131  QTreeWidgetItem *userd = new QTreeWidgetItem(items[2]);
132  userd->setText(0, its.first->m_name.c_str());
133  userd->setText(1, QString::number(its.first->m_auth_id));
134  userd->setText(2, its.first->m_auth_name.c_str());
135  }
136  ++its.first;
137  }
138 
139  unsigned int ntl = m_ui->m_SRSTreeWidget->topLevelItemCount();
140  for(unsigned int i = 0; i < ntl; ++i)
141  {
142  QTreeWidgetItem* item = m_ui->m_SRSTreeWidget->topLevelItem(i);
143  item->setExpanded(true);
144  }
145 
146  m_ui->m_SRSTreeWidget->resizeColumnToContents(0);
147  m_ui->m_SRSTreeWidget->resizeColumnToContents(1);
148  m_ui->m_SRSTreeWidget->resizeColumnToContents(2);
149 
150  // Set the field with the PJ4 description
151  m_ui->m_p4descPlainTextEdit->setLineWrapMode(QPlainTextEdit::WidgetWidth);
152  QFontMetrics m (m_ui->m_p4descPlainTextEdit->font());
153  int RowHeight = m.lineSpacing() ;
154  m_ui->m_p4descPlainTextEdit->setFixedHeight(3*RowHeight);
155 }
156 
157 
159 {
160  delete m_ui;
161 }
162 
163 const std::pair<int, std::string>& te::qt::widgets::SRSManagerDialog::getSelectedSRS() const
164 {
165  return m_selSrsId;
166 }
167 
168 
169 std::pair<int, std::string> te::qt::widgets::SRSManagerDialog::getSRS(QWidget* parent, const QString& title)
170 {
171  SRSManagerDialog dlg(parent);
172 
173  if(!title.isEmpty())
174  dlg.setWindowTitle(title);
175 
176  if(dlg.exec() == QDialog::Accepted)
177  return std::pair<int, std::string>(dlg.m_selSrsId.first, dlg.m_selSrsId.second);
178 
179  return std::pair<int, std::string>(TE_UNKNOWN_SRS, "");
180 }
181 
183 {
184  QList<QTreeWidgetItem*> items = m_ui->m_SRSTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 0);
185  items.append(m_ui->m_SRSTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 1));
186  filter(items);
187 }
188 
189 
190 void te::qt::widgets::SRSManagerDialog::filter(const QList<QTreeWidgetItem*>& items)
191 {
192  for(int i = 0; i < m_ui->m_SRSTreeWidget->topLevelItemCount(); ++i)
193  {
194  QTreeWidgetItem* item = m_ui->m_SRSTreeWidget->topLevelItem(i);
195 
196  for(int j = 0; j < item->childCount(); ++j)
197  {
198  QTreeWidgetItem* srs = item->child(j);
199  bool hide = items.indexOf(srs) == -1;
200  srs->setHidden(hide);
201  }
202  }
203  update();
204 }
205 
206 
208 {
209  if (!item || item->text(1).isEmpty())
210  {
211  m_selSrsId.first = TE_UNKNOWN_SRS;
212  m_selSrsId.second = "";
213  m_ui->m_p4descPlainTextEdit->clear();
214  return;
215  }
216  m_selSrsId.first = item->text(1).toUInt();
217  m_selSrsId.second = std::string(item->text(2).toUtf8().data());
218  m_ui->m_p4descPlainTextEdit->setPlainText(te::srs::SpatialReferenceSystemManager::getInstance().getP4Txt(m_selSrsId.first,std::string(item->text(2).toUtf8().data())).c_str());
219 }
220 
222 {
223  if (!item)
224  return;
225  QString val = m_ui->m_SRSRecentTableWidget->item(item->row(),1)->text();
226  QList<QTreeWidgetItem *> ilist = m_ui->m_SRSTreeWidget->findItems(val,Qt::MatchContains|Qt::MatchRecursive, 1);
227  if (ilist.empty())
228  return;
229  m_ui->m_SRSTreeWidget->setCurrentItem(ilist[0]);
230  onSRSTreeWidgetItemClicked(ilist[0],0);
231 }
232 
234 {
235  if(m_ui->m_SRSRecentTableWidget->selectedItems().size() == 0 && m_ui->m_SRSTreeWidget->selectedItems().size() == 0)
236  {
237  QMessageBox::warning(this, "SRSDialog", tr("No SRS selected."));
238  return;
239  }
240 
241  if (m_selSrsId.first == TE_UNKNOWN_SRS || m_selSrsId.first == -1)
242  accept();
243  else
244  {
245  QStringList aux;
246  aux << QString::number(m_selSrsId.first);
247  for (int i=0; (i<=3 && i<m_recentSRS.size()); ++i)
248  if (QString::number(m_selSrsId.first) != m_recentSRS[i])
249  aux << m_recentSRS[i];
250 
251  QString value = aux.join(",");
252  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
253  sett.setValue("SRSRecentlyUsed", value);
254  accept();
255  }
256 }
257 
259 {
260  m_selSrsId.first = -1;
261  m_selSrsId.second = "";
262  reject();
263 }
264 
266 {
267  QMessageBox::critical(this, "", tr("Not implemented yet!"));
268 }
269 
271 {
272  try
273  {
274  te::qt::widgets::SRSDialog dlg (this);
275  int res = dlg.exec();
276  if (res == QDialog::Accepted)
277  {
278  // insert into the manager
280 
281  // insert into the user interface
282  QString srid = QString::number(dlg.getSRID());
283  QTreeWidgetItem* udSRSItem = m_ui->m_SRSTreeWidget->topLevelItem(2);
284  QTreeWidgetItem *userd = new QTreeWidgetItem(udSRSItem);
285  userd->setText(0,dlg.getName().c_str());
286  userd->setText(1,srid);
287  userd->setText(2,"USER");
288  m_ui->m_SRSTreeWidget->resizeColumnToContents(0);
289  m_ui->m_SRSTreeWidget->resizeColumnToContents(1);
290  m_ui->m_SRSTreeWidget->resizeColumnToContents(2);
291 
292  QList<QTreeWidgetItem *> ilist = m_ui->m_SRSTreeWidget->findItems(srid,Qt::MatchContains|Qt::MatchRecursive, 1);
293  if (ilist.empty())
294  return;
295  m_ui->m_SRSTreeWidget->setCurrentItem(ilist[0]);
296  onSRSTreeWidgetItemClicked(ilist[0],0);
297  }
298  }
299  catch(const std::exception& e)
300  {
301  QMessageBox::warning(this, "SRSDialog", e.what());
302  }
303  catch(...)
304  {
305  QMessageBox::warning(this, "SRSDialog",tr("Unknown error while trying to define a new SRS!"));
306  }
307 }
308 
310 {
311  if (m_selSrsId.first == -1)
312  {
313  QMessageBox::warning(this, "SRSDialog",tr("Select a USER defined SRS to modify."));
314  return;
315  }
316 
317  if (m_selSrsId.first <= 100000)
318  {
319  QMessageBox::warning(this, "SRSDialog",tr("Only a USER defined SRS can be modified."));
320  return;
321  }
322 
323  // call the interface with the data selected
324  std::string name = te::srs::SpatialReferenceSystemManager::getInstance().getName(m_selSrsId.first,"USER");
325  std::string p4d = te::srs::SpatialReferenceSystemManager::getInstance().getP4Txt(m_selSrsId.first,"USER");
326  te::qt::widgets::SRSDialog dlg(this,nullptr,m_selSrsId.first,name,p4d);
327  int res = dlg.exec();
328  if (res == QDialog::Accepted)
329  {
330 
331  // update the manager
334 
335  // update the user interface
336  QString srid = QString::number(m_selSrsId.first);
337  QList<QTreeWidgetItem *> ilist = m_ui->m_SRSTreeWidget->findItems(srid,Qt::MatchContains|Qt::MatchRecursive, 1);
338  if (!ilist.empty())
339  {
340  ilist[0]->setText(0,dlg.getName().c_str());
341  ilist[0]->setText(1,srid);
342  ilist[0]->setText(2,"USER");
343  }
344  }
345 }
346 
348 {
349  if (m_selSrsId.first == -1)
350  {
351  QMessageBox::warning(this,"SRSDialog",tr("Select a USER defined SRS to remove."));
352  return;
353  }
354 
355  if (m_selSrsId.first <= 100000)
356  {
357  QMessageBox::warning(this,"SRSDialog",tr("Only a USER defined SRS can be removed."));
358  return;
359  }
360 
361  // remove it from the manager
363 
364  // remove it from the manager interface
365  QString srid = QString::number(m_selSrsId.first);
366  QList<QTreeWidgetItem *> ilist = m_ui->m_SRSTreeWidget->findItems(srid,Qt::MatchContains|Qt::MatchRecursive, 1);
367  if (!ilist.empty())
368  {
369  QTreeWidgetItem* parent = ilist[0]->parent();
370  parent->removeChild(ilist[0]);
371  }
372 
373  // check if it also should be removed from the recently used list and interface
374  QList<QTableWidgetItem *> rilist = m_ui->m_SRSRecentTableWidget->findItems(srid, Qt::MatchContains|Qt::MatchRecursive);
375  if (!rilist.empty())
376  {
377  m_ui->m_SRSRecentTableWidget->removeRow(m_ui->m_SRSRecentTableWidget->row(rilist[0]));
378  m_recentSRS.removeOne(srid);
379 
380  QStringList aux;
381  for (int i=0; (i<=3 && i<m_recentSRS.size()); ++i)
382  aux << m_recentSRS[i];
383 
384  QString value = aux.join(",");
385  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
386  sett.setValue("SRSRecentlyUsed", value);
387  }
389  m_selSrsId.second="";
390 }
std::string getP4Desc() const
Definition: SRSDialog.cpp:165
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
boost::multi_index::nth_index< srs_set, 0 >::type::iterator iterator
An iterator by SRS <id,authority>
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
std::pair< int, std::string > m_selSrsId
The selected SRS.
std::string getName() const
Definition: SRSDialog.cpp:160
void filter(const QList< QTreeWidgetItem * > &items)
void onSRSRecentTableWidgetItemClicked(QTableWidgetItem *)
void onSearchLineEditTextChanged(const QString &text)
A dialog used to build a SRSDialog element.
Definition: SRSDialog.h:56
unsigned int getSRID() const
Definition: SRSDialog.cpp:154
SRSManagerDialog(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a basic dialog which is a child of parent, with widget flags set to f. ...
A dialog used to build a SRSManagerDialog element.
void onSRSTreeWidgetItemClicked(QTreeWidgetItem *, int)
const std::pair< int, std::string > & getSelectedSRS() const
Returns the selected SRS in the window.
Ui::SRSManagerDialogForm * m_ui
Dialog form.
static std::pair< int, std::string > getSRS(QWidget *parent, const QString &title="")
Pops up a modal SRS selector dialog with the given window title, lets the user select a SRS...