Skip to content

Commit

Permalink
Use QgsCoordinateUtils::calculateCoordinatePrecisionForCrs
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent authored and agiudiceandrea committed Sep 6, 2021
1 parent a25c2d8 commit 1d3b809
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
27 changes: 19 additions & 8 deletions src/app/vertextool/qgsvertexeditor.cpp
Expand Up @@ -17,6 +17,7 @@
***************************************************************************/

#include "qgsvertexeditor.h"
#include "qgscoordinateutils.h"
#include "qgsmapcanvas.h"
#include "qgsmessagelog.h"
#include "qgslockedfeature.h"
Expand Down Expand Up @@ -333,11 +334,6 @@ QgsVertexEditor::QgsVertexEditor( QgsMapCanvas *canvas )
mTableView = new QTableView( this );
mTableView->setSelectionMode( QTableWidget::ExtendedSelection );
mTableView->setSelectionBehavior( QTableWidget::SelectRows );
mTableView->setItemDelegateForColumn( 0, new CoordinateItemDelegate( this ) );
mTableView->setItemDelegateForColumn( 1, new CoordinateItemDelegate( this ) );
mTableView->setItemDelegateForColumn( 2, new CoordinateItemDelegate( this ) );
mTableView->setItemDelegateForColumn( 3, new CoordinateItemDelegate( this ) );
mTableView->setItemDelegateForColumn( 4, new CoordinateItemDelegate( this ) );
mTableView->setVisible( false );
mTableView->setModel( mVertexModel );
connect( mTableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVertexEditor::updateVertexSelection );
Expand All @@ -363,6 +359,16 @@ void QgsVertexEditor::updateEditor( QgsLockedFeature *lockedFeature )
mTableView->setVisible( true );

connect( mLockedFeature, &QgsLockedFeature::selectionChanged, this, &QgsVertexEditor::updateTableSelection );

if ( mLockedFeature->layer() )
{
QgsCoordinateReferenceSystem crs = mLockedFeature->layer()->crs();
mTableView->setItemDelegateForColumn( 0, new CoordinateItemDelegate( crs, this ) );
mTableView->setItemDelegateForColumn( 1, new CoordinateItemDelegate( crs, this ) );
mTableView->setItemDelegateForColumn( 2, new CoordinateItemDelegate( crs, this ) );
mTableView->setItemDelegateForColumn( 3, new CoordinateItemDelegate( crs, this ) );
mTableView->setItemDelegateForColumn( 4, new CoordinateItemDelegate( crs, this ) );
}
}
else
{
Expand Down Expand Up @@ -464,15 +470,15 @@ void QgsVertexEditor::closeEvent( QCloseEvent *event )
// CoordinateItemDelegate
//

CoordinateItemDelegate::CoordinateItemDelegate( QObject *parent )
: QStyledItemDelegate( parent )
CoordinateItemDelegate::CoordinateItemDelegate( const QgsCoordinateReferenceSystem &crs, QObject *parent )
: QStyledItemDelegate( parent ), mCrs( crs )
{

}

QString CoordinateItemDelegate::displayText( const QVariant &value, const QLocale &locale ) const
{
return locale.toString( value.toDouble(), 'f', 4 );
return locale.toString( value.toDouble(), 'f', displayDecimalPlaces() );
}

QWidget *CoordinateItemDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index ) const
Expand Down Expand Up @@ -502,3 +508,8 @@ void CoordinateItemDelegate::setEditorData( QWidget *editor, const QModelIndex &
lineEdit->setText( displayText( index.data( ).toDouble( ), QLocale() ) );
}
}

int CoordinateItemDelegate::displayDecimalPlaces() const
{
return QgsCoordinateUtils::calculateCoordinatePrecisionForCrs( mCrs, QgsProject::instance() );
}
7 changes: 6 additions & 1 deletion src/app/vertextool/qgsvertexeditor.h
Expand Up @@ -133,14 +133,19 @@ class APP_EXPORT CoordinateItemDelegate : public QStyledItemDelegate

public:

explicit CoordinateItemDelegate( QObject *parent = nullptr );
explicit CoordinateItemDelegate( const QgsCoordinateReferenceSystem &crs, QObject *parent = nullptr );

QString displayText( const QVariant &value, const QLocale &locale ) const override;

protected:
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem & /*option*/, const QModelIndex &index ) const override;
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
void setEditorData( QWidget *editor, const QModelIndex &index ) const override;

private:
//! Returns number of decimal places to display after the dot
int displayDecimalPlaces() const;
QgsCoordinateReferenceSystem mCrs;
};


Expand Down
25 changes: 25 additions & 0 deletions src/core/qgscoordinateutils.cpp
Expand Up @@ -67,6 +67,31 @@ int QgsCoordinateUtils::calculateCoordinatePrecision( double mapUnitsPerPixel, c
return dp;
}

int QgsCoordinateUtils::calculateCoordinatePrecisionForCrs( const QgsCoordinateReferenceSystem &crs, QgsProject *project )
{
QgsProject *prj = project;
if ( !prj )
{
prj = QgsProject::instance();
}

bool automatic = prj->readBoolEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/Automatic" ) );
if ( !automatic )
{
return prj->readNumEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DecimalPlaces" ), 6 );
}

QgsUnitTypes::DistanceUnit unit = crs.mapUnits();
if ( unit == QgsUnitTypes::DistanceDegrees )
{
return 8;
}
else
{
return 3;
}
}

QString QgsCoordinateUtils::formatCoordinateForProject( QgsProject *project, const QgsPointXY &point, const QgsCoordinateReferenceSystem &destCrs, int precision )
{
if ( !project )
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgscoordinateutils.h
Expand Up @@ -60,6 +60,15 @@ class CORE_EXPORT QgsCoordinateUtils
*/
Q_INVOKABLE static int calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs, QgsProject *project = nullptr );

/**
* Calculates coordinate precision for a CRS / Project. Considers CRS units and project settings
* \param crs Coordinate system
* \param project QGIS project. Takes QgsProject::instance() if NULL
* \returns number of decimal places behind the dot
* \since QGIS 3.18
*/
Q_INVOKABLE static int calculateCoordinatePrecisionForCrs( const QgsCoordinateReferenceSystem &crs, QgsProject *project = nullptr );

/**
* Formats a \a point coordinate for use with the specified \a project, respecting the project's
* coordinate display settings.
Expand Down

0 comments on commit 1d3b809

Please sign in to comment.