Skip to content

Commit 25a162c

Browse files
committedApr 9, 2016
don't apply loaded style when layer properties dialog is cancelled
1 parent 156721b commit 25a162c

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed
 

‎src/app/qgsrasterlayerproperties.cpp

Lines changed: 17 additions & 2 deletions
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 ) ) );
@@ -1713,6 +1714,8 @@ void QgsRasterLayerProperties::loadStyle_clicked()
17131714
if ( !fileName.endsWith( ".qml", Qt::CaseInsensitive ) )
17141715
fileName += ".qml";
17151716

1717+
mOldStyle = mRasterLayer->styleManager()->style( mRasterLayer->styleManager()->currentStyle() );
1718+
17161719
bool defaultLoadedFlag = false;
17171720
QString message = mRasterLayer->loadNamedStyle( fileName, defaultLoadedFlag );
17181721
if ( defaultLoadedFlag )
@@ -1782,4 +1785,16 @@ bool QgsRasterLayerProperties::rasterIsMultiBandColor()
17821785
return mRasterLayer && nullptr != dynamic_cast<QgsMultiBandColorRenderer*>( mRasterLayer->renderer() );
17831786
}
17841787

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

‎src/app/qgsrasterlayerproperties.h

Lines changed: 7 additions & 0 deletions
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

Lines changed: 13 additions & 1 deletion
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"
@@ -650,6 +649,17 @@ void QgsVectorLayerProperties::onCancel()
650649

651650
mLayer->setSubsetString( mOriginalSubsetSQL );
652651
}
652+
653+
if ( mOldStyle.xmlData() != mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() ).xmlData() )
654+
{
655+
// need to reset style to previous - style applied directly to the layer (not in apply())
656+
QString myMessage;
657+
QDomDocument doc( "qgis" );
658+
int errorLine, errorColumn;
659+
doc.setContent( mOldStyle.xmlData(), false, &myMessage, &errorLine, &errorColumn );
660+
mLayer->importNamedStyle( doc, myMessage );
661+
syncToLayer();
662+
}
653663
}
654664

655665
void QgsVectorLayerProperties::on_pbnQueryBuilder_clicked()
@@ -816,6 +826,8 @@ void QgsVectorLayerProperties::loadStyle_clicked()
816826
return;
817827
}
818828

829+
mOldStyle = mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() );
830+
819831
QString myMessage;
820832
bool defaultLoadedFlag = false;
821833

‎src/app/qgsvectorlayerproperties.h

Lines changed: 5 additions & 0 deletions
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

@@ -189,6 +190,10 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
189190
//! 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
190191
QList< QgsVectorJoinInfo > mOldJoins;
191192

193+
/** Previous layer style. Used to reset style to previous state if new style
194+
* was loaded but dialog is cancelled */
195+
QgsMapLayerStyle mOldStyle;
196+
192197
void initDiagramTab();
193198

194199
/** Adds a new join to mJoinTreeWidget*/

0 commit comments

Comments
 (0)
Please sign in to comment.