QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
QgsFeatureRenderer::copyRendererData
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
Definition: qgsrenderer.cpp:52
QgsSymbolWidgetContext::globalProjectAtlasMapLayerScopes
QList< QgsExpressionContextScope * > globalProjectAtlasMapLayerScopes(const QgsMapLayer *layer) const
Returns list of scopes: global, project, atlas, map, layer.
Definition: qgssymbolwidgetcontext.cpp:92
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsSymbolWidgetContext::mapCanvas
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
Definition: qgssymbolwidgetcontext.cpp:54
QgsSymbolButton::changed
void changed()
Emitted when the symbol's settings are changed.
QgsVectorLayer::wkbType
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
Definition: qgsvectorlayer.cpp:725
QgsVectorLayer::dataProvider
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Definition: qgsvectorlayer.cpp:676
QgsSymbol::defaultSymbol
static QgsSymbol * defaultSymbol(QgsWkbTypes::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Definition: qgssymbol.cpp:673
QgsEmbeddedSymbolRendererWidget::QgsEmbeddedSymbolRendererWidget
QgsEmbeddedSymbolRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Constructor.
Definition: qgsembeddedsymbolrendererwidget.cpp:30
QgsRendererWidget::mLayer
QgsVectorLayer * mLayer
Definition: qgsrendererwidget.h:112
QgsRendererWidget::context
QgsSymbolWidgetContext context() const
Returns the context in which the renderer widget is shown, e.g., the associated map canvas and expres...
Definition: qgsrendererwidget.cpp:346
QgsSymbolWidgetContext
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
Definition: qgssymbolwidgetcontext.h:35
QgsEmbeddedSymbolRendererWidget::setContext
void setContext(const QgsSymbolWidgetContext &context) override
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
Definition: qgsembeddedsymbolrendererwidget.cpp:92
QgsSymbol::symbolTypeForGeometryType
static Qgis::SymbolType symbolTypeForGeometryType(QgsWkbTypes::GeometryType type)
Returns the default symbol type required for the specified geometry type.
Definition: qgssymbol.cpp:549
qgsembeddedsymbolrendererwidget.h
QgsVectorDataProvider::FeatureSymbology
@ FeatureSymbology
Provider is able retrieve embedded symbology associated with individual features. Since QGIS 3....
Definition: qgsvectordataprovider.h:98
qgsapplication.h
QgsVectorDataProvider::capabilities
virtual Q_INVOKABLE QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
Definition: qgsvectordataprovider.cpp:208
QgsEmbeddedSymbolRenderer::convertFromRenderer
static QgsEmbeddedSymbolRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Creates a QgsEmbeddedSymbolRenderer from an existing renderer.
Definition: qgsembeddedsymbolrenderer.cpp:119
qgsrendererregistry.h
QgsEmbeddedSymbolRendererWidget::createExpressionContext
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgsembeddedsymbolrendererwidget.cpp:102
QgsPanelWidget::widgetChanged
void widgetChanged()
Emitted when the widget state changes.
QgsSymbolWidgetContext::expressionContext
QgsExpressionContext * expressionContext() const
Returns the expression context used for the widget, if set.
Definition: qgssymbolwidgetcontext.cpp:77
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:113
QgsRendererWidget
Base class for renderer settings widgets.
Definition: qgsrendererwidget.h:45
qgsvectorlayer.h
QgsFeatureRenderer
Definition: qgsrenderer.h:101
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:140
QgsStyle
Definition: qgsstyle.h:159
QgsEmbeddedSymbolRenderer
A vector feature renderer which uses embedded feature symbology to render per-feature symbols.
Definition: qgsembeddedsymbolrenderer.h:35
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsEmbeddedSymbolRendererWidget::renderer
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
Definition: qgsembeddedsymbolrendererwidget.cpp:87
QgsRendererWidget::mContext
QgsSymbolWidgetContext mContext
Context in which widget is shown.
Definition: qgsrendererwidget.h:131
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:76
QgsWkbTypes::geometryType
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
qgsembeddedsymbolrenderer.h
QgsSymbolWidgetContext::messageBar
QgsMessageBar * messageBar() const
Returns the message bar associated with the widget.
Definition: qgssymbolwidgetcontext.cpp:64
qgslogger.h
QgsEmbeddedSymbolRendererWidget::~QgsEmbeddedSymbolRendererWidget
~QgsEmbeddedSymbolRendererWidget() override
QgsEmbeddedSymbolRendererWidget::create
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Static creation method.
Definition: qgsembeddedsymbolrendererwidget.cpp:25
QgsRendererWidget::setContext
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
Definition: qgsrendererwidget.cpp:341
qgssymbol.h
QgsSymbolWidgetContext::additionalExpressionContextScopes
QList< QgsExpressionContextScope > additionalExpressionContextScopes() const
Returns the list of additional expression context scopes to show as available within the layer.
Definition: qgssymbolwidgetcontext.cpp:87