Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 3, 2018
1 parent 6749c8d commit 429374e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 50 deletions.
35 changes: 17 additions & 18 deletions src/app/qgsmeasuredialog.cpp
Expand Up @@ -33,7 +33,9 @@

QgsMeasureDialog::QgsMeasureDialog( QgsMeasureTool *tool, Qt::WindowFlags f )
: QDialog( tool->canvas()->topLevelWidget(), f )
, mMeasureArea( tool->measureArea() )
, mTool( tool )
, mCanvas( tool->canvas() )
{
setupUi( this );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsMeasureDialog::showHelp );
Expand All @@ -47,16 +49,13 @@ QgsMeasureDialog::QgsMeasureDialog( QgsMeasureTool *tool, Qt::WindowFlags f )
buttonBox->addButton( cb, QDialogButtonBox::ActionRole );
connect( cb, &QAbstractButton::clicked, this, &QgsMeasureDialog::openConfigTab );

mMeasureArea = tool->measureArea();
mTotal = 0.;

repopulateComboBoxUnits( mMeasureArea );
if ( mMeasureArea )
mUnitsCombo->setCurrentIndex( mUnitsCombo->findData( QgsProject::instance()->areaUnits() ) );
else
mUnitsCombo->setCurrentIndex( mUnitsCombo->findData( QgsProject::instance()->distanceUnits() ) );

if ( !mTool->canvas()->mapSettings().destinationCrs().isValid() )
if ( !mCanvas->mapSettings().destinationCrs().isValid() )
{
mUnitsCombo->setEnabled( false );
if ( mMeasureArea )
Expand All @@ -69,7 +68,7 @@ QgsMeasureDialog::QgsMeasureDialog( QgsMeasureTool *tool, Qt::WindowFlags f )

connect( mUnitsCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsMeasureDialog::unitsChanged );
connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsMeasureDialog::reject );
connect( mTool->canvas(), &QgsMapCanvas::destinationCrsChanged, this, &QgsMeasureDialog::crsChanged );
connect( mCanvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsMeasureDialog::crsChanged );

groupBox->setCollapsed( true );
}
Expand All @@ -81,7 +80,7 @@ void QgsMeasureDialog::openConfigTab()

