QGIS API Documentation  3.20.0-Odense (decaadbb31)
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  return;
53  }
54  setupUi( this );
55 
56  mDefaultSymbolToolButton->setSymbolType( QgsSymbol::symbolTypeForGeometryType( type ) );
57 
58  // try to recognize the previous renderer
59  // (null renderer means "no previous renderer")
60  if ( renderer )
61  {
63  }
64  if ( ! mRenderer )
65  {
66  // use default embedded renderer
67  mRenderer.reset( new QgsEmbeddedSymbolRenderer( QgsSymbol::defaultSymbol( type ) ) );
68  }
69 
70  mDefaultSymbolToolButton->setSymbol( mRenderer->defaultSymbol()->clone() );
71  mDefaultSymbolToolButton->setDialogTitle( tr( "Default symbol" ) );
72  mDefaultSymbolToolButton->setLayer( mLayer );
73  mDefaultSymbolToolButton->registerExpressionContextGenerator( this );
74 
75  connect( mDefaultSymbolToolButton, &QgsSymbolButton::changed, this, [ = ]
76  {
77  mRenderer->setDefaultSymbol( mDefaultSymbolToolButton->symbol()->clone() );
78  emit widgetChanged();
79  } );
80 }
81 
83 
85 {
86  return mRenderer.get();
87 }
88 
90 {
92  if ( mDefaultSymbolToolButton )
93  {
94  mDefaultSymbolToolButton->setMapCanvas( context.mapCanvas() );
95  mDefaultSymbolToolButton->setMessageBar( context.messageBar() );
96  }
97 }
98 
100 {
102  if ( QgsExpressionContext *expressionContext = mContext.expressionContext() )
103  context = *expressionContext;
104  else
106 
107  const QList< QgsExpressionContextScope > scopes = mContext.additionalExpressionContextScopes();
108  for ( const QgsExpressionContextScope &s : scopes )
109  {
111  }
112  return context;
113 }
114 
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...
QString name
Definition: qgsmaplayer.h:73
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:938
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141