QGIS API Documentation 3.41.0-Master (cea29feecf2)
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"
23#include "moc_qgsmaskingwidget.cpp"
27#include "qgsvectorlayer.h"
28#include "qgssymbol.h"
31#include "qgsproject.h"
32#include "qgsvectorlayerutils.h"
33#include "qgsmasksymbollayer.h"
35#include "qgsmessagebaritem.h"
36
37QgsMaskingWidget::QgsMaskingWidget( QWidget *parent )
38 : QgsPanelWidget( parent )
39{
40 setupUi( this );
41
42 connect( mMaskTargetsWidget, &QgsSymbolLayerSelectionWidget::changed, this, &QgsMaskingWidget::onSelectionChanged );
43 connect( mMaskSourcesWidget, &QgsMaskSourceSelectionWidget::changed, this, &QgsMaskingWidget::onSelectionChanged );
44}
45
46void QgsMaskingWidget::onSelectionChanged()
47{
48 // display message if configuration is not consistent
49 const bool printMessage = mMaskTargetsWidget->selection().empty() != mMaskSourcesWidget->selection().empty();
50
51 if ( mMessageBarItem && !printMessage )
52 {
53 mMessageBar->popWidget( mMessageBarItem );
54 delete mMessageBarItem;
55 }
56 else if ( !mMessageBarItem && printMessage )
57 {
58 mMessageBarItem = new QgsMessageBarItem( tr( "Select both masking and masked symbol layers or your configuration will be lost" ), Qgis::MessageLevel::Warning, 0, this );
59 mMessageBar->pushItem( mMessageBarItem );
60 }
61
62 emit widgetChanged();
63}
64
74QList<QPair<QString, QList<QgsSymbolLayerReference>>> symbolLayerMasks( const QgsVectorLayer *layer )
75{
76 if ( !layer->renderer() )
77 return {};
78
79 QList<QPair<QString, QList<QgsSymbolLayerReference>>> mMasks;
80 SymbolLayerVisitor collector( [&]( const QgsSymbolLayer *sl, const QString &lid ) {
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 if ( sl->layerType() == "MaskMarker" )
178 {
179 QgsMaskMarkerSymbolLayer *maskSl = const_cast<QgsMaskMarkerSymbolLayer *>( static_cast<const QgsMaskMarkerSymbolLayer *>( sl ) );
180
181 const QgsSymbolLayerReferenceList masks = maskSl->masks();
182 QgsSymbolLayerReferenceList newMasks;
183 for ( const QgsSymbolLayerReference &ref : masks )
184 {
185 // copy the original masks, only those with another destination layer
186 if ( ref.layerId() != mLayer->id() )
187 newMasks.append( ref );
188 }
189 for ( const QgsMaskSourceSelectionWidget::MaskSource &source : maskSources )
190 {
191 if ( !source.isLabeling && source.layerId == layerIt.key() && source.symbolLayerId == slId )
192 {
193 // ... then add the new masked symbol layers, if any
194 for ( const QString &maskedId : maskedSymbolLayers )
195 {
196 newMasks.append( QgsSymbolLayerReference( mLayer->id(), maskedId ) );
197 }
198 // invalidate the cache of the source layer
199 layersToRefresh.insert( source.layerId );
200 }
201 }
202 maskSl->setMasks( newMasks );
203 }
204 } );
205 if ( vl->renderer() )
206 vl->renderer()->accept( &maskSetter );
207
208 //
209 // Now reset label masks
210 if ( !vl->labeling() )
211 continue;
212 for ( const QString &labelProvider : vl->labeling()->subProviders() )
213 {
214 // clear symbol layers
215 QgsPalLayerSettings settings = vl->labeling()->settings( labelProvider );
216 QgsTextFormat format = settings.format();
217 if ( !format.mask().enabled() )
218 continue;
219 const QgsSymbolLayerReferenceList masks = format.mask().maskedSymbolLayers();
221 for ( const QgsSymbolLayerReference &ref : masks )
222 {
223 // copy the original masks, only those with another destination layer
224 if ( ref.layerId() != mLayer->id() )
225 newMasks.append( ref );
226 }
227 for ( const QgsMaskSourceSelectionWidget::MaskSource &source : maskSources )
228 {
229 // ... then add the new masked symbol layers, if any
230
231 if ( source.isLabeling && source.layerId == layerIt.key() && source.symbolLayerId == labelProvider )
232 {
233 for ( const QString &maskedId : maskedSymbolLayers )
234 {
235 newMasks.append( QgsSymbolLayerReference( mLayer->id(), maskedId ) );
236 }
237 // invalidate the cache of the source layer
238 layersToRefresh.insert( source.layerId );
239 }
240 }
241 format.mask().setMaskedSymbolLayers( newMasks );
242 settings.setFormat( format );
243 vl->labeling()->setSettings( new QgsPalLayerSettings( settings ), labelProvider );
244 }
245 }
246
248 // trigger refresh of the current layer
249 mLayer->triggerRepaint();
250 // trigger refresh of dependent layers (i.e. mask source layers)
251 for ( const QString &layerId : layersToRefresh )
252 {
253 QgsMapLayer *layer = QgsProject::instance()->mapLayer( layerId );
254 layer->triggerRepaint();
255 }
256}
257
258SymbolLayerVisitor::SymbolLayerVisitor( SymbolLayerVisitor::SymbolLayerCallback callback )
259 : mCallback( callback )
260{}
261
262bool SymbolLayerVisitor::visitEnter( const QgsStyleEntityVisitorInterface::Node &node )
263{
265 return false;
266
267 mSymbolKey = node.identifier;
268 return true;
269}
270
271void SymbolLayerVisitor::visitSymbol( const QgsSymbol *symbol, const QString &leafIdentifier, QVector<int> rootPath )
272{
273 for ( int idx = 0; idx < symbol->symbolLayerCount(); idx++ )
274 {
275 QVector<int> indexPath = rootPath;
276 indexPath.push_back( idx );
277
278 const QgsSymbolLayer *sl = symbol->symbolLayer( idx );
279
280 mCallback( sl, sl->id() );
281
282 // recurse over sub symbols
283 const QgsSymbol *subSymbol = const_cast<QgsSymbolLayer *>( sl )->subSymbol();
284 if ( subSymbol )
285 visitSymbol( subSymbol, leafIdentifier, indexPath );
286 }
287}
288
289bool SymbolLayerVisitor::visit( const QgsStyleEntityVisitorInterface::StyleLeaf &leaf )
290{
291 if ( leaf.entity && leaf.entity->type() == QgsStyle::SymbolEntity )
292 {
293 auto symbolEntity = static_cast<const QgsStyleSymbolEntity *>( leaf.entity );
294 if ( symbolEntity->symbol() )
295 visitSymbol( symbolEntity->symbol(), leaf.identifier, {} );
296 }
297 return true;
298}
299
@ Warning
Warning message.
Definition qgis.h:156
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:76
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:1396
@ SymbolEntity
Symbols.
Definition qgsstyle.h:204
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:231
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:353
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.