void QgsMeasureDialog::crsChanged()
{
if ( !mTool->canvas()->mapSettings().destinationCrs().isValid() )
if ( !mCanvas->mapSettings().destinationCrs().isValid() )
{
mUnitsCombo->setEnabled( false );
if ( mMeasureArea )
Expand All @@ -101,11 +100,11 @@ void QgsMeasureDialog::updateSettings()
QgsSettings settings;

mDecimalPlaces = settings.value( QStringLiteral( "qgis/measure/decimalplaces" ), "3" ).toInt();
mCanvasUnits = mTool->canvas()->mapUnits();
mCanvasUnits = mCanvas->mapUnits();
// Configure QgsDistanceArea
mDistanceUnits = QgsProject::instance()->distanceUnits();
mAreaUnits = QgsProject::instance()->areaUnits();
mDa.setSourceCrs( mTool->canvas()->mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
mDa.setSourceCrs( mCanvas->mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
mDa.setEllipsoid( QgsProject::instance()->ellipsoid() );

mTable->clear();
Expand Down Expand Up @@ -306,20 +305,20 @@ void QgsMeasureDialog::updateUi()

if ( mMeasureArea )
{
if ( !mTool->canvas()->mapSettings().destinationCrs().isValid() )
if ( !mCanvas->mapSettings().destinationCrs().isValid() )
{
// no CRS => no units, newb!
toolTip += "<br> * " + tr( "No map projection set, so area is calculated using Cartesian calculations." );
toolTip += "<br> * " + tr( "Units are unknown." );
forceCartesian = true;
convertToDisplayUnits = false;
}
else if ( mTool->canvas()->mapSettings().destinationCrs().mapUnits() == QgsUnitTypes::DistanceDegrees
else if ( mCanvas->mapSettings().destinationCrs().mapUnits() == QgsUnitTypes::DistanceDegrees
&& ( mAreaUnits == QgsUnitTypes::AreaSquareDegrees || mAreaUnits == QgsUnitTypes::AreaUnknownUnit ) )
{
//both source and destination units are degrees
toolTip += "<br> * " + tr( "Both project CRS (%1) and measured area are in degrees, so area is calculated using Cartesian calculations in square degrees." ).arg(
mTool->canvas()->mapSettings().destinationCrs().description() );
mCanvas->mapSettings().destinationCrs().description() );
forceCartesian = true;
convertToDisplayUnits = false; //not required since we will be measuring in degrees
}
Expand All @@ -335,10 +334,10 @@ void QgsMeasureDialog::updateUi()
}
else
{
resultUnit = QgsUnitTypes::distanceToAreaUnit( mTool->canvas()->mapSettings().destinationCrs().mapUnits() );
resultUnit = QgsUnitTypes::distanceToAreaUnit( mCanvas->mapSettings().destinationCrs().mapUnits() );
toolTip += "<br> * " + tr( "Project ellipsoidal calculation is not selected." ) + ' ';
toolTip += tr( "Area is calculated in %1, based on project CRS (%2)." ).arg( QgsUnitTypes::toString( resultUnit ),
mTool->canvas()->mapSettings().destinationCrs().description() );
mCanvas->mapSettings().destinationCrs().description() );
}
setWindowTitle( tr( "Measure" ) );

Expand Down Expand Up @@ -376,20 +375,20 @@ void QgsMeasureDialog::updateUi()
}
else
{
if ( !mTool->canvas()->mapSettings().destinationCrs().isValid() )
if ( !mCanvas->mapSettings().destinationCrs().isValid() )
{
// no CRS => no units, newb!
toolTip += "<br> * " + tr( "No map projection set, so distance is calculated using Cartesian calculations." );
toolTip += "<br> * " + tr( "Units are unknown." );
forceCartesian = true;
convertToDisplayUnits = false;
}
else if ( mTool->canvas()->mapSettings().destinationCrs().mapUnits() == QgsUnitTypes::DistanceDegrees
else if ( mCanvas->mapSettings().destinationCrs().mapUnits() == QgsUnitTypes::DistanceDegrees
&& mDistanceUnits == QgsUnitTypes::DistanceDegrees )
{
//both source and destination units are degrees
toolTip += "<br> * " + tr( "Both project CRS (%1) and measured length are in degrees, so distance is calculated using Cartesian calculations in degrees." ).arg(
mTool->canvas()->mapSettings().destinationCrs().description() );
mCanvas->mapSettings().destinationCrs().description() );
forceCartesian = true;
convertToDisplayUnits = false; //not required since we will be measuring in degrees
}
Expand All @@ -405,10 +404,10 @@ void QgsMeasureDialog::updateUi()
}
else
{
resultUnit = mTool->canvas()->mapSettings().destinationCrs().mapUnits();
resultUnit = mCanvas->mapSettings().destinationCrs().mapUnits();
toolTip += "<br> * " + tr( "Project ellipsoidal calculation is not selected." ) + ' ';
toolTip += tr( "Distance is calculated in %1, based on project CRS (%2)." ).arg( QgsUnitTypes::toString( resultUnit ),
mTool->canvas()->mapSettings().destinationCrs().description() );
mCanvas->mapSettings().destinationCrs().description() );
}
setWindowTitle( tr( "Measure" ) );

Expand Down
15 changes: 9 additions & 6 deletions src/app/qgsmeasuredialog.h
Expand Up @@ -26,6 +26,7 @@

class QCloseEvent;
class QgsMeasureTool;
class QgsMapCanvas;

class APP_EXPORT QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase
{
Expand Down Expand Up @@ -92,29 +93,31 @@ class APP_EXPORT QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase

double convertArea( double area, QgsUnitTypes::AreaUnit toUnit ) const;

double mTotal;
double mTotal = 0.0;

//! indicates whether we're measuring distances or areas
bool mMeasureArea;
bool mMeasureArea = false;

//! Number of decimal places we want.
int mDecimalPlaces;
int mDecimalPlaces = 3;

//! Current unit for input values
QgsUnitTypes::DistanceUnit mCanvasUnits;
QgsUnitTypes::DistanceUnit mCanvasUnits = QgsUnitTypes::DistanceUnknownUnit;

//! Current unit for distance values
QgsUnitTypes::DistanceUnit mDistanceUnits;
QgsUnitTypes::DistanceUnit mDistanceUnits = QgsUnitTypes::DistanceUnknownUnit;

//! Current unit for area values
QgsUnitTypes::AreaUnit mAreaUnits;
QgsUnitTypes::AreaUnit mAreaUnits = QgsUnitTypes::AreaUnknownUnit;

//! Our measurement object
QgsDistanceArea mDa;

//! pointer to measure tool which owns this dialog
QgsMeasureTool *mTool = nullptr;

QgsMapCanvas *mCanvas = nullptr;

QgsPointXY mLastMousePoint;

void showHelp();
Expand Down
14 changes: 9 additions & 5 deletions src/app/qgsmeasuretool.cpp
Expand Up @@ -34,17 +34,14 @@

QgsMeasureTool::QgsMeasureTool( QgsMapCanvas *canvas, bool measureArea )
: QgsMapTool( canvas )
, mWrongProjectProjection( false )
, mMeasureArea( measureArea )
, mSnapIndicator( new QgsSnapIndicator( canvas ) )
{
mMeasureArea = measureArea;

mRubberBand = new QgsRubberBand( canvas, mMeasureArea ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry );
mRubberBandPoints = new QgsRubberBand( canvas, QgsWkbTypes::PointGeometry );

setCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::CrossHair ) );

mDone = true;
// Append point we will move
mPoints.append( QgsPointXY( 0, 0 ) );
mDestinationCrs = canvas->mapSettings().destinationCrs();
Expand All @@ -56,7 +53,14 @@ QgsMeasureTool::QgsMeasureTool( QgsMapCanvas *canvas, bool measureArea )
connect( canvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsMeasureTool::updateSettings );
}

