QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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 {
82 if ( ! sl->masks().isEmpty() )
83 mMasks.push_back( qMakePair( lid, sl->masks() ) );
84 } );
85 layer->renderer()->accept( &collector );
86 return mMasks;
87}
88
89void QgsMaskingWidget::setLayer( QgsVectorLayer *layer )
90{
91 mLayer = layer;
92 populate();
93}
94
95void QgsMaskingWidget::populate()
96{
97 const QSignalBlocker blockerSourceWidget( mMaskSourcesWidget );
98 const QSignalBlocker blockerTargetWidget( mMaskTargetsWidget );
99
100 mMaskSourcesWidget->update();
101 mMaskTargetsWidget->setLayer( mLayer );
102
103 // collect masks and filter on those which have the current layer as destination
104 QSet<QString> maskedSymbolLayers;
105 QList<QgsMaskSourceSelectionWidget::MaskSource> maskSources;
106 QMap<QString, QgsMapLayer *> layers = QgsProject::instance()->mapLayers();
107
109 for ( auto layerIt = layers.begin(); layerIt != layers.end(); layerIt++ )
110 {
111 const QString layerId = layerIt.key();
112 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layerIt.value() );
113 if ( ! vl )
114 continue;
115
116 // collect symbol layer masks
117 const QList<QPair<QString, QList<QgsSymbolLayerReference>>> slMasks = symbolLayerMasks( vl );
118 for ( const QPair<QString, QList<QgsSymbolLayerReference>> &p : slMasks )
119 {
120 const QString &sourceSymbolLayerId = p.first;
121 for ( const QgsSymbolLayerReference &ref : p.second )
122 {
123 if ( ref.layerId() == mLayer->id() )
124 {
125 // add to the set of destinations
126 maskedSymbolLayers.insert( ref.symbolLayerIdV2() );
127 // add to the list of mask sources
128 source.layerId = layerId;
129 source.isLabeling = false;
130 source.symbolLayerId = sourceSymbolLayerId;
131 maskSources.append( source );
132 }
133 }
134 }
135
136 // collect label masks
137 QHash<QString, QgsMaskedLayers> labelMasks = QgsVectorLayerUtils::labelMasks( vl );
138 for ( auto it = labelMasks.begin(); it != labelMasks.end(); it++ )
139 {
140 const QString &ruleKey = it.key();
141 for ( auto it2 = it.value().begin(); it2 != it.value().end(); it2++ )
142 {
143 if ( it2.key() == mLayer->id() )
144 {
145 // merge with masked symbol layers
146 maskedSymbolLayers.unite( it2.value().symbolLayerIds );
147 // add the mask source
148 source.layerId = layerId;
149 source.isLabeling = true;
150 source.symbolLayerId = ruleKey;
151 maskSources.append( source );
152 }
153 }
154 }
155 }
156
157 mMaskSourcesWidget->setSelection( maskSources );
158 mMaskTargetsWidget->setSelection( maskedSymbolLayers );
159}
160
161void QgsMaskingWidget::apply()
162{
163 QList<QgsMaskSourceSelectionWidget::MaskSource> maskSources = mMaskSourcesWidget->selection();
164 QSet<QString> maskedSymbolLayers = mMaskTargetsWidget->selection();
165
166 QSet<QString> layersToRefresh;
167
168 QMap<QString, QgsMapLayer *> layers = QgsProject::instance()->mapLayers();
169 for ( auto layerIt = layers.begin(); layerIt != layers.end(); layerIt++ )
170 {
171 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layerIt.value() );
172 if ( ! vl )
173 continue;
174
175 //
176 // First reset symbol layer masks
177 SymbolLayerVisitor maskSetter( [&]( const QgsSymbolLayer * sl, const QString & slId )
178 {
179 if ( sl->layerType() == "MaskMarker" )
180 {
181 QgsMaskMarkerSymbolLayer *maskSl = const_cast<QgsMaskMarkerSymbolLayer *>( static_cast<const QgsMaskMarkerSymbolLayer *>( sl ) );
182
183 const QgsSymbolLayerReferenceList masks = maskSl->masks();
184 QgsSymbolLayerReferenceList newMasks;
185 for ( const QgsSymbolLayerReference &ref : masks )
186 {
187 // copy the original masks, only those with another destination layer
188 if ( ref.layerId() != mLayer->id() )
189 newMasks.append( ref );
190 }
191 for ( const QgsMaskSourceSelectionWidget::MaskSource &source : maskSources )
192 {
193 if ( ! source.isLabeling && source.layerId == layerIt.key() && source.symbolLayerId == slId )
194 {
195 // ... then add the new masked symbol layers, if any
196 for ( const QString &maskedId : maskedSymbolLayers )
197 {
198 newMasks.append( QgsSymbolLayerReference( mLayer->id(), maskedId ) );
199 }
200 // invalidate the cache of the source layer
201 layersToRefresh.insert( source.layerId );
202 }
203 }
204 maskSl->setMasks( newMasks );
205 }
206 } );
207 if ( vl->renderer() )
208 vl->renderer()->accept( &maskSetter );
209
210 //
211 // Now reset label masks
212 if ( ! vl->labeling() )
213 continue;
214 for ( const QString &labelProvider : vl->labeling()->subProviders() )
215 {
216 // clear symbol layers
217 QgsPalLayerSettings settings = vl->labeling()->settings( labelProvider );
218 QgsTextFormat format = settings.format();
219 if ( ! format.mask().enabled() )
220 continue;
221 const QgsSymbolLayerReferenceList masks = format.mask().maskedSymbolLayers();
223 for ( const QgsSymbolLayerReference &ref : masks )
224 {
225 // copy the original masks, only those with another destination layer
226 if ( ref.layerId() != mLayer->id() )
227 newMasks.append( ref );
228 }
229 for ( const QgsMaskSourceSelectionWidget::MaskSource &source : maskSources )
230 {
231 // ... then add the new masked symbol layers, if any
232
233 if ( source.isLabeling && source.layerId == layerIt.key() && source.symbolLayerId == labelProvider )
234 {
235 for ( const QString &maskedId : maskedSymbolLayers )
236 {
237 newMasks.append( QgsSymbolLayerReference( mLayer->id(), maskedId ) );
238 }
239 // invalidate the cache of the source layer
240 layersToRefresh.insert( source.layerId );
241 }
242 }
243 format.mask().setMaskedSymbolLayers( newMasks );
244 settings.setFormat( format );
245 vl->labeling()->setSettings( new QgsPalLayerSettings( settings ), labelProvider );
246 }
247 }
248
250 // trigger refresh of the current layer
251 mLayer->triggerRepaint();
252 // trigger refresh of dependent layers (i.e. mask source layers)
253 for ( const QString &layerId : layersToRefresh )
254 {
255 QgsMapLayer *layer = QgsProject::instance()->mapLayer( layerId );
256 layer->triggerRepaint();
257 }
258}
259
260SymbolLayerVisitor::SymbolLayerVisitor( SymbolLayerVisitor::SymbolLayerCallback callback ) :
261 mCallback( callback )
262{}
263
264bool SymbolLayerVisitor::visitEnter( const QgsStyleEntityVisitorInterface::Node &node )
265{
267 return false;
268
269 mSymbolKey = node.identifier;
270 return true;
271}
272
273void SymbolLayerVisitor::visitSymbol( const QgsSymbol *symbol, const QString &leafIdentifier, QVector<int> rootPath )
274{
275 for ( int idx = 0; idx < symbol->symbolLayerCount(); idx++ )
276 {
277 QVector<int> indexPath = rootPath;
278 indexPath.push_back( idx );
279
280 const QgsSymbolLayer *sl = symbol->symbolLayer( idx );
281
282 mCallback( sl, sl->id() );
283
284 // recurse over sub symbols
285 const QgsSymbol *subSymbol = const_cast<QgsSymbolLayer *>( sl )->subSymbol();
286 if ( subSymbol )
287 visitSymbol( subSymbol, leafIdentifier, indexPath );
288 }
289}
290
291bool SymbolLayerVisitor::visit( const QgsStyleEntityVisitorInterface::StyleLeaf &leaf )
292{
293 if ( leaf.entity && leaf.entity->type() == QgsStyle::SymbolEntity )
294 {
295 auto symbolEntity = static_cast<const QgsStyleSymbolEntity *>( leaf.entity );
296 if ( symbolEntity->symbol() )
297 visitSymbol( symbolEntity->symbol(), leaf.identifier, {} );
298 }
299 return true;
300}
301
@ 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:352
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.