Skip to content

Commit cdf697d

Browse files
authored
Merge pull request #6068 from elpaso/bugfix-17845-raster-transparency-from-display
[bugfix] Transfer focus to canvas when selecting transparency
2 parents e2fcf70 + eeaca68 commit cdf697d

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
@@ -12613,9 +12613,21 @@ void QgisApp::showLayerProperties( QgsMapLayer *ml )
1261312613
#else
1261412614
QgsRasterLayerProperties *rlp = new QgsRasterLayerProperties( ml, mMapCanvas, this );
1261512615
#endif
12616-
12617-
rlp->exec();
12618-
delete rlp; // delete since dialog cannot be reused without updating code
12616+
// Cannot use exec here due to raster transparency map tool:
12617+
// in order to pass focus to the canvas, the dialog needs to
12618+
// be hidden and shown in non-modal mode.
12619+
rlp->setModal( true );
12620+
rlp->show();
12621+
// Delete (later, for safety) since dialog cannot be reused without
12622+
// updating code
12623+
connect( rlp, &QgsRasterLayerProperties::accepted, [ rlp ]
12624+
{
12625+
rlp->deleteLater();
12626+
} );
12627+
connect( rlp, &QgsRasterLayerProperties::rejected, [ rlp ]
12628+
{
12629+
rlp->deleteLater();
12630+
} );
1261912631
}
1262012632
else if ( ml->type() == QgsMapLayer::VectorLayer ) // VECTOR
1262112633
{

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;
210210

211211
QgsRasterHistogramWidget *mHistogramWidget = nullptr;
212212

0 commit comments

Comments
 (0)