Skip to content

Commit

Permalink
Merge pull request #6068 from elpaso/bugfix-17845-raster-transparency…
Browse files Browse the repository at this point in the history
…-from-display

[bugfix] Transfer focus to canvas when selecting transparency
  • Loading branch information
elpaso committed Jan 29, 2018
2 parents e2fcf70 + eeaca68 commit cdf697d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
18 changes: 15 additions & 3 deletions src/app/qgisapp.cpp
Expand Up @@ -12613,9 +12613,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
{
Expand Down
28 changes: 13 additions & 15 deletions src/app/qgsrasterlayerproperties.cpp
Expand Up @@ -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<QgsMapToolEmitPoint>( canvas );
connect( mPixelSelectorTool.get(), &QgsMapToolEmitPoint::canvasClicked, this, &QgsRasterLayerProperties::pixelSelected );
}
else
{
Expand Down Expand Up @@ -455,10 +454,6 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer *lyr, QgsMapCanv

QgsRasterLayerProperties::~QgsRasterLayerProperties()
{
if ( mPixelSelectorTool )
{
delete mPixelSelectorTool;
}
}

void QgsRasterLayerProperties::setupTransparencyTable( int nBands )
Expand Down Expand Up @@ -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() );
}
}

Expand Down Expand Up @@ -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 )
{
Expand All @@ -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 );
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsrasterlayerproperties.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -206,7 +206,7 @@ class APP_EXPORT QgsRasterLayerProperties : public QgsOptionsDialogBase, private
qreal mGradientWidth;

QgsMapCanvas *mMapCanvas = nullptr;
QgsMapToolEmitPoint *mPixelSelectorTool = nullptr;
std::unique_ptr<QgsMapToolEmitPoint> mPixelSelectorTool;

QgsRasterHistogramWidget *mHistogramWidget = nullptr;

Expand Down

0 comments on commit cdf697d

Please sign in to comment.