QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsembeddedsymbolrendererwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsembeddedsymbolrendererwidget.cpp
3  ---------------------
4  begin : March 2021
5  copyright : (C) 2021 by Nyall Dawson
6  email : nyall dot dawson at gmail 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  ***************************************************************************/
17 #include "qgsrendererregistry.h"
18 
19 #include "qgssymbol.h"
20 
21 #include "qgslogger.h"
22 #include "qgsvectorlayer.h"
23 #include "qgsapplication.h"
24 
26 {
27  return new QgsEmbeddedSymbolRendererWidget( layer, style, renderer );
28 }
29 
31  : QgsRendererWidget( layer, style )
32 {
33  if ( !layer )
34  {
35  return;
36  }
37 
39 
40  // the renderer only applies to layers with providers supporting embedded symbols
42  {
43  //setup blank dialog
44  mRenderer.reset( nullptr );
45  QGridLayout *layout = new QGridLayout( this );
46  QLabel *label = new QLabel( tr( "The embedded symbols renderer can only be used with layers\n"
47  "containing embedded styling information.\n\n"
48  "'%1' does not contain embedded styling and cannot be displayed." )
49  .arg( layer->name() ), this );
50  this->setLayout( layout );
51  layout->addWidget( label );
52  mDefaultSymbolToolButton = nullptr;
53  return;
54  }
55  setupUi( this );
56 
57  mDefaultSymbolToolButton->setSymbolType( QgsSymbol::symbolTypeForGeometryType( type ) );
58 
59  // try to recognize the previous renderer
60  // (null renderer means "no previous renderer")
61  if ( renderer )
62  {
64  }
65  if ( ! mRenderer )
66  {
67  // use default embedded renderer
68  mRenderer.reset( new QgsEmbeddedSymbolRenderer( QgsSymbol::defaultSymbol( type ) ) );
69  if ( renderer )
70  renderer->copyRendererData( mRenderer.get() );
71  }
72 
73  mDefaultSymbolToolButton->setSymbol( mRenderer->defaultSymbol()->clone() );
74  mDefaultSymbolToolButton->setDialogTitle( tr( "Default symbol" ) );
75  mDefaultSymbolToolButton->setLayer( mLayer );
76  mDefaultSymbolToolButton->registerExpressionContextGenerator( this );
77 
78  connect( mDefaultSymbolToolButton, &QgsSymbolButton::changed, this, [ = ]
79  {
80  mRenderer->setDefaultSymbol( mDefaultSymbolToolButton->symbol()->clone() );
81  emit widgetChanged();
82  } );
83 }
84 
86 
88 {
89  return mRenderer.get();
90 }
91 
93 {
95  if ( mDefaultSymbolToolButton )
96  {
97  mDefaultSymbolToolButton->setMapCanvas( context.mapCanvas() );
98  mDefaultSymbolToolButton->setMessageBar( context.messageBar() );
99  }
100 }
101 
103 {
105  if ( QgsExpressionContext *expressionContext = mContext.expressionContext() )
106  context = *expressionContext;
107  else
109 
110  const QList< QgsExpressionContextScope > scopes = mContext.additionalExpressionContextScopes();
111  for ( const QgsExpressionContextScope &s : scopes )
112  {
114  }
115  return context;
116 }
117 
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Static creation method.
QgsEmbeddedSymbolRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Constructor.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void setContext(const QgsSymbolWidgetContext &context) override
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
A vector feature renderer which uses embedded feature symbology to render per-feature symbols.
static QgsEmbeddedSymbolRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Creates a QgsEmbeddedSymbolRenderer from an existing renderer.
Single scope for storing variables and functions for use within a QgsExpressionContext.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
Definition: qgsrenderer.cpp:52
QString name
Definition: qgsmaplayer.h:76
void widgetChanged()
Emitted when the widget state changes.
Base class for renderer settings widgets.
QgsSymbolWidgetContext mContext
Context in which widget is shown.
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
QgsSymbolWidgetContext context() const
Returns the context in which the renderer widget is shown, e.g., the associated map canvas and expres...
QgsVectorLayer * mLayer
void changed()
Emitted when the symbol's settings are changed.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
QList< QgsExpressionContextScope > additionalExpressionContextScopes() const
Returns the list of additional expression context scopes to show as available within the layer.
QList< QgsExpressionContextScope * > globalProjectAtlasMapLayerScopes(const QgsMapLayer *layer) const
Returns list of scopes: global, project, atlas, map, layer.
QgsExpressionContext * expressionContext() const
Returns the expression context used for the widget, if set.
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
QgsMessageBar * messageBar() const
Returns the message bar associated with the widget.
static Qgis::SymbolType symbolTypeForGeometryType(QgsWkbTypes::GeometryType type)
Returns the default symbol type required for the specified geometry type.
Definition: qgssymbol.cpp:246
static QgsSymbol * defaultSymbol(QgsWkbTypes::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Definition: qgssymbol.cpp:355
@ FeatureSymbology
Provider is able retrieve embedded symbology associated with individual features. Since QGIS 3....
virtual Q_INVOKABLE QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:968
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141