Skip to content

Commit 429374e

Browse files
committed
Code cleanup
1 parent 6749c8d commit 429374e

4 files changed

Lines changed: 50 additions & 50 deletions

File tree

‎src/app/qgsmeasuredialog.cpp‎

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333

3434
QgsMeasureDialog::QgsMeasureDialog( QgsMeasureTool *tool, Qt::WindowFlags f )
3535
: QDialog( tool->canvas()->topLevelWidget(), f )
36+
, mMeasureArea( tool->measureArea() )
3637
, mTool( tool )
38+
, mCanvas( tool->canvas() )
3739
{
3840
setupUi( this );
3941
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsMeasureDialog::showHelp );
@@ -47,16 +49,13 @@ QgsMeasureDialog::QgsMeasureDialog( QgsMeasureTool *tool, Qt::WindowFlags f )
4749
buttonBox->addButton( cb, QDialogButtonBox::ActionRole );
4850
connect( cb, &QAbstractButton::clicked, this, &QgsMeasureDialog::openConfigTab );
4951

50-
mMeasureArea = tool->measureArea();
51-
mTotal = 0.;
52-
5352
repopulateComboBoxUnits( mMeasureArea );
5453
if ( mMeasureArea )
5554
mUnitsCombo->setCurrentIndex( mUnitsCombo->findData( QgsProject::instance()->areaUnits() ) );
5655
else
5756
mUnitsCombo->setCurrentIndex( mUnitsCombo->findData( QgsProject::instance()->distanceUnits() ) );
5857

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

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

7473
groupBox->setCollapsed( true );
7574
}
@@ -81,7 +80,7 @@ void QgsMeasureDialog::openConfigTab()
8180

