QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsdatumtransformdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdatumtransformdialog.cpp
3  ---------------------------
4  begin : November 2013
5  copyright : (C) 2013 by Marco Hugentobler
6  email : marco.hugentobler at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgscoordinatetransform.h"
20 #include "qgslogger.h"
21 
22 #include <QDir>
23 #include <QSettings>
24 
25 QgsDatumTransformDialog::QgsDatumTransformDialog( const QString& layerName, const QList< QList< int > > &dt, QWidget *parent, Qt::WindowFlags f )
26  : QDialog( parent, f )
27  , mDt( dt )
28 {
29  setupUi( this );
30 
31  QApplication::setOverrideCursor( Qt::ArrowCursor );
32 
33  setWindowTitle( tr( "Select datum transformations for layer" ) + " " + layerName );
34 
35  QSettings settings;
36  restoreGeometry( settings.value( "/Windows/DatumTransformDialog/geometry" ).toByteArray() );
37  mHideDeprecatedCheckBox->setChecked( settings.value( "/Windows/DatumTransformDialog/hideDeprecated", false ).toBool() );
38  mRememberSelectionCheckBox->setChecked( settings.value( "/Windows/DatumTransformDialog/rememberSelection", false ).toBool() );
39 
40  for ( int i = 0; i < 2; i++ )
41  {
42  mDatumTransformTreeWidget->setColumnWidth( i, settings.value( QString( "/Windows/DatumTransformDialog/columnWidths/%1" ).arg( i ), mDatumTransformTreeWidget->columnWidth( i ) ).toInt() );
43  }
44 
45  load();
46 }
47 
49 {
50  QgsDebugMsg( "Entered." );
51 
52  mDatumTransformTreeWidget->clear();
53 
54  QList< QList< int > >::const_iterator it = mDt.constBegin();
55  for ( ; it != mDt.constEnd(); ++it )
56  {
57  QTreeWidgetItem *item = new QTreeWidgetItem();
58  bool itemDisabled = false;
59  bool itemHidden = false;
60 
61  for ( int i = 0; i < 2 && i < it->size(); ++i )
62  {
63  int nr = it->at( i );
64  item->setData( i, Qt::UserRole, nr );
65  if ( nr == -1 )
66  continue;
67 
68  item->setText( i, QgsCoordinateTransform::datumTransformString( nr ) );
69 
70  //Describe datums in a tooltip
71  QString srcGeoProj, destGeoProj, remarks, scope;
72  int epsgNr;
73  bool preferred, deprecated;
74  if ( !QgsCoordinateTransform::datumTransformCrsInfo( nr, epsgNr, srcGeoProj, destGeoProj, remarks, scope, preferred, deprecated ) )
75  continue;
76 
77  if ( mHideDeprecatedCheckBox->isChecked() && deprecated )
78  {
79  itemHidden = true;
80  }
81 
82  QString toolTipString;
83  if ( gridShiftTransformation( item->text( i ) ) )
84  {
85  toolTipString.append( QString( "<p><b>NTv2</b></p>" ) );
86  }
87 
88  if ( epsgNr > 0 )
89  toolTipString.append( QString( "<p><b>EPSG Transformations Code:</b> %1</p>" ).arg( epsgNr ) );
90 
91  toolTipString.append( QString( "<p><b>Source CRS:</b> %1</p><p><b>Destination CRS:</b> %2</p>" ).arg( srcGeoProj ).arg( destGeoProj ) );
92 
93  if ( !remarks.isEmpty() )
94  toolTipString.append( QString( "<p><b>Remarks:</b> %1</p>" ).arg( remarks ) );
95  if ( !scope.isEmpty() )
96  toolTipString.append( QString( "<p><b>Scope:</b> %1</p>" ).arg( scope ) );
97  if ( preferred )
98  toolTipString.append( "<p><b>Preferred transformation</b></p>" );
99  if ( deprecated )
100  toolTipString.append( "<p><b>Deprecated transformation</b></p>" );
101 
102  item->setToolTip( i, toolTipString );
103 
104  if ( gridShiftTransformation( item->text( i ) ) && !testGridShiftFileAvailability( item, i ) )
105  {
106  itemDisabled = true;
107  }
108  }
109 
110  if ( !itemHidden )
111  {
112  item->setDisabled( itemDisabled );
113  mDatumTransformTreeWidget->addTopLevelItem( item );
114  }
115  else
116  {
117  delete item;
118  }
119  }
120 }
121 
123 {
124  QSettings settings;
125  settings.setValue( "/Windows/DatumTransformDialog/geometry", saveGeometry() );
126  settings.setValue( "/Windows/DatumTransformDialog/hideDeprecated", mHideDeprecatedCheckBox->isChecked() );
127  settings.setValue( "/Windows/DatumTransformDialog/rememberSelection", mRememberSelectionCheckBox->isChecked() );
128 
129  for ( int i = 0; i < 2; i++ )
130  {
131  settings.setValue( QString( "/Windows/DatumTransformDialog/columnWidths/%1" ).arg( i ), mDatumTransformTreeWidget->columnWidth( i ) );
132  }
133 
134  QApplication::restoreOverrideCursor();
135 }
136 
138 {
139  QList<int> list;
140  QTreeWidgetItem * item = mDatumTransformTreeWidget->currentItem();
141  if ( item )
142  {
143  for ( int i = 0; i < 2; ++i )
144  {
145  int transformNr = item->data( i, Qt::UserRole ).toInt();
146  list << transformNr;
147  }
148  }
149  return list;
150 }
151 
153 {
154  return mRememberSelectionCheckBox->isChecked();
155 }
156 
157 bool QgsDatumTransformDialog::gridShiftTransformation( const QString& itemText ) const
158 {
159  return !itemText.isEmpty() && !itemText.contains( "towgs84", Qt::CaseInsensitive );
160 }
161 
162 bool QgsDatumTransformDialog::testGridShiftFileAvailability( QTreeWidgetItem* item, int col ) const
163 {
164  if ( !item )
165  {
166  return true;
167  }
168 
169  QString itemText = item->text( col );
170  if ( itemText.isEmpty() )
171  {
172  return true;
173  }
174 
175  char* projLib = getenv( "PROJ_LIB" );
176  if ( !projLib ) //no information about installation directory
177  {
178  return true;
179  }
180 
181  QStringList itemEqualSplit = itemText.split( "=" );
182  QString filename;
183  for ( int i = 1; i < itemEqualSplit.size(); ++i )
184  {
185  if ( i > 1 )
186  {
187  filename.append( "=" );
188  }
189  filename.append( itemEqualSplit.at( i ) );
190  }
191 
192  QDir projDir( projLib );
193  if ( projDir.exists() )
194  {
195  //look if filename in directory
196  QStringList fileList = projDir.entryList();
197  QStringList::const_iterator fileIt = fileList.constBegin();
198  for ( ; fileIt != fileList.constEnd(); ++fileIt )
199  {
200 #if defined(Q_OS_WIN)
201  if ( fileIt->compare( filename, Qt::CaseInsensitive ) == 0 )
202 #else
203  if ( fileIt->compare( filename ) == 0 )
204 #endif //Q_OS_WIN
205  {
206  return true;
207  }
208  }
209  item->setToolTip( col, tr( "File '%1' not found in directory '%2'" ).arg( filename ).arg( projDir.absolutePath() ) );
210  return false; //not found in PROJ_LIB directory
211  }
212  return true;
213 }
214 
216 {
217  load();
218 }