Skip to content
Permalink
Browse files

Use QgsCoordinateUtils::calculateCoordinatePrecisionForCrs

  • Loading branch information
mhugent committed Nov 11, 2020
1 parent d49d458 commit bda92e1f3dc7b82106aad99fcb833303177d4410
@@ -17,6 +17,7 @@
***************************************************************************/

#include "qgsvertexeditor.h"
#include "qgscoordinateutils.h"
#include "qgsmapcanvas.h"
#include "qgsmessagelog.h"
#include "qgslockedfeature.h"
@@ -338,11 +339,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 );
@@ -368,6 +364,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
{
@@ -469,16 +475,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
{
int dp = QgsProject::instance()->readNumEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DecimalPlaces" ), 4 );
return locale.toString( value.toDouble(), 'f', dp );
return locale.toString( value.toDouble(), 'f', displayDecimalPlaces() );
}

QWidget *CoordinateItemDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index ) const
@@ -505,7 +510,11 @@ void CoordinateItemDelegate::setEditorData( QWidget *editor, const QModelIndex &
QLineEdit *lineEdit = qobject_cast<QLineEdit *>( editor );
if ( lineEdit && index.isValid() )
{
int dp = QgsProject::instance()->readNumEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DecimalPlaces" ), 4 );
lineEdit->setText( QLocale().toString( index.data( ).toDouble( ), 'f', dp ) );
lineEdit->setText( QLocale().toString( index.data( ).toDouble( ), 'f', displayDecimalPlaces() ) );
}
}

int CoordinateItemDelegate::displayDecimalPlaces() const
{
return QgsCoordinateUtils::calculateCoordinatePrecisionForCrs( mCrs, QgsProject::instance() );
}
@@ -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;
};


@@ -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 )
@@ -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.

0 comments on commit bda92e1

Please sign in to comment.
You can’t perform that action at this time.