QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
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 
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, 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 
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, projDir.absolutePath() ) );
221  return false; //not found in PROJ_LIB directory
222  }
223  return true;
224 }
225 
227 {
228  load();
229 }
230 
232 {
233  if ( !current )
234  return;
235 
236  mLabelSrcDescription->setText( current->toolTip( 0 ) );
237  mLabelDstDescription->setText( current->toolTip( 1 ) );
238 }
239 
240 void QgsDatumTransformDialog::updateTitle()
241 {
242  mLabelLayer->setText( mLayerName );
244  crs.createFromString( mSrcCRSauthId );
245  mLabelSrcCrs->setText( QString( "%1 - %2" ).arg( mSrcCRSauthId, crs.isValid() ? crs.description() : tr( "unknown" ) ) );
246  crs.createFromString( mDestCRSauthId );
247  mLabelDstCrs->setText( QString( "%1 - %2" ).arg( mDestCRSauthId, crs.isValid() ? crs.description() : tr( "unknown" ) ) );
248 }
QByteArray toByteArray() const
void on_mHideDeprecatedCheckBox_stateChanged(int state)
QString & append(QChar ch)
void setupUi(QWidget *widget)
QList< int > selectedDatumTransform()
getter for selected datum tranformations
void setToolTip(int column, const QString &toolTip)
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
const T & at(int i) const
void on_mDatumTransformTreeWidget_currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)
virtual void setData(int column, int role, const QVariant &value)
virtual QVariant data(int column, int role) const
QString tr(const char *sourceText, const char *disambiguation, int n)
bool createFromString(const QString &theDefinition)
Set up this CRS from a string definition, by default a WKT definition.
int size() const
void setDatumTransformInfo(const QString &srcCRSauthId, const QString &destCRSauthId)
void setValue(const QString &key, const QVariant &value)
bool exists() const
int toInt(bool *ok) const
bool restoreGeometry(const QByteArray &geometry)
static QString datumTransformString(int datumTransform)
QString toolTip(int column) const
bool isEmpty() const
void setOverrideCursor(const QCursor &cursor)
QString description() const
Returns the descriptive name of the CRS, eg "WGS 84" or "GDA 94 / Vicgrid94".
void restoreOverrideCursor()
bool contains(QChar ch, Qt::CaseSensitivity cs) const
bool rememberSelection() const
dialog shall remember the selection
QVariant value(const QString &key, const QVariant &defaultValue) const
QByteArray saveGeometry() const
void setDisabled(bool disabled)
QString absolutePath() const
QStringList entryList(QFlags< QDir::Filter > filters, QFlags< QDir::SortFlag > sort) const
Class for storing a coordinate reference system (CRS)
void setText(int column, const QString &text)
bool toBool() const
typedef WindowFlags
QgsDatumTransformDialog(const QString &layerName, const QList< QList< int > > &dt, QWidget *parent=nullptr, const Qt::WindowFlags &f=nullptr)
const_iterator constEnd() const
const_iterator constBegin() const
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)
QString text(int column) const
bool isValid() const
Returns whether this CRS is correctly initialized and usable.