Skip to content

Commit

Permalink
Only save effect element if it is non-default
Browse files Browse the repository at this point in the history
(decreases file size of qgs project files)
  • Loading branch information
nyalldawson committed Sep 30, 2015
1 parent 29ac48e commit 0587a59
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 9 deletions.
9 changes: 9 additions & 0 deletions python/core/effects/qgspainteffectregistry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,18 @@ class QgsPaintEffectRegistry
* effects. All effects except the standard draw source effect are disabled,
* but are included so that they can be easily drawn just by enabling the effect.
* @returns default effects stack
* @see isDefaultStack()
*/
static QgsPaintEffect* defaultStack() /Factory/;

/** Tests whether a paint effect matches the default effects stack.
* @param effect paint effect to test
* @returns true if effect is default stack
* @note added in QGIS 2.12
* @see defaultStack()
*/
static bool isDefaultStack( QgsPaintEffect* effect );

protected:
QgsPaintEffectRegistry();
~QgsPaintEffectRegistry();
Expand Down
32 changes: 32 additions & 0 deletions src/core/effects/qgspainteffectregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ QStringList QgsPaintEffectRegistry::effects() const

QgsPaintEffect* QgsPaintEffectRegistry::defaultStack()
{
//NOTE - also remember to update isDefaultStack below if making changes to this list
QgsEffectStack* stack = new QgsEffectStack();
QgsDropShadowEffect* dropShadow = new QgsDropShadowEffect();
dropShadow->setEnabled( false );
Expand All @@ -138,3 +139,34 @@ QgsPaintEffect* QgsPaintEffectRegistry::defaultStack()
stack->appendEffect( innerGlow );
return stack;
}

bool QgsPaintEffectRegistry::isDefaultStack( QgsPaintEffect* effect )
{
QgsEffectStack* effectStack = dynamic_cast< QgsEffectStack* >( effect );
if ( !effectStack )
return false;

if ( effectStack->count() != 5 )
return false;

for ( int i = 0; i < 5; ++i )
{
//only the third effect should be enabled
if ( effectStack->effect( i )->enabled() != ( i == 2 ) )
return false;
}

if ( !dynamic_cast< QgsDropShadowEffect* >( effectStack->effect( 0 ) ) )
return false;
if ( !dynamic_cast< QgsOuterGlowEffect* >( effectStack->effect( 1 ) ) )
return false;
if ( !dynamic_cast< QgsDrawSourceEffect* >( effectStack->effect( 2 ) ) )
return false;
if ( !dynamic_cast< QgsInnerShadowEffect* >( effectStack->effect( 3 ) ) )
return false;
if ( !dynamic_cast< QgsInnerGlowEffect* >( effectStack->effect( 4 ) ) )
return false;

//we don't go as far as to check the individual effect's properties
return true;
}
9 changes: 9 additions & 0 deletions src/core/effects/qgspainteffectregistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,18 @@ class CORE_EXPORT QgsPaintEffectRegistry
* effects. All effects except the standard draw source effect are disabled,
* but are included so that they can be easily drawn just by enabling the effect.
* @returns default effects stack
* @see isDefaultStack()
*/
static QgsPaintEffect* defaultStack();

/** Tests whether a paint effect matches the default effects stack.
* @param effect paint effect to test
* @returns true if effect is default stack
* @note added in QGIS 2.12
* @see defaultStack()
*/
static bool isDefaultStack( QgsPaintEffect* effect );

protected:
QgsPaintEffectRegistry();
~QgsPaintEffectRegistry();
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology-ng/qgscategorizedsymbolrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "qgspointdisplacementrenderer.h"
#include "qgsinvertedpolygonrenderer.h"
#include "qgspainteffect.h"
#include "qgspainteffectregistry.h"
#include "qgsscaleexpression.h"
#include "qgsdatadefined.h"

