30 #include <QPushButton>
31 #include <QRegularExpression>
41 mLabelSrcDescription->setTextInteractionFlags( Qt::TextBrowserInteraction );
42 mLabelSrcDescription->setOpenExternalLinks(
true );
43 mInstallGridButton->hide();
45 connect( mInstallGridButton, &QPushButton::clicked,
this, &QgsCoordinateOperationWidget::installGrid );
46 connect( mAllowFallbackCheckBox, &QCheckBox::toggled,
this, [ = ]
51 mCoordinateOperationTableWidget->setColumnCount( 3 );
54 headers << tr(
"Transformation" ) << tr(
"Accuracy (meters)" ) << tr(
"Area of Use" );
55 mCoordinateOperationTableWidget->setHorizontalHeaderLabels( headers );
57 mHideDeprecatedCheckBox->setVisible(
false );
58 mShowSupersededCheckBox->setVisible(
true );
59 mLabelDstDescription->hide();
61 connect( mHideDeprecatedCheckBox, &QCheckBox::stateChanged,
this, [ = ] { loadAvailableOperations(); } );
62 connect( mShowSupersededCheckBox, &QCheckBox::toggled,
this, &QgsCoordinateOperationWidget::showSupersededToggled );
63 connect( mCoordinateOperationTableWidget, &QTableWidget::currentItemChanged,
this, &QgsCoordinateOperationWidget::tableCurrentItemChanged );
66 mLabelSrcDescription->clear();
67 mLabelDstDescription->clear();
78 mainCanvasPoly << mainCanvasPoly.at( 0 );
101 mMakeDefaultCheckBox->setVisible( show );
106 return mMakeDefaultCheckBox->isChecked();
111 return !mCoordinateOperationTableWidget->selectedItems().isEmpty();
116 QList<QgsCoordinateOperationWidget::OperationDetails> res;
117 res.reserve( mDatumTransforms.size() );
121 op.
proj = details.proj;
130 void QgsCoordinateOperationWidget::loadAvailableOperations()
132 mCoordinateOperationTableWidget->setRowCount( 0 );
135 int preferredInitialRow = -1;
139 std::unique_ptr< QTableWidgetItem > item = std::make_unique< QTableWidgetItem >();
140 item->setData( ProjRole, transform.proj );
141 item->setData( AvailableRole, transform.isAvailable );
142 item->setFlags( item->flags() & ~Qt::ItemIsEditable );
144 QString name = transform.name;
145 if ( !transform.authority.isEmpty() && !transform.code.isEmpty() )
146 name += QStringLiteral(
" %1 %2:%3" ).arg( QString( QChar( 0x2013 ) ), transform.authority, transform.code );
147 item->setText( name );
151 QFont f = item->font();
154 item->setForeground( QBrush( QColor( 0, 120, 0 ) ) );
157 if ( !transform.isAvailable )
159 item->setForeground( QBrush( palette().color( QPalette::Disabled, QPalette::Text ) ) );
162 if ( preferredInitialRow < 0 && transform.isAvailable )
165 preferredInitialRow = row;
168 QString missingMessage;
169 if ( !transform.isAvailable )
171 QStringList gridMessages;
172 QStringList missingGrids;
173 QStringList missingGridPackages;
174 QStringList missingGridUrls;
182 missingGridUrls << grid.
url;
183 QString m = tr(
"This transformation requires the grid file “%1”, which is not available for use on the system." ).arg( grid.
shortName );
184 if ( !grid.
url.isEmpty() )
188 m +=
' ' + tr(
"This grid is part of the <i>%1</i> package, available for download from <a href=\"%2\">%2</a>." ).arg( grid.
packageName, grid.
url );
192 m +=
' ' + tr(
"This grid is available for download from <a href=\"%1\">%1</a>." ).arg( grid.
url );
199 item->setData( MissingGridsRole, missingGrids );
200 item->setData( MissingGridPackageNamesRole, missingGridPackages );
201 item->setData( MissingGridUrlsRole, missingGridUrls );
203 if ( gridMessages.count() > 1 )
205 for (
int k = 0; k < gridMessages.count(); ++k )
206 gridMessages[k] = QStringLiteral(
"<li>%1</li>" ).arg( gridMessages.at( k ) );
208 missingMessage = QStringLiteral(
"<ul>%1</ul" ).arg( gridMessages.join( QString() ) );
210 else if ( !gridMessages.empty() )
212 missingMessage = gridMessages.constFirst();
216 QStringList areasOfUse;
217 QStringList authorityCodes;
220 QString lastSingleOpScope;
221 QString lastSingleOpRemarks;
225 if ( !singleOpDetails.
scope.isEmpty() )
227 text += QStringLiteral(
"<b>%1</b>: %2" ).arg( tr(
"Scope" ), formatScope( singleOpDetails.
scope ) );
228 lastSingleOpScope = singleOpDetails.
scope;
230 if ( !singleOpDetails.
remarks.isEmpty() )
232 if ( !text.isEmpty() )
233 text += QLatin1String(
"<br>" );
234 text += QStringLiteral(
"<b>%1</b>: %2" ).arg( tr(
"Remarks" ), singleOpDetails.
remarks );
235 lastSingleOpRemarks = singleOpDetails.
remarks;
237 if ( !singleOpDetails.
areaOfUse.isEmpty() )
239 if ( !areasOfUse.contains( singleOpDetails.
areaOfUse ) )
242 if ( !singleOpDetails.
authority.isEmpty() && !singleOpDetails.
code.isEmpty() )
244 const QString identifier = QStringLiteral(
"%1:%2" ).arg( singleOpDetails.
authority, singleOpDetails.
code );
245 if ( !authorityCodes.contains( identifier ) )
246 authorityCodes << identifier;
249 if ( !text.isEmpty() )
251 opText.append( text );
256 if ( !transform.scope.isEmpty() && transform.scope != lastSingleOpScope )
258 text += QStringLiteral(
"<b>%1</b>: %2" ).arg( tr(
"Scope" ), transform.scope );
260 if ( !transform.remarks.isEmpty() && transform.remarks != lastSingleOpRemarks )
262 if ( !text.isEmpty() )
263 text += QLatin1String(
"<br>" );
264 text += QStringLiteral(
"<b>%1</b>: %2" ).arg( tr(
"Remarks" ), transform.remarks );
266 if ( !text.isEmpty() )
268 opText.append( text );
271 if ( opText.count() > 1 )
273 for (
int k = 0; k < opText.count(); ++k )
274 opText[k] = QStringLiteral(
"<li>%1</li>" ).arg( opText.at( k ) );
277 if ( !transform.areaOfUse.isEmpty() && !areasOfUse.contains( transform.areaOfUse ) )
278 areasOfUse << transform.areaOfUse;
279 item->setData( BoundsRole, transform.bounds );
281 const QString
id = !transform.authority.isEmpty() && !transform.code.isEmpty() ? QStringLiteral(
"%1:%2" ).arg( transform.authority, transform.code ) : QString();
282 if ( !
id.isEmpty() && !authorityCodes.contains(
id ) )
283 authorityCodes << id;
285 const QColor disabled = palette().color( QPalette::Disabled, QPalette::Text );
286 const QColor active = palette().color( QPalette::Active, QPalette::Text );
288 const QColor codeColor(
static_cast< int >( active.red() * 0.6 + disabled.red() * 0.4 ),
289 static_cast< int >( active.green() * 0.6 + disabled.green() * 0.4 ),
290 static_cast< int >( active.blue() * 0.6 + disabled.blue() * 0.4 ) );
291 const QString toolTipString = QStringLiteral(
"<b>%1</b>" ).arg( transform.name )
292 + ( !opText.empty() ? ( opText.count() == 1 ? QStringLiteral(
"<p>%1</p>" ).arg( opText.at( 0 ) ) : QStringLiteral(
"<ul>%1</ul>" ).arg( opText.join( QString() ) ) ) : QString() )
293 + ( !areasOfUse.empty() ? QStringLiteral(
"<p><b>%1</b>: %2</p>" ).arg( tr(
"Area of use" ), areasOfUse.join( QLatin1String(
", " ) ) ) : QString() )
294 + ( !authorityCodes.empty() ? QStringLiteral(
"<p><b>%1</b>: %2</p>" ).arg( tr(
"Identifiers" ), authorityCodes.join( QLatin1String(
", " ) ) ) : QString() )
295 + ( !missingMessage.isEmpty() ? QStringLiteral(
"<p><b style=\"color: red\">%1</b></p>" ).arg( missingMessage ) : QString() )
296 + QStringLiteral(
"<p><code style=\"color: %1\">%2</code></p>" ).arg( codeColor.name(), transform.proj );
298 item->setToolTip( toolTipString );
299 mCoordinateOperationTableWidget->setRowCount( row + 1 );
300 mCoordinateOperationTableWidget->setItem( row, 0, item.release() );
302 item = std::make_unique< QTableWidgetItem >();
303 item->setFlags( item->flags() & ~Qt::ItemIsEditable );
304 item->setText( transform.accuracy >= 0 ? QLocale().toString( transform.accuracy ) : tr(
"Unknown" ) );
305 item->setToolTip( toolTipString );
306 if ( !transform.isAvailable )
308 item->setForeground( QBrush( palette().color( QPalette::Disabled, QPalette::Text ) ) );
310 mCoordinateOperationTableWidget->setItem( row, 1, item.release() );
313 item = std::make_unique< QTableWidgetItem >();
314 item->setFlags( item->flags() & ~Qt::ItemIsEditable );
315 item->setText( areasOfUse.join( QLatin1String(
", " ) ) );
316 item->setToolTip( toolTipString );
317 if ( !transform.isAvailable )
319 item->setForeground( QBrush( palette().color( QPalette::Disabled, QPalette::Text ) ) );
321 mCoordinateOperationTableWidget->setItem( row, 2, item.release() );
326 if ( mCoordinateOperationTableWidget->currentRow() < 0 )
327 mCoordinateOperationTableWidget->selectRow( preferredInitialRow >= 0 ? preferredInitialRow : 0 );
329 mCoordinateOperationTableWidget->resizeColumnsToContents();
331 tableCurrentItemChanged(
nullptr,
nullptr );
337 settings.
setValue( QStringLiteral(
"Windows/DatumTransformDialog/hideDeprecated" ), mHideDeprecatedCheckBox->isChecked() );
339 for (
int i = 0; i < 2; i++ )
341 settings.
setValue( QStringLiteral(
"Windows/DatumTransformDialog/columnWidths/%1" ).arg( i ), mCoordinateOperationTableWidget->columnWidth( i ) );
352 if ( transform.isAvailable )
354 preferred.
proj = transform.proj;
362 QString QgsCoordinateOperationWidget::formatScope(
const QString &s )
366 QRegularExpression reGNSS( QStringLiteral(
"\\bGNSS\\b" ) );
367 scope.replace( reGNSS, QObject::tr(
"GNSS (Global Navigation Satellite System)" ) );
369 QRegularExpression reCORS( QStringLiteral(
"\\bCORS\\b" ) );
370 scope.replace( reCORS, QObject::tr(
"CORS (Continually Operating Reference Station)" ) );
377 int row = mCoordinateOperationTableWidget->currentRow();
382 QTableWidgetItem *srcItem = mCoordinateOperationTableWidget->item( row, 0 );
384 QTableWidgetItem *destItem = mCoordinateOperationTableWidget->item( row, 1 );
386 op.
proj = srcItem ? srcItem->data( ProjRole ).toString() : QString();
387 op.
isAvailable = srcItem ? srcItem->data( AvailableRole ).toBool() :
true;
401 int prevRow = mCoordinateOperationTableWidget->currentRow();
403 for (
int row = 0; row < mCoordinateOperationTableWidget->rowCount(); ++row )
405 QTableWidgetItem *srcItem = mCoordinateOperationTableWidget->item( row, 0 );
406 if ( srcItem && srcItem->data( ProjRole ).toString() == operation.
proj )
408 mCoordinateOperationTableWidget->selectRow( row );
413 bool fallbackChanged = mAllowFallbackCheckBox->isChecked() != operation.
allowFallback;
414 mAllowFallbackCheckBox->setChecked( operation.
allowFallback );
417 if ( mCoordinateOperationTableWidget->currentRow() != prevRow || fallbackChanged )
439 mAllowFallbackCheckBox->setVisible( visible );
442 bool QgsCoordinateOperationWidget::gridShiftTransformation(
const QString &itemText )
const
444 return !itemText.isEmpty() && !itemText.contains( QLatin1String(
"towgs84" ), Qt::CaseInsensitive );
447 bool QgsCoordinateOperationWidget::testGridShiftFileAvailability( QTableWidgetItem *item )
const
454 QString itemText = item->text();
455 if ( itemText.isEmpty() )
460 char *projLib = getenv(
"PROJ_LIB" );
466 QStringList itemEqualSplit = itemText.split(
'=' );
468 for (
int i = 1; i < itemEqualSplit.size(); ++i )
472 filename.append(
'=' );
474 filename.append( itemEqualSplit.at( i ) );
477 QDir projDir( projLib );
478 if ( projDir.exists() )
481 QStringList fileList = projDir.entryList();
482 QStringList::const_iterator fileIt = fileList.constBegin();
483 for ( ; fileIt != fileList.constEnd(); ++fileIt )
485 #if defined(Q_OS_WIN)
486 if ( fileIt->compare( filename, Qt::CaseInsensitive ) == 0 )
488 if ( fileIt->compare( filename ) == 0 )
494 item->setToolTip( tr(
"File '%1' not found in directory '%2'" ).arg( filename, projDir.absolutePath() ) );
500 void QgsCoordinateOperationWidget::tableCurrentItemChanged( QTableWidgetItem *, QTableWidgetItem * )
502 int row = mCoordinateOperationTableWidget->currentRow();
505 mLabelSrcDescription->clear();
506 mLabelDstDescription->clear();
508 mInstallGridButton->hide();
512 QTableWidgetItem *srcItem = mCoordinateOperationTableWidget->item( row, 0 );
513 mLabelSrcDescription->setText( srcItem ? srcItem->toolTip() : QString() );
524 mAreaCanvas->setPreviewRect( rect );
527 const QStringList missingGrids = srcItem->data( MissingGridsRole ).toStringList();
528 mInstallGridButton->setVisible( !missingGrids.empty() );
529 if ( !missingGrids.empty() )
531 mInstallGridButton->setText( tr(
"Install “%1” Grid…" ).arg( missingGrids.at( 0 ) ) );
538 mInstallGridButton->hide();
540 QTableWidgetItem *destItem = mCoordinateOperationTableWidget->item( row, 1 );
541 mLabelDstDescription->setText( destItem ? destItem->toolTip() : QString() );
544 if ( newOp.proj != mPreviousOp.
proj && !mBlockSignals )
553 loadAvailableOperations();
560 loadAvailableOperations();
563 void QgsCoordinateOperationWidget::showSupersededToggled(
bool )
566 loadAvailableOperations();
569 void QgsCoordinateOperationWidget::installGrid()
571 int row = mCoordinateOperationTableWidget->currentRow();
572 QTableWidgetItem *srcItem = mCoordinateOperationTableWidget->item( row, 0 );
576 const QStringList missingGrids = srcItem->data( MissingGridsRole ).toStringList();
577 if ( missingGrids.empty() )
580 const QStringList missingGridPackagesNames = srcItem->data( MissingGridPackageNamesRole ).toStringList();
581 const QString packageName = missingGridPackagesNames.value( 0 );
582 const QStringList missingGridUrls = srcItem->data( MissingGridUrlsRole ).toStringList();
583 const QString gridUrl = missingGridUrls.value( 0 );
585 QString downloadMessage;
586 if ( !packageName.isEmpty() )
588 downloadMessage = tr(
"This grid is part of the “<i>%1</i>” package, available for download from <a href=\"%2\">%2</a>." ).arg( packageName, gridUrl );
590 else if ( !gridUrl.isEmpty() )
592 downloadMessage = tr(
"This grid is available for download from <a href=\"%1\">%1</a>." ).arg( gridUrl );
595 const QString longMessage = tr(
"<p>This transformation requires the grid file “%1”, which is not available for use on the system.</p>" ).arg( missingGrids.at( 0 ) );
597 QgsInstallGridShiftFileDialog *dlg =
new QgsInstallGridShiftFileDialog( missingGrids.at( 0 ),
this );
598 dlg->setAttribute( Qt::WA_DeleteOnClose );
599 dlg->setWindowTitle( tr(
"Install Grid File" ) );
600 dlg->setDescription( longMessage );
601 dlg->setDownloadMessage( downloadMessage );
This class represents a coordinate reference system (CRS).
QgsRectangle bounds() const
Returns the approximate bounds for the region the CRS is usable within.
static Q_INVOKABLE QgsCoordinateReferenceSystem fromEpsgId(long epsg)
Creates a CRS from a given EPSG ID.
Contains information about the context in which a coordinate transform is executed.
bool allowFallbackTransform(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns true if approximate "ballpark" transforms may be used when transforming between a source and ...
QString calculateCoordinateOperation(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns the Proj coordinate operation string to use when transforming from the specified source CRS t...
Custom exception class for Coordinate Reference System related exceptions.
A geometry is the spatial representation of a feature.
QgsGeometry densifyByCount(int extraNodesPerSegment) const
Returns a copy of the geometry which has been densified by adding the specified number of extra nodes...
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
static QgsGeometry fromQPolygonF(const QPolygonF &polygon)
Construct geometry from a QPolygonF.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QPolygonF visiblePolygon() const
Returns the visible area as a polygon (may be rotated)
static QgsProject * instance()
Returns the QgsProject singleton instance.
A rectangle specified with double values.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
This class is a composition of two QSettings instances:
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
const QgsCoordinateReferenceSystem & crs