From 3def1fbdf6315cad7023ce08c5f494bf3963b88b Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 14 Aug 2025 12:59:15 +1000 Subject: [PATCH] Use nicer signal for scale ratio widget updates --- .../qgselevationprofilecanvas.py | 2 +- .../qgselevationprofilecanvas.sip.in | 7 ++++ .../qgselevationprofilecanvas.py | 2 +- .../qgselevationprofilecanvas.sip.in | 7 ++++ .../elevation/qgselevationprofilewidget.cpp | 33 ++++++++++--------- .../elevation/qgselevationprofilecanvas.cpp | 10 ++++++ src/gui/elevation/qgselevationprofilecanvas.h | 7 ++++ 7 files changed, 50 insertions(+), 18 deletions(-) diff --git a/python/PyQt6/gui/auto_additions/qgselevationprofilecanvas.py b/python/PyQt6/gui/auto_additions/qgselevationprofilecanvas.py index 118df89acbd..d665561e270 100644 --- a/python/PyQt6/gui/auto_additions/qgselevationprofilecanvas.py +++ b/python/PyQt6/gui/auto_additions/qgselevationprofilecanvas.py @@ -1,6 +1,6 @@ # The following has been generated automatically from src/gui/elevation/qgselevationprofilecanvas.h try: - QgsElevationProfileCanvas.__attribute_docs__ = {'activeJobCountChanged': 'Emitted when the number of active background jobs changes.\n', 'canvasPointHovered': 'Emitted when the mouse hovers over the specified point (in canvas\ncoordinates).\n\nThe ``profilePoint`` argument gives the hovered profile point, which may\nbe snapped.\n'} + QgsElevationProfileCanvas.__attribute_docs__ = {'activeJobCountChanged': 'Emitted when the number of active background jobs changes.\n', 'canvasPointHovered': 'Emitted when the mouse hovers over the specified point (in canvas\ncoordinates).\n\nThe ``profilePoint`` argument gives the hovered profile point, which may\nbe snapped.\n', 'scaleChanged': 'Emitted when the plot scale is changed.\n\n.. versionadded:: 4.0\n'} QgsElevationProfileCanvas.__overridden_methods__ = ['crs', 'toMapCoordinates', 'toCanvasCoordinates', 'resizeEvent', 'paintEvent', 'panContentsBy', 'centerPlotOn', 'scalePlot', 'snapToPlot', 'zoomToRect', 'wheelZoom', 'mouseMoveEvent', 'refresh'] QgsElevationProfileCanvas.__signal_arguments__ = {'activeJobCountChanged': ['count: int'], 'canvasPointHovered': ['point: QgsPointXY', 'profilePoint: QgsProfilePoint']} QgsElevationProfileCanvas.__group__ = ['elevation'] diff --git a/python/PyQt6/gui/auto_generated/elevation/qgselevationprofilecanvas.sip.in b/python/PyQt6/gui/auto_generated/elevation/qgselevationprofilecanvas.sip.in index e5711ba2964..f5ddc5ed480 100644 --- a/python/PyQt6/gui/auto_generated/elevation/qgselevationprofilecanvas.sip.in +++ b/python/PyQt6/gui/auto_generated/elevation/qgselevationprofilecanvas.sip.in @@ -328,6 +328,13 @@ coordinates). The ``profilePoint`` argument gives the hovered profile point, which may be snapped. +%End + + void scaleChanged(); +%Docstring +Emitted when the plot scale is changed. + +.. versionadded:: 4.0 %End public slots: diff --git a/python/gui/auto_additions/qgselevationprofilecanvas.py b/python/gui/auto_additions/qgselevationprofilecanvas.py index 118df89acbd..d665561e270 100644 --- a/python/gui/auto_additions/qgselevationprofilecanvas.py +++ b/python/gui/auto_additions/qgselevationprofilecanvas.py @@ -1,6 +1,6 @@ # The following has been generated automatically from src/gui/elevation/qgselevationprofilecanvas.h try: - QgsElevationProfileCanvas.__attribute_docs__ = {'activeJobCountChanged': 'Emitted when the number of active background jobs changes.\n', 'canvasPointHovered': 'Emitted when the mouse hovers over the specified point (in canvas\ncoordinates).\n\nThe ``profilePoint`` argument gives the hovered profile point, which may\nbe snapped.\n'} + QgsElevationProfileCanvas.__attribute_docs__ = {'activeJobCountChanged': 'Emitted when the number of active background jobs changes.\n', 'canvasPointHovered': 'Emitted when the mouse hovers over the specified point (in canvas\ncoordinates).\n\nThe ``profilePoint`` argument gives the hovered profile point, which may\nbe snapped.\n', 'scaleChanged': 'Emitted when the plot scale is changed.\n\n.. versionadded:: 4.0\n'} QgsElevationProfileCanvas.__overridden_methods__ = ['crs', 'toMapCoordinates', 'toCanvasCoordinates', 'resizeEvent', 'paintEvent', 'panContentsBy', 'centerPlotOn', 'scalePlot', 'snapToPlot', 'zoomToRect', 'wheelZoom', 'mouseMoveEvent', 'refresh'] QgsElevationProfileCanvas.__signal_arguments__ = {'activeJobCountChanged': ['count: int'], 'canvasPointHovered': ['point: QgsPointXY', 'profilePoint: QgsProfilePoint']} QgsElevationProfileCanvas.__group__ = ['elevation'] diff --git a/python/gui/auto_generated/elevation/qgselevationprofilecanvas.sip.in b/python/gui/auto_generated/elevation/qgselevationprofilecanvas.sip.in index e5711ba2964..f5ddc5ed480 100644 --- a/python/gui/auto_generated/elevation/qgselevationprofilecanvas.sip.in +++ b/python/gui/auto_generated/elevation/qgselevationprofilecanvas.sip.in @@ -328,6 +328,13 @@ coordinates). The ``profilePoint`` argument gives the hovered profile point, which may be snapped. +%End + + void scaleChanged(); +%Docstring +Emitted when the plot scale is changed. + +.. versionadded:: 4.0 %End public slots: diff --git a/src/app/elevation/qgselevationprofilewidget.cpp b/src/app/elevation/qgselevationprofilewidget.cpp index e0576635b99..a626cb0597c 100644 --- a/src/app/elevation/qgselevationprofilewidget.cpp +++ b/src/app/elevation/qgselevationprofilewidget.cpp @@ -355,15 +355,6 @@ QgsElevationProfileWidget::QgsElevationProfileWidget( const QString &name ) mOptionsMenu->addAction( mLockRatioAction ); mScaleRatioSettingsAction = new QgsElevationProfileScaleRatioWidgetSettingsAction( mOptionsMenu ); - connect( mScaleRatioSettingsAction->scaleRatioWidget(), &QgsScaleComboBox::scaleChanged, this, [this]( double scale ) { - const double distanceToElevationRatio = 1.0 / scale; - if ( mBlockScaleRatioChanges ) - return; - - mCanvas->setAxisScaleRatio( distanceToElevationRatio ); - createOrUpdateRubberBands(); - scheduleUpdate(); - } ); mOptionsMenu->addAction( mScaleRatioSettingsAction ); @@ -457,13 +448,6 @@ QgsElevationProfileWidget::QgsElevationProfileWidget( const QString &name ) toolBar->addWidget( mBtnOptions ); - connect( mOptionsMenu, &QMenu::aboutToShow, this, [this] { - mBlockScaleRatioChanges++; - const double distanceToElevationScaleRatio = mCanvas->axisScaleRatio(); - mScaleRatioSettingsAction->scaleRatioWidget()->setScale( 1.0 / distanceToElevationScaleRatio ); - mBlockScaleRatioChanges--; - } ); - mProgressPendingJobs = new QProgressBar( this ); mProgressPendingJobs->setRange( 0, 0 ); mProgressPendingJobs->hide(); @@ -518,6 +502,23 @@ QgsElevationProfileWidget::QgsElevationProfileWidget( const QString &name ) connect( QgsProject::instance()->elevationProperties(), &QgsProjectElevationProperties::changed, this, &QgsElevationProfileWidget::onProjectElevationPropertiesChanged ); connect( QgsProject::instance(), &QgsProject::crs3DChanged, this, &QgsElevationProfileWidget::onProjectElevationPropertiesChanged ); + connect( mCanvas, &QgsElevationProfileCanvas::scaleChanged, this, [this] { + mBlockScaleRatioChanges++; + const double distanceToElevationScaleRatio = mCanvas->axisScaleRatio(); + mScaleRatioSettingsAction->scaleRatioWidget()->setScale( 1.0 / distanceToElevationScaleRatio ); + mBlockScaleRatioChanges--; + } ); + + connect( mScaleRatioSettingsAction->scaleRatioWidget(), &QgsScaleComboBox::scaleChanged, this, [this]( double scale ) { + const double distanceToElevationRatio = 1.0 / scale; + if ( mBlockScaleRatioChanges ) + return; + + mCanvas->setAxisScaleRatio( distanceToElevationRatio ); + createOrUpdateRubberBands(); + scheduleUpdate(); + } ); + updateCanvasLayers(); } diff --git a/src/gui/elevation/qgselevationprofilecanvas.cpp b/src/gui/elevation/qgselevationprofilecanvas.cpp index fec3d90e981..6cb0bdd6859 100644 --- a/src/gui/elevation/qgselevationprofilecanvas.cpp +++ b/src/gui/elevation/qgselevationprofilecanvas.cpp @@ -639,6 +639,7 @@ void QgsElevationProfileCanvas::setDistanceUnit( Qgis::DistanceUnit unit ) mPlotItem->setXMinimum( oldMin / mPlotItem->mXScaleFactor ); mPlotItem->setXMaximum( oldMax / mPlotItem->mXScaleFactor ); mPlotItem->updatePlot(); + emit scaleChanged(); } void QgsElevationProfileCanvas::setBackgroundColor( const QColor &color ) @@ -700,6 +701,7 @@ void QgsElevationProfileCanvas::setLockAxisScales( bool lock ) refineResults(); mPlotItem->updatePlot(); emit plotAreaChanged(); + emit scaleChanged(); } } @@ -727,6 +729,7 @@ void QgsElevationProfileCanvas::setAxisScaleRatio( double scale ) refineResults(); mPlotItem->updatePlot(); emit plotAreaChanged(); + emit scaleChanged(); } QgsPointXY QgsElevationProfileCanvas::snapToPlot( QPoint point ) @@ -774,6 +777,7 @@ void QgsElevationProfileCanvas::scalePlot( double xFactor, double yFactor ) refineResults(); mPlotItem->updatePlot(); emit plotAreaChanged(); + emit scaleChanged(); } void QgsElevationProfileCanvas::zoomToRect( const QRectF &rect ) @@ -798,6 +802,7 @@ void QgsElevationProfileCanvas::zoomToRect( const QRectF &rect ) refineResults(); mPlotItem->updatePlot(); emit plotAreaChanged(); + emit scaleChanged(); } void QgsElevationProfileCanvas::wheelZoom( QWheelEvent *event ) @@ -854,6 +859,7 @@ void QgsElevationProfileCanvas::wheelZoom( QWheelEvent *event ) scalePlot( 1 / zoomFactor ); } emit plotAreaChanged(); + emit scaleChanged(); } void QgsElevationProfileCanvas::mouseMoveEvent( QMouseEvent *e ) @@ -1226,6 +1232,8 @@ void QgsElevationProfileCanvas::resizeEvent( QResizeEvent *event ) mPlotItem->updateRect(); mCrossHairsItem->updateRect(); + + emit scaleChanged(); } void QgsElevationProfileCanvas::paintEvent( QPaintEvent *event ) @@ -1344,6 +1352,7 @@ void QgsElevationProfileCanvas::zoomFull() refineResults(); mPlotItem->updatePlot(); emit plotAreaChanged(); + emit scaleChanged(); } void QgsElevationProfileCanvas::setVisiblePlotRange( double minimumDistance, double maximumDistance, double minimumElevation, double maximumElevation ) @@ -1360,6 +1369,7 @@ void QgsElevationProfileCanvas::setVisiblePlotRange( double minimumDistance, dou refineResults(); mPlotItem->updatePlot(); emit plotAreaChanged(); + emit scaleChanged(); } QgsDoubleRange QgsElevationProfileCanvas::visibleDistanceRange() const diff --git a/src/gui/elevation/qgselevationprofilecanvas.h b/src/gui/elevation/qgselevationprofilecanvas.h index 231e75e76ed..f5406ce14d9 100644 --- a/src/gui/elevation/qgselevationprofilecanvas.h +++ b/src/gui/elevation/qgselevationprofilecanvas.h @@ -322,6 +322,13 @@ class GUI_EXPORT QgsElevationProfileCanvas : public QgsPlotCanvas */ void canvasPointHovered( const QgsPointXY &point, const QgsProfilePoint &profilePoint ); + /** + * Emitted when the plot scale is changed. + * + * \since QGIS 4.0 + */ + void scaleChanged(); + public slots: /**