QGIS API Documentation 3.39.0-Master (47f7b3a4989)
Loading...
Searching...
No Matches
qgsmaskingwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaskingwidget.cpp
3 ---------------------
4 begin : September 2019
5 copyright : (C) 2019 by Hugo Mercier
6 email : hugo dot mercier at oslandia dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16
18
19#include <QSet>
20#include <QCheckBox>
21
22#include "qgsmaskingwidget.h"
26#include "qgsvectorlayer.h"
27#include "qgssymbol.h"
30#include "qgsproject.h"
31#include "qgsvectorlayerutils.h"
32#include "qgsmasksymbollayer.h"
34#include "qgsmessagebaritem.h"
35
36QgsMaskingWidget::QgsMaskingWidget( QWidget *parent ) :
37 QgsPanelWidget( parent )
38{
39 setupUi( this );
40
41 connect( mMaskTargetsWidget, &QgsSymbolLayerSelectionWidget::changed, this, &QgsMaskingWidget::onSelectionChanged );
42 connect( mMaskSourcesWidget, &QgsMaskSourceSelectionWidget::changed, this, &QgsMaskingWidget::onSelectionChanged );
43}
44
45void QgsMaskingWidget::onSelectionChanged()
46{
47 // display message if configuration is not consistent
48 const bool printMessage = mMaskTargetsWidget->selection().empty() != mMaskSourcesWidget->selection().empty();
49
50 if ( mMessageBarItem && !printMessage )
51 {
52 mMessageBar->popWidget( mMessageBarItem );
53 delete mMessageBarItem;
54 }
55 else if ( !mMessageBarItem && printMessage )
56 {
57 mMessageBarItem = new QgsMessageBarItem( tr( "Select both sources and symbol layers or your configuration will be lost" ), Qgis::MessageLevel::Warning, 0, this );
58 mMessageBar->pushItem( mMessageBarItem );
59 }
60
61 emit widgetChanged();
62}
63
73QList<QPair<QString, QList<QgsSymbolLayerReference>>> symbolLayerMasks( const QgsVectorLayer *layer )
74{
75 if ( ! layer->renderer() )
76 return {};
77
78 QList<QPair<QString, QList<QgsSymbolLayerReference>>> mMasks;
79 SymbolLayerVisitor collector( [&]( const QgsSymbolLayer * sl, const QString & lid )
80 {
81 if ( ! sl->masks().isEmpty() )
82 mMasks.push_back( qMakePair( lid, sl->masks() ) );
83 } );
84 layer->renderer()->accept( &collector );
85 return mMasks;
86}
87
88void QgsMaskingWidget::setLayer( QgsVectorLayer *layer )
89{
90 mLayer = layer;
91 populate();
92}
93
94void QgsMaskingWidget::populate()
95{
96 const QSignalBlocker blockerSourceWidget( mMaskSourcesWidget );
97 const QSignalBlocker blockerTargetWidget( mMaskTargetsWidget );
98
99 mMaskSourcesWidget->update();
100 mMaskTargetsWidget->setLayer( mLayer );
101
102 // collect masks and filter on those which have the current layer as destination
103 QSet<QString> maskedSymbolLayers;
104 QList<QgsMaskSourceSelectionWidget::MaskSource> maskSources;
105 QMap<QString, QgsMapLayer *> layers = QgsProject::instance()->mapLayers();
106
108 for ( auto layerIt = layers.begin(); layerIt != layers.end(); layerIt++ )
109 {
110 const QString layerId = layerIt.key();
111 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layerIt.value() );
112 if ( ! vl )
113 continue;
114
115 // collect symbol layer masks
116 const QList<QPair<QString, QList<QgsSymbolLayerReference>>> slMasks = symbolLayerMasks( vl );
117 for ( const QPair<QString, QList<QgsSymbolLayerReference>> &p : slMasks )
118 {
119 const QString &sourceSymbolLayerId = p.first;
120 for ( const QgsSymbolLayerReference &ref : p.second )
121 {
122 if ( ref.layerId() == mLayer->id() )
123 {
124 // add to the set of destinations
125 maskedSymbolLayers.insert( ref.symbolLayerIdV2() );
126 // add to the list of mask sources
127 source.layerId = layerId;
128 source.isLabeling = false;
129 source.symbolLayerId = sourceSymbolLayerId;
130 maskSources.append( source );
131 }
132 }
133 }
134
135 // collect label masks
136 QHash<QString, QgsMaskedLayers> labelMasks = QgsVectorLayerUtils::labelMasks( vl );
137 for ( auto it = labelMasks.begin(); it != labelMasks.end(); it++ )
138 {
139 const QString &ruleKey = it.key();
140 for ( auto it2 = it.value().begin(); it2 != it.value().end(); it2++ )
141 {
142 if ( it2.key() == mLayer->id() )
143 {
144 // merge with masked symbol layers
145 maskedSymbolLayers.unite( it2.value().symbolLayerIds );
146 // add the mask source
147 source.layerId = layerId;
148 source.isLabeling = true;
149 source.symbolLayerId = ruleKey;
150 maskSources.append( source );
151 }
152 }
153 }
154 }
155
156 mMaskSourcesWidget->setSelection( maskSources );
157 mMaskTargetsWidget->setSelection( maskedSymbolLayers );
158}
159
160void QgsMaskingWidget::apply()
161{
162 QList<QgsMaskSourceSelectionWidget::MaskSource> maskSources = mMaskSourcesWidget->selection();
163 QSet<QString> maskedSymbolLayers = mMaskTargetsWidget->selection();
164
165 QSet<QString> layersToRefresh;
166
167 QMap<QString, QgsMapLayer *> layers = QgsProject::instance()->mapLayers();
168 for ( auto layerIt = layers.begin(); layerIt != layers.end(); layerIt++ )
169 {
170 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layerIt.value() );
171 if ( ! vl )
172 continue;
173
174 //
175 // First reset symbol layer masks
176 SymbolLayerVisitor maskSetter( [&]( const QgsSymbolLayer * sl, const QString & slId )
177 {
178 if ( sl->layerType() == "MaskMarker" )
179 {
180 QgsMaskMarkerSymbolLayer *maskSl = const_cast<QgsMaskMarkerSymbolLayer *>( static_cast<const QgsMaskMarkerSymbolLayer *>( sl ) );
181
182 const QgsSymbolLayerReferenceList masks = maskSl->masks();
183 QgsSymbolLayerReferenceList newMasks;
184 for ( const QgsSymbolLayerReference &ref : masks )
185 {
186 // copy the original masks, only those with another destination layer
187 if ( ref.layerId() != mLayer->id() )
188 newMasks.append( ref );
189 }
190 for ( const QgsMaskSourceSelectionWidget::MaskSource &source : maskSources )
191 {
192 if ( ! source.isLabeling && source.layerId == layerIt.key() && source.symbolLayerId == slId )
193 {
194 // ... then add the new masked symbol layers, if any
195 for ( const QString &maskedId : maskedSymbolLayers )
196 {
197 newMasks.append( QgsSymbolLayerReference( mLayer->id(), maskedId ) );
198 }
199 // invalidate the cache of the source layer
200 layersToRefresh.insert( source.layerId );
201 }
202 }
203 maskSl->setMasks( newMasks );
204 }
205 } );
206 if ( vl->renderer() )
207 vl->renderer()->accept( &maskSetter );
208
209 //
210 // Now reset label masks
211 if ( ! vl->labeling() )
212 continue;
213 for ( const QString &labelProvider : vl->labeling()->subProviders() )
214 {
215 // clear symbol layers
216 QgsPalLayerSettings settings = vl->labeling()->settings( labelProvider );
217 QgsTextFormat format = settings.format();
218 if ( ! format.mask().enabled() )
219 continue;
220 const QgsSymbolLayerReferenceList masks = format.mask().maskedSymbolLayers();
222 for ( const QgsSymbolLayerReference &ref : masks )
223 {
224 // copy the original masks, only those with another destination layer
225 if ( ref.layerId() != mLayer->id() )
226 newMasks.append( ref );
227 }
228 for ( const QgsMaskSourceSelectionWidget::MaskSource &source : maskSources )
229 {
230 // ... then add the new masked symbol layers, if any
231
232 if ( source.isLabeling && source.layerId == layerIt.key() && source.symbolLayerId == labelProvider )
233 {
234 for ( const QString &maskedId : maskedSymbolLayers )
235 {
236 newMasks.append( QgsSymbolLayerReference( mLayer->id(), maskedId ) );
237 }
238 // invalidate the cache of the source layer
239 layersToRefresh.insert( source.layerId );
240 }
241 }
242 format.mask().setMaskedSymbolLayers( newMasks );
243 settings.setFormat( format );
244 vl->labeling()->setSettings( new QgsPalLayerSettings( settings ), labelProvider );
245 }
246 }
247
249 // trigger refresh of the current layer
250 mLayer->triggerRepaint();
251 // trigger refresh of dependent layers (i.e. mask source layers)
252 for ( const QString &layerId : layersToRefresh )
253 {
254 QgsMapLayer *layer = QgsProject::instance()->mapLayer( layerId );
255 layer->triggerRepaint();
256 }
257}
258
259SymbolLayerVisitor::SymbolLayerVisitor( SymbolLayerVisitor::SymbolLayerCallback callback ) :
260 mCallback( callback )
261{}
262
263bool SymbolLayerVisitor::visitEnter( const QgsStyleEntityVisitorInterface::Node &node )
264{
266 return false;
267
268 mSymbolKey = node.identifier;
269 return true;
270}
271
272void SymbolLayerVisitor::visitSymbol( const QgsSymbol *symbol, const QString &leafIdentifier, QVector<int> rootPath )
273{
274 for ( int idx = 0; idx < symbol->symbolLayerCount(); idx++ )
275 {
276 QVector<int> indexPath = rootPath;
277 indexPath.push_back( idx );
278
279 const QgsSymbolLayer *sl = symbol->symbolLayer( idx );
280
281 mCallback( sl, sl->id() );
282
283 // recurse over sub symbols
284 const QgsSymbol *subSymbol = const_cast<QgsSymbolLayer *>( sl )->subSymbol();
285 if ( subSymbol )
286 visitSymbol( subSymbol, leafIdentifier, indexPath );
287 }
288}
289
290bool SymbolLayerVisitor::visit( const QgsStyleEntityVisitorInterface::StyleLeaf &leaf )
291{
292 if ( leaf.entity && leaf.entity->type() == QgsStyle::SymbolEntity )
293 {
294 auto symbolEntity = static_cast<const QgsStyleSymbolEntity *>( leaf.entity );
295 if ( symbolEntity->symbol() )
296 visitSymbol( symbolEntity->symbol(), leaf.identifier, {} );
297 }
298 return true;
299}
300
@ Warning
Warning message.
Definition qgis.h:101
virtual void setSettings(QgsPalLayerSettings *settings, const QString &providerId=QString())=0
Set pal settings for a specific provider (takes ownership).
virtual QgsPalLayerSettings settings(const QString &providerId=QString()) const =0
Gets associated label settings.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
Base class for all map layer types.
Definition qgsmaplayer.h:75
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
void changed()
Emitted when an item was changed.
Represents an item shown within a QgsMessageBar widget.
Contains settings for how a map layer will be labeled.
void setFormat(const QgsTextFormat &format)
Sets the label text formatting settings, e.g., font settings, buffer settings, etc.
const QgsTextFormat & format() const
Returns the label text formatting settings, e.g., font settings, buffer settings, etc.
Base class for any widget that can be shown as a inline panel.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
void setDirty(bool b=true)
Flag the project as dirty (modified).
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
virtual QgsStyle::StyleEntity type() const =0
Returns the type of style entity.
@ SymbolRule
Rule based symbology or label child rule.
A symbol entity for QgsStyle databases.
Definition qgsstyle.h:1372
@ SymbolEntity
Symbols.
Definition qgsstyle.h:180
Type used to refer to a specific symbol layer in a symbol of a layer.
void changed()
Signal emitted when something the configuration is changed.
virtual QString layerType() const =0
Returns a string that represents this layer type.
QString id() const
Returns symbol layer identifier This id is unique in the whole project.
virtual QList< QgsSymbolLayerReference > masks() const
Returns masks defined by this symbol layer.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:94
QgsSymbolLayer * symbolLayer(int layer)
Returns the symbol layer at the specified index.
int symbolLayerCount() const
Returns the total number of symbol layers contained in the symbol.
Definition qgssymbol.h:215
Container for all settings relating to text rendering.
QgsTextMaskSettings & mask()
Returns a reference to the masking settings.
void setMaskedSymbolLayers(const QList< QgsSymbolLayerReference > &maskedLayers)
Sets the symbol layers that will be masked by this buffer.
QList< QgsSymbolLayerReference > maskedSymbolLayers() const
Returns a list of references to symbol layers that are masked by this buffer.
bool enabled() const
Returns whether the mask is enabled.
static QHash< QString, QgsMaskedLayers > labelMasks(const QgsVectorLayer *)
Returns masks defined in labeling options of a layer.
Represents a vector layer which manages a vector based data sets.
const QgsAbstractVectorLayerLabeling * labeling() const
Access to const labeling configuration.
QgsFeatureRenderer * renderer()
Returns the feature renderer used for rendering the features in the layer in 2D map views.
QList< QgsSymbolLayerReference > QgsSymbolLayerReferenceList
bool isLabeling
Whether it is a labeling mask or not.
Contains information relating to a node (i.e.
QString identifier
A string identifying the node.
QgsStyleEntityVisitorInterface::NodeType type
Node type.
Contains information relating to the style entity currently being visited.
const QgsStyleEntityInterface * entity
Reference to style entity being visited.
QString identifier
A string identifying the style entity.