Skip to content

Commit

Permalink
[FEATURE] Allow symbol layers to be temporarily disabled
Browse files Browse the repository at this point in the history
Adds a new checkbox at the bottom of each symbol layer's
properties which allows you to control whether the layer is
enabled or not.

Disabled layers are not drawn, but are saved and can be
enabled at a later stage.

This makes it easier to tweak symbol appearance without
having to totally delete a symbol layer.
  • Loading branch information
nyalldawson committed Oct 18, 2016
1 parent 2a873b8 commit b8c2e68
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 7 deletions.
16 changes: 16 additions & 0 deletions python/core/symbology-ng/qgssymbollayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ class QgsSymbolLayer

virtual ~QgsSymbolLayer();

/**
* Returns true if symbol layer is enabled and will be drawn.
* @note added in QGIS 3.0
* @see setEnabled()
*/
bool enabled() const;

/**
* Sets whether symbol layer is enabled and should be drawn. Disabled
* layers are not drawn, but remain part of the symbol and can be re-enabled
* when desired.
* @note added in QGIS 3.0
* @see enabled()
*/
void setEnabled( bool enabled );

/**
* The fill color.
*/
Expand Down
29 changes: 26 additions & 3 deletions src/core/symbology-ng/qgssymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,12 @@ void QgsSymbol::startRender( QgsRenderContext& context, const QgsFields& fields
mSymbolRenderContext->setExpressionContextScope( scope );

Q_FOREACH ( QgsSymbolLayer* layer, mLayers )
{
if ( !layer->enabled() )
continue;

layer->startRender( symbolContext );
}
}

void QgsSymbol::stopRender( QgsRenderContext& context )
Expand All @@ -405,7 +410,12 @@ void QgsSymbol::stopRender( QgsRenderContext& context )
if ( mSymbolRenderContext )
{
Q_FOREACH ( QgsSymbolLayer* layer, mLayers )
{
if ( !layer->enabled() )
continue;

layer->stopRender( *mSymbolRenderContext );
}
}