QVector<QgsPointXY> QgsMeasureTool::points()
QgsMeasureTool::~QgsMeasureTool()
{
// important - dialog is not parented to this tool (it's parented to the main window)
// but we want to clean it up now
delete mDialog;
}

QVector<QgsPointXY> QgsMeasureTool::points() const
{
return mPoints;
}
Expand Down
36 changes: 15 additions & 21 deletions src/app/qgsmeasuretool.h
Expand Up @@ -35,13 +35,15 @@ class APP_EXPORT QgsMeasureTool : public QgsMapTool

QgsMeasureTool( QgsMapCanvas *canvas, bool measureArea );

~QgsMeasureTool() override;

Flags flags() const override { return QgsMapTool::AllowZoomRect; }

//! returns whether measuring distance or area
bool measureArea() { return mMeasureArea; }
bool measureArea() const { return mMeasureArea; }

//! When we have added our last point, and not following
bool done() { return mDone; }
bool done() const { return mDone; }

//! Reset and start new
void restart();
Expand All @@ -50,29 +52,19 @@ class APP_EXPORT QgsMeasureTool : public QgsMapTool
void addPoint( const QgsPointXY &point );

//! Returns reference to array of the points
QVector<QgsPointXY> points();
QVector<QgsPointXY> points() const;

// Inherited from QgsMapTool

//! Mouse move event for overriding
void canvasMoveEvent( QgsMapMouseEvent *e ) override;

//! Mouse press event for overriding
void canvasPressEvent( QgsMapMouseEvent *e ) override;

//! Mouse release event for overriding
void canvasReleaseEvent( QgsMapMouseEvent *e ) override;

//! called when set as currently active map tool
void activate() override;

//! called when map tool is being deactivated
void deactivate() override;

void keyPressEvent( QKeyEvent *e ) override;

public slots:
//! updates the projections we're using
//! Updates the projections we're using
void updateSettings();

protected:
Expand All @@ -87,15 +79,17 @@ class APP_EXPORT QgsMeasureTool : public QgsMapTool
//! Rubberband widget tracking the added nodes to line
QgsRubberBand *mRubberBandPoints = nullptr;

//! indicates whether we're measuring distances or areas
bool mMeasureArea;
//! Indicates whether we're measuring distances or areas
bool mMeasureArea = false;

//! indicates whether we've just done a right mouse click
bool mDone;
//! Indicates whether we've just done a right mouse click
bool mDone = true;

//! indicates whether we've recently warned the user about having the wrong
// project projection
bool mWrongProjectProjection;
/**
* Indicates whether we've recently warned the user about having the wrong
* project projection.
*/
bool mWrongProjectProjection = false;

//! Destination CoordinateReferenceSystem used by the MapCanvas
QgsCoordinateReferenceSystem mDestinationCrs;
Expand Down

0 comments on commit 429374e

Please sign in to comment.