Skip to content

Commit ca9f267

Browse files
committed
don't apply loaded style when layer properties dialog is cancelled
(cherry picked from commit 25a162c)
1 parent 5c24d1a commit ca9f267

4 files changed

+42
-3
lines changed

src/app/qgsrasterlayerproperties.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "qgsmapcanvas.h"
3131
#include "qgsmaplayerregistry.h"
3232
#include "qgsmaplayerstyleguiutils.h"
33-
#include "qgsmaplayerstylemanager.h"
3433
#include "qgsmaprenderer.h"
3534
#include "qgsmaptoolemitpoint.h"
3635
#include "qgsmaptopixel.h"
@@ -106,6 +105,8 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
106105
connect( lyr->styleManager(), SIGNAL( currentStyleChanged( QString ) ), this, SLOT( syncToLayer() ) );
107106

108107
connect( this, SIGNAL( accepted() ), this, SLOT( apply() ) );
108+
connect( this, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
109+
109110
connect( buttonBox->button( QDialogButtonBox::Apply ), SIGNAL( clicked() ), this, SLOT( apply() ) );
110111

111112
connect( mOptionsStackedWidget, SIGNAL( currentChanged( int ) ), this, SLOT( mOptionsStackedWidget_CurrentChanged( int ) ) );
@@ -1707,6 +1708,8 @@ void QgsRasterLayerProperties::loadStyle_clicked()
17071708
if ( !fileName.endsWith( ".qml", Qt::CaseInsensitive ) )
17081709
fileName += ".qml";
17091710

1711+
mOldStyle = mRasterLayer->styleManager()->style( mRasterLayer->styleManager()->currentStyle() );
1712+
17101713
bool defaultLoadedFlag = false;
17111714
QString message = mRasterLayer->loadNamedStyle( fileName, defaultLoadedFlag );
17121715
if ( defaultLoadedFlag )
@@ -1776,4 +1779,16 @@ bool QgsRasterLayerProperties::rasterIsMultiBandColor()
17761779
return mRasterLayer && nullptr != dynamic_cast<QgsMultiBandColorRenderer*>( mRasterLayer->renderer() );
17771780
}
17781781

1779-
1782+
void QgsRasterLayerProperties::onCancel()
1783+
{
1784+
if ( mOldStyle.xmlData() != mRasterLayer->styleManager()->style( mRasterLayer->styleManager()->currentStyle() ).xmlData() )
1785+
{
1786+
// need to reset style to previous - style applied directly to the layer (not in apply())
1787+
QString myMessage;
1788+
QDomDocument doc( "qgis" );
1789+
int errorLine, errorColumn;
1790+
doc.setContent( mOldStyle.xmlData(), false, &myMessage, &errorLine, &errorColumn );
1791+
mRasterLayer->importNamedStyle( doc, myMessage );
1792+
syncToLayer();
1793+
}
1794+
}

src/app/qgsrasterlayerproperties.h

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qgsmaptool.h"
2626
#include "qgscolorrampshader.h"
2727
#include "qgscontexthelp.h"
28+
#include "qgsmaplayerstylemanager.h"
2829

2930
class QgsMapLayer;
3031
class QgsMapCanvas;
@@ -57,6 +58,8 @@ class APP_EXPORT QgsRasterLayerProperties : public QgsOptionsDialogBase, private
5758
//TODO: Verify that these all need to be public
5859
/** \brief Applies the settings made in the dialog without closing the box */
5960
void apply();
61+
/** Called when cancel button is pressed */
62+
void onCancel();
6063
/** \brief Slot to update layer display name as original is edited. */
6164
void on_mLayerOrigNameLineEd_textEdited( const QString& text );
6265
/** \brief this slot asks the rasterlayer to construct pyramids */
@@ -186,5 +189,9 @@ class APP_EXPORT QgsRasterLayerProperties : public QgsOptionsDialogBase, private
186189
QgsRasterHistogramWidget* mHistogramWidget;
187190

188191
QVector<bool> mTransparencyToEdited;
192+
193+
/** Previous layer style. Used to reset style to previous state if new style
194+
* was loaded but dialog is cancelled */
195+
QgsMapLayerStyle mOldStyle;
189196
};
190197
#endif

src/app/qgsvectorlayerproperties.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include "qgslogger.h"
3838
#include "qgsmaplayerregistry.h"
3939
#include "qgsmaplayerstyleguiutils.h"
40-
#include "qgsmaplayerstylemanager.h"
4140
#include "qgspluginmetadata.h"
4241
#include "qgspluginregistry.h"
4342
#include "qgsproject.h"
@@ -660,6 +659,17 @@ void QgsVectorLayerProperties::onCancel()
660659

661660
layer->setSubsetString( mOriginalSubsetSQL );
662661
}
662+
663+
if ( mOldStyle.xmlData() != mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() ).xmlData() )
664+
{
665+
// need to reset style to previous - style applied directly to the layer (not in apply())
666+
QString myMessage;
667+
QDomDocument doc( "qgis" );
668+
int errorLine, errorColumn;
669+
doc.setContent( mOldStyle.xmlData(), false, &myMessage, &errorLine, &errorColumn );
670+
mLayer->importNamedStyle( doc, myMessage );
671+
syncToLayer();
672+
}
663673
}
664674

665675
void QgsVectorLayerProperties::on_pbnQueryBuilder_clicked()
@@ -826,6 +836,8 @@ void QgsVectorLayerProperties::loadStyle_clicked()
826836
return;
827837
}
828838

839+
mOldStyle = mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() );
840+
829841
QString myMessage;
830842
bool defaultLoadedFlag = false;
831843

src/app/qgsvectorlayerproperties.h

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "qgsmapcanvas.h"
3131
#include "qgscontexthelp.h"
3232
#include "qgsexpressionbuilderdialog.h"
33+
#include "qgsmaplayerstylemanager.h"
3334

3435
class QgsMapLayer;
3536

@@ -187,6 +188,10 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
187188
//! List of joins of a layer at the time of creation of the dialog. Used to return joins to previous state if dialog is cancelled
188189
QList< QgsVectorJoinInfo > mOldJoins;
189190

191+
/** Previous layer style. Used to reset style to previous state if new style
192+
* was loaded but dialog is cancelled */
193+
QgsMapLayerStyle mOldStyle;
194+
190195
void initDiagramTab();
191196

192197
/** Buffer pixmap which takes the picture of renderers before they are assigned to the vector layer*/

0 commit comments

Comments
 (0)