delete mSymbolRenderContext;
Expand Down Expand Up @@ -442,6 +452,9 @@ void QgsSymbol::drawPreviewIcon( QPainter* painter, QSize size, QgsRenderContext

Q_FOREACH ( QgsSymbolLayer* layer, mLayers )
{
if ( !layer->enabled() )
continue;

if ( mType == Fill && layer->type() == Line )
{
// line symbol layer would normally draw just a line
Expand Down Expand Up @@ -587,6 +600,7 @@ QgsSymbolLayerList QgsSymbol::cloneLayers() const
QgsSymbolLayer* layer = ( *it )->clone();
layer->setLocked(( *it )->isLocked() );
layer->setRenderingPass(( *it )->renderingPass() );
layer->setEnabled(( *it )->enabled() );
lst.append( layer );
}
return lst;
Expand Down Expand Up @@ -1417,7 +1431,7 @@ void QgsMarkerSymbol::renderPoint( QPointF point, const QgsFeature* f, QgsRender
if ( layerIdx != -1 )
{
QgsSymbolLayer* symbolLayer = mLayers.value( layerIdx );
if ( symbolLayer )
if ( symbolLayer && symbolLayer->enabled() )
{
if ( symbolLayer->type() == QgsSymbol::Marker )
{
Expand All @@ -1432,6 +1446,9 @@ void QgsMarkerSymbol::renderPoint( QPointF point, const QgsFeature* f, QgsRender

Q_FOREACH ( QgsSymbolLayer* symbolLayer, mLayers )
{
if ( !symbolLayer->enabled() )
continue;

if ( symbolLayer->type() == QgsSymbol::Marker )
{
QgsMarkerSymbolLayer* markerLayer = static_cast<QgsMarkerSymbolLayer*>( symbolLayer );
Expand Down Expand Up @@ -1625,7 +1642,7 @@ void QgsLineSymbol::renderPolyline( const QPolygonF& points, const QgsFeature* f
if ( layerIdx != -1 )
{
QgsSymbolLayer* symbolLayer = mLayers.value( layerIdx );
if ( symbolLayer )
if ( symbolLayer && symbolLayer->enabled() )
{
if ( symbolLayer->type() == QgsSymbol::Line )
{
Expand All @@ -1640,6 +1657,9 @@ void QgsLineSymbol::renderPolyline( const QPolygonF& points, const QgsFeature* f

Q_FOREACH ( QgsSymbolLayer* symbolLayer, mLayers )
{
if ( !symbolLayer->enabled() )
continue;

if ( symbolLayer->type() == QgsSymbol::Line )
{
QgsLineSymbolLayer* lineLayer = static_cast<QgsLineSymbolLayer*>( symbolLayer );
Expand Down Expand Up @@ -1704,7 +1724,7 @@ void QgsFillSymbol::renderPolygon( const QPolygonF& points, QList<QPolygonF>* ri
if ( layerIdx != -1 )
{
QgsSymbolLayer* symbolLayer = mLayers.value( layerIdx );
if ( symbolLayer )
if ( symbolLayer && symbolLayer->enabled() )
{
if ( symbolLayer->type() == Fill || symbolLayer->type() == Line )
renderPolygonUsingLayer( symbolLayer, points, rings, symbolContext );
Expand All @@ -1716,6 +1736,9 @@ void QgsFillSymbol::renderPolygon( const QPolygonF& points, QList<QPolygonF>* ri

Q_FOREACH ( QgsSymbolLayer* symbolLayer, mLayers )
{
if ( !symbolLayer->enabled() )
continue;

if ( symbolLayer->type() == Fill || symbolLayer->type() == Line )
renderPolygonUsingLayer( symbolLayer, points, rings, symbolContext );
else
Expand Down
1 change: 1 addition & 0 deletions src/core/symbology-ng/qgssymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ void QgsSymbolLayer::setPaintEffect( QgsPaintEffect *effect )

QgsSymbolLayer::QgsSymbolLayer( QgsSymbol::SymbolType type, bool locked )
: mType( type )
, mEnabled( true )
, mLocked( locked )
, mRenderingPass( 0 )
, mPaintEffect( nullptr )
Expand Down
20 changes: 20 additions & 0 deletions src/core/symbology-ng/qgssymbollayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ class CORE_EXPORT QgsSymbolLayer

virtual ~QgsSymbolLayer();

/**
* Returns true if symbol layer is enabled and will be drawn.
* @note added in QGIS 3.0
* @see setEnabled()
*/
bool enabled() const { return mEnabled; }

/**
* Sets whether symbol layer is enabled and should be drawn. Disabled
* layers are not drawn, but remain part of the symbol and can be re-enabled
* when desired.
* @note added in QGIS 3.0
* @see enabled()
*/
void setEnabled( bool enabled ) { mEnabled = enabled; }

/**
* The fill color.
*/
Expand Down Expand Up @@ -266,6 +282,10 @@ class CORE_EXPORT QgsSymbolLayer
QgsSymbolLayer( QgsSymbol::SymbolType type, bool locked = false );

QgsSymbol::SymbolType mType;

//! True if layer is enabled and should be drawn
bool mEnabled;

bool mLocked;
QColor mColor;
int mRenderingPass;
Expand Down
3 changes: 3 additions & 0 deletions src/core/symbology-ng/qgssymbollayerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ QgsSymbolLayer* QgsSymbolLayerUtils::loadSymbolLayer( QDomElement& element )
{
QString layerClass = element.attribute( "class" );
bool locked = element.attribute( "locked" ).toInt();
bool enabled = element.attribute( "enabled", "1" ).toInt();
int pass = element.attribute( "pass" ).toInt();

// parse properties
Expand All @@ -863,6 +864,7 @@ QgsSymbolLayer* QgsSymbolLayerUtils::loadSymbolLayer( QDomElement& element )
{
layer->setLocked( locked );
layer->setRenderingPass( pass );
layer->setEnabled( enabled );

//restore layer effect
QDomElement effectElem = element.firstChildElement( "effect" );
Expand Down Expand Up @@ -910,6 +912,7 @@ QDomElement QgsSymbolLayerUtils::saveSymbol( const QString& name, QgsSymbol* sym

QDomElement layerEl = doc.createElement( "layer" );
layerEl.setAttribute( "class", layer->layerType() );
layerEl.setAttribute( "enabled", layer->enabled() );
layerEl.setAttribute( "locked", layer->isLocked() );
layerEl.setAttribute( "pass", layer->renderingPass() );
saveProperties( layer->properties(), doc, layerEl );
Expand Down
7 changes: 7 additions & 0 deletions src/gui/symbology-ng/qgslayerpropertieswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ QgsLayerPropertiesWidget::QgsLayerPropertiesWidget( QgsSymbolLayer* layer, const
// update layer type combo box
int idx = cboLayerType->findData( mLayer->layerType() );
cboLayerType->setCurrentIndex( idx );
mEnabledCheckBox->setChecked( mLayer->enabled() );
// set the corresponding widget
updateSymbolLayerWidget( layer );
connect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) );
Expand Down Expand Up @@ -236,3 +237,9 @@ void QgsLayerPropertiesWidget::reloadLayer()
{
emit changeLayer( mLayer );
}

void QgsLayerPropertiesWidget::on_mEnabledCheckBox_toggled( bool enabled )
{
mLayer->setEnabled( enabled );
emitSignalChanged();
}
1 change: 1 addition & 0 deletions src/gui/symbology-ng/qgslayerpropertieswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class GUI_EXPORT QgsLayerPropertiesWidget : public QgsPanelWidget, private Ui::L

private slots:
void reloadLayer();
void on_mEnabledCheckBox_toggled( bool enabled );

private:

Expand Down
34 changes: 32 additions & 2 deletions src/ui/symbollayer/widget_layerproperties.ui
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<number>3</number>
</property>
<item row="1" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1">
<item>
<widget class="QLabel" name="label">
<property name="text">
Expand All @@ -50,6 +50,9 @@
</item>
</layout>
</item>
<item row="4" column="0">
<widget class="QgsEffectStackCompactWidget" name="mEffectWidget" native="true"/>
</item>
<item row="2" column="0">
<widget class="QStackedWidget" name="stackedWidget">
<widget class="QWidget" name="pageDummy">
Expand All @@ -69,7 +72,34 @@
</widget>
</item>
<item row="3" column="0">
<widget class="QgsEffectStackCompactWidget" name="mEffectWidget" native="true"/>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="spacing">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="mEnabledCheckBox">
<property name="text">
<string>Enable layer</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
Expand Down
Loading

0 comments on commit b8c2e68

Please sign in to comment.