Expand Down Expand Up @@ -686,7 +687,7 @@ QDomElement QgsCategorizedSymbolRendererV2::save( QDomDocument& doc )
sizeScaleElem.setAttribute( "scalemethod", QgsSymbolLayerV2Utils::encodeScaleMethod( mScaleMethod ) );
rendererElem.appendChild( sizeScaleElem );

if ( mPaintEffect )
if ( mPaintEffect && !QgsPaintEffectRegistry::isDefaultStack( mPaintEffect ) )
mPaintEffect->saveProperties( doc, rendererElem );

return rendererElem;
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "qgspointdisplacementrenderer.h"
#include "qgsinvertedpolygonrenderer.h"
#include "qgspainteffect.h"
#include "qgspainteffectregistry.h"
#include "qgsscaleexpression.h"
#include "qgsdatadefined.h"

Expand Down Expand Up @@ -1156,7 +1157,7 @@ QDomElement QgsGraduatedSymbolRendererV2::save( QDomDocument& doc )
mLabelFormat.saveToDomElement( labelFormatElem );
rendererElem.appendChild( labelFormatElem );

if ( mPaintEffect )
if ( mPaintEffect && !QgsPaintEffectRegistry::isDefaultStack( mPaintEffect ) )
mPaintEffect->saveProperties( doc, rendererElem );

return rendererElem;
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology-ng/qgsheatmaprenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "qgsvectorcolorrampv2.h"
#include "qgsrendercontext.h"
#include "qgspainteffect.h"
#include "qgspainteffectregistry.h"

#include <QDomDocument>
#include <QDomElement>
Expand Down Expand Up @@ -361,7 +362,7 @@ QDomElement QgsHeatmapRenderer::save( QDomDocument& doc )
}
rendererElem.setAttribute( "invert_ramp", QString::number( mInvertRamp ) );

if ( mPaintEffect )
if ( mPaintEffect && !QgsPaintEffectRegistry::isDefaultStack( mPaintEffect ) )
mPaintEffect->saveProperties( doc, rendererElem );

return rendererElem;
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology-ng/qgsinvertedpolygonrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgssymbollayerv2.h"
#include "qgsogcutils.h"
#include "qgspainteffect.h"
#include "qgspainteffectregistry.h"

#include <QDomDocument>
#include <QDomElement>
Expand Down Expand Up @@ -375,7 +376,7 @@ QDomElement QgsInvertedPolygonRenderer::save( QDomDocument& doc )
rendererElem.appendChild( embeddedRendererElem );
}

if ( mPaintEffect )
if ( mPaintEffect && !QgsPaintEffectRegistry::isDefaultStack( mPaintEffect ) )
mPaintEffect->saveProperties( doc, rendererElem );

return rendererElem;
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology-ng/qgspointdisplacementrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgsvectorlayer.h"
#include "qgssinglesymbolrendererv2.h"
#include "qgspainteffect.h"
#include "qgspainteffectregistry.h"
#include "qgsfontutils.h"
#include "qgsmultipointv2.h"
#include "qgspointv2.h"
Expand Down Expand Up @@ -412,7 +413,7 @@ QDomElement QgsPointDisplacementRenderer::save( QDomDocument& doc )
rendererElement.appendChild( centerSymbolElem );
}

if ( mPaintEffect )
if ( mPaintEffect && !QgsPaintEffectRegistry::isDefaultStack( mPaintEffect ) )
mPaintEffect->saveProperties( doc, rendererElement );

return rendererElement;
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgsrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ QDomElement QgsFeatureRendererV2::save( QDomDocument& doc )
QDomElement rendererElem = doc.createElement( RENDERER_TAG_NAME );
rendererElem.setAttribute( "forceraster", ( mForceRaster ? "1" : "0" ) );

if ( mPaintEffect )
if ( mPaintEffect && !QgsPaintEffectRegistry::isDefaultStack( mPaintEffect ) )
mPaintEffect->saveProperties( doc, rendererElem );

return rendererElem;
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology-ng/qgsrulebasedrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "qgspointdisplacementrenderer.h"
#include "qgsinvertedpolygonrenderer.h"
#include "qgspainteffect.h"
#include "qgspainteffectregistry.h"
#include "qgsdatadefined.h"