8281
void QgsMeasureDialog::crsChanged()
8382
{
84-
if ( !mTool->canvas()->mapSettings().destinationCrs().isValid() )
83+
if ( !mCanvas->mapSettings().destinationCrs().isValid() )
8584
{
8685
mUnitsCombo->setEnabled( false );
8786
if ( mMeasureArea )
@@ -101,11 +100,11 @@ void QgsMeasureDialog::updateSettings()
101100
QgsSettings settings;
102101

103102
mDecimalPlaces = settings.value( QStringLiteral( "qgis/measure/decimalplaces" ), "3" ).toInt();
104-
mCanvasUnits = mTool->canvas()->mapUnits();
103+
mCanvasUnits = mCanvas->mapUnits();
105104
// Configure QgsDistanceArea
106105
mDistanceUnits = QgsProject::instance()->distanceUnits();
107106
mAreaUnits = QgsProject::instance()->areaUnits();
108-
mDa.setSourceCrs( mTool->canvas()->mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
107+
mDa.setSourceCrs( mCanvas->mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
109108
mDa.setEllipsoid( QgsProject::instance()->ellipsoid() );
110109

111110
mTable->clear();
@@ -306,20 +305,20 @@ void QgsMeasureDialog::updateUi()
306305

307306
if ( mMeasureArea )
308307
{
309-
if ( !mTool->canvas()->mapSettings().destinationCrs().isValid() )
308+
if ( !mCanvas->mapSettings().destinationCrs().isValid() )
310309
{
311310
// no CRS => no units, newb!
312311
toolTip += "<br> * " + tr( "No map projection set, so area is calculated using Cartesian calculations." );
313312
toolTip += "<br> * " + tr( "Units are unknown." );
314313
forceCartesian = true;
315314
convertToDisplayUnits = false;
316315
}
317-
else if ( mTool->canvas()->mapSettings().destinationCrs().mapUnits() == QgsUnitTypes::DistanceDegrees
316+
else if ( mCanvas->mapSettings().destinationCrs().mapUnits() == QgsUnitTypes::DistanceDegrees
318317
&& ( mAreaUnits == QgsUnitTypes::AreaSquareDegrees || mAreaUnits == QgsUnitTypes::AreaUnknownUnit ) )
319318
{
320319
//both source and destination units are degrees
321320
toolTip += "<br> * " + tr( "Both project CRS (%1) and measured area are in degrees, so area is calculated using Cartesian calculations in square degrees." ).arg(
322-
mTool->canvas()->mapSettings().destinationCrs().description() );
321+
mCanvas->mapSettings().destinationCrs().description() );
323322
forceCartesian = true;
324323
convertToDisplayUnits = false; //not required since we will be measuring in degrees
325324
}
@@ -335,10 +334,10 @@ void QgsMeasureDialog::updateUi()
335334
}
336335
else
337336
{
338-
resultUnit = QgsUnitTypes::distanceToAreaUnit( mTool->canvas()->mapSettings().destinationCrs().mapUnits() );
337+
resultUnit = QgsUnitTypes::distanceToAreaUnit( mCanvas->mapSettings().destinationCrs().mapUnits() );
339338
toolTip += "<br> * " + tr( "Project ellipsoidal calculation is not selected." ) + ' ';
340339
toolTip += tr( "Area is calculated in %1, based on project CRS (%2)." ).arg( QgsUnitTypes::toString( resultUnit ),
341-
mTool->canvas()->mapSettings().destinationCrs().description() );
340+
mCanvas->mapSettings().destinationCrs().description() );
342341
}
343342
setWindowTitle( tr( "Measure" ) );
344343

@@ -376,20 +375,20 @@ void QgsMeasureDialog::updateUi()
376375
}
377376
else
378377
{
379-
if ( !mTool->canvas()->mapSettings().destinationCrs().isValid() )
378+
if ( !mCanvas->mapSettings().destinationCrs().isValid() )
380379
{
381380
// no CRS => no units, newb!
382381
toolTip += "<br> * " + tr( "No map projection set, so distance is calculated using Cartesian calculations." );
383382
toolTip += "<br> * " + tr( "Units are unknown." );
384383
forceCartesian = true;
385384
convertToDisplayUnits = false;
386385
}
387-
else if ( mTool->canvas()->mapSettings().destinationCrs().mapUnits() == QgsUnitTypes::DistanceDegrees
386+
else if ( mCanvas->mapSettings().destinationCrs().mapUnits() == QgsUnitTypes::DistanceDegrees
388387
&& mDistanceUnits == QgsUnitTypes::DistanceDegrees )
389388
{
390389
//both source and destination units are degrees
391390
toolTip += "<br> * " + tr( "Both project CRS (%1) and measured length are in degrees, so distance is calculated using Cartesian calculations in degrees." ).arg(
392-
mTool->canvas()->mapSettings().destinationCrs().description() );
391+
mCanvas->mapSettings().destinationCrs().description() );
393392
forceCartesian = true;
394393
convertToDisplayUnits = false; //not required since we will be measuring in degrees
395394
}
@@ -405,10 +404,10 @@ void QgsMeasureDialog::updateUi()
405404
}
406405
else
407406
{
408-
resultUnit = mTool->canvas()->mapSettings().destinationCrs().mapUnits();
407+
resultUnit = mCanvas->mapSettings().destinationCrs().mapUnits();
409408
toolTip += "<br> * " + tr( "Project ellipsoidal calculation is not selected." ) + ' ';
410409
toolTip += tr( "Distance is calculated in %1, based on project CRS (%2)." ).arg( QgsUnitTypes::toString( resultUnit ),
411-
mTool->canvas()->mapSettings().destinationCrs().description() );
410+
mCanvas->mapSettings().destinationCrs().description() );
412411
}
413412
setWindowTitle( tr( "Measure" ) );
414413

‎src/app/qgsmeasuredialog.h‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
class QCloseEvent;
2828
class QgsMeasureTool;
29+
class QgsMapCanvas;
2930

3031
class APP_EXPORT QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase
3132
{
@@ -92,29 +93,31 @@ class APP_EXPORT QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase
9293

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

95-
double mTotal;
96+
double mTotal = 0.0;
9697

9798
//! indicates whether we're measuring distances or areas
98-
bool mMeasureArea;
99+
bool mMeasureArea = false;
99100

100101
//! Number of decimal places we want.
101-
int mDecimalPlaces;
102+
int mDecimalPlaces = 3;
102103

103104
//! Current unit for input values
104-
QgsUnitTypes::DistanceUnit mCanvasUnits;
105+
QgsUnitTypes::DistanceUnit mCanvasUnits = QgsUnitTypes::DistanceUnknownUnit;
105106

106107
//! Current unit for distance values
107-
QgsUnitTypes::DistanceUnit mDistanceUnits;
108+
QgsUnitTypes::DistanceUnit mDistanceUnits = QgsUnitTypes::DistanceUnknownUnit;
108109

109110
//! Current unit for area values
110-
QgsUnitTypes::AreaUnit mAreaUnits;
111+
QgsUnitTypes::AreaUnit mAreaUnits = QgsUnitTypes::AreaUnknownUnit;
111112

112113
//! Our measurement object
113114
QgsDistanceArea mDa;
114115

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

119+
QgsMapCanvas *mCanvas = nullptr;
120+
118121
QgsPointXY mLastMousePoint;
119122

120123
void showHelp();

‎src/app/qgsmeasuretool.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,14 @@
3434

3535
QgsMeasureTool::QgsMeasureTool( QgsMapCanvas *canvas, bool measureArea )
3636
: QgsMapTool( canvas )
37-
, mWrongProjectProjection( false )
37+
, mMeasureArea( measureArea )
3838
, mSnapIndicator( new QgsSnapIndicator( canvas ) )
3939
{
40-
mMeasureArea = measureArea;
41-
4240
mRubberBand = new QgsRubberBand( canvas, mMeasureArea ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry );
4341
mRubberBandPoints = new QgsRubberBand( canvas, QgsWkbTypes::PointGeometry );
4442

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

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

59-
QVector<QgsPointXY> QgsMeasureTool::points()
56+
QgsMeasureTool::~QgsMeasureTool()
57+
{
58+
// important - dialog is not parented to this tool (it's parented to the main window)
59+
// but we want to clean it up now
60+
delete mDialog;
61+
}
62+
63+
QVector<QgsPointXY> QgsMeasureTool::points() const
6064
{
6165
return mPoints;
6266
}

‎src/app/qgsmeasuretool.h‎

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ class APP_EXPORT QgsMeasureTool : public QgsMapTool
3535

3636
QgsMeasureTool( QgsMapCanvas *canvas, bool measureArea );
3737

38+
~QgsMeasureTool() override;
39+
3840
Flags flags() const override { return QgsMapTool::AllowZoomRect; }
3941

4042
//! returns whether measuring distance or area
41-
bool measureArea() { return mMeasureArea; }
43+
bool measureArea() const { return mMeasureArea; }
4244

4345
//! When we have added our last point, and not following
44-
bool done() { return mDone; }
46+
bool done() const { return mDone; }
4547

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

5254
//! Returns reference to array of the points
53-
QVector<QgsPointXY> points();
55+
QVector<QgsPointXY> points() const;
5456

5557
// Inherited from QgsMapTool
5658

57-
//! Mouse move event for overriding
5859
void canvasMoveEvent( QgsMapMouseEvent *e ) override;
59-
60-
//! Mouse press event for overriding
6160
void canvasPressEvent( QgsMapMouseEvent *e ) override;
62-
63-
//! Mouse release event for overriding
6461
void canvasReleaseEvent( QgsMapMouseEvent *e ) override;
65-
66-
//! called when set as currently active map tool
6762
void activate() override;
68-
69-
//! called when map tool is being deactivated
7063
void deactivate() override;
71-
7264
void keyPressEvent( QKeyEvent *e ) override;
7365

7466
public slots:
75-
//! updates the projections we're using
67+
//! Updates the projections we're using
7668
void updateSettings();
7769

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

90-
//! indicates whether we're measuring distances or areas
91-
bool mMeasureArea;
82+
//! Indicates whether we're measuring distances or areas
83+
bool mMeasureArea = false;
9284

93-
//! indicates whether we've just done a right mouse click
94-
bool mDone;
85+
//! Indicates whether we've just done a right mouse click
86+
bool mDone = true;
9587

96-
//! indicates whether we've recently warned the user about having the wrong
97-
// project projection
98-
bool mWrongProjectProjection;
88+
/**
89+
* Indicates whether we've recently warned the user about having the wrong
90+
* project projection.
91+
*/
92+
bool mWrongProjectProjection = false;
9993

10094
//! Destination CoordinateReferenceSystem used by the MapCanvas
10195
QgsCoordinateReferenceSystem mDestinationCrs;

0 commit comments

Comments
 (0)