From 75e613c3adbe7465af29b3eaef3c7b0d612ace5a Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Mon, 15 Jan 2018 14:26:44 +0100 Subject: [PATCH] [bugfix] Transfer focus to canvas when selecting transparency This is an attempt to fix #17845 Setting Raster Transparency using "Add values from display" doesn't work Fixes #17845 --- src/app/qgisapp.cpp | 18 +++++++++++++++--- src/app/qgsrasterlayerproperties.cpp | 28 +++++++++++++--------------- src/app/qgsrasterlayerproperties.h | 6 +++--- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index 58c29e7439b1..d3e7154d1c3d 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -12629,9 +12629,21 @@ void QgisApp::showLayerProperties( QgsMapLayer *ml ) #else QgsRasterLayerProperties *rlp = new QgsRasterLayerProperties( ml, mMapCanvas, this ); #endif - - rlp->exec(); - delete rlp; // delete since dialog cannot be reused without updating code + // Cannot use exec here due to raster transparency map tool: + // in order to pass focus to the canvas, the dialog needs to + // be hidden and shown in non-modal mode. + rlp->setModal( true ); + rlp->show(); + // Delete (later, for safety) since dialog cannot be reused without + // updating code + connect( rlp, &QgsRasterLayerProperties::accepted, [ rlp ] + { + rlp->deleteLater(); + } ); + connect( rlp, &QgsRasterLayerProperties::rejected, [ rlp ] + { + rlp->deleteLater(); + } ); } else if ( ml->type() == QgsMapLayer::VectorLayer ) // VECTOR { diff --git a/src/app/qgsrasterlayerproperties.cpp b/src/app/qgsrasterlayerproperties.cpp index c7ebeb4c3ed9..80d8b0bdda18 100644 --- a/src/app/qgsrasterlayerproperties.cpp +++ b/src/app/qgsrasterlayerproperties.cpp @@ -176,11 +176,10 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer *lyr, QgsMapCanv pbnImportTransparentPixelValues->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionFileOpen.svg" ) ) ); pbnExportTransparentPixelValues->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionFileSave.svg" ) ) ); - mPixelSelectorTool = nullptr; if ( mMapCanvas ) { - mPixelSelectorTool = new QgsMapToolEmitPoint( canvas ); - connect( mPixelSelectorTool, &QgsMapToolEmitPoint::canvasClicked, this, &QgsRasterLayerProperties::pixelSelected ); + mPixelSelectorTool = qgis::make_unique( canvas ); + connect( mPixelSelectorTool.get(), &QgsMapToolEmitPoint::canvasClicked, this, &QgsRasterLayerProperties::pixelSelected ); } else { @@ -455,10 +454,6 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer *lyr, QgsMapCanv QgsRasterLayerProperties::~QgsRasterLayerProperties() { - if ( mPixelSelectorTool ) - { - delete mPixelSelectorTool; - } } void QgsRasterLayerProperties::setupTransparencyTable( int nBands ) @@ -1197,16 +1192,18 @@ void QgsRasterLayerProperties::pbnAddValuesFromDisplay_clicked() { if ( mMapCanvas && mPixelSelectorTool ) { - mMapCanvas->setMapTool( mPixelSelectorTool ); //Need to work around the modality of the dialog but can not just hide() it. + // According to Qt5 docs, to change modality the dialog needs to be hidden + // and shown it again. + hide(); setModal( false ); - showMinimized(); - //Q_ASSERT( parentWidget()->parentWidget() ); - parentWidget()->activateWindow(); - parentWidget()->raise(); - //lower(); + // Transfer focus to the canvas to use the selector tool + mMapCanvas->window()->raise(); + mMapCanvas->window()->activateWindow(); + mMapCanvas->window()->setFocus(); + mMapCanvas->setMapTool( mPixelSelectorTool.get() ); } } @@ -1630,8 +1627,9 @@ void QgsRasterLayerProperties::pbnRemoveSelectedRow_clicked() } } -void QgsRasterLayerProperties::pixelSelected( const QgsPointXY &canvasPoint ) +void QgsRasterLayerProperties::pixelSelected( const QgsPointXY &canvasPoint, const Qt::MouseButton &btn ) { + Q_UNUSED( btn ); QgsRasterRenderer *renderer = mRendererWidget->renderer(); if ( !renderer ) { @@ -1645,7 +1643,7 @@ void QgsRasterLayerProperties::pixelSelected( const QgsPointXY &canvasPoint ) //Get the pixel values and add a new entry to the transparency table if ( mMapCanvas && mPixelSelectorTool ) { - mMapCanvas->unsetMapTool( mPixelSelectorTool ); + mMapCanvas->unsetMapTool( mPixelSelectorTool.get() ); const QgsMapSettings &ms = mMapCanvas->mapSettings(); QgsPointXY myPoint = ms.mapToLayerCoordinates( mRasterLayer, canvasPoint ); diff --git a/src/app/qgsrasterlayerproperties.h b/src/app/qgsrasterlayerproperties.h index 19f473c28fcc..74d1839f4e96 100644 --- a/src/app/qgsrasterlayerproperties.h +++ b/src/app/qgsrasterlayerproperties.h @@ -26,13 +26,13 @@ #include "qgsguiutils.h" #include "qgshelp.h" #include "qgsmaplayerstylemanager.h" +#include "qgsmaptoolemitpoint.h" #include "qgis_app.h" class QgsPointXY; class QgsMapLayer; class QgsMapCanvas; class QgsRasterLayer; -class QgsMapToolEmitPoint; class QgsMetadataWidget; class QgsRasterRenderer; class QgsRasterRendererWidget; @@ -91,7 +91,7 @@ class APP_EXPORT QgsRasterLayerProperties : public QgsOptionsDialogBase, private */ //void on_btnResetNull_clicked(); - void pixelSelected( const QgsPointXY & ); + void pixelSelected( const QgsPointXY &, const Qt::MouseButton & ); private slots: void mRenderTypeComboBox_currentIndexChanged( int index ); @@ -206,7 +206,7 @@ class APP_EXPORT QgsRasterLayerProperties : public QgsOptionsDialogBase, private qreal mGradientWidth; QgsMapCanvas *mMapCanvas = nullptr; - QgsMapToolEmitPoint *mPixelSelectorTool = nullptr; + std::unique_ptr mPixelSelectorTool = nullptr; QgsRasterHistogramWidget *mHistogramWidget = nullptr;