#include <QSet>
Expand Down Expand Up @@ -947,7 +948,7 @@ QDomElement QgsRuleBasedRendererV2::save( QDomDocument& doc )
QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols( symbols, "symbols", doc );
rendererElem.appendChild( symbolsElem );

if ( mPaintEffect )
if ( mPaintEffect && !QgsPaintEffectRegistry::isDefaultStack( mPaintEffect ) )
mPaintEffect->saveProperties( doc, rendererElem );

return rendererElem;
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology-ng/qgssinglesymbolrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "qgspointdisplacementrenderer.h"
#include "qgsinvertedpolygonrenderer.h"
#include "qgspainteffect.h"
#include "qgspainteffectregistry.h"
#include "qgsscaleexpression.h"
#include "qgsdatadefined.h"

Expand Down Expand Up @@ -377,7 +378,7 @@ QDomElement QgsSingleSymbolRendererV2::save( QDomDocument& doc )
sizeScaleElem.setAttribute( "scalemethod", QgsSymbolLayerV2Utils::encodeScaleMethod( mScaleMethod ) );
rendererElem.appendChild( sizeScaleElem );

if ( mPaintEffect )
if ( mPaintEffect && !QgsPaintEffectRegistry::isDefaultStack( mPaintEffect ) )
mPaintEffect->saveProperties( doc, rendererElem );

return rendererElem;
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology-ng/qgssymbollayerv2utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,8 @@ QDomElement QgsSymbolLayerV2Utils::saveSymbol( QString name, QgsSymbolV2* symbol
layerEl.setAttribute( "locked", layer->isLocked() );
layerEl.setAttribute( "pass", layer->renderingPass() );
saveProperties( layer->properties(), doc, layerEl );
layer->paintEffect()->saveProperties( doc, layerEl );
if ( !QgsPaintEffectRegistry::isDefaultStack( layer->paintEffect() ) )
layer->paintEffect()->saveProperties( doc, layerEl );

if ( layer->subSymbol() != NULL )
{
Expand Down
21 changes: 21 additions & 0 deletions tests/src/core/testqgspainteffectregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "qgspainteffectregistry.h"
#include "qgspainteffect.h"
#include "qgseffectstack.h"
#include <QObject>
#include <QtTest/QtTest>

Expand Down Expand Up @@ -50,6 +51,7 @@ class TestQgsPaintEffectRegistry : public QObject
void addEffect(); // check adding an effect to an empty registry
void fetchEffects(); //check fetching effects
void createEffect(); //check creating effect
void defaultStack(); //check creating/testing default stack

private:

Expand Down Expand Up @@ -152,5 +154,24 @@ void TestQgsPaintEffectRegistry::createEffect()
QVERIFY( !effect );
}

void TestQgsPaintEffectRegistry::defaultStack()
{
QgsPaintEffectRegistry* registry = QgsPaintEffectRegistry::instance();
QgsEffectStack* effect = static_cast<QgsEffectStack*>( registry->defaultStack() );
QVERIFY( registry->isDefaultStack( effect ) );
effect->effect( 1 )->setEnabled( true );
QVERIFY( !registry->isDefaultStack( effect ) );
effect->effect( 1 )->setEnabled( false );
effect->effect( 2 )->setEnabled( false ); //third effect should be enabled by default
QVERIFY( !registry->isDefaultStack( effect ) );
effect->effect( 2 )->setEnabled( true );
effect->appendEffect( new QgsEffectStack() );
QVERIFY( !registry->isDefaultStack( effect ) );
delete effect;
QgsPaintEffect* effect2 = new DummyPaintEffect();
QVERIFY( !registry->isDefaultStack( effect2 ) );
delete effect2;
}

QTEST_MAIN( TestQgsPaintEffectRegistry )
#include "testqgspainteffectregistry.moc"

0 comments on commit 0587a59

Please sign in to comment.