All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <ui_SRSManagerDialogForm.h>
31 #include <QApplication>
32 #include <QMessageBox>
33 #include <QSettings>
34 #include <QString>
35 
36 #include <iostream>
37 
38 te::qt::widgets::SRSManagerDialog::SRSManagerDialog(QWidget* parent, Qt::WindowFlags f):
39  QDialog(parent, f),
40  m_ui(new Ui::SRSManagerDialogForm)
41 {
42  m_ui->setupUi(this);
43  m_selSrsId.first = -1;
44  m_selSrsId.second = "";
45 
46  // Assign the edit/add/remove button icons
47  m_ui->m_addSRSToolButton->setIcon(QIcon::fromTheme("list-add"));
48  m_ui->m_removeSRSToolButton->setIcon(QIcon::fromTheme("list-remove"));
49  m_ui->m_editSRSToolButton->setIcon(QIcon::fromTheme("preferences-system"));
50 
51  // Signals & slots
52  connect(m_ui->m_SRSTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem*, int)), SLOT(onSRSTreeWidgetItemClicked(QTreeWidgetItem* , int)));
53  connect(m_ui->m_okPushButton, SIGNAL(clicked()), SLOT(onOkPushButtonClicked()));
54  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), SLOT(onCancelPushButtonClicked()));
55  //connect(m_ui->m_helpPushButton, SIGNAL(clicked()), SLOT(onHelpPushButtonClicked()));
56  connect(m_ui->m_searchedSRSLineEdit, SIGNAL(textChanged(const QString&)),SLOT(onSearchLineEditTextChanged(const QString&)));
57  connect(m_ui->m_SRSRecentTableWidget, SIGNAL(itemClicked(QTableWidgetItem*)), SLOT(onSRSRecentTableWidgetItemClicked(QTableWidgetItem*)));
58 
59  m_ui->m_helpPushButton->setPageReference("widgets/srs/srs.html");
60 
61  // Builds the table with the recently used SRS ids
62  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
63  QString value = sett.value("SRSRecentlyUsed", "").toString();
64 
65  m_ui->m_SRSRecentTableWidget->clear();
66  if (!value.isEmpty())
67  {
68  m_recentSRS = value.split(',');
69  int aux = 0;
70  int srid;
71 
72  QStringList::const_iterator it;
73  for (it = m_recentSRS.constBegin(); it != m_recentSRS.constEnd(); ++it)
74  {
75  srid = (*it).toInt();
76  std::string auth("EPSG");
77  if (srid >= 100000)
78  auth = "USER";
79  m_ui->m_SRSRecentTableWidget->setItem(aux, 0, new QTableWidgetItem(te::srs::SpatialReferenceSystemManager::getInstance().getName(srid,auth).c_str()));
80  m_ui->m_SRSRecentTableWidget->setItem(aux, 1, new QTableWidgetItem(QString::number(srid)));
81  m_ui->m_SRSRecentTableWidget->setItem(aux, 2, new QTableWidgetItem(auth.c_str()));
82  ++aux;
83  }
84  }
85 
86  QStringList hLabels;
87  hLabels << "Name" << "ID" << "Authority";
88  m_ui->m_SRSRecentTableWidget->setHorizontalHeaderLabels(hLabels);
89 
90  m_ui->m_SRSRecentTableWidget->resizeColumnToContents(0);
91  m_ui->m_SRSRecentTableWidget->resizeColumnToContents(1);
92  m_ui->m_SRSRecentTableWidget->resizeColumnToContents(2);
93 
94 
95  // Builds the available SRS tree
96  QList<QTreeWidgetItem *> items;
97 
98  items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(tr("Geoographic SRS"))));
99  items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(tr("Projected SRS"))));
100  items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(tr("User defined SRS"))));
101  m_ui->m_SRSTreeWidget->insertTopLevelItems(0, items);
102 
104  te::srs::SpatialReferenceSystemManager::iterator> its = te::srs::SpatialReferenceSystemManager::getInstance().getIterators();
105 
106  while (its.first != its.second)
107  {
108  if (its.first->m_auth_name == "EPSG")
109  {
110  if (its.first->m_auth_id < 5000)
111  {
112  QTreeWidgetItem *geog = new QTreeWidgetItem(items[0]);
113  geog->setText(0, its.first->m_name.c_str());
114  geog->setText(1, QString("%1").arg(its.first->m_auth_id));
115  geog->setText(2, "EPSG");
116  }
117  else
118  {
119  QTreeWidgetItem *proj = new QTreeWidgetItem(items[1]);
120  proj->setText(0, its.first->m_name.c_str());
121  proj->setText(1, QString("%1").arg(its.first->m_auth_id));
122  proj->setText(2, "EPSG");
123  }
124  }
125  else
126  {
127  QTreeWidgetItem *userd = new QTreeWidgetItem(items[2]);
128  userd->setText(0, its.first->m_name.c_str());
129  userd->setText(1, QString("%1").arg(its.first->m_auth_id));
130  userd->setText(2, its.first->m_auth_name.c_str());
131  }
132  ++its.first;
133  }
134 
135  unsigned int ntl = m_ui->m_SRSTreeWidget->topLevelItemCount();
136  for(unsigned int i = 0; i < ntl; ++i)
137  {
138  QTreeWidgetItem* item = m_ui->m_SRSTreeWidget->topLevelItem(i);
139  item->setExpanded(true);
140  }
141 
142  m_ui->m_SRSTreeWidget->resizeColumnToContents(0);
143  m_ui->m_SRSTreeWidget->resizeColumnToContents(1);
144  m_ui->m_SRSTreeWidget->resizeColumnToContents(2);
145 
146  // Set the field with the PJ4 description
147  m_ui->m_p4descPlainTextEdit->setLineWrapMode(QPlainTextEdit::WidgetWidth);
148  QFontMetrics m (m_ui->m_p4descPlainTextEdit->font());
149  int RowHeight = m.lineSpacing() ;
150  m_ui->m_p4descPlainTextEdit->setFixedHeight(3*RowHeight);
151 }
152 
153 
155 {
156  delete m_ui;
157 }
158 
159 const std::pair<int, std::string>& te::qt::widgets::SRSManagerDialog::getSelectedSRS() const
160 {
161  return m_selSrsId;
162 }
163 
164 
165 std::pair<int, std::string> te::qt::widgets::SRSManagerDialog::getSRS(QWidget* parent, const QString& title)
166 {
167  SRSManagerDialog dlg(parent);
168 
169  if(!title.isEmpty())
170  dlg.setWindowTitle(title);
171 
172  if(dlg.exec() == QDialog::Accepted)
173  return std::pair<int, std::string>(dlg.m_selSrsId.first, dlg.m_selSrsId.second);
174 
175  return std::pair<int, std::string>(TE_UNKNOWN_SRS, "");
176 }
177 
179 {
180  QList<QTreeWidgetItem*> items = m_ui->m_SRSTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 0);
181  items.append(m_ui->m_SRSTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 1));
182  filter(items);
183 }
184 
185 
186 void te::qt::widgets::SRSManagerDialog::filter(const QList<QTreeWidgetItem*>& items)
187 {
188  for(int i = 0; i < m_ui->m_SRSTreeWidget->topLevelItemCount(); ++i)
189  {
190  QTreeWidgetItem* item = m_ui->m_SRSTreeWidget->topLevelItem(i);
191 
192  for(int j = 0; j < item->childCount(); ++j)
193  {
194  QTreeWidgetItem* srs = item->child(j);
195  bool hide = items.indexOf(srs) == -1;
196  srs->setHidden(hide);
197  }
198  }
199  update();
200 }
201 
202 
203 void te::qt::widgets::SRSManagerDialog::onSRSTreeWidgetItemClicked(QTreeWidgetItem * item, int /*column*/)
204 {
205  if (!item || item->text(1).isEmpty())
206  {
207  m_selSrsId.first = TE_UNKNOWN_SRS;
208  m_selSrsId.second = "";
209  m_ui->m_p4descPlainTextEdit->clear();
210  return;
211  }
212  m_selSrsId.first = item->text(1).toUInt();
213  m_selSrsId.second = std::string(item->text(2).toLatin1());
214  m_ui->m_p4descPlainTextEdit->setPlainText(te::srs::SpatialReferenceSystemManager::getInstance().getP4Txt(m_selSrsId.first,std::string(item->text(2).toLatin1())).c_str());
215 }
216 
218 {
219  if (!item)
220  return;
221  QString val = m_ui->m_SRSRecentTableWidget->item(item->row(),1)->text();
222  QList<QTreeWidgetItem *> ilist = m_ui->m_SRSTreeWidget->findItems(val,Qt::MatchContains|Qt::MatchRecursive, 1);
223  if (ilist.empty())
224  return;
225  m_ui->m_SRSTreeWidget->setCurrentItem(ilist[0]);
226  onSRSTreeWidgetItemClicked(ilist[0],0);
227 }
228 
230 {
231  if (m_selSrsId.first == TE_UNKNOWN_SRS)
232  accept();
233  else
234  {
235  QStringList aux;
236  aux << QString::number(m_selSrsId.first);
237  for (int i=0; (i<=3 && i<m_recentSRS.size()); ++i)
238  if (QString::number(m_selSrsId.first) != m_recentSRS[i])
239  aux << m_recentSRS[i];
240 
241  QString value = aux.join(",");
242  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
243  sett.setValue("SRSRecentlyUsed", value);
244  accept();
245  }
246 }
247 
249 {
250  m_selSrsId.first = -1;
251  m_selSrsId.second = "";
252  reject();
253 }
254 
256 {
257  QMessageBox::critical(this, "", tr("Not implemented yet!"));
258 }
259 
boost::multi_index::nth_index< srs_set, 0 >::type::iterator iterator
An iterator by SRS
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:44
std::pair< int, std::string > m_selSrsId
The selected SRS.
void filter(const QList< QTreeWidgetItem * > &items)
void onSRSRecentTableWidgetItemClicked(QTableWidgetItem *)
void onSearchLineEditTextChanged(const QString &text)
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...