QGIS API Documentation  2.99.0-Master (6a61179)
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( QStringLiteral( "/Windows/DatumTransformDialog/geometry" ) ).toByteArray() );
38  mHideDeprecatedCheckBox->setChecked( settings.value( QStringLiteral( "/Windows/DatumTransformDialog/hideDeprecated" ), false ).toBool() );
39  mRememberSelectionCheckBox->setChecked( settings.value( QStringLiteral( "/Windows/DatumTransformDialog/rememberSelection" ), false ).toBool() );
40 
41  mLabelSrcDescription->setText( QLatin1String( "" ) );
42  mLabelDstDescription->setText( QLatin1String( "" ) );
43 
44  for ( int i = 0; i < 2; i++ )
45  {
46  mDatumTransformTreeWidget->setColumnWidth( i, settings.value( QStringLiteral( "/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( QStringLiteral( "<p><b>NTv2</b></p>" ) );
90  }
91 
92  if ( epsgNr > 0 )
93  toolTipString.append( QStringLiteral( "<p><b>EPSG Transformations Code:</b> %1</p>" ).arg( epsgNr ) );
94 
95  toolTipString.append( QStringLiteral( "<p><b>Source CRS:</b> %1</p><p><b>Destination CRS:</b> %2</p>" ).arg( srcGeoProj, destGeoProj ) );
96 
97  if ( !remarks.isEmpty() )
98  toolTipString.append( QStringLiteral( "<p><b>Remarks:</b> %1</p>" ).arg( remarks ) );
99  if ( !scope.isEmpty() )
100  toolTipString.append( QStringLiteral( "<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( QStringLiteral( "/Windows/DatumTransformDialog/geometry" ), saveGeometry() );
130  settings.setValue( QStringLiteral( "/Windows/DatumTransformDialog/hideDeprecated" ), mHideDeprecatedCheckBox->isChecked() );
131  settings.setValue( QStringLiteral( "/Windows/DatumTransformDialog/rememberSelection" ), mRememberSelectionCheckBox->isChecked() );
132 
133  for ( int i = 0; i < 2; i++ )
134  {
135  settings.setValue( QStringLiteral( "/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  list.reserve( 2 );
155  for ( int i = 0; i < 2; ++i )
156  {
157  int transformNr = item->data( i, Qt::UserRole ).toInt();
158  list << transformNr;
159  }
160  }
161  return list;
162 }
163 
165 {
166  return mRememberSelectionCheckBox->isChecked();
167 }
168 
169 bool QgsDatumTransformDialog::gridShiftTransformation( const QString& itemText ) const
170 {
171  return !itemText.isEmpty() && !itemText.contains( QLatin1String( "towgs84" ), Qt::CaseInsensitive );
172 }
173 
174 bool QgsDatumTransformDialog::testGridShiftFileAvailability( QTreeWidgetItem* item, int col ) const
175 {
176  if ( !item )
177  {
178  return true;
179  }
180 
181  QString itemText = item->text( col );
182  if ( itemText.isEmpty() )
183  {
184  return true;
185  }
186 
187  char* projLib = getenv( "PROJ_LIB" );
188  if ( !projLib ) //no information about installation directory
189  {
190  return true;
191  }
192 
193  QStringList itemEqualSplit = itemText.split( '=' );
194  QString filename;
195  for ( int i = 1; i < itemEqualSplit.size(); ++i )
196  {
197  if ( i > 1 )
198  {
199  filename.append( '=' );
200  }
201  filename.append( itemEqualSplit.at( i ) );
202  }
203 
204  QDir projDir( projLib );
205  if ( projDir.exists() )
206  {
207  //look if filename in directory
208  QStringList fileList = projDir.entryList();
209  QStringList::const_iterator fileIt = fileList.constBegin();
210  for ( ; fileIt != fileList.constEnd(); ++fileIt )
211  {
212 #if defined(Q_OS_WIN)
213  if ( fileIt->compare( filename, Qt::CaseInsensitive ) == 0 )
214 #else
215  if ( fileIt->compare( filename ) == 0 )
216 #endif //Q_OS_WIN
217  {
218  return true;
219  }
220  }
221  item->setToolTip( col, tr( "File '%1' not found in directory '%2'" ).arg( filename, projDir.absolutePath() ) );
222  return false; //not found in PROJ_LIB directory
223  }
224  return true;
225 }
226 
228 {
229  load();
230 }
231 
232 void QgsDatumTransformDialog::on_mDatumTransformTreeWidget_currentItemChanged( QTreeWidgetItem *current, QTreeWidgetItem * )
233 {
234  if ( !current )
235  return;
236 
237  mLabelSrcDescription->setText( current->toolTip( 0 ) );
238  mLabelDstDescription->setText( current->toolTip( 1 ) );
239 }
240 
241 void QgsDatumTransformDialog::updateTitle()
242 {
243  mLabelLayer->setText( mLayerName );
245  crs.createFromString( mSrcCRSauthId );
246  mLabelSrcCrs->setText( QStringLiteral( "%1 - %2" ).arg( mSrcCRSauthId, crs.isValid() ? crs.description() : tr( "unknown" ) ) );
247  crs.createFromString( mDestCRSauthId );
248  mLabelDstCrs->setText( QStringLiteral( "%1 - %2" ).arg( mDestCRSauthId, crs.isValid() ? crs.description() : tr( "unknown" ) ) );
249 }
void on_mHideDeprecatedCheckBox_stateChanged(int state)
QgsDatumTransformDialog(const QString &layerName, const QList< QList< int > > &dt, QWidget *parent=nullptr, Qt::WindowFlags f=0)
QList< int > selectedDatumTransform()
getter for selected datum tranformations
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void on_mDatumTransformTreeWidget_currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)
bool createFromString(const QString &theDefinition)
Set up this CRS from a string definition.
void setDatumTransformInfo(const QString &srcCRSauthId, const QString &destCRSauthId)
static QString datumTransformString(int datumTransform)
QString description() const
Returns the descriptive name of the CRS, eg "WGS 84" or "GDA 94 / Vicgrid94".
bool rememberSelection() const
dialog shall remember the selection
This class represents a coordinate reference system (CRS).
static bool datumTransformCrsInfo(int datumTransform, int &epsgNr, QString &srcProjection, QString &dstProjection, QString &remarks, QString &scope, bool &preferred, bool &deprecated)
Gets name of source and dest geographical CRS (to show in a tooltip)
bool isValid() const
Returns whether this CRS is correctly initialized and usable.