36 QgsMaskingWidget::QgsMaskingWidget( QWidget *parent ) :
42 void QgsMaskingWidget::onSelectionChanged()
45 const bool printMessage = mMaskTargetsWidget->selection().empty() != mMaskSourcesWidget->selection().empty();
47 if ( mMessageBarItem && !printMessage )
49 mMessageBar->popWidget( mMessageBarItem );
50 delete mMessageBarItem;
52 else if ( !mMessageBarItem && printMessage )
54 mMessageBarItem =
new QgsMessageBarItem( tr(
"Select both sources and symbol layers or your configuration will be lost" ), Qgis::MessageLevel::Warning, 0,
this );
55 mMessageBar->pushItem( mMessageBarItem );
61 void QgsMaskingWidget::showEvent( QShowEvent *event )
71 mMustPopulate =
false;
93 QList<QPair<QgsSymbolLayerId, QList<QgsSymbolLayerReference>>> symbolLayerMasks(
const QgsVectorLayer *layer )
98 QList<QPair<QgsSymbolLayerId, QList<QgsSymbolLayerReference>>> mMasks;
101 if ( ! sl->
masks().isEmpty() )
102 mMasks.push_back( qMakePair( lid, sl->
masks() ) );
110 if ( mLayer != layer )
113 mMustPopulate =
true;
117 void QgsMaskingWidget::populate()
119 mMaskSourcesWidget->update();
120 mMaskTargetsWidget->setLayer( mLayer );
123 QSet<QgsSymbolLayerId> maskedSymbolLayers;
124 QList<QgsMaskSourceSelectionWidget::MaskSource> maskSources;
128 for (
auto layerIt = layers.begin(); layerIt != layers.end(); layerIt++ )
130 const QString layerId = layerIt.key();
131 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layerIt.value() );
136 const QList<QPair<QgsSymbolLayerId, QList<QgsSymbolLayerReference>>> slMasks = symbolLayerMasks( vl );
137 for (
const QPair<
QgsSymbolLayerId, QList<QgsSymbolLayerReference>> &p : slMasks )
142 if ( ref.
layerId() == mLayer->id() )
150 maskSources.append( source );
157 for (
auto it = labelMasks.begin(); it != labelMasks.end(); it++ )
159 const QString &ruleKey = it.key();
160 for (
auto it2 = it.value().begin(); it2 != it.value().end(); it2++ )
162 if ( it2.key() == mLayer->id() )
165 maskedSymbolLayers.unite( it2.value().symbolLayerIds );
170 maskSources.append( source );
176 mMaskSourcesWidget->setSelection( maskSources );
177 mMaskTargetsWidget->setSelection( maskedSymbolLayers );
180 void QgsMaskingWidget::apply()
182 QList<QgsMaskSourceSelectionWidget::MaskSource> maskSources = mMaskSourcesWidget->selection();
183 QSet<QgsSymbolLayerId> maskedSymbolLayers = mMaskTargetsWidget->selection();
185 QSet<QString> layersToRefresh;
188 for (
auto layerIt = layers.begin(); layerIt != layers.end(); layerIt++ )
190 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layerIt.value() );
200 QgsMaskMarkerSymbolLayer *maskSl = const_cast<QgsMaskMarkerSymbolLayer *>( static_cast<const QgsMaskMarkerSymbolLayer *>( sl ) );
202 const QgsSymbolLayerReferenceList masks = maskSl->masks();
203 QgsSymbolLayerReferenceList newMasks;
204 for ( const QgsSymbolLayerReference &ref : masks )
207 if ( ref.layerId() != mLayer->id() )
208 newMasks.append( ref );
215 for ( const QgsSymbolLayerId &maskedId : maskedSymbolLayers )
217 newMasks.append( QgsSymbolLayerReference( mLayer->id(), maskedId ) );
220 layersToRefresh.insert( source.
layerId );
223 maskSl->setMasks( newMasks );
245 if ( ref.
layerId() != mLayer->id() )
246 newMasks.append( ref );
259 layersToRefresh.insert( source.
layerId );
270 mLayer->triggerRepaint();
272 for (
const QString &layerId : layersToRefresh )
279 bool QgsMaskingWidget::hasBeenPopulated()
281 return !mMustPopulate;
284 SymbolLayerVisitor::SymbolLayerVisitor( SymbolLayerVisitor::SymbolLayerCallback callback ) :
285 mCallback( callback )
297 void SymbolLayerVisitor::visitSymbol(
const QgsSymbol *symbol,
const QString &leafIdentifier, QVector<int> rootPath )
301 QVector<int> indexPath = rootPath;
302 indexPath.push_back( idx );
306 mCallback( sl,
QgsSymbolLayerId( mSymbolKey + leafIdentifier, indexPath ) );
311 visitSymbol( subSymbol, leafIdentifier, indexPath );
320 if ( symbolEntity->symbol() )
321 visitSymbol( symbolEntity->symbol(), leaf.
identifier, {} );