QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
qgslayerpropertieswidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayerpropertieswidget.cpp
3  ----------------------------
4  begin : June 2012
5  copyright : (C) 2012 by Arunmozhi
6  email : aruntheguy at gmail.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 
17 
18 #include <QFile>
19 #include <QStandardItem>
20 #include <QKeyEvent>
21 #include <QMessageBox>
22 #include <QPicture>
23 
24 #include "qgssymbollayer.h"
25 #include "qgssymbollayerregistry.h"
26 #include "qgspainteffectregistry.h"
27 
28 #include "qgsapplication.h"
29 #include "qgslogger.h"
30 
31 #include "qgssymbollayerwidget.h"
35 #include "qgssymbol.h" //for the unit
36 #include "qgspanelwidget.h"
37 #include "qgsmapcanvas.h"
38 #include "qgspainteffect.h"
39 #include "qgsproject.h"
40 #include "qgsvectorlayer.h"
43 
44 static bool _initWidgetFunction( const QString &name, QgsSymbolLayerWidgetFunc f )
45 {
47 
48  QgsSymbolLayerAbstractMetadata *abstractMetadata = reg->symbolLayerMetadata( name );
49  if ( !abstractMetadata )
50  {
51  QgsDebugMsg( "Failed to find symbol layer's entry in registry: " + name );
52  return false;
53  }
54  QgsSymbolLayerMetadata *metadata = dynamic_cast<QgsSymbolLayerMetadata *>( abstractMetadata );
55  if ( !metadata )
56  {
57  QgsDebugMsg( "Failed to cast symbol layer's metadata: " + name );
58  return false;
59  }
60  metadata->setWidgetFunction( f );
61  return true;
62 }
63 
64 static void _initWidgetFunctions()
65 {
66  static bool sInitialized = false;
67  if ( sInitialized )
68  return;
69 
70  _initWidgetFunction( QStringLiteral( "SimpleLine" ), QgsSimpleLineSymbolLayerWidget::create );
71  _initWidgetFunction( QStringLiteral( "MarkerLine" ), QgsMarkerLineSymbolLayerWidget::create );
72  _initWidgetFunction( QStringLiteral( "HashLine" ), QgsHashedLineSymbolLayerWidget::create );
73  _initWidgetFunction( QStringLiteral( "ArrowLine" ), QgsArrowSymbolLayerWidget::create );
74 
75  _initWidgetFunction( QStringLiteral( "SimpleMarker" ), QgsSimpleMarkerSymbolLayerWidget::create );
76  _initWidgetFunction( QStringLiteral( "FilledMarker" ), QgsFilledMarkerSymbolLayerWidget::create );
77  _initWidgetFunction( QStringLiteral( "SvgMarker" ), QgsSvgMarkerSymbolLayerWidget::create );
78  _initWidgetFunction( QStringLiteral( "RasterMarker" ), QgsRasterMarkerSymbolLayerWidget::create );
79  _initWidgetFunction( QStringLiteral( "FontMarker" ), QgsFontMarkerSymbolLayerWidget::create );
80  _initWidgetFunction( QStringLiteral( "EllipseMarker" ), QgsEllipseSymbolLayerWidget::create );
81  _initWidgetFunction( QStringLiteral( "VectorField" ), QgsVectorFieldSymbolLayerWidget::create );
82  _initWidgetFunction( QStringLiteral( "MaskMarker" ), QgsMaskMarkerSymbolLayerWidget::create );
83 
84  _initWidgetFunction( QStringLiteral( "SimpleFill" ), QgsSimpleFillSymbolLayerWidget::create );
85  _initWidgetFunction( QStringLiteral( "GradientFill" ), QgsGradientFillSymbolLayerWidget::create );
86  _initWidgetFunction( QStringLiteral( "ShapeburstFill" ), QgsShapeburstFillSymbolLayerWidget::create );
87  _initWidgetFunction( QStringLiteral( "RasterFill" ), QgsRasterFillSymbolLayerWidget::create );
88  _initWidgetFunction( QStringLiteral( "SVGFill" ), QgsSVGFillSymbolLayerWidget::create );
89  _initWidgetFunction( QStringLiteral( "CentroidFill" ), QgsCentroidFillSymbolLayerWidget::create );
90  _initWidgetFunction( QStringLiteral( "LinePatternFill" ), QgsLinePatternFillSymbolLayerWidget::create );
91  _initWidgetFunction( QStringLiteral( "PointPatternFill" ), QgsPointPatternFillSymbolLayerWidget::create );
92  _initWidgetFunction( QStringLiteral( "RandomMarkerFill" ), QgsRandomMarkerFillSymbolLayerWidget::create );
93 
94  _initWidgetFunction( QStringLiteral( "GeometryGenerator" ), QgsGeometryGeneratorSymbolLayerWidget::create );
95 
96  sInitialized = true;
97 }
98 
99 
101  : QgsPanelWidget( parent )
102  , mLayer( layer )
103  , mSymbol( symbol )
104  , mVectorLayer( vl )
105 {
106 
107  setupUi( this );
108  connect( mEnabledCheckBox, &QCheckBox::toggled, this, &QgsLayerPropertiesWidget::mEnabledCheckBox_toggled );
109  // initialize the sub-widgets
110  // XXX Should this thing be here this way? Initialize all the widgets just for the sake of one layer?
111  // TODO Make this on demand creation
112  _initWidgetFunctions();
113 
114  // TODO Algorithm
115  //
116  // 3. populate the combo box with the supported layer type
117  // 4. set the present layer type
118  // 5. create the widget for the present layer type and set in stacked widget
119  // 6. connect comboBox type changed to two things
120  // 1. emit signal that type has beed changed
121  // 2. remove the widget and place the new widget corresponding to the changed layer type
122  //
124  // update layer type combo box
125  int idx = cboLayerType->findData( mLayer->layerType() );
126  cboLayerType->setCurrentIndex( idx );
127 
128  connect( mEnabledCheckBox, &QAbstractButton::toggled, mEnabledDDBtn, &QWidget::setEnabled );
129  mEnabledCheckBox->setChecked( mLayer->enabled() );
130 
131  // set the corresponding widget
132  updateSymbolLayerWidget( layer );
133  connect( cboLayerType, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayerPropertiesWidget::layerTypeChanged );
134 
136 
137  this->connectChildPanel( mEffectWidget );
138 
139  if ( !mLayer->paintEffect() )
140  {
142  mLayer->paintEffect()->setEnabled( false );
143  }
144  mEffectWidget->setPaintEffect( mLayer->paintEffect() );
145 
147 }
148 
150 {
151  mContext = context;
152 
153  QgsSymbolLayerWidget *w = dynamic_cast< QgsSymbolLayerWidget * >( stackedWidget->currentWidget() );
154  if ( w )
155  w->setContext( mContext );
156 }
157 
159 {
160  return mContext;
161 }
162 
164 {
165  QgsPanelWidget::setDockMode( dockMode );
166  mEffectWidget->setDockMode( this->dockMode() );
167 }
168 
170 {
171  QStringList symbolLayerIds = QgsApplication::symbolLayerRegistry()->symbolLayersForType( mSymbol->type() );
172 
173  const auto constSymbolLayerIds = symbolLayerIds;
174  for ( const QString &symbolLayerId : constSymbolLayerIds )
175  cboLayerType->addItem( QgsApplication::symbolLayerRegistry()->symbolLayerMetadata( symbolLayerId )->visibleName(), symbolLayerId );
176 
177  if ( mSymbol->type() == QgsSymbol::Fill )
178  {
180  const auto constLineLayerIds = lineLayerIds;
181  for ( const QString &lineLayerId : constLineLayerIds )
182  {
184  if ( layerInfo->type() != QgsSymbol::Hybrid )
185  {
186  QString visibleName = layerInfo->visibleName();
187  QString name = QString( tr( "Outline: %1" ) ).arg( visibleName );
188  cboLayerType->addItem( name, lineLayerId );
189  }
190  }
191  }
192 }
193 
195 {
196  if ( stackedWidget->currentWidget() != pageDummy )
197  {
198  // stop updating from the original widget
199  if ( QgsSymbolLayerWidget *w = qobject_cast< QgsSymbolLayerWidget * >( stackedWidget->currentWidget() ) )
201  stackedWidget->removeWidget( stackedWidget->currentWidget() );
202  }
203 
205 
206  QString layerType = layer->layerType();
207  QgsSymbolLayerAbstractMetadata *am = pReg->symbolLayerMetadata( layerType );
208  if ( am )
209  {
211  if ( w )
212  {
213  w->setContext( mContext );
214  w->setSymbolLayer( layer );
215  stackedWidget->addWidget( w );
216  stackedWidget->setCurrentWidget( w );
217  // start receiving updates from widget
219  connect( w, &QgsSymbolLayerWidget::symbolChanged, this, &QgsLayerPropertiesWidget::reloadLayer );
220  return;
221  }
222  }
223  // When anything is not right
224  stackedWidget->setCurrentWidget( pageDummy );
225 }
226 
228 {
229  if ( mContext.expressionContext() )
230  return *mContext.expressionContext();
231 
232  QgsExpressionContext expContext;
236 
237  if ( mContext.mapCanvas() )
238  {
241  }
242  else
243  {
245  }
246 
248 
250  if ( mLayer )
251  {
252  //cheat a bit - set the symbol color variable to match the symbol layer's color (when we should really be using the *symbols*
253  //color, but that's not accessible here). 99% of the time these will be the same anyway
255  }
256  expContext << symbolScope;
261 
262  // additional scopes
263  const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
264  for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
265  {
266  expContext.appendScope( new QgsExpressionContextScope( scope ) );
267  }
268 
269  //TODO - show actual value
270  expContext.setOriginalValueVariable( QVariant() );
271 
276 
277  return expContext;
278 }
279 
281 {
283  connect( button, &QgsPropertyOverrideButton::changed, this, &QgsLayerPropertiesWidget::updateProperty );
284  button->registerExpressionContextGenerator( this );
285 }
286 
287 void QgsLayerPropertiesWidget::updateProperty()
288 {
289  QgsPropertyOverrideButton *button = qobject_cast<QgsPropertyOverrideButton *>( sender() );
290  QgsSymbolLayer::Property key = static_cast< QgsSymbolLayer::Property >( button->propertyKey() );
291  mLayer->setDataDefinedProperty( key, button->toProperty() );
292  emit changed();
293 }
294 
296 {
297  QgsSymbolLayer *layer = mLayer;
298  if ( !layer )
299  return;
300  QString newLayerType = cboLayerType->currentData().toString();
301  if ( layer->layerType() == newLayerType )
302  return;
303 
304  // get creation function for new layer from registry
306  QgsSymbolLayerAbstractMetadata *am = pReg->symbolLayerMetadata( newLayerType );
307  if ( !am ) // check whether the metadata is assigned
308  return;
309 
310  // change layer to a new (with different type)
311  // base new layer on existing layer's properties
312  QgsSymbolLayer *newLayer = am->createSymbolLayer( layer->properties() );
313  if ( !newLayer )
314  return;
315 
316  updateSymbolLayerWidget( newLayer );
317  emit changeLayer( newLayer );
318 }
319 
321 {
322  emit changed();
323 
324  // also update paint effect preview
325  bool paintEffectToggled = false;
326  if ( mLayer->paintEffect() && mLayer->paintEffect()->enabled() )
327  {
328  mLayer->paintEffect()->setEnabled( false );
329  paintEffectToggled = true;
330  }
331  mEffectWidget->setPreviewPicture( QgsSymbolLayerUtils::symbolLayerPreviewPicture( mLayer, QgsUnitTypes::RenderMillimeters, QSize( 60, 60 ) ) );
332  if ( paintEffectToggled )
333  {
334  mLayer->paintEffect()->setEnabled( true );
335  }
336  emit widgetChanged();
337 }
338 
339 void QgsLayerPropertiesWidget::reloadLayer()
340 {
341  emit changeLayer( mLayer );
342 }
343 
344 void QgsLayerPropertiesWidget::mEnabledCheckBox_toggled( bool enabled )
345 {
346  mLayer->setEnabled( enabled );
348 }
static const QString EXPR_ORIGINAL_VALUE
Inbuilt variable name for value original value variable.
static QgsSymbolLayerRegistry * symbolLayerRegistry()
Returns the application&#39;s symbol layer registry, used for managing symbol layers. ...
static QgsExpressionContextScope * updateSymbolScope(const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope=nullptr)
Updates a symbol scope related to a QgsSymbol to an expression context.
static const QString EXPR_CLUSTER_COLOR
Inbuilt variable name for cluster color variable.
Single variable definition for use within a QgsExpressionContextScope.
void connectChildPanel(QgsPanelWidget *panel)
Connect the given sub panel widgets showPanel signals to this current panels main showPanel event to ...
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsRasterMarkerSymbolLayerWidget.
void symbolChanged()
Should be emitted whenever the sub symbol changed on this symbol layer configuration.
Stores metadata about one symbol layer class.
bool dockMode()
Returns the dock mode state.
static const QString EXPR_GEOMETRY_POINT_COUNT
Inbuilt variable name for point count variable.
static QPicture symbolLayerPreviewPicture(const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit units, QSize size, const QgsMapUnitScale &scale=QgsMapUnitScale())
Draws a symbol layer preview to a QPicture.
QgsPaintEffect * paintEffect() const
Returns the current paint effect for the layer.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:62
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsFilledMarkerSymbolLayerWidget.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsGradientFillSymbolLayerWidget.
void setWidgetFunction(QgsSymbolLayerWidgetFunc f)
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsSymbolWidgetContext context() const
Returns the context in which the symbol widget is shown, e.g., the associated map canvas and expressi...
QStringList symbolLayersForType(QgsSymbol::SymbolType type)
Returns a list of available symbol layers for a specified symbol type.
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs...
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsShapeburstFillSymbolLayerWidget.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsSymbolLayerWidget.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsSVGFillSymbolLayerWidget.
Base class for any widget that can be shown as a inline panel.
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the symbol widget is shown, e.g., the associated map canvas and expression ...
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsFontMarkerSymbolLayerWidget.
Line symbol.
Definition: qgssymbol.h:87
virtual QgsStringMap properties() const =0
Should be reimplemented by subclasses to return a string map that contains the configuration informat...
void updateSymbolLayerWidget(QgsSymbolLayer *layer)
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsLinePatternFillSymbolLayerWidget.
virtual void setDockMode(bool dockMode)
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs...
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsSimpleLineSymbolLayerWidget.
The QgsMapSettings class contains configuration for rendering of the map.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsMarkerLineSymbolLayerWidget.
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
void setEnabled(bool enabled)
Sets whether the effect is enabled.
Convenience metadata class that uses static functions to create symbol layer and its widget...
void changed()
Should be emitted whenever configuration changes happened on this symbol layer configuration.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsPointPatternFillSymbolLayerWidget.
A button for controlling property overrides which may apply to a widget.
virtual void setSymbolLayer(QgsSymbolLayer *layer)=0
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
static QgsSymbolLayerWidget * create(QgsVectorLayer *layer)
Static creation method.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
static QgsPaintEffect * defaultStack()
Returns a new effect stack consisting of a sensible selection of default effects. ...
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsCentroidFillSymbolLayerWidget.
QgsSymbolLayerAbstractMetadata * symbolLayerMetadata(const QString &name) const
Returns metadata for specified symbol layer. Returns nullptr if not found.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsHashedLineSymbolLayerWidget.
Registry of available symbol layer classes.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown...
QgsProperty toProperty() const
Returns a QgsProperty object encapsulating the current state of the widget.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsSvgMarkerSymbolLayerWidget.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Will be registered as factory.
void registerExpressionContextGenerator(QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the symbol layer&#39;s property collection, used for data defined overrides...
virtual QColor color() const
The fill color.
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
QgsLayerPropertiesWidget(QgsSymbolLayer *layer, const QgsSymbol *symbol, QgsVectorLayer *vl, QWidget *parent=nullptr)
Constructor for QgsLayerPropertiesWidget.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsRasterFillSymbolLayerWidget.
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the layer.
bool enabled() const
Returns whether the effect is enabled.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setEnabled(bool enabled)
Sets whether symbol layer is enabled and should be drawn.
void widgetChanged()
Emitted when the widget state changes.
static QgsSymbolLayerWidget * create(QgsVectorLayer *layer)
Static creation method.
void registerDataDefinedButton(QgsPropertyOverrideButton *button, QgsSymbolLayer::Property key)
Registers a data defined override button.
virtual QgsSymbolLayer * createSymbolLayer(const QgsStringMap &map)=0
Create a symbol layer of this type given the map of properties.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsRandomMarkerFillSymbolLayerWidget.
QgsSymbolLayerWidget *(* QgsSymbolLayerWidgetFunc)(QgsVectorLayer *)
void changed()
Emitted when property definition changes.
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsSimpleMarkerSymbolLayerWidget.
QgsExpressionContext * expressionContext() const
Returns the expression context used for the widget, if set.
Fill symbol.
Definition: qgssymbol.h:88
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object...
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:583
bool enabled() const
Returns true if symbol layer is enabled and will be drawn.
SymbolType type() const
Returns the symbol&#39;s type.
Definition: qgssymbol.h:121
void changed()
Emitted when the paint effect properties change.
QgsSymbol::SymbolType type() const
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsVectorFieldSymbolLayerWidget.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:450
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the symbol widget is shown, e.g., the associated map canvas and expression ...
int propertyKey() const
Returns the property key linked to the button.
void changeLayer(QgsSymbolLayer *)
static const QString EXPR_CLUSTER_SIZE
Inbuilt variable name for cluster size variable.
static const QString EXPR_GEOMETRY_POINT_NUM
Inbuilt variable name for point number variable.
static QgsSymbolLayerWidget * create(QgsVectorLayer *vl)
Creates a new QgsSimpleFillSymbolLayerWidget.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user...
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the symbol layer property definitions.
static const QString EXPR_GEOMETRY_PART_NUM
Inbuilt variable name for geometry part number variable.
Represents a vector layer which manages a vector based data sets.
static const QString EXPR_GEOMETRY_PART_COUNT
Inbuilt variable name for geometry part count variable.
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
void init(int propertyKey, const QgsProperty &property, const QgsPropertiesDefinition &definitions, const QgsVectorLayer *layer=nullptr, bool auxiliaryStorageEnabled=false)
Initialize a newly constructed property button (useful if button was included in a UI layout)...
Whether symbol layer is enabled.
Hybrid symbol.
Definition: qgssymbol.h:89
Property
Data definable properties.
virtual QgsSymbolLayerWidget * createSymbolLayerWidget(QgsVectorLayer *)
Create widget for symbol layer of this type. Can return nullptr if there&#39;s no GUI.
virtual void setDataDefinedProperty(Property key, const QgsProperty &property)
Sets a data defined property for the layer.
QList< QgsExpressionContextScope > additionalExpressionContextScopes() const
Returns the list of additional expression context scopes to show as available within the layer...
virtual QString layerType() const =0
Returns a string that represents this layer type.