QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsmasksourceselectionwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmasksourceselectionwidget.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 #include <QTreeWidget>
17 #include <QVBoxLayout>
18 
20 #include "qgsproject.h"
21 #include "qgsvectorlayer.h"
22 #include "qgslegendsymbolitem.h"
23 #include "symbology/qgsrenderer.h"
24 #include "qgsstyleentityvisitor.h"
26 #include "qgsguiutils.h"
27 #include "qgslayertree.h"
28 #include "qgslayertreelayer.h"
29 #include "qgsvectorlayerlabeling.h"
30 #include "qgsvectorlayerutils.h"
32 
33 static void expandAll( QTreeWidgetItem *item )
34 {
35  for ( int i = 0; i < item->childCount(); i++ )
36  expandAll( item->child( i ) );
37  item->setExpanded( true );
38 }
39 
41 {
42  std::cout << lid.symbolKey().toLocal8Bit().constData() << "/";
43  QVector<int> path = lid.symbolLayerIndexPath();
44  for ( int i = 0; i < path.size(); i++ )
45  {
46  std::cout << path[i] << "/";
47  }
48 }
49 
51 {
52  std::cout << ref.layerId().toLocal8Bit().constData() << "/";
54 }
55 
57  : QWidget( parent )
58 {
59  mTree = new QTreeWidget( this );
60  mTree->setHeaderHidden( true );
61 
62  connect( mTree, &QTreeWidget::itemChanged, this, [&]( QTreeWidgetItem *, int ) { emit this->changed(); } );
63 
64  // place the tree in a layout
65  QVBoxLayout *vbox = new QVBoxLayout();
66  vbox->setContentsMargins( 0, 0, 0, 0 );
67  vbox->addWidget( mTree );
68 
69  setLayout( vbox );
70 }
71 
73 {
74  mTree->clear();
75  mItems.clear();
76 
77  class SymbolLayerFillVisitor : public QgsStyleEntityVisitorInterface
78  {
79  public:
80  SymbolLayerFillVisitor( QTreeWidgetItem *layerItem, const QgsVectorLayer *layer, QHash<QgsSymbolLayerReference, QTreeWidgetItem *> &items ):
81  mLayerItem( layerItem ), mLayer( layer ), mItems( items )
82  {}
83 
84  bool visitEnter( const QgsStyleEntityVisitorInterface::Node &node ) override
85  {
87  return false;
88 
89  mCurrentIdentifier = node.identifier;
90  mCurrentDescription = node.description;
91 
92  return true;
93  }
94 
95  bool visitSymbol( QTreeWidgetItem *rootItem, const QString &identifier, const QgsSymbol *symbol, QVector<int> rootPath )
96  {
97  bool ret = false;
98  for ( int idx = 0; idx < symbol->symbolLayerCount(); idx++ )
99  {
100  QgsSymbolLayer *sl = const_cast<QgsSymbol *>( symbol )->symbolLayer( idx );
101  QgsSymbol *subSymbol = sl->subSymbol();
102 
103  QVector<int> indexPath = rootPath;
104  indexPath.append( idx );
105 
106  std::unique_ptr< QTreeWidgetItem > slItem = qgis::make_unique< QTreeWidgetItem >( rootItem );
108  slItem->setIcon( 0, slIcon );
109  if ( sl->layerType() == "MaskMarker" )
110  {
111  slItem->setText( 0, QObject::tr( "Mask symbol layer" ) );
112  slItem->setFlags( slItem->flags() | Qt::ItemIsUserCheckable );
113  slItem->setCheckState( 0, Qt::Unchecked );
114  }
115 
116  if ( ( sl->layerType() == "MaskMarker" ) ||
117  ( subSymbol && visitSymbol( slItem.get(), identifier, subSymbol, indexPath ) ) )
118  {
119  QgsSymbolLayerReference ref( mLayer->id(), QgsSymbolLayerId( mCurrentIdentifier + identifier, indexPath ) );
120  mItems[ref] = slItem.get();
121  rootItem->addChild( slItem.release() );
122  ret = true;
123  }
124  }
125  return ret;
126  }
127 
128  bool visit( const QgsStyleEntityVisitorInterface::StyleLeaf &leaf ) override
129  {
130  if ( ! leaf.entity || leaf.entity->type() != QgsStyle::SymbolEntity )
131  return true;
132 
133  const auto symbolEntity = static_cast<const QgsStyleSymbolEntity *>( leaf.entity );
134  const QgsSymbol *symbol = symbolEntity->symbol();
135  if ( ! symbol )
136  return true;
137 
138  std::unique_ptr< QTreeWidgetItem > symbolItem = qgis::make_unique< QTreeWidgetItem >( mLayerItem, QStringList() << ( mCurrentDescription + leaf.description ) );
139  QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( symbol, QSize( iconSize, iconSize ) );
140  symbolItem->setIcon( 0, icon );
141 
142  if ( visitSymbol( symbolItem.get(), leaf.identifier, symbol, {} ) )
143  mLayerItem->addChild( symbolItem.release() );
144 
145  return true;
146  }
147 
148  const int iconSize = QgsGuiUtils::scaleIconSize( 16 );
149  QString mCurrentDescription;
150  QString mCurrentIdentifier;
151  QTreeWidgetItem *mLayerItem;
152  const QgsVectorLayer *mLayer;
153  QHash<QgsSymbolLayerReference, QTreeWidgetItem *> &mItems;
154  };
155 
156  class LabelMasksVisitor : public QgsStyleEntityVisitorInterface
157  {
158  public:
159  LabelMasksVisitor( QTreeWidgetItem *layerItem, const QgsVectorLayer *layer, QHash<QgsSymbolLayerReference, QTreeWidgetItem *> &items ):
160  mLayerItem( layerItem ), mLayer( layer ), mItems( items )
161  {}
162  bool visitEnter( const QgsStyleEntityVisitorInterface::Node &node ) override
163  {
165  {
166  currentRule = node.identifier;
167  currentDescription = node.description;
168  return true;
169  }
170  return false;
171  }
172  bool visit( const QgsStyleEntityVisitorInterface::StyleLeaf &leaf ) override
173  {
174  if ( leaf.entity && leaf.entity->type() == QgsStyle::LabelSettingsEntity )
175  {
176  auto labelSettingsEntity = static_cast<const QgsStyleLabelSettingsEntity *>( leaf.entity );
177  if ( labelSettingsEntity->settings().format().mask().enabled() )
178  {
179  QString maskTitle = currentRule.isEmpty()
180  ? QObject::tr( "Label mask" )
181  : QObject::tr( "Label mask for '%1' rule" ).arg( currentDescription );
182  QTreeWidgetItem *slItem = new QTreeWidgetItem( mLayerItem, QStringList() << maskTitle );
183  slItem->setFlags( slItem->flags() | Qt::ItemIsUserCheckable );
184  slItem->setCheckState( 0, Qt::Unchecked );
185  mLayerItem->addChild( slItem );
186  mItems[QgsSymbolLayerReference( "__labels__" + mLayer->id(), { currentRule, 0 } )] = slItem;
187  }
188  }
189  return true;
190  }
191 
192  QHash<QString, QHash<QString, QSet<QgsSymbolLayerId>>> masks;
193  // Current label rule, empty string for a simple labeling
194  QString currentRule;
195  QString currentDescription;
196  QTreeWidgetItem *mLayerItem;
197  const QgsVectorLayer *mLayer;
198  QHash<QgsSymbolLayerReference, QTreeWidgetItem *> &mItems;
199  };
200 
201  // populate the tree
202  const auto layers = QgsProject::instance()->layerTreeRoot()->findLayers();
203  for ( const QgsLayerTreeLayer *layerTreeLayer : layers )
204  {
205  const QgsMapLayer *layer = layerTreeLayer->layer();
206  const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer );
207  if ( ! vl )
208  continue;
209  if ( ! vl->renderer() )
210  continue;
211 
212  std::unique_ptr< QTreeWidgetItem > layerItem = qgis::make_unique< QTreeWidgetItem >( mTree, QStringList() << layer->name() );
213  layerItem->setData( 0, Qt::UserRole, vl );
214 
215  if ( vl->labeling() )
216  {
217  LabelMasksVisitor lblVisitor( layerItem.get(), vl, mItems );
218  vl->labeling()->accept( &lblVisitor );
219  }
220 
221  SymbolLayerFillVisitor slVisitor( layerItem.get(), vl, mItems );
222  vl->renderer()->accept( &slVisitor );
223 
224  if ( layerItem->childCount() > 0 )
225  mTree->addTopLevelItem( layerItem.release() );
226  }
227 
228  expandAll( mTree->invisibleRootItem() );
229 }
230 
232 QList<QgsMaskSourceSelectionWidget::MaskSource> QgsMaskSourceSelectionWidget::selection() const
233 {
234  QList<QgsMaskSourceSelectionWidget::MaskSource> sel;
235  for ( auto it = mItems.begin(); it != mItems.end(); it++ )
236  {
237  if ( it.value()->checkState( 0 ) == Qt::Checked )
238  {
239  const QgsSymbolLayerReference &ref = it.key();
241  source.isLabeling = ref.layerId().startsWith( "__labels__" );
242  source.layerId = source.isLabeling ? ref.layerId().mid( 10 ) : ref.layerId();
243  source.symbolLayerId = ref.symbolLayerId();
244  sel.append( source );
245  }
246  }
247  return sel;
248 }
249 
251 void QgsMaskSourceSelectionWidget::setSelection( const QList<QgsMaskSourceSelectionWidget::MaskSource> &sel )
252 {
253  // Clear current selection
254  for ( auto it = mItems.begin(); it != mItems.end(); it++ )
255  {
256  it.value()->setCheckState( 0, Qt::Unchecked );
257  }
258 
259  for ( const MaskSource &src : sel )
260  {
261  QString layerId = ( src.isLabeling ? "__labels__" : "" ) + src.layerId;
262  auto it = mItems.find( QgsSymbolLayerReference( layerId, src.symbolLayerId ) );
263  if ( it != mItems.end() )
264  {
265  it.value()->setCheckState( 0, Qt::Checked );
266  }
267  }
268 }
269 
QgsMaskSourceSelectionWidget::selection
QList< MaskSource > selection() const
Returns the current selection.
Definition: qgsmasksourceselectionwidget.cpp:232
QgsMaskSourceSelectionWidget::update
void update()
Updates the possible sources, from the project layers.
Definition: qgsmasksourceselectionwidget.cpp:72
QgsLayerTreeGroup::findLayers
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
Definition: qgslayertreegroup.cpp:223
QgsMaskSourceSelectionWidget::MaskSource::layerId
QString layerId
The source layer id.
Definition: qgsmasksourceselectionwidget.h:45
QgsStyleSymbolEntity
A symbol entity for QgsStyle databases.
Definition: qgsstyle.h:1201
QgsFeatureRenderer::accept
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
Definition: qgsrenderer.cpp:465
printSymbolLayerRef
void printSymbolLayerRef(const QgsSymbolLayerReference &ref)
Definition: qgsmasksourceselectionwidget.cpp:50
qgssymbollayerutils.h
QgsSymbolLayerReference
Type used to refer to a specific symbol layer in a symbol of a layer.
Definition: qgssymbollayerreference.h:118
QgsStyleEntityVisitorInterface::Node::description
QString description
A string describing the node.
Definition: qgsstyleentityvisitor.h:141
qgslayertreelayer.h
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:468
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g.
Definition: qgsstyleentityvisitor.h:34
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
QgsMaskSourceSelectionWidget::setSelection
void setSelection(const QList< MaskSource > &sel)
Sets the symbol layer selection.
Definition: qgsmasksourceselectionwidget.cpp:251
QgsStyleEntityVisitorInterface::StyleLeaf
Contains information relating to the style entity currently being visited.
Definition: qgsstyleentityvisitor.h:61
QgsStyle::SymbolEntity
@ SymbolEntity
Symbols.
Definition: qgsstyle.h:180
qgsmasksourceselectionwidget.h
QgsGuiUtils::iconSize
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
Definition: qgsguiutils.cpp:250
QgsStyle::LabelSettingsEntity
@ LabelSettingsEntity
Label settings.
Definition: qgsstyle.h:185
printSymbolLayerId
void printSymbolLayerId(const QgsSymbolLayerId &lid)
Definition: qgsmasksourceselectionwidget.cpp:40
QgsSymbolLayerUtils::symbolPreviewIcon
static QIcon symbolPreviewIcon(const QgsSymbol *symbol, QSize size, int padding=0, QgsLegendPatchShape *shape=nullptr)
Returns an icon preview for a color ramp.
Definition: qgssymbollayerutils.cpp:762
QgsProject::layerTreeRoot
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
Definition: qgsproject.cpp:3100
QgsSymbolLayer
Definition: qgssymbollayer.h:53
QgsMaskSourceSelectionWidget::MaskSource::isLabeling
bool isLabeling
Whether it is a labeling mask or not.
Definition: qgsmasksourceselectionwidget.h:48
QgsVectorLayer::labeling
const QgsAbstractVectorLayerLabeling * labeling() const
Access to const labeling configuration.
Definition: qgsvectorlayer.h:1648
QgsLayerTreeLayer
Layer tree node points to a map layer.
Definition: qgslayertreelayer.h:44
QgsMaskSourceSelectionWidget::QgsMaskSourceSelectionWidget
QgsMaskSourceSelectionWidget(QWidget *parent=nullptr)
constructor
Definition: qgsmasksourceselectionwidget.cpp:56
QgsMaskSourceSelectionWidget::MaskSource
Definition: qgsmasksourceselectionwidget.h:43
qgslegendsymbolitem.h
QgsStyleEntityVisitorInterface::StyleLeaf::description
QString description
A string describing the style entity.
Definition: qgsstyleentityvisitor.h:98
qgsmasksymbollayer.h
qgsvectorlayerutils.h
QgsStyleEntityInterface::type
virtual QgsStyle::StyleEntity type() const =0
Returns the type of style entity.
QgsMaskSourceSelectionWidget::MaskSource::symbolLayerId
QgsSymbolLayerId symbolLayerId
The symbol layer id.
Definition: qgsmasksourceselectionwidget.h:51
QgsStyleEntityVisitorInterface::NodeType::SymbolRule
@ SymbolRule
Rule based symbology or label child rule.
QgsStyleEntityVisitorInterface::Node
Contains information relating to a node (i.e.
Definition: qgsstyleentityvisitor.h:111
qgsrenderer.h
QgsSymbolLayerReference::symbolLayerId
QgsSymbolLayerId symbolLayerId() const
The symbol layer's id.
Definition: qgssymbollayerreference.h:136
qgslayertree.h
QgsStyleLabelSettingsEntity
A label settings entity for QgsStyle databases.
Definition: qgsstyle.h:1294
QgsSymbolLayer::subSymbol
virtual QgsSymbol * subSymbol()
Returns the symbol's sub symbol, if present.
Definition: qgssymbollayer.h:353
QgsStyleEntityVisitorInterface::StyleLeaf::entity
const QgsStyleEntityInterface * entity
Reference to style entity being visited.
Definition: qgsstyleentityvisitor.h:103
qgsvectorlayer.h
QgsSymbolLayerId::symbolLayerIndexPath
QVector< int > symbolLayerIndexPath() const
Returns the symbol layer index path inside the symbol.
Definition: qgssymbollayerreference.h:86
QgsStyleEntityVisitorInterface::StyleLeaf::identifier
QString identifier
A string identifying the style entity.
Definition: qgsstyleentityvisitor.h:85
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsMaskSourceSelectionWidget::changed
void changed()
Emitted when an item was changed.
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:86
QgsSymbolLayerId
We may need stable references to symbol layers, when pointers to symbol layers is not usable (when a ...
Definition: qgssymbollayerreference.h:54
QgsSymbolLayer::layerType
virtual QString layerType() const =0
Returns a string that represents this layer type.
QgsSymbolLayerId::symbolKey
QString symbolKey() const
Returns the key associated to the symbol.
Definition: qgssymbollayerreference.h:81
qgsvectorlayerlabeling.h
QgsSymbolLayerReference::layerId
QString layerId() const
The referenced vector layer / feature renderer.
Definition: qgssymbollayerreference.h:131
qgsguiutils.h
QgsGuiUtils::scaleIconSize
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
Definition: qgsguiutils.cpp:245
QgsStyleEntityVisitorInterface::Node::identifier
QString identifier
A string identifying the node.
Definition: qgsstyleentityvisitor.h:133
QgsAbstractVectorLayerLabeling::accept
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the labeling...
Definition: qgsvectorlayerlabeling.cpp:44
QgsSymbolLayerUtils::symbolLayerPreviewIcon
static QIcon symbolLayerPreviewIcon(const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale=QgsMapUnitScale())
Draws a symbol layer preview to an icon.
Definition: qgssymbollayerutils.cpp:851
qgsproject.h
QgsStyleEntityVisitorInterface::Node::type
QgsStyleEntityVisitorInterface::NodeType type
Node type.
Definition: qgsstyleentityvisitor.h:125
qgsstyleentityvisitor.h
QgsSymbol::symbolLayerCount
int symbolLayerCount() const
Returns the total number of symbol layers contained in the symbol.
Definition: qgssymbol.h:183
QgsVectorLayer::renderer
QgsFeatureRenderer * renderer()
Returns renderer.
Definition: qgsvectorlayer.h:892