Skip to content

Commit

Permalink
Fix missing point when changing projection
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ committed Apr 17, 2023
1 parent 39d004c commit 972e052
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 51 deletions.
65 changes: 29 additions & 36 deletions src/app/qgsmeasuredialog.cpp
Expand Up @@ -114,8 +114,7 @@ void QgsMeasureDialog::projChanged()
mDa.setEllipsoid( QgsProject::instance()->ellipsoid() );
}

mTable->clear();
mTotal = 0.;
// Update the table and information displayed to the user
updateUi();
}

Expand All @@ -139,8 +138,6 @@ void QgsMeasureDialog::crsChanged()
mUnitsCombo->setEnabled( true );
}

mTable->clear();
mTotal = 0.;
updateUi();
}

Expand All @@ -155,8 +152,9 @@ void QgsMeasureDialog::updateSettings()
mMapDistanceUnits = QgsProject::instance()->crs().mapUnits();
mAreaUnits = QgsProject::instance()->areaUnits();
mDa.setSourceCrs( mCanvas->mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
projChanged();

// Calling projChanged() will set the ellipsoid and clear then re-populate the table
projChanged();

if ( mCartesian->isChecked() || !mCanvas->mapSettings().destinationCrs().isValid() ||
( mCanvas->mapSettings().destinationCrs().mapUnits() == Qgis::DistanceUnit::Degrees
Expand Down Expand Up @@ -199,24 +197,12 @@ void QgsMeasureDialog::unitsChanged( int index )
}
}

mTable->clear();
mTotal = 0.;
updateUi();

if ( !mTool->done() )
{
// re-add temporary mouse cursor position
addPoint();
mouseMove( mLastMousePoint );
}
}

void QgsMeasureDialog::restart()
{
mTool->restart();

mTable->clear();
mTotal = 0.;
updateUi();
}

Expand Down Expand Up @@ -392,6 +378,10 @@ QString QgsMeasureDialog::formatArea( double area, bool convertUnits ) const

void QgsMeasureDialog::updateUi()
{
// Clear the table
mTable->clear();
mTotal = 0.;

// Set tooltip to indicate how we calculate measurements
QString toolTip = tr( "The calculations are based on:" );

Expand Down Expand Up @@ -625,51 +615,54 @@ void QgsMeasureDialog::updateUi()
}
else
{
// Repopulate the table
QVector<QgsPointXY>::const_iterator it;
bool firstPoint = true;

QgsPointXY p1, p2;
QgsPointXY previousPoint, point;
mTotal = 0;
const QVector< QgsPointXY > tmpPoints = mTool->points();
for ( it = tmpPoints.constBegin(); it != tmpPoints.constEnd(); ++it )
{
p2 = *it;
if ( firstPoint )
{
// Create an additional item for the first point (no distance)
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText( Columns::X, QLocale().toString( p2.x(), 'f', mDecimalPlacesCoordinates ) );
item->setText( Columns::Y, QLocale().toString( p2.y(), 'f', mDecimalPlacesCoordinates ) );
mTable->addTopLevelItem( item );
}
else
point = *it;

QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText( Columns::X, QLocale().toString( point.x(), 'f', mDecimalPlacesCoordinates ) );
item->setText( Columns::Y, QLocale().toString( point.y(), 'f', mDecimalPlacesCoordinates ) );

if ( !firstPoint )
{
double d = -1;
d = mDa.measureLine( p1, p2 );
d = mDa.measureLine( previousPoint, point );
if ( mConvertToDisplayUnits )
{
if ( mDistanceUnits == Qgis::DistanceUnit::Unknown && mMapDistanceUnits != Qgis::DistanceUnit::Unknown )
d = convertLength( d, mMapDistanceUnits );
else
d = convertLength( d, mDistanceUnits );
}

QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText( Columns::X, QLocale().toString( p2.x(), 'f', mDecimalPlacesCoordinates ) );
item->setText( Columns::Y, QLocale().toString( p2.y(), 'f', mDecimalPlacesCoordinates ) );
item->setText( Columns::Distance, QLocale().toString( d, 'f', mDecimalPlaces ) );
item->setTextAlignment( Columns::Distance, Qt::AlignRight );
mTable->addTopLevelItem( item );
mTable->scrollToItem( item );
}
p1 = p2;

mTable->addTopLevelItem( item );
mTable->scrollToItem( item );

previousPoint = point;
firstPoint = false;
}

mTotal = mDa.measureLine( mTool->points() );
mTable->show(); // Show the table with items
mSpacer->changeSize( 40, 5, QSizePolicy::Fixed, QSizePolicy::Maximum );
editTotal->setText( formatDistance( mTotal, mConvertToDisplayUnits ) );

if ( !mTool->done() )
{
// re-add temporary mouse cursor position
addPoint();
mouseMove( mLastMousePoint );
}
}
}

Expand Down
21 changes: 6 additions & 15 deletions src/app/qgsmeasuretool.cpp
Expand Up @@ -95,17 +95,6 @@ void QgsMeasureTool::deactivate()
mDialog->hide();
mRubberBand->hide();
mRubberBandPoints->hide();

// Deactivating the tool does not reset the measure.
// Remove the last temporary point as to not duplicate it when
// the tool is re activated
int nbTempVertices = mRubberBand->numberOfVertices();
int nbVertices = mRubberBandPoints->numberOfVertices();
if ( nbTempVertices > nbVertices )
{
mRubberBand->removeLastPoint();
}

QgsMapTool::deactivate();
}

Expand Down Expand Up @@ -161,18 +150,20 @@ void QgsMeasureTool::updateSettings()
}

mRubberBand->updatePosition();
mRubberBand->update();
mRubberBandPoints->updatePosition();
mRubberBandPoints->update();
}
mDestinationCrs = mCanvas->mapSettings().destinationCrs();

// Update the dialog. This will clear then re-populate the table
mDialog->updateSettings();

if ( !mDone && mRubberBand->size() > 0 )
int nbTempVertices = mRubberBand->numberOfVertices();
int nbVertices = mRubberBandPoints->numberOfVertices();

// Add a temporary point to the rubber band if the user is currently measuring
if ( !mDone && mRubberBand->size() > 0 && nbTempVertices <= nbVertices )
{
mRubberBand->addPoint( mPoints.last() );
mDialog->addPoint();
}
if ( mRubberBand->size() > 0 )
{
Expand Down

0 comments on commit 972e052

Please sign in to comment.