QGIS API Documentation  2.8.2-Wien
 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  , mLayerName( layerName )
29 {
30  setupUi( this );
31 
32  QApplication::setOverrideCursor( Qt::ArrowCursor );
33 
34  updateTitle();
35 
36  QSettings settings;
37  restoreGeometry( settings.value( "/Windows/DatumTransformDialog/geometry" ).toByteArray() );
38  mHideDeprecatedCheckBox->setChecked( settings.value( "/Windows/DatumTransformDialog/hideDeprecated", false ).toBool() );
39  mRememberSelectionCheckBox->setChecked( settings.value( "/Windows/DatumTransformDialog/rememberSelection", false ).toBool() );
40 
41  mLabelSrcDescription->setText( "" );
42  mLabelDstDescription->setText( "" );
43 
44  for ( int i = 0; i < 2; i++ )
45  {
46  mDatumTransformTreeWidget->setColumnWidth( i, settings.value( QString( "/Windows/DatumTransformDialog/columnWidths/%1" ).arg( i ), mDatumTransformTreeWidget->columnWidth( i ) ).toInt() );
47  }
48 
49  load();
50 }
51 
52 void QgsDatumTransformDialog::load()
53 {
54  QgsDebugMsg( "Entered." );
55 
56  mDatumTransformTreeWidget->clear();
57 
58  QList< QList< int > >::const_iterator it = mDt.constBegin();
59  for ( ; it != mDt.constEnd(); ++it )
60  {
61  QTreeWidgetItem *item = new QTreeWidgetItem();
62  bool itemDisabled = false;
63  bool itemHidden = false;
64 
65  for ( int i = 0; i < 2 && i < it->size(); ++i )
66  {
67  int nr = it->at( i );
68  item->setData( i, Qt::UserRole, nr );
69  if ( nr == -1 )
70  continue;
71 
72  item->setText( i, QgsCoordinateTransform::datumTransformString( nr ) );
73 
74  //Describe datums in a tooltip
75  QString srcGeoProj, destGeoProj, remarks, scope;
76  int epsgNr;
77  bool preferred, deprecated;
78  if ( !QgsCoordinateTransform::datumTransformCrsInfo( nr, epsgNr, srcGeoProj, destGeoProj, remarks, scope, preferred, deprecated ) )
79  continue;
80 
81  if ( mHideDeprecatedCheckBox->isChecked() && deprecated )
82  {
83  itemHidden = true;
84  }
85 
86  QString toolTipString;
87  if ( gridShiftTransformation( item->text( i ) ) )
88  {
89  toolTipString.append( QString( "<p><b>NTv2</b></p>" ) );
90  }
91 
92  if ( epsgNr > 0 )
93  toolTipString.append( QString( "<p><b>EPSG Transformations Code:</b> %1</p>" ).arg( epsgNr ) );
94 
95  toolTipString.append( QString( "<p><b>Source CRS:</b> %1</p><p><b>Destination CRS:</b> %2</p>" ).arg( srcGeoProj ).arg( destGeoProj ) );
96 
97  if ( !remarks.isEmpty() )
98  toolTipString.append( QString( "<p><b>Remarks:</b> %1</p>" ).arg( remarks ) );
99  if ( !scope.isEmpty() )
100  toolTipString.append( QString( "<p><b>Scope:</b> %1</p>" ).arg( scope ) );
101  if ( preferred )
102  toolTipString.append( "<p><b>Preferred transformation</b></p>" );
103  if ( deprecated )
104  toolTipString.append( "<p><b>Deprecated transformation</b></p>" );
105 
106  item->setToolTip( i, toolTipString );
107 
108  if ( gridShiftTransformation( item->text( i ) ) && !testGridShiftFileAvailability( item, i ) )
109  {
110  itemDisabled = true;
111  }
112  }
113 
114  if ( !itemHidden )
115  {
116  item->setDisabled( itemDisabled );
117  mDatumTransformTreeWidget->addTopLevelItem( item );
118  }
119  else
120  {
121  delete item;
122  }
123  }
124 }
125 
127 {
128  QSettings settings;
129  settings.setValue( "/Windows/DatumTransformDialog/geometry", saveGeometry() );
130  settings.setValue( "/Windows/DatumTransformDialog/hideDeprecated", mHideDeprecatedCheckBox->isChecked() );
131  settings.setValue( "/Windows/DatumTransformDialog/rememberSelection", mRememberSelectionCheckBox->isChecked() );
132 
133  for ( int i = 0; i < 2; i++ )
134  {
135  settings.setValue( QString( "/Windows/DatumTransformDialog/columnWidths/%1" ).arg( i ), mDatumTransformTreeWidget->columnWidth( i ) );
136  }
137 
138  QApplication::restoreOverrideCursor();
139 }
140 
141 void QgsDatumTransformDialog::setDatumTransformInfo( const QString& srcCRSauthId, const QString& destCRSauthId )
142 {
143  mSrcCRSauthId = srcCRSauthId;
144  mDestCRSauthId = destCRSauthId;
145  updateTitle();
146 }
147 
149 {
150  QList<int> list;
151  QTreeWidgetItem * item = mDatumTransformTreeWidget->currentItem();
152  if ( item )
153  {
154  for ( int i = 0; i < 2; ++i )
155  {
156  int transformNr = item->data( i, Qt::UserRole ).toInt();
157  list << transformNr;
158  }
159  }
160  return list;
161 }
162 
164 {
165  return mRememberSelectionCheckBox->isChecked();
166 }
167 
168 bool QgsDatumTransformDialog::gridShiftTransformation( const QString& itemText ) const
169 {
170  return !itemText.isEmpty() && !itemText.contains( "towgs84", Qt::CaseInsensitive );
171 }
172 
173 bool QgsDatumTransformDialog::testGridShiftFileAvailability( QTreeWidgetItem* item, int col ) const
174 {
175  if ( !item )
176  {
177  return true;
178  }
179 
180  QString itemText = item->text( col );
181  if ( itemText.isEmpty() )
182  {
183  return true;
184  }
185 
186  char* projLib = getenv( "PROJ_LIB" );
187  if ( !projLib ) //no information about installation directory
188  {
189  return true;
190  }
191 
192  QStringList itemEqualSplit = itemText.split( "=" );
193  QString filename;
194  for ( int i = 1; i < itemEqualSplit.size(); ++i )
195  {
196  if ( i > 1 )
197  {
198  filename.append( "=" );
199  }
200  filename.append( itemEqualSplit.at( i ) );
201  }
202 
203  QDir projDir( projLib );
204  if ( projDir.exists() )
205  {
206  //look if filename in directory
207  QStringList fileList = projDir.entryList();
208  QStringList::const_iterator fileIt = fileList.constBegin();
209  for ( ; fileIt != fileList.constEnd(); ++fileIt )
210  {
211 #if defined(Q_OS_WIN)
212  if ( fileIt->compare( filename, Qt::CaseInsensitive ) == 0 )
213 #else
214  if ( fileIt->compare( filename ) == 0 )
215 #endif //Q_OS_WIN
216  {
217  return true;
218  }
219  }
220  item->setToolTip( col, tr( "File '%1' not found in directory '%2'" ).arg( filename ).arg( projDir.absolutePath() ) );
221  return false; //not found in PROJ_LIB directory
222  }
223  return true;
224 }
225 
227 {
228  load();
229 }
230 
231 void QgsDatumTransformDialog::on_mDatumTransformTreeWidget_currentItemChanged( QTreeWidgetItem *current, QTreeWidgetItem * )
232 {
233  mLabelSrcDescription->setText( current->toolTip( 0 ) );
234  mLabelDstDescription->setText( current->toolTip( 1 ) );
235 }
236 
237 void QgsDatumTransformDialog::updateTitle()
238 {
239  mLabelLayer->setText( mLayerName );
241  crs.createFromString( mSrcCRSauthId );
242  mLabelSrcCrs->setText( QString( "%1 - %2" ).arg( mSrcCRSauthId ).arg( crs.isValid() ? crs.description() : tr( "unknown" ) ) );
243  crs.createFromString( mDestCRSauthId );
244  mLabelDstCrs->setText( QString( "%1 - %2" ).arg( mDestCRSauthId ).arg( crs.isValid() ? crs.description() : tr( "unknown" ) ) );
245 }