Skip to content

Commit 75e613c

Browse files
committed
[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
1 parent 066d677 commit 75e613c

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

src/app/qgisapp.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -12629,9 +12629,21 @@ void QgisApp::showLayerProperties( QgsMapLayer *ml )
1262912629
#else
1263012630
QgsRasterLayerProperties *rlp = new QgsRasterLayerProperties( ml, mMapCanvas, this );
1263112631
#endif
12632-
12633-
rlp->exec();
12634-
delete rlp; // delete since dialog cannot be reused without updating code
12632+
// Cannot use exec here due to raster transparency map tool:
12633+
// in order to pass focus to the canvas, the dialog needs to
12634+
// be hidden and shown in non-modal mode.
12635+
rlp->setModal( true );
12636+
rlp->show();
12637+
// Delete (later, for safety) since dialog cannot be reused without
12638+
// updating code
12639+
connect( rlp, &QgsRasterLayerProperties::accepted, [ rlp ]
12640+
{
12641+
rlp->deleteLater();
12642+
} );
12643+
connect( rlp, &QgsRasterLayerProperties::rejected, [ rlp ]
12644+
{
12645+
rlp->deleteLater();
12646+
} );
1263512647
}
1263612648
else if ( ml->type() == QgsMapLayer::VectorLayer ) // VECTOR
1263712649
{

src/app/qgsrasterlayerproperties.cpp

+13-15
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,10 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer *lyr, QgsMapCanv
176176
pbnImportTransparentPixelValues->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionFileOpen.svg" ) ) );
177177
pbnExportTransparentPixelValues->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionFileSave.svg" ) ) );
178178

179-
mPixelSelectorTool = nullptr;
180179
if ( mMapCanvas )
181180
{
182-
mPixelSelectorTool = new QgsMapToolEmitPoint( canvas );
183-
connect( mPixelSelectorTool, &QgsMapToolEmitPoint::canvasClicked, this, &QgsRasterLayerProperties::pixelSelected );
181+
mPixelSelectorTool = qgis::make_unique<QgsMapToolEmitPoint>( canvas );
182+
connect( mPixelSelectorTool.get(), &QgsMapToolEmitPoint::canvasClicked, this, &QgsRasterLayerProperties::pixelSelected );
184183
}
185184
else
186185
{
@@ -455,10 +454,6 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer *lyr, QgsMapCanv
455454

456455
QgsRasterLayerProperties::~QgsRasterLayerProperties()
457456
{
458-
if ( mPixelSelectorTool )
459-
{
460-
delete mPixelSelectorTool;
461-
}
462457
}
463458

464459
void QgsRasterLayerProperties::setupTransparencyTable( int nBands )
@@ -1197,16 +1192,18 @@ void QgsRasterLayerProperties::pbnAddValuesFromDisplay_clicked()
11971192
{
11981193
if ( mMapCanvas && mPixelSelectorTool )
11991194
{
1200-
mMapCanvas->setMapTool( mPixelSelectorTool );
12011195
//Need to work around the modality of the dialog but can not just hide() it.
1196+
// According to Qt5 docs, to change modality the dialog needs to be hidden
1197+
// and shown it again.
1198+
hide();
12021199
setModal( false );
1203-
12041200
showMinimized();
12051201

1206-
//Q_ASSERT( parentWidget()->parentWidget() );
1207-
parentWidget()->activateWindow();
1208-
parentWidget()->raise();
1209-
//lower();
1202+
// Transfer focus to the canvas to use the selector tool
1203+
mMapCanvas->window()->raise();
1204+
mMapCanvas->window()->activateWindow();
1205+
mMapCanvas->window()->setFocus();
1206+
mMapCanvas->setMapTool( mPixelSelectorTool.get() );
12101207
}
12111208
}
12121209

@@ -1630,8 +1627,9 @@ void QgsRasterLayerProperties::pbnRemoveSelectedRow_clicked()
16301627
}
16311628
}
16321629

1633-
void QgsRasterLayerProperties::pixelSelected( const QgsPointXY &canvasPoint )
1630+
void QgsRasterLayerProperties::pixelSelected( const QgsPointXY &canvasPoint, const Qt::MouseButton &btn )
16341631
{
1632+
Q_UNUSED( btn );
16351633
QgsRasterRenderer *renderer = mRendererWidget->renderer();
16361634
if ( !renderer )
16371635
{
@@ -1645,7 +1643,7 @@ void QgsRasterLayerProperties::pixelSelected( const QgsPointXY &canvasPoint )
16451643
//Get the pixel values and add a new entry to the transparency table
16461644
if ( mMapCanvas && mPixelSelectorTool )
16471645
{
1648-
mMapCanvas->unsetMapTool( mPixelSelectorTool );
1646+
mMapCanvas->unsetMapTool( mPixelSelectorTool.get() );
16491647

16501648
const QgsMapSettings &ms = mMapCanvas->mapSettings();
16511649
QgsPointXY myPoint = ms.mapToLayerCoordinates( mRasterLayer, canvasPoint );

src/app/qgsrasterlayerproperties.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
#include "qgsguiutils.h"
2727
#include "qgshelp.h"
2828
#include "qgsmaplayerstylemanager.h"
29+
#include "qgsmaptoolemitpoint.h"
2930
#include "qgis_app.h"
3031

3132
class QgsPointXY;
3233
class QgsMapLayer;
3334
class QgsMapCanvas;
3435
class QgsRasterLayer;
35-
class QgsMapToolEmitPoint;
3636
class QgsMetadataWidget;
3737
class QgsRasterRenderer;
3838
class QgsRasterRendererWidget;
@@ -91,7 +91,7 @@ class APP_EXPORT QgsRasterLayerProperties : public QgsOptionsDialogBase, private
9191
*/
9292
//void on_btnResetNull_clicked();
9393

94-
void pixelSelected( const QgsPointXY & );
94+
void pixelSelected( const QgsPointXY &, const Qt::MouseButton & );
9595

9696
private slots:
9797
void mRenderTypeComboBox_currentIndexChanged( int index );
@@ -206,7 +206,7 @@ class APP_EXPORT QgsRasterLayerProperties : public QgsOptionsDialogBase, private
206206
qreal mGradientWidth;
207207

208208
QgsMapCanvas *mMapCanvas = nullptr;
209-
QgsMapToolEmitPoint *mPixelSelectorTool = nullptr;
209+
std::unique_ptr<QgsMapToolEmitPoint> mPixelSelectorTool = nullptr;
210210

211211
QgsRasterHistogramWidget *mHistogramWidget = nullptr;
212212

0 commit comments

Comments
